-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALU.v
36 lines (27 loc) · 904 Bytes
/
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
// ALU alu(out, overflow_flag, A, B, func);
module ALU(
output reg [31:0] out,
output overflow_flag,
input signed [31:0] A, B,
input [3:0] func
);
wire signed [32:0] A_B_add = {A[31], A} + {B[31], B};
wire signed [32:0] A_B_sub = {A[31], A} - {B[31], B};
always @ (*)
case (func)
4'h0: out = A_B_add[31:0];
4'h1: out = A_B_sub[31:0];
4'h2: out = A << B;
4'h3: out = A >> B;
4'h4: out = A & B;
4'h5: out = A | B;
4'h6: out = A ^ B;
4'h7: out = ~(A | B);
4'h8: out = {31'b0, A < B}; // less
4'h9: out = {31'b0, {1'b0, A} < {1'b0, B}}; // less unsigned
4'he: out = {B[15:0], 16'b0};
default: out = 32'h_dead_beef;
endcase
assign overflow_flag = ( (A_B_add[32] != A_B_add[31]) & (func == 4'h0) )
| ( (A_B_sub[32] != A_B_sub[31]) & (func == 4'h1) );
endmodule