-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.v
423 lines (380 loc) · 11.2 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 02:06:35 06/27/2019
// Design Name:
// Module Name: Controller
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module Controller(
input [5:0]op, funct,
input clk, INT, NMI, INTD,
output reg isBranch, pcWrite, lorD, MemWrite, output reg [1:0] MemtoReg, output reg IRWrite, INA,
output reg [1:0]aluControl, aluSrcB, PCSource, output reg aluSrcA, RegWrite, output reg [1:0] RegDst, output reg isInterrupted);
reg [4:0] state = 5'b01100, nextstate; //state will stat at 12
reg [1:0] aluOp;
reg INTFlag = 0;
reg NMIFlag = 0;
parameter fetch=0;
parameter decode=1;
parameter memAddr=2;
parameter memRead=3;
parameter memWriteBack=4;
parameter memWrite=5;
parameter execute=6;
parameter aluWriteBack=7;
parameter branch=8;
parameter jump=9;
parameter addIExecute=10;
parameter addIwriteBack=11;
parameter preFetch=12;
parameter jumpAndLinkRegister=13;
parameter jumpRegister=14;
parameter jumpAndLink=15;
parameter sw = 6'b101011;
parameter lw = 6'b100011;
parameter beq = 6'b000100;
parameter jumps = 6'b000010;
parameter rType = 6'b000000;
parameter iType1 = 6'b001100;
parameter iType2 = 6'b001101;
parameter iType3 = 6'b001110;
parameter iType4 = 6'b001111;
parameter jumpAndLinkFunct = 6'b000011;
parameter jumpAndLinkRegisterFunct = 6'b001001;
parameter jumpRegisterFunct = 6'b001000;
//always@(posedge INT,posedge NMI)
//begin
// if(INT == 1) INTFlag = 1;
//if(NMI == 1) NMIFlag = 1;
//if(state == fetch) begin
//INTFlag = 0;NMIFlag = 0;
//end
//end
initial
begin
INA=0;
isBranch=0;
pcWrite=0;
lorD=0;
MemWrite=0;
MemtoReg=0;
IRWrite=0;
aluControl=0;
aluSrcB=0;
PCSource=0;
aluSrcA=0;
RegWrite=0;
RegDst=0;
isInterrupted=0;
end
always@(posedge clk)
begin
state=nextstate;
end
always @(state, op, INT, NMI )
begin
if(INT == 1) INTFlag = 1;
if(NMI == 1) NMIFlag = 1;
case(state)
preFetch:
begin
if(INTD == 1)
begin
if(NMIFlag == 0) // do nothing
begin
isInterrupted = 1'b0;
nextstate = fetch;
INA = 0;
INTFlag = 0;NMIFlag = 0;
end
else // NMI should be taken
begin
RegDst = 2'b10;
MemtoReg=2'b10;
isInterrupted = 1'b1;
INA = 0;
nextstate = fetch;
INTFlag = 0;NMIFlag = 0;
end
end
else
begin
if (INTFlag == 0 & NMIFlag == 0 ) // do nothing
begin
isInterrupted = 1'b0;
nextstate = fetch;
INA = 0;
INTFlag = 0;NMIFlag = 0;
end
else if (INTFlag == 1 & NMIFlag == 0 ) // INT should be taken
begin
RegDst = 2'b10;
MemtoReg=2'b10;
isInterrupted = 1'b1;
INA = 1;
nextstate = fetch;
INTFlag = 0;NMIFlag = 0;
end
else if (INTFlag == 1 & NMIFlag == 1 ) // NMI should be taken
begin
RegDst = 2'b10;
MemtoReg=2'b10;
isInterrupted = 1'b1;
INA = 0;
nextstate = fetch;
INTFlag = 0;NMIFlag = 0;
end
else if (INTFlag == 0 & NMIFlag == 1 ) // NMI should be taken
begin
RegDst = 2'b10;
MemtoReg=2'b10;
isInterrupted = 1'b1;
INA = 0;
nextstate = fetch;
INTFlag = 0;NMIFlag = 0;
end
end
end
fetch:
begin
aluSrcA=1'b0;
lorD= 1'b0;
IRWrite=1'b1;
aluSrcB=2'b01;
aluOp= 2'b00;
pcWrite=1'b1;
PCSource=2'b00;
RegWrite = 1'b0;
MemWrite=1'b0;
isBranch= 1'b0;
MemtoReg=2'b00;
nextstate=decode;
//isInterrupted=0;
end
decode:
begin
IRWrite=1'b0;
aluSrcA=1'b0;
aluSrcB=2'b11;
pcWrite=1'b0;
aluOp= 2'b00;
if(op==lw | op==sw)
begin
nextstate=memAddr;
end
if(op==rType)
begin
nextstate=execute;
end
if(op==beq)
begin
nextstate=branch;
end
if(op==jumps)
begin
nextstate=jump;
end
if(op==jumpAndLinkFunct)
begin
nextstate=jumpAndLink;
end
if( (op==iType1)| (op==iType2)| (op==iType3)| (op==iType4) )
begin
nextstate=addIExecute;
end
end
memAddr:
begin
aluSrcA = 1'b1;
aluSrcB= 2'b10;
aluOp = 2'b00;
if(op==lw)
begin
nextstate=memRead;
end
if(op==sw)
begin
nextstate=memWrite;
end
end
memRead:
begin
lorD = 1'b1;
nextstate=memWriteBack;
end
memWriteBack:
begin
RegDst = 2'b00;
RegWrite = 1'b1;
MemtoReg= 2'b01;
nextstate=preFetch;
end
memWrite:
begin
MemWrite=1'b1;
lorD= 1'b1;
nextstate=preFetch;
end
execute:
begin
aluSrcA= 1'b1;
aluSrcB= 2'b00;
aluOp = 2'b10;
if(funct == jumpAndLinkRegisterFunct)
begin
nextstate = jumpAndLinkRegister;
end
else if(funct == jumpRegisterFunct)
begin
nextstate = jumpRegister;
end
else
begin
nextstate = aluWriteBack;
end
end
aluWriteBack:
begin
RegDst= 2'b01;
RegWrite = 1'b1;
MemtoReg = 2'b00;
nextstate= preFetch;
end
branch:
begin
aluSrcA= 1'b1;
aluSrcB= 2'b00;
aluOp=2'b01;
isBranch= 1'b1;
PCSource = 2'b01;
nextstate= preFetch;
end
jump:
begin
pcWrite= 1'b1;
PCSource= 2'b10;
nextstate= preFetch;
end
addIExecute:
begin
aluSrcA= 1'b1;
aluSrcB= 2'b10;
aluOp = 2'b00;
nextstate = addIwriteBack;
end
addIwriteBack:
begin
RegDst= 2'b01;
RegWrite = 1'b1;
MemtoReg = 2'b00;
nextstate= preFetch;
end
jumpAndLinkRegister:
begin
aluSrcA = 1;
aluSrcB = 0;
PCSource = 0;
RegDst = 2'b11;
MemtoReg = 2'b10;
RegWrite = 1;
aluOp = 2'b10;
isBranch = 0;
pcWrite = 0;
lorD=0;
MemWrite=0;
IRWrite=0;
nextstate= preFetch;
end
jumpRegister:
begin
aluSrcA = 1;
aluSrcB = 0;
PCSource = 0;
// RegDst = 2'b00;
// MemtoReg = 2'b10;
RegWrite = 0;
aluOp = 2'b10;
isBranch=0;
pcWrite=1;
aluSrcB = 0;
lorD=0;
MemWrite=0;
IRWrite=0;
nextstate= preFetch;
end
jumpAndLink:
begin
// aluSrcA = 1;
// aluSrcB = 0;
lorD = 0;
IRWrite=0;
MemWrite=0;
PCSource = 2'b10;
pcWrite = 0;
RegDst = 2'b11;
MemtoReg = 2'b10;
RegWrite = 1;
aluOp = 2'b00;
nextstate= preFetch;
end
endcase
end
always @(aluOp)
begin
if(aluOp == 2'b10) //delegate it ...
begin
case(funct)
6'b100000: //ADD
begin
aluControl = 2'b10;
end
6'b100100: //AND
begin
aluControl = 2'b00;
end
6'b100110: //XOR
begin
aluControl = 2'b01;
end
6'b100010: //SUB
begin
aluControl = 2'b11;
end
6'b001001: //ADD for jal
begin
aluControl = 2'b10;
end
6'b001000: //ADD for jr
begin
aluControl = 2'b10;
end
default : aluControl = 2'bz;
endcase
end
else if(aluOp == 2'b00)
begin
aluControl = 2'b10; //ADD
end
else if(aluOp == 2'b01)
begin
aluControl = 2'b11; //SUB
end
else
begin
aluControl = 2'bz; //impossible !
end
end
endmodule