-
Notifications
You must be signed in to change notification settings - Fork 2
/
ALU_FSM.v
292 lines (244 loc) · 5.79 KB
/
ALU_FSM.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 15:22:19 09/03/2015
// Design Name:
// Module Name: alu
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
`include "parameters.vh"
module ALU_FSM( a, b, c, opcode, clk, reset_btn, seven_segment, enable, led);
input clk, reset_btn;
output a, b, c, opcode;
output wire [6:0] seven_segment;
output wire [3:0] enable;
output wire [3:0] led;
wire clk, reset_btn;
wire [15:0] a;
wire [15:0] b;
wire [15:0] c;
wire [15:0] opcode;
wire [4:0] flags;
reg [3:0] current_op=4'b0;
reg carry_in = 1'b0;
reg [15:0] a_output;
reg [15:0] b_output;
reg [15:0] opcode_output;
reg [32:0] counter;
assign a = a_output;
assign b = b_output;
assign opcode = opcode_output;
// Instantiate the Unit Under Test (UUT)
ALU alu (
.a(a),
.b(b),
.opcode(opcode),
.carry_in(carry_in),
.c(c),
.flags(flags)
);
BCD_To_7Seg bcg(
.binary(c),
.clk(clk),
.seven_segment(seven_segment),
.enable(enable),
.leds(led)
);
localparam EXT_ADD_NUM=4'd0,
EXT_ADD_OP = 16'b1010000,
EXT_ADD_A = 16'b100,
EXT_ADD_B = 16'b100,
EXT_OR_NUM=4'd1,
EXT_OR_OP = 16'b100000,
EXT_OR_A = 16'b1,
EXT_OR_B = 16'b10,
EXT_XOR_NUM=4'd2,
EXT_XOR_OP = 16'b110000,
EXT_XOR_A = 16'b10,
EXT_XOR_B = 16'b10,
EXT_AND_NUM=4'd3,
EXT_AND_OP = 16'b10000,
EXT_AND_A = 16'b10,
EXT_AND_B = 16'b10,
EXT_SUB_NUM=4'd4,
EXT_SUB_OP = 16'b0000000010010000,
EXT_SUB_A = 16'd4,
EXT_SUB_B = 16'd1,
EXT_CMP_NUM=4'd5,
EXT_CMP_OP = 16'b10110000,
EXT_CMP_A = 16'b100,
EXT_CMP_B = 16'b100,
ADDI_NUM=4'd6,
ADDI_OP = 16'b101000000000100,
ADDI_A = 16'b100,
ADDI_B = 16'b0,
EXT_LSHI_LEFT_NUM=4'd7,
EXT_LSHI_LEFT_OP = 16'b1000000000000010,
EXT_LSHI_LEFT_A = 16'b1,
EXT_LSHI_LEFT_B = 16'b0,
EXT_LSHI_RIGHT_NUM=4'd8,
EXT_LSHI_RIGHT_OP = 16'b1000000000010010,
EXT_LSHI_RIGHT_A = 16'b100,
EXT_LSHI_RIGHT_B = 16'b0,
EXT_ASHUI_LEFT_NUM=4'd9,
EXT_ASHUI_LEFT_OP = 16'b1000000000100010,
EXT_ASHUI_LEFT_A = 16'b1111111111111110,
EXT_ASHUI_LEFT_B = 16'b0,
EXT_ASHUI_RIGHT_NUM=4'd10,
EXT_ASHUI_RIGHT_OP = 16'b1000000000110010,
EXT_ASHUI_RIGHT_A = 16'b1111111111111000,
EXT_ASHUI_RIGHT_B = 16'b0,
EXT_LSH_NUM=4'd11,
EXT_LSH_OP = 16'b1000000001000000,
EXT_LSH_A = 16'b1,
EXT_LSH_B = 16'b10,
EXT_ASHU_NUM=4'd12,
EXT_ASHU_OP = 16'b1000000001100000,
EXT_ASHU_A = 16'b10,
EXT_ASHU_B = 16'b1111111111111111,
SUBI_NUM=4'd13,
SUBI_OP = 16'b1001000000000010,
SUBI_A = 16'b100,
SUBI_B = 16'b0,
CMPI_NUM=4'd14,
CMPI_OP = 16'b1011000000000100,
CMPI_A = 16'b10,
CMPI_B = 16'b0;
always @(posedge clk)
begin
if(reset_btn == 1'b1)
begin
current_op <= 4'd0;
end
else
begin
if(counter == 32'b10001111000011010001100000000)
begin
current_op <= current_op + 1'd1;
counter <= 32'b0;
end
else
begin
counter <= counter+ 1'b1;
end
end
end
always @(*)
begin
if(reset_btn == 1'b1)
begin
opcode_output = 16'b0;
a_output = 16'b0;
b_output =16'b0;
end
case(current_op)
EXT_ADD_NUM:
begin
opcode_output = EXT_ADD_OP;
a_output = EXT_ADD_A;
b_output = EXT_ADD_B;
end
EXT_OR_NUM:
begin
opcode_output = EXT_OR_OP;
a_output = EXT_OR_A;
b_output = EXT_OR_B;
end
EXT_XOR_NUM:
begin
opcode_output = EXT_XOR_OP;
a_output=EXT_XOR_A;
b_output = EXT_XOR_B;
end
EXT_AND_NUM:
begin
opcode_output = EXT_AND_OP;
a_output = EXT_AND_A;
b_output = EXT_AND_B;
end
EXT_SUB_NUM:
begin
opcode_output = EXT_SUB_OP;
a_output = EXT_SUB_A;
b_output = EXT_SUB_B;
end
EXT_CMP_NUM:
begin
opcode_output = EXT_CMP_OP;
a_output = EXT_CMP_A;
b_output = EXT_CMP_B;
end
ADDI_NUM:
begin
opcode_output = ADDI_OP;
a_output = ADDI_A;
b_output = ADDI_B;
end
EXT_LSHI_LEFT_NUM:
begin
opcode_output = EXT_LSHI_LEFT_OP;
a_output = EXT_LSHI_LEFT_A;
b_output = EXT_LSHI_LEFT_B;
end
EXT_LSHI_RIGHT_NUM:
begin
opcode_output = EXT_LSHI_RIGHT_OP;
a_output = EXT_LSHI_RIGHT_A;
b_output = EXT_LSHI_RIGHT_B;
end
EXT_ASHUI_LEFT_NUM:
begin
opcode_output = EXT_ASHUI_LEFT_OP;
a_output = EXT_ASHUI_LEFT_A;
b_output = EXT_ASHUI_LEFT_B;
end
EXT_ASHUI_RIGHT_NUM:
begin
opcode_output = EXT_ASHUI_RIGHT_OP;
a_output = EXT_ASHUI_RIGHT_A;
b_output = EXT_ASHUI_RIGHT_B;
end
EXT_LSH_NUM:
begin
opcode_output = EXT_LSH_OP;
a_output = EXT_LSH_A;
b_output = EXT_LSH_B;
end
EXT_ASHU_NUM:
begin
opcode_output = EXT_ASHU_OP;
a_output = EXT_ASHU_A;
b_output = EXT_ASHU_B;
end
SUBI_NUM:
begin
opcode_output = SUBI_OP;
a_output = SUBI_A;
b_output = SUBI_B;
end
CMPI_NUM:
begin
opcode_output = CMPI_OP;
a_output = CMPI_A;
b_output = CMPI_B;
end
default:
begin
opcode_output = 16'b0;
a_output = 16'b0;
b_output =16'b0;
end
endcase
end
endmodule