-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.v
360 lines (327 loc) · 6.84 KB
/
decoder.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
`include "global_config.h"
`include "nettype.h"
`include "stddef.h"
`include "cpu.h"
`include "isa.h"
`timescale 1ns/1ns
module decoder(
if_pc,if_insn,if_en,
gpr_rd_data_0,gpr_rd_data_1,gpr_rd_addr_0,gpr_rd_addr_1,
id_en,id_dst_addr,id_gpr_we_,id_mem_op,
ex_en,ex_dst_addr,ex_gpr_we_,ex_fwd_data,
mem_fwd_data,
exe_mode,creg_rd_data,creg_rd_addr,
alu_op,alu_in_0,alu_in_1,br_addr,br_taken,br_flag,
mem_op,mem_wr_data,ctrl_op,dst_addr,gpr_we_,exp_code,ld_hazard
);
input if_en;
input[31:0] gpr_rd_data_0,gpr_rd_data_1;
input id_en;
input[4:0] id_dst_addr;
input id_gpr_we_;
input[1:0] id_mem_op;
input ex_en;
input[4:0] ex_dst_addr;
input ex_gpr_we_;
input[31:0] ex_fwd_data;
input[31:0] mem_fwd_data;
input exe_mode;
input[31:0] creg_rd_data;
input[29:0] if_pc;
input[31:0] if_insn;
output[4:0] gpr_rd_addr_0;
output[4:0] gpr_rd_addr_1;
output[4:0] creg_rd_addr;
output[3:0] alu_op;
output[31:0] alu_in_0,alu_in_1;
output[29:0] br_addr;
output br_taken;
output br_flag;
output[1:0] mem_op;
output[31:0] mem_wr_data;
output[1:0] ctrl_op;
output[4:0] dst_addr;
output gpr_we_;
output[2:0] exp_code;
output ld_hazard;
wire[29:0] if_pc;
wire[31:0] if_insn;
reg if_en;
reg[3:0] alu_op;
reg[31:0] alu_in_0,alu_in_1;
reg[29:0] br_addr;
reg br_flag,br_taken;
reg[1:0] mem_op;
reg[1:0] ctrl_op;
reg[4:0] dst_addr;
reg gpr_we_;
reg[2:0] exp_code;
reg ld_hazard;
wire[`IsaOpBus] op = if_insn[`IsaOpLoc];
wire[`RegAddrBus] ra_addr = if_insn[`IsaRaAddrLoc];
wire[`RegAddrBus] rb_addr = if_insn[`IsaRbAddrLoc];
wire[`RegAddrBus] rc_addr = if_insn[`IsaRcAddrLoc];
wire[`IsaImmBus] imm = if_insn[`IsaImmLoc];
wire[`WordDataBus] imm_s = {{`ISA_EXT_W{imm[`ISA_IMM_MSB]}},imm};
wire[`WordDataBus] imm_u = {{`ISA_EXT_W{1'b0}},imm};
assign gpr_rd_addr_0 = ra_addr;
assign gpr_rd_addr_1 = rb_addr;
assign creg_rd_addr = ra_addr;
reg[`WordDataBus] ra_data;
wire signed[`WordDataBus] s_ra_data = $signed(ra_data);
reg[`WordDataBus] rb_data;
wire signed[`WordDataBus] s_rb_data = $signed(rb_data);
assign mem_wr_data = rb_data;
wire[`WordDataBus] ret_addr = if_pc + 1'b1;
wire[`WordDataBus] br_target = if_pc + imm_s[`WORD_ADDR_MSB:0];
wire[`WordDataBus] jr_target = ra_data[`WordAddrLoc];
always@(*)
begin
if((id_en == `ENABLE) && (id_gpr_we_ == `ENABLE_)
&& (id_dst_addr == ra_addr))
begin
ra_data = ex_fwd_data;
end
else if((ex_en == `ENABLE) && (id_gpr_we_ == `ENABLE_)
&&(ex_dst_addr == ra_addr))
begin
ra_data = mem_fwd_data;
end
else
begin
ra_data = gpr_rd_data_0;
end
if((id_en == `ENABLE) && (id_gpr_we_ == `ENABLE_)
&& (id_dst_addr == rb_addr))
begin
rb_data = ex_fwd_data;
end
else if((ex_en == `ENABLE) && (id_gpr_we_ == `ENABLE_)
&& (ex_dst_addr == rb_addr))
begin
rb_data = mem_fwd_data;
end
else
begin
rb_data = gpr_rd_data_1;
end
end
always@(*)
begin
if((id_en == `ENABLE) && (id_mem_op == `MEM_OP_LDW)
&& ((id_dst_addr == ra_addr)||(id_dst_addr == rb_addr)))
begin
ld_hazard = `ENABLE;
end
else
begin
ld_hazard = `DISABLE;
end
end
always@(*)
begin
alu_op = `ALU_OP_NOP;
alu_in_0 = ra_data;
alu_in_1 = rb_data;
br_taken = `DISABLE;
br_flag = `DISABLE;
br_addr = {`WORD_ADDR_W{1'b0}};
mem_op = `MEM_OP_NOP;
ctrl_op = `CTRL_OP_NOP;
dst_addr = rb_addr;
gpr_we_ = `DISABLE_;
exp_code = `ISA_EXP_NO_EXP;
case(op)
`ISA_OP_ANDR:
begin
alu_op = `ALU_OP_AND;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_ANDI:
begin
alu_op = `ALU_OP_AND;
alu_in_1 = imm_u;
gpr_we_ = `ENABLE_;
end
`ISA_OP_ORR:
begin
alu_op = `ALU_OP_OR;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_ORI:
begin
alu_op = `ALU_OP_OR;
alu_in_1 = imm_u;
gpr_we_ = `ENABLE_;
end
`ISA_OP_XORR:
begin
alu_op = `ALU_OP_XOR;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_XORI:
begin
alu_op = `ALU_OP_XOR;
alu_in_1 = imm_u;
gpr_we_ = `ENABLE_;
end
`ISA_OP_ADDSR:
begin
alu_op = `ALU_OP_ADDS;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_ADDSI:
begin
alu_op = `ALU_OP_ADDS;
alu_in_1 = imm_s;
gpr_we_ = `ENABLE_;
end
`ISA_OP_ADDUR:
begin
alu_op = `ALU_OP_ADDU;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_ADDUI:
begin
alu_op = `ALU_OP_ADDU;
alu_in_1 = imm_u;
gpr_we_ = `ENABLE_;
end
`ISA_OP_SUBSR:
begin
alu_op = `ALU_OP_SUBS;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_SUBUR:
begin
alu_op = `ALU_OP_SUBU;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_SHRLR:
begin
alu_op = `ALU_OP_SHRL;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_SHRLI:
begin
alu_op = `ALU_OP_SHRL;
alu_in_1 = imm_u;
gpr_we_ = `ENABLE_;
end
`ISA_OP_SHLLR:
begin
alu_op = `ALU_OP_SHLL;
dst_addr = rc_addr;
gpr_we_ = `ENABLE_;
end
`ISA_OP_SHLLI:
begin
alu_op = `ALU_OP_SHLL;
alu_in_1 = imm_u;
gpr_we_ = `ENABLE_;
end
`ISA_OP_BE:
begin
br_addr = br_target;
br_taken = (ra_data == rb_data) ? `ENABLE : `DISABLE;
br_flag = `ENABLE;
end
`ISA_OP_BNE:
begin
br_addr = br_target;
br_taken = (ra_data != rb_data) ? `ENABLE : `DISABLE;
br_flag = `ENABLE;
end
`ISA_OP_BSGT:
begin
br_addr = br_target;
br_taken = (s_ra_data < s_rb_data) ? `ENABLE : `DISABLE;
br_flag = `ENABLE;
end
`ISA_OP_BUGT:
begin
br_addr = br_target;
br_taken = (ra_data < rb_data) ? `ENABLE : `DISABLE;
br_flag = `ENABLE;
end
`ISA_OP_JMP:
begin
br_addr = jr_target;
br_taken = `ENABLE;
br_flag = `ENABLE;
end
`ISA_OP_CALL:
begin
alu_in_0 = {ret_addr,{`BYTE_OFFSET_W{1'b0}}};
br_addr = jr_target;
br_taken = `ENABLE;
br_flag = `ENABLE;
dst_addr = `REG_ADDR_W'd31;
gpr_we_ = `ENABLE_;
end
`ISA_OP_LDW:
begin
alu_op = `ALU_OP_ADDU;
alu_in_1 =imm_s;
mem_op = `MEM_OP_LDW;
gpr_we_ = `ENABLE_;
end
`ISA_OP_STW:
begin
alu_op = `ALU_OP_ADDU;
alu_in_1 = imm_s;
mem_op = `MEM_OP_STW;
end
`ISA_OP_TRAP:
begin
exp_code = `ISA_EXP_TRAP;
end
`ISA_OP_RDCR:
begin
if(exe_mode == `CPU_KERNEL_MODE)
begin
alu_in_0 = creg_rd_data;
gpr_we_ = `ENABLE_;
end
else
begin
exp_code = `ISA_EXP_PRV_VIO;
end
end
`ISA_OP_WRCR:
begin
if(exe_mode == `CPU_KERNEL_MODE)
begin
ctrl_op = `CTRL_OP_WRCR;
end
else
begin
exp_code = `ISA_EXP_PRV_VIO;
end
end
`ISA_OP_EXRT:
begin
if(exe_mode == `CPU_KERNEL_MODE)
begin
ctrl_op = `CTRL_OP_EXRT;
end
else
begin
exp_code = `ISA_EXP_PRV_VIO;
end
end
default:
begin
exp_code = `ISA_EXP_UNDEF_INSN;
end
endcase
end
endmodule