-
Notifications
You must be signed in to change notification settings - Fork 1
/
alu.sv
83 lines (78 loc) · 1.89 KB
/
alu.sv
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
`include "define.vh"
module alu(
input logic [5:0] alucode,
input logic [31:0] op1,
input logic [31:0] op2,
output logic [31:0] alu_result,
output logic br_taken
);
always_latch begin
case (alucode)
`ALU_ADD, `ALU_LB, `ALU_LH, `ALU_LW, `ALU_LBU, `ALU_LHU, `ALU_SB, `ALU_SH, `ALU_SW: begin
alu_result = op1 + op2;
br_taken = `DISABLE;
end
`ALU_SUB: begin
alu_result = op1 - op2;
br_taken = `DISABLE;
end
`ALU_SLL: begin
alu_result = op1 << op2[4:0]; // op2[4:0]
br_taken = `DISABLE;
end
`ALU_SLT: begin
alu_result = {31'b0, $signed(op1) < $signed(op2)};
br_taken = `DISABLE;
end
`ALU_SLTU: begin
alu_result = {31'b0, op1 < op2};
br_taken = `DISABLE;
end
`ALU_XOR: begin
alu_result = op1 ^ op2;
br_taken = `DISABLE;
end
`ALU_SRL: begin
alu_result = op1 >> op2[4:0]; // op2[4:0]
br_taken = `DISABLE;
end
`ALU_SRA: begin
alu_result = $signed(op1) >>> $signed(op2[4:0]); // op2[4:0]
br_taken = `DISABLE;
end
`ALU_OR: begin
alu_result = op1 | op2;
br_taken = `DISABLE;
end
`ALU_AND: begin
alu_result = op1 & op2;
br_taken = `DISABLE;
end
`ALU_LUI: begin
alu_result = op2;
br_taken = `DISABLE;
end
`ALU_JAL: begin
alu_result = op2 + 32'h4;
br_taken = `ENABLE;
end
`ALU_JALR: begin
alu_result = op2 + 32'h4;
br_taken = `ENABLE;
end
`ALU_BEQ, `ALU_BNE, `ALU_BLT, `ALU_BGE, `ALU_BLTU, `ALU_BGEU: begin
alu_result = 32'b0;
case (alucode)
`ALU_BEQ: br_taken = op1 == op2 ? `ENABLE : `DISABLE;
`ALU_BNE: br_taken = op1 == op2 ? `DISABLE : `ENABLE;
`ALU_BLT: br_taken = $signed(op1) < $signed(op2) ? `ENABLE : `DISABLE;
`ALU_BGE: br_taken = $signed(op1) < $signed(op2) ? `DISABLE : `ENABLE;
`ALU_BLTU: br_taken = op1 < op2 ? `ENABLE : `DISABLE;
`ALU_BGEU: br_taken = op1 < op2 ? `DISABLE : `ENABLE;
default: ;
endcase
end
default: ;
endcase
end
endmodule