-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctrl.v
311 lines (266 loc) · 8.52 KB
/
ctrl.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
`timescale 1ns / 1ps
// nexys3MIPSSoC is a MIPS implementation originated from COAD projects
// Copyright (C) 2014 @Wenri, @dtopn, @Speed
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
module ctrl(clk,
reset,
Inst_in,
zero,
overflow,
MIO_ready,
MemRead,
MemWrite,
ALU_operation,
state_out,
CPU_MIO,
IorD,
IRWrite,
RegDst,
RegWrite,
MemtoReg,
ALUSrcA,
ALUSrcB,
PCSource,
PCWrite,
PCWriteCond,
Beq,
CauseWrite,
IntCause,
EPCWrite,
Co0Write,
Ireq,
Iack,
Enable_i
);
input clk,reset;
input zero,overflow,MIO_ready,Ireq,Enable_i;
input [31:0] Inst_in;
output [2:0] ALU_operation,MemtoReg,PCSource;
output CPU_MIO,MemRead,MemWrite,IorD,IRWrite,RegWrite,ALUSrcA,PCWrite,PCWriteCond,Beq,CauseWrite,IntCause,EPCWrite,Iack,Co0Write;
output [4:0] state_out;
output [1:0] RegDst,ALUSrcB;
wire [4:0] state_out;
wire reset,MIO_ready,Ireq,Enable_i;
reg CPU_MIO,MemRead,MemWrite,IorD,IRWrite,RegWrite,ALUSrcA,PCWrite,PCWriteCond,Beq,CauseWrite,EPCWrite,Iack,Co0Write;
reg [1:0] RegDst,ALUSrcB,IntCause;
reg [2:0] ALU_operation, MemtoReg, PCSource;
reg [4:0] state;
parameter IF = 5'b00000, ID=5'b00001, EX_R= 5'b00010, EX_Mem=5'b00011, EX_I=5'b00100,
Lui_WB=5'b00101, EX_beq=5'b00110, EX_bne= 5'b00111, EX_jr= 5'b01000, EX_JAL=5'b01001,
Exe_J = 5'b01010, MEM_RD=5'b01011, MEM_WD= 5'b01100, WB_R= 5'b01101, WB_I=5'b01110,
WB_LW=5'b01111, EX_jalr=5'b10000, EX_INT=5'b10001, EX_ERET=5'b10010, Error=5'b11111;
parameter AND=3'b000, OR=3'b001, ADD=3'b010, SUB=3'b110, NOR=3'b100, SLT=3'b111, XOR=3'b011, SRL=3'b101;
`define CPU_ctrl_signals {PCSource[2],MemtoReg[2],Co0Write,CauseWrite,EPCWrite,PCWrite,PCWriteCond,IorD,MemRead,MemWrite,IRWrite,MemtoReg[1:0],PCSource[1:0],ALUSrcB,ALUSrcA,RegWrite,RegDst,CPU_MIO}
// EX_INT 0, 1, 1, 0, 0, 0, 0, 0, 00, 11, 01, 0, 0, 00, 0
// EX_ERET 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 00, 00, 11, 0, 0, 00, 0
// EX_JM 1, 0, 0, 0, 0, 0, 00, 10, 11, 0, 0, 00, 0
// IF 1, 0, 0, 1, 0, 1, 00, 00, 01, 0, 0, 00, 1
// IF 0, 0, 0, 0, 0, 0, 00, 00, 11, 0, 0, 00, 0
// 1 0 0 0 0 0 10 10 00 0 0 00 0
//
`define nSignals 22
assign state_out=state;
always @ (posedge clk or posedge reset)
if (reset==1) begin
`CPU_ctrl_signals<=`nSignals'h12821; //12821
ALU_operation<=ADD;
state <= IF;
Iack <= 0;
end
else begin
Iack <= 0;
case (state)
IF: begin
if(MIO_ready) begin
if(Ireq)begin
Iack <= 1;
`CPU_ctrl_signals<=`nSignals'h701A0;
ALU_operation<=SUB;
state <= EX_INT;
end else begin
`CPU_ctrl_signals<=`nSignals'h00060;
ALU_operation<=ADD;
state <= ID;
end
end else begin
state <=IF;
`CPU_ctrl_signals<=`nSignals'h12821;
end
end
ID: if (!Enable_i) begin
`CPU_ctrl_signals<=`nSignals'h00060;
ALU_operation<=ADD;
state <= ID;
end else begin
case (Inst_in[31:26])
6'b000000:begin //R-type OP
`CPU_ctrl_signals<=`nSignals'h00010;
state <= EX_R;
case (Inst_in[5:0])
6'b100000: ALU_operation<=ADD;
6'b100010: ALU_operation<=SUB;
6'b100100: ALU_operation<=AND;
6'b100101: ALU_operation<=OR;
6'b100111: ALU_operation<=NOR;
6'b101010: ALU_operation<=SLT;
6'b000010: ALU_operation<=SRL; //shfit 1bit right
6'b000000: ALU_operation<=XOR;
6'b001000: begin
`CPU_ctrl_signals<=`nSignals'h10010;
ALU_operation<=ADD; state <= EX_jr; end
6'b001001:begin
`CPU_ctrl_signals<=`nSignals'h00208; //rd << current_pc==ori_pc+4
ALU_operation<=ADD; state <=EX_jalr;end
default: ALU_operation <= ADD;
endcase
end
6'b100011:begin //Lw
`CPU_ctrl_signals<=`nSignals'h00050;
ALU_operation<=ADD;
state <= EX_Mem;
end
6'b101011:begin //Sw
`CPU_ctrl_signals<=`nSignals'h00050;
ALU_operation<=ADD;
state <= EX_Mem;
end
6'b000010:begin //Jump
`CPU_ctrl_signals<=`nSignals'h10160;
state <= Exe_J;
end
6'b000100:begin //Beq
`CPU_ctrl_signals<=`nSignals'h08090; Beq<=1;
ALU_operation<= SUB; state <= EX_beq; end
6'b000101:begin //Bne
`CPU_ctrl_signals<=`nSignals'h08090; Beq<=0;
ALU_operation<= SUB; state <= EX_bne; end
6'b000011:begin //Jal
`CPU_ctrl_signals<=`nSignals'h1076c;
state <= EX_JAL;
end
6'b001000:begin //Addi
`CPU_ctrl_signals<=`nSignals'h00050;
ALU_operation <= ADD;
state <= EX_I;
end
6'b001100:begin //Andi
`CPU_ctrl_signals<=`nSignals'h00050;
ALU_operation <= AND;
state <= EX_I;
end
6'b001101:begin //Ori
`CPU_ctrl_signals<=`nSignals'h00050;
ALU_operation <= OR;
state <= EX_I;
end
6'b001110:begin //Xori
`CPU_ctrl_signals<=`nSignals'h00050;
ALU_operation <= XOR;
state <= EX_I;
end
6'b001010:begin //Slti
`CPU_ctrl_signals<=`nSignals'h00050;
ALU_operation <= SLT;
state <= EX_I;
end
6'b001111:begin //Lui
`CPU_ctrl_signals<=`nSignals'h00468;
state <= Lui_WB;
end
6'b010000: if(Inst_in[25]) begin //COP0
case (Inst_in[5:0])
6'b011000: begin
`CPU_ctrl_signals<=`nSignals'h210060;
state <= EX_ERET;
end
default: begin
`CPU_ctrl_signals<=`nSignals'h12821;
state <= Error;
end
endcase
end
default: begin
`CPU_ctrl_signals<=`nSignals'h12821;
state <= Error;
end
endcase
end //end ID
EX_jalr:begin
`CPU_ctrl_signals<=`nSignals'h10018;
ALU_operation<=ADD; state <= EX_jr;
end
EX_Mem:begin
if(Inst_in[31:26]==6'b100011)begin
`CPU_ctrl_signals<=`nSignals'h06051; state <= MEM_RD; end
else if(Inst_in[31:26]==6'b101011)begin
`CPU_ctrl_signals<=`nSignals'h05051; state <= MEM_WD; end
end
MEM_RD:begin
if(MIO_ready)begin
`CPU_ctrl_signals<=`nSignals'h00208; state <= WB_LW; end
else begin
state <=MEM_RD; `CPU_ctrl_signals<=`nSignals'h06050; end
end
MEM_WD:begin
if(MIO_ready)begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
else begin
state <=MEM_WD; `CPU_ctrl_signals<=`nSignals'h05050; end
end
WB_LW:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <=IF; end
EX_R:begin
`CPU_ctrl_signals<=`nSignals'h0001a; state <= WB_R; end
EX_I:begin
`CPU_ctrl_signals<=`nSignals'h00058; state <= WB_I; end
WB_R:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
WB_I:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
Exe_J:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
EX_bne:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
EX_beq:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
EX_jr:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
EX_JAL:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
Lui_WB:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
EX_INT:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
EX_ERET:begin
`CPU_ctrl_signals<=`nSignals'h12821;
ALU_operation<=ADD; state <= IF; end
Error: state <= Error;
default: begin
`CPU_ctrl_signals<=`nSignals'h12821; Beq<=0;
ALU_operation<=ADD; state <= Error; end
endcase
end
endmodule