-
Notifications
You must be signed in to change notification settings - Fork 4
/
decode.sv
243 lines (231 loc) · 6.92 KB
/
decode.sv
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
import definitions::*;
import cache_interface_types::*;
module decode(
input instruction_t instr_i,
output decode_out_t decode_o
);
always_comb begin
decode_o.regfile_we = 0;
decode_o.csr_we = 0;
decode_o.dcache_wr_enable = 0;
decode_o.dcache_rd_signed = 0;
decode_o.is_branch = 0;
decode_o.is_jump = 0;
decode_o.is_ecall = 0;
decode_o.is_mem_access = 0;
decode_o.is_muldiv = 0;
/* Fixed values */
decode_o.muldiv_op = funct3_op_muldiv_t'(instr_i.rtype.funct3);
/* Default don't care values */
decode_o.alu_op = ALU_OP_IN1_PASSTHROUGH;
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_REGFILE_OUT2;
decode_o.compare_unit_op = COMPARE_UNIT_OP_EQ;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_ALU_OUT;
decode_o.dcache_rd_size = CACHE_ACCESS_SIZE_WORD;
decode_o.dcache_wr_size = CACHE_ACCESS_SIZE_WORD;
priority case (instr_i.common.opcode)
OPCODE_LUI: begin
decode_o.regfile_we = 1;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_ALU_OUT;
decode_o.alu_op = ALU_OP_IN1_PASSTHROUGH;
decode_o.alu_in1_sel = ALU_IN1_SEL_IMM;
end
OPCODE_AUIPC: begin
decode_o.regfile_we = 1;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_ALU_OUT;
decode_o.alu_op = ALU_OP_ADD;
decode_o.alu_in1_sel = ALU_IN1_SEL_IMM;
decode_o.alu_in2_sel = ALU_IN2_SEL_PC;
end
OPCODE_JAL: begin
decode_o.regfile_we = 1;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_PC_4;
decode_o.alu_op = ALU_OP_ADD;
decode_o.alu_in1_sel = ALU_IN1_SEL_IMM;
decode_o.alu_in2_sel = ALU_IN2_SEL_PC;
decode_o.is_jump = 1;
end
OPCODE_JALR: begin
decode_o.regfile_we = 1;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_PC_4;
decode_o.alu_op = ALU_OP_ADD;
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_IMM;
decode_o.is_jump = 1;
end
OPCODE_BRANCH: begin
decode_o.alu_op = ALU_OP_ADD;
decode_o.alu_in1_sel = ALU_IN1_SEL_IMM;
decode_o.alu_in2_sel = ALU_IN2_SEL_PC;
decode_o.is_branch = 1;
priority case (instr_i.btype.funct3)
FUNCT3_BRANCH_BEQ:
decode_o.compare_unit_op = COMPARE_UNIT_OP_EQ;
FUNCT3_BRANCH_BNE:
decode_o.compare_unit_op = COMPARE_UNIT_OP_NE;
FUNCT3_BRANCH_BLT:
decode_o.compare_unit_op = COMPARE_UNIT_OP_LT;
FUNCT3_BRANCH_BGE:
decode_o.compare_unit_op = COMPARE_UNIT_OP_GE;
FUNCT3_BRANCH_BLTU:
decode_o.compare_unit_op = COMPARE_UNIT_OP_LTU;
FUNCT3_BRANCH_BGEU:
decode_o.compare_unit_op = COMPARE_UNIT_OP_GEU;
endcase
end
OPCODE_LOAD: begin
decode_o.regfile_we = 1;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_DMEM_RD_DATA;
decode_o.alu_op = ALU_OP_ADD;
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_IMM;
decode_o.is_mem_access = 1;
priority case (instr_i.itype.funct3)
FUNCT3_LOAD_LB, FUNCT3_LOAD_LBU:
decode_o.dcache_rd_size = CACHE_ACCESS_SIZE_BYTE;
FUNCT3_LOAD_LH, FUNCT3_LOAD_LHU:
decode_o.dcache_rd_size = CACHE_ACCESS_SIZE_HALF;
FUNCT3_LOAD_LW:
decode_o.dcache_rd_size = CACHE_ACCESS_SIZE_WORD;
endcase
priority case (instr_i.itype.funct3)
FUNCT3_LOAD_LB, FUNCT3_LOAD_LH, FUNCT3_LOAD_LW:
decode_o.dcache_rd_signed = 1;
endcase
end
OPCODE_STORE: begin
decode_o.dcache_wr_enable = 1;
decode_o.alu_op = ALU_OP_ADD;
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_IMM;
decode_o.is_mem_access = 1;
priority case (instr_i.itype.funct3)
FUNCT3_STORE_SB:
decode_o.dcache_wr_size = CACHE_ACCESS_SIZE_BYTE;
FUNCT3_STORE_SH:
decode_o.dcache_wr_size = CACHE_ACCESS_SIZE_HALF;
FUNCT3_STORE_SW:
decode_o.dcache_wr_size = CACHE_ACCESS_SIZE_WORD;
endcase
end
OPCODE_OP_IMM: begin
decode_o.regfile_we = 1;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_ALU_OUT;
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_IMM;
priority case (instr_i.itype.funct3)
FUNCT3_OP_IMM_ADDI:
decode_o.alu_op = ALU_OP_ADD;
FUNCT3_OP_IMM_SLTI:
decode_o.alu_op = ALU_OP_SLT;
FUNCT3_OP_IMM_SLTIU:
decode_o.alu_op = ALU_OP_SLTU;
FUNCT3_OP_IMM_XORI:
decode_o.alu_op = ALU_OP_XOR;
FUNCT3_OP_IMM_ORI:
decode_o.alu_op = ALU_OP_OR;
FUNCT3_OP_IMM_ANDI:
decode_o.alu_op = ALU_OP_AND;
FUNCT3_OP_IMM_SLLI:
decode_o.alu_op = ALU_OP_SLL;
FUNCT3_OP_IMM_SRI:
if (instr_i.itype.imm[10])
decode_o.alu_op = ALU_OP_SRA;
else
decode_o.alu_op = ALU_OP_SRL;
endcase
end
OPCODE_OP: begin
decode_o.regfile_we = 1;
if (instr_i.rtype.funct7 == FUNCT7_OP_MULDIV) begin
decode_o.is_muldiv = 1;
end else begin
decode_o.regfile_wr_sel = REGFILE_WR_SEL_ALU_OUT;
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_REGFILE_OUT2;
priority case (instr_i.rtype.funct3)
FUNCT3_OP_ADD_SUB: begin
if (instr_i.rtype.funct7[5])
decode_o.alu_op = ALU_OP_SUB;
else
decode_o.alu_op = ALU_OP_ADD;
end
FUNCT3_OP_SLL:
decode_o.alu_op = ALU_OP_SLL;
FUNCT3_OP_SLT:
decode_o.alu_op = ALU_OP_SLT;
FUNCT3_OP_SLTU:
decode_o.alu_op = ALU_OP_SLTU;
FUNCT3_OP_XOR:
decode_o.alu_op = ALU_OP_XOR;
FUNCT3_OP_SR:
if (instr_i.rtype.funct7[5])
decode_o.alu_op = ALU_OP_SRA;
else
decode_o.alu_op = ALU_OP_SRL;
FUNCT3_OP_OR:
decode_o.alu_op = ALU_OP_OR;
FUNCT3_OP_AND:
decode_o.alu_op = ALU_OP_AND;
endcase
end
end
OPCODE_MISC_MEM: begin
priority case (instr_i.itype.funct3)
FUNCT3_MISC_MEM_FENCE:
; /* TODO */
FUNCT3_MISC_MEM_FENCE_I:
; /* TODO */
endcase
end
OPCODE_SYSTEM: begin
decode_o.regfile_we = 1;
decode_o.csr_we = 1;
decode_o.regfile_wr_sel = REGFILE_WR_SEL_CSR_OUT;
priority case (instr_i.itype.funct3)
FUNCT3_SYSTEM_PRIV: begin
decode_o.regfile_we = 0;
decode_o.csr_we = 0;
priority case (instr_i.itype.imm)
FUNCT12_SYSTEM_PRIV_ECALL:
/* TODO */
decode_o.is_ecall = 1;
FUNCT12_SYSTEM_PRIV_EBREAK:
/* TODO */
;
endcase
end
FUNCT3_SYSTEM_CSRRW: begin
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_op = ALU_OP_IN1_PASSTHROUGH;
end
FUNCT3_SYSTEM_CSRRS: begin
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_CSR_OUT;
decode_o.alu_op = ALU_OP_OR;
end
FUNCT3_SYSTEM_CSRRC: begin
decode_o.alu_in1_sel = ALU_IN1_SEL_REGFILE_OUT1;
decode_o.alu_in2_sel = ALU_IN2_SEL_CSR_OUT;
decode_o.alu_op = ALU_OP_XOR;
end
FUNCT3_SYSTEM_CSRRWI: begin
decode_o.alu_in1_sel = ALU_IN1_SEL_IMM;
decode_o.alu_op = ALU_OP_IN1_PASSTHROUGH;
end
FUNCT3_SYSTEM_CSRRSI: begin
decode_o.alu_in1_sel = ALU_IN1_SEL_IMM;
decode_o.alu_in2_sel = ALU_IN2_SEL_CSR_OUT;
decode_o.alu_op = ALU_OP_OR;
end
FUNCT3_SYSTEM_CSRRCI: begin
decode_o.alu_in1_sel = ALU_IN1_SEL_IMM;
decode_o.alu_in2_sel = ALU_IN2_SEL_CSR_OUT;
decode_o.alu_op = ALU_OP_XOR;
end
endcase
end
endcase
end
endmodule