-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArithmeticLogicUnit.v
More file actions
206 lines (192 loc) · 5.8 KB
/
Copy pathArithmeticLogicUnit.v
File metadata and controls
206 lines (192 loc) · 5.8 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
`timescale 1ns / 1ps
module ArithmeticLogicUnit (
input wire [31:0] A,
input wire [31:0] B,
input wire [4:0] FunSel,
input wire WF,
input wire Clock,
output reg [31:0] ALUOut,
output reg [3:0] FlagsOut
);
reg [32:0] carryChecker;
reg [31:0] signExtendedA, signExtendedB;
always @(*) begin
signExtendedA <= {{16{A[15]}}, A[15:0]};
signExtendedB <= {{16{B[15]}}, B[15:0]};
case (FunSel)
5'b00000: begin
ALUOut <= signExtendedA;
end
5'b00001: begin
ALUOut <= signExtendedB;
end
5'b00010: begin
ALUOut <= ~signExtendedA;
end
5'b00011: begin
ALUOut <= ~signExtendedB;
end
5'b00100: begin
carryChecker <= signExtendedA + signExtendedB;
ALUOut <= signExtendedA + signExtendedB;
end
5'b00101: begin
carryChecker <= signExtendedA + signExtendedB + FlagsOut[2];
ALUOut <= signExtendedA + signExtendedB + FlagsOut[2];
end
5'b00110: begin
carryChecker <= signExtendedA - signExtendedB;
ALUOut <= signExtendedA - signExtendedB;
end
5'b00111: ALUOut <= signExtendedA & signExtendedB;
5'b01000: ALUOut <= signExtendedA | signExtendedB;
5'b01001: ALUOut <= signExtendedA ^ signExtendedB;
5'b01010: ALUOut <= ~(signExtendedA & signExtendedB);
5'b01011: begin
ALUOut <= signExtendedA << 1;
end
5'b01100: begin
ALUOut <= signExtendedA >> 1;
end
5'b01101: begin
ALUOut <= signExtendedA >>> 1;
end
5'b01110: begin
ALUOut <= {signExtendedA[30:0], signExtendedA[31]};
end
5'b01111: begin
ALUOut <= {signExtendedA[0], signExtendedA[31:1]};
end
// 32 bit operations
5'b10000: begin
ALUOut <= A;
end
5'b10001: begin
ALUOut <= B;
end
5'b10010: begin
ALUOut <= ~A;
end
5'b10011: begin
ALUOut <= ~B;
end
5'b10100: begin
carryChecker <= A + B;
ALUOut <= A + B;
end
5'b10101: begin
carryChecker <= A + B + FlagsOut[2];
ALUOut <= carryChecker[31:0];
end
5'b10110: begin
carryChecker <= A - B;
ALUOut <= A - B;
end
5'b10111: ALUOut <= A & B;
5'b11000: ALUOut <= A | B;
5'b11001: ALUOut <= A ^ B;
5'b11010: ALUOut <= ~(A & B);
5'b11011: begin
ALUOut <= A << 1;
end
5'b11100: begin
ALUOut <= A >> 1;
end
5'b11101: begin
ALUOut <= A >>> 1;
end
5'b11110: begin
ALUOut <= {A[30:0], A[31]};
end
5'b11111: begin
ALUOut <= {A[0], A[31:1]};
end
default: ALUOut <= ALUOut;
endcase
end
always @(posedge Clock) begin
if (WF) begin
case (FunSel)
5'b00000: begin end
5'b00001: begin end
5'b00010: begin end
5'b00011: begin end
5'b00100: begin
FlagsOut[2] <= carryChecker[32];
FlagsOut[0] <= (signExtendedA[31] && signExtendedB[31] && ~ALUOut[15]) || (~signExtendedA[31] && ~signExtendedB[31] && ALUOut[15]);
end
5'b00101: begin
FlagsOut[3] <= ALUOut == 0;
FlagsOut[2] <= carryChecker[32];
FlagsOut[0] <= (signExtendedA[31] && signExtendedB[31] && ~ALUOut[15]) || (~signExtendedA[31] && ~signExtendedB[31] && ALUOut[15]);
end
5'b00110: begin
FlagsOut[2] <= carryChecker[32];
FlagsOut[0] <= (~signExtendedA[31] && signExtendedB[31] && ALUOut[15]) || (signExtendedA[31] && ~signExtendedB[31] && ~ALUOut[15]);
end
5'b00111: begin end
5'b01000: begin end
5'b01001: begin end
5'b01010: begin end
5'b01011: begin
FlagsOut[2] <= signExtendedA[31];
end
5'b01100: begin
FlagsOut[2] <= signExtendedA[0];
end
5'b01101: begin
FlagsOut[2] <= signExtendedA[0];
end
5'b01110: begin
FlagsOut[2] <= signExtendedA[31];
end
5'b01111: begin
FlagsOut[2] <= signExtendedA[0];
end
// 32 bit operations
5'b10000: begin end
5'b10001: begin end
5'b10010: begin end
5'b10011: begin end
5'b10100: begin
FlagsOut[2] <= carryChecker[32];
FlagsOut[0] <= (A[31] && B[31] && ~ALUOut[31]) || (~A[31] && ~B[31] && ALUOut[31]);
end
5'b10101: begin
FlagsOut[3] <= ALUOut == 0;
FlagsOut[2] <= carryChecker[32];
FlagsOut[0] <= (A[31] && B[31] && ~ALUOut[31]) || (~A[31] && ~B[31] && ALUOut[31]);
end
5'b10110: begin
FlagsOut[2] <= carryChecker[32];
FlagsOut[0] <= (~A[31] && B[31] && ALUOut[31]) || (A[31] && ~B[31] && ~ALUOut[31]);
end
5'b11011: begin
FlagsOut[2] <= A[31];
end
5'b11100: begin
FlagsOut[2] <= A[0];
end
5'b11101: begin
FlagsOut[2] <= A[0];
end
5'b11110: begin
FlagsOut[2] <= A[31];
end
5'b11111: begin
FlagsOut[2] <= A[0];
end
endcase
if (~(FunSel[3:0] == 4'b1101)) begin
if (FunSel[4]) begin
FlagsOut[1] <= ALUOut[31];
end else begin
FlagsOut[1] <= ALUOut[15];
end
end
if (~(FunSel[3:0] == 4'b0101)) begin
FlagsOut[3] <= ALUOut == 0;
end
end
end
endmodule