-
Notifications
You must be signed in to change notification settings - Fork 1
/
alu.v
102 lines (95 loc) · 1.54 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
`include "global_config.h"
`include "nettype.h"
`include "stddef.h"
`include "cpu.h"
`include "isa.h"
module alu(in_0,in_1,op,out,of);
input[31:0] in_0;
input[31:0] in_1;
input[3:0] op;
output[31:0] out;
output of;
reg[31:0] out;
reg of;
wire signed[`WordDataBus] s_in_0 = $signed(in_0);
wire signed[`WordDataBus] s_in_1 = $signed(in_1);
wire signed[`WordDataBus] s_out = $signed(out);
always@(*)
begin
case(op)
`ALU_OP_AND:
begin
out = in_0 & in_1;
end
`ALU_OP_OR:
begin
out = in_0 | in_1;
end
`ALU_OP_XOR:
begin
out = in_0 ^ in_1;
end
`ALU_OP_ADDS:
begin
out = in_0 + in_1;
end
`ALU_OP_ADDU:
begin
out = in_0 + in_1;
end
`ALU_OP_SUBS:
begin
out = in_0 - in_1;
end
`ALU_OP_SUBU:
begin
out = in_0 - in_1;
end
`ALU_OP_SHRL:
begin
out = in_0 >> in_1[`ShAmountLoc];
end
`ALU_OP_SHLL:
begin
out = in_0 << in_1[`ShAmountLoc];
end
default:
begin
out = in_0;
end
endcase
end
always@(*)
begin
case(op)
`ALU_OP_ADDS:
begin
if(((s_in_0 > 0) && (s_in_1 > 0) && (s_out < 0)) ||
((s_in_0 < 0) && (s_in_1 < 0) && (s_out > 0)))
begin
of = `ENABLE;
end
else
begin
of = `DISABLE;
end
end
`ALU_OP_SUBS:
begin
if(((s_in_0 < 0) && (s_in_1 > 0) && (s_out > 0))||
((s_in_0 > 0) && (s_in_1 < 0) && (s_out <0)))
begin
of = `ENABLE;
end
else
begin
of = `DISABLE;
end
end
default:
begin
of = `DISABLE;
end
endcase
end
endmodule