-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalu.v
56 lines (51 loc) · 1.85 KB
/
alu.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: Santa Clara University
// Engineer: Jonathan Trinh
//
// Create Date: 01/15/2019 02:12:29 PM
// Design Name:
// Module Name: alu
// Project Name: ALU
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module alu(A, B, op, out, zero_flag, negative_flag);
input [31:0] A, B; // defining inputs and outputs
input [3:0] op;
output [31:0] out;
output zero_flag, negative_flag;
reg [31:0] out;
reg zero_flag, negative_flag;
always@(A, B, op) begin // define ALU behavior
case(op)
4'b0000: out = A + B; // if opcode is 0, add operands
4'b0001: out = A + 1; // if opcode is 1, add 1 to first operand
4'b0010: out = ~A + 1; // if opcode is 2, negate first operand
4'b0011: out = A + ~B + 1; // if opcode is 3, subtract first and second operand
4'b0100: out = A; // if opcode is 4, pass first operand through
endcase
end
always@(op, out) begin
if(op != 4'b0100) begin //Only set flags if not passing
negative_flag = 0; // default values
zero_flag = 0;
if (out[31]==32'd1) begin // check leading bit of the result; if it is 1, we know it is negative and set flag accordingly
negative_flag=1;
zero_flag=0;
end
if (out == 32'd0) begin // check if the result is 0 and if so, set the flag
negative_flag = 0;
zero_flag = 1;
end
end
end
endmodule