-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.v
363 lines (328 loc) · 6.35 KB
/
controller.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
module controller (
input wire clock,
input wire [4:0] rs1,
input wire [4:0] rs2,
input wire [4:0] rd,
input wire [6:0] opcode,
input wire [2:0] f3,
input wire [6:0] f7,
input wire [31:0] immediate,
input wire reset,
output reg [39:0] char_out,
output reg PCSRC,
output reg ALUSRC,
output reg MEMTOREG,
output reg WRITEBACK_EN,
output reg REG_EN,
output reg [3:0] branch,
output reg [8:0] type,
output reg [5:0] alu_op,
output reg JUMP_SRC
);
initial begin
char_out = "XXXXX";
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
WRITEBACK_EN = 0;
REG_EN = 0;
JUMP_SRC = 0;
type = 0;
alu_op = 6'd0;
branch = 0;
//timer = 0;
end
always @(posedge clock or posedge reset) begin
if (reset == 1'b1) begin
char_out = "XXXXX";
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
WRITEBACK_EN = 0;
REG_EN = 0;
JUMP_SRC = 0;
type = 0;
alu_op = 6'd0;
branch = 0;
end else begin
if (opcode == 7'd0) begin
//NOP
char_out <= "NOP ";
type = 9'b000000000;
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
REG_EN = 0;
JUMP_SRC = 0;
WRITEBACK_EN = 0;
alu_op = 6'd0;
branch = 0;
end
if (opcode == 7'b0110011) begin
//R-TYPE
type = 9'b100000000;
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
WRITEBACK_EN = 0;
JUMP_SRC = 0;
REG_EN = 1;
branch = 0;
if (f3 == 3'h0) begin
//add or sub
if (f7 == 7'h00) begin
//add
char_out <= "ADD ";
alu_op = 6'd0;
end
else begin
//sub
char_out <= "SUB ";
alu_op = 6'd1;
end
end
if (f3 == 3'h1) begin
//shift left logical
char_out <= "SLL ";
alu_op = 6'd5;
end
if (f3 == 3'h2) begin
//set less than
char_out <= "SLT ";
alu_op = 6'd8;
end
if (f3 == 3'h3) begin
//set less than u
char_out <= "SLTU ";
alu_op = 6'd9;
end
if (f3 == 3'h4) begin
//xor
char_out <= "XOR ";
alu_op = 6'd2;
end
if (f3 == 3'h5) begin
//shift right (log or arth)
if (f7 == 7'h00) begin
//shift right logical
char_out <= "SRL ";
alu_op = 6'd6;
end
else begin
//shift right arith
char_out <= "SRA ";
alu_op = 6'd7;
end
end
if (f3 == 3'h6) begin
//or
char_out <= "OR ";
alu_op = 6'd2;
end
if (f3 == 3'h7) begin
//and
char_out <= "AND ";
alu_op = 6'd4;
end
end
/* I TYPE addi, xori, ori, andi, slli, srli, srai, slti, sltiu */
if (opcode == 7'b0010011) begin
type = 9'b010000000;
PCSRC = 0;
ALUSRC = 1;
MEMTOREG = 0;
WRITEBACK_EN = 0;
JUMP_SRC = 0;
REG_EN = 1;
branch = 0;
if (f3 == 3'h0) begin // addi
char_out <= "ADDI ";
alu_op = 6'd10;
end
if (f3 == 3'h4) begin // xori
char_out <= "XORI ";
alu_op = 6'd11;
end
if (f3 == 3'h6) begin // ori
char_out <= "ORI ";
alu_op = 6'd12;
end
if (f3 == 3'h7) begin // andi
char_out <= "ANDI ";
alu_op = 6'd13;
end
if (f3 == 3'h1) begin // slli
char_out <= "SLLI ";
alu_op = 6'd14;
end
if (f3 == 3'h5) begin // srli & srai
if (f7 == 7'h00) begin
char_out <= "SRLI ";
alu_op = 6'd15;
end
if (f7 == 7'h20) begin
char_out <= "SRAI ";
alu_op = 6'd16;
end
end
if (f3 == 3'h2) begin // slti
char_out <= "SLTI ";
alu_op = 6'd17;
end
if (f3 == 3'h3) begin // sltiu
char_out <= "SLTIU";
alu_op = 6'd18;
end
end
/* I TYPE lb, lh, lw, lbu, lhu */
if (opcode == 7'b0000011) begin
type = 9'b00100000;
PCSRC = 0;
ALUSRC = 1;
MEMTOREG = 1;
WRITEBACK_EN = 0;
JUMP_SRC = 0;
REG_EN = 1;
branch = 0;
if (f3 == 3'h0) begin // lb
char_out <= "LB ";
end
if (f3 == 3'h1) begin // lh
char_out <= "LH ";
end
if (f3 == 3'h2) begin // lw
char_out <= "LW ";
end
if (f3 == 3'h4) begin // lbu
char_out <= "LBU ";
end
if (f3 == 3'h5) begin // lhu
char_out <= "LHU ";
end
end
if (opcode == 7'b0100011) begin
//S-TYPE
type = 9'b000100000;
PCSRC = 0;
ALUSRC = 1;
MEMTOREG = 0;
WRITEBACK_EN = 1;
JUMP_SRC = 0;
REG_EN = 0;
branch = 0;
if (f3 == 3'h0) begin
//store byte
char_out <= "SB ";
end
if (f3 == 3'h1) begin
//store half
char_out <= "SH ";
end
if (f3 == 3'h2) begin
//store word
char_out <= "SW ";
end
end
/* B TYPE beq, bne, blt, bge, bltu, bgeu */
if (opcode == 7'b1100011) begin
type = 9'b000010000;
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
WRITEBACK_EN = 0;
JUMP_SRC = 0;
REG_EN = 0;
if (f3 == 3'h0) begin // beq
char_out <= "BEQ ";
branch = 4'd1;
end
if (f3 == 3'h1) begin // bne
char_out <= "BNE ";
branch = 4'd2;
end
if (f3 == 3'h4) begin // blt
char_out <= "BLT ";
branch = 4'd3;
end
if (f3 == 3'h5) begin // bge
char_out <= "BGE ";
branch = 4'd4;
end
if (f3 == 3'h6) begin // bltu
char_out <= "BLTU ";
branch = 4'd5;
end
if (f3 == 3'h7) begin // bgeu
char_out <= "BGEU ";
branch = 4'd6;
end
end
if (opcode == 7'b1101111) begin
//J-TYPE
type = 9'b000001000;
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
WRITEBACK_EN = 0;
JUMP_SRC = 1;
REG_EN = 1;
branch = 4'd7;
//jump and link
char_out <= "JAL ";
end
if (opcode == 7'b0110111) begin
//U-TYPE
type = 9'b000000100;
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
WRITEBACK_EN = 0;
REG_EN = 1;
branch = 0;
//load upper imm
char_out <= "LUI ";
end
if (opcode == 7'b0010111) begin
//U-TYPE
type = 9'b000000010;
PCSRC = 0;
ALUSRC = 1;
MEMTOREG = 0;
WRITEBACK_EN = 0;
REG_EN = 1;
branch = 0;
alu_op = 6'd19;
//add upper imm to pc
char_out <= "AUIPC";
end
/* I TYPE jalr */
if (opcode == 7'b1100111) begin
type = 9'b000000001;
PCSRC = 0;
ALUSRC = 0;
MEMTOREG = 0;
WRITEBACK_EN = 0;
REG_EN = 1;
JUMP_SRC = 1;
branch = 4'd8;
if (f3 == 3'h0) begin // jalr
char_out <= "JALR ";
end
end
end
end
//reg [4:0] timer;
//reg branch;
//always @(posedge clock_50) begin
// if (branch != 0 && timer >= 5'd10) begin
// branch_send <= branch;
// end else
// if (branch != 0 && timer < 5'd10) begin
// timer <= timer + 1;
// end else
// if (branch == 0) begin
// branch_send <= 0;
// timer <= 0;
// end
//end
endmodule