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
|
module control_unit (
input clk,
input rstn,
input [31:0] instr,
input alu_zero,
output reg [2:0] imm_src,
output pc_we,
output reg mem_addr_src,
output reg mem_we,
output reg instr_we,
output reg [1:0] result_src,
output [3:0] alu_op,
output reg [1:0] alu_a_src,
output reg [1:0] alu_b_src,
output reg rf_we
);
parameter s00_fetch = 4'h0,
s01_decode = 4'h1,
s02_mem_addr = 4'h2,
s03_mem_read = 4'h3,
s04_mem_wb = 4'h4,
s05_mem_write = 4'h5,
s06_execute_r = 4'h6,
s07_alu_wb = 4'h7,
s08_execute_i = 4'h8,
s09_jal = 4'h9,
s10_jalr = 4'ha,
s11_br = 4'hb;
parameter INSTRUCTION_FORMAT_UNKNOWN = 3'b000,
INSTRUCTION_FORMAT_R = 3'b001,
INSTRUCTION_FORMAT_I = 3'b010,
INSTRUCTION_FORMAT_S = 3'b011,
INSTRUCTION_FORMAT_B = 3'b100,
INSTRUCTION_FORMAT_U = 3'b101,
INSTRUCTION_FORMAT_J = 3'b110;
wire [6:0] opcode;
wire [2:0] funct3;
wire [6:0] funct7;
assign opcode = instr[6:0];
assign funct3 = instr[14:12];
assign funct7 = instr[31:25];
always @ (*) begin
case (opcode)
7'b0110011: imm_src <= INSTRUCTION_FORMAT_R; // ADD, SUB, SLL, SLT, SLTU, XOR, SRL, SRA, OR, AND
7'b0010011: imm_src <= INSTRUCTION_FORMAT_I; // ADDI, SLTI, SLTIU, XORI, ORI, ANDI, SLLI, SRLI, SRAI
7'b0000011: imm_src <= INSTRUCTION_FORMAT_I; // LB, LH, LW, LBU, LHU
7'b1100111: imm_src <= INSTRUCTION_FORMAT_I; // JALR
7'b0100011: imm_src <= INSTRUCTION_FORMAT_S; // SB, SH, SW
7'b1100011: imm_src <= INSTRUCTION_FORMAT_B; // BEQ, BNE, BLT, BGE, BLTU, BGEU
7'b0110111: imm_src <= INSTRUCTION_FORMAT_U; // LUI
7'b0010111: imm_src <= INSTRUCTION_FORMAT_U; // AUIPC
7'b1101111: imm_src <= INSTRUCTION_FORMAT_J; // JAL
7'b0001111: imm_src <= INSTRUCTION_FORMAT_I; // FENCE, FENCE.I
7'b1110011: imm_src <= INSTRUCTION_FORMAT_I; // ECALL, EBREAK, CSRRW, CSRRS, CSRRC, CSRRWI, CSRRSI, CSRRCI
default: imm_src <= INSTRUCTION_FORMAT_UNKNOWN;
endcase
end
reg [3:0] state, next_state;
always @ (posedge clk) begin
if (!rstn) state <= s00_fetch;
else state <= next_state;
end
always @ (*) begin
case(state)
s00_fetch: next_state <= s01_decode;
s01_decode: case(opcode)
7'b0000011: next_state <= s02_mem_addr;
7'b0100011: next_state <= s02_mem_addr;
7'b0110011: next_state <= s06_execute_r;
7'b0010011: next_state <= s08_execute_i;
7'b1101111: next_state <= s09_jal;
7'b1100111: next_state <= s10_jalr;
7'b1100011: next_state <= s11_br;
default: next_state <= s00_fetch;
endcase
s02_mem_addr: case(opcode)
7'b0000011: next_state <= s03_mem_read;
7'b0100011: next_state <= s05_mem_write;
default: next_state <= s00_fetch;
endcase
s03_mem_read: next_state <= s04_mem_wb;
s04_mem_wb: next_state <= s00_fetch;
s05_mem_write: next_state <= s00_fetch;
s06_execute_r: next_state <= s07_alu_wb;
s07_alu_wb: next_state <= s00_fetch;
s08_execute_i: next_state <= s07_alu_wb;
s09_jal: next_state <= s07_alu_wb;
s10_jalr: next_state <= s07_alu_wb;
s11_br: next_state <= s00_fetch;
default: next_state <= s00_fetch;
endcase
end
reg branch;
reg pc_update;
reg alu_ctrl;
assign pc_we = ((alu_zero ^ funct3[0] ^ funct3[2]) & branch) | pc_update;
always @ (*) begin
case(state)
s00_fetch: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b00;
alu_b_src <= 2'b10;
alu_ctrl <= 1'b1;
result_src <= 2'b10;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b1;
pc_update <= 1'b1;
branch <= 1'b0;
end
s01_decode: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b01;
alu_b_src <= 2'b01;
alu_ctrl <= 1'b1;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s02_mem_addr: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b10;
alu_b_src <= 2'b01;
alu_ctrl <= 1'b1;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s03_mem_read: begin
mem_addr_src <= 1'b1;
alu_a_src <= 2'b00;
alu_b_src <= 2'b00;
alu_ctrl <= 1'b0;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s04_mem_wb: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b00;
alu_b_src <= 2'b00;
alu_ctrl <= 1'b0;
result_src <= 2'b01;
mem_we <= 1'b0;
rf_we <= 1'b1;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s05_mem_write: begin
mem_addr_src <= 1'b1;
alu_a_src <= 2'b00;
alu_b_src <= 2'b00;
alu_ctrl <= 1'b0;
result_src <= 2'b00;
mem_we <= 1'b1;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s06_execute_r: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b10;
alu_b_src <= 2'b00;
alu_ctrl <= 1'b0;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s07_alu_wb: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b00;
alu_b_src <= 2'b00;
alu_ctrl <= 1'b0;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b1;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s08_execute_i: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b10;
alu_b_src <= 2'b01;
alu_ctrl <= 1'b0;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
s09_jal: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b01;
alu_b_src <= 2'b10;
alu_ctrl <= 1'b1;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b1;
branch <= 1'b0;
end
s10_jalr: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b10;
alu_b_src <= 2'b01;
alu_ctrl <= 1'b1;
result_src <= 2'b10;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b1;
branch <= 1'b0;
end
s11_br: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b10;
alu_b_src <= 2'b00;
alu_ctrl <= 1'b0;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b1;
end
default: begin
mem_addr_src <= 1'b0;
alu_a_src <= 2'b00;
alu_b_src <= 2'b00;
alu_ctrl <= 1'b0;
result_src <= 2'b00;
mem_we <= 1'b0;
rf_we <= 1'b0;
instr_we <= 1'b0;
pc_update <= 1'b0;
branch <= 1'b0;
end
endcase
end
alu_op_decode aod (
.opcode(opcode),
.alu_ctrl(alu_ctrl),
.funct3(funct3),
.funct7(funct7),
.alu_op(alu_op)
);
endmodule
|