-
Notifications
You must be signed in to change notification settings - Fork 1
/
EX_impl.c
366 lines (350 loc) · 14.2 KB
/
EX_impl.c
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
#include "EX_impl.h"
#include "csr.h"
extern struct ex_to_cmt ex_to_cmt_sig[2];
#include "interface.h"
void ALU_calc(struct decode_info decoded, struct rename_info renamed, int offset) {
ex_to_cmt_sig[1].alu[offset].pc = decoded.pc;
ex_to_cmt_sig[1].alu[offset].slot_valid = true;
ex_to_cmt_sig[1].alu[offset].rd_valid = true;
ex_to_cmt_sig[1].alu[offset].ard = decoded.rd;
// arithmetic instructions
switch (decoded.alu_type) {
case ADD:
if (decoded.instr_type == TYPE_I) {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data + decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data + renamed.rs2_data;
}
printf("add PC %x rd %d data = %x rs1 %d rs2 %d imm %d\n", decoded.pc, decoded.rd, ex_to_cmt_sig[1].alu[offset].rd_data, renamed.rs1_phy, renamed.rs2_phy, decoded.imm);
break;
case ADDW:
if (decoded.instr_type == TYPE_I) {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)((int32_t)renamed.rs1_data + (int32_t)decoded.imm);
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)((int32_t)renamed.rs1_data + (int32_t)renamed.rs2_data);
}
break;
case SUB:
if (decoded.instr_type == TYPE_I) {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data - decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data - renamed.rs2_data;
}
break;
case SUBW:
if (decoded.instr_type == TYPE_I) {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)((int32_t)renamed.rs1_data - (int32_t)decoded.imm);
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)((int32_t)renamed.rs1_data - (int32_t)renamed.rs2_data);
}
break;
case SLT:
if (decoded.instr_type == TYPE_I) {
if ((int32_t)renamed.rs1_data < (int32_t)decoded.imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = 1;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = 0;
}
} else {
if ((int32_t)renamed.rs1_data < (int32_t)renamed.rs2_data) {
ex_to_cmt_sig[1].alu[offset].rd_data = 1;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = 0;
}
}
break;
case SLTU:
if (decoded.instr_type == TYPE_I) {
if (renamed.rs1_data < decoded.imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = 1;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = 0;
}
} else {
if (renamed.rs1_data < renamed.rs2_data) {
ex_to_cmt_sig[1].alu[offset].rd_data = 1;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = 0;
}
}
break;
case OR:
if (decoded.instr_type == TYPE_I) {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data | decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data | renamed.rs2_data;
}
break;
case XOR:
if (decoded.instr_type == TYPE_I) {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data ^ decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data ^ renamed.rs2_data;
}
break;
case AND:
if (decoded.instr_type == TYPE_I) {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data & decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data & renamed.rs2_data;
}
break;
case SLL:
if (decoded.shift_imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data << decoded.imm;
printf("slli imm %d\n", decoded.imm);
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data << (renamed.rs2_data & 0x1f);
}
break;
case SLLW:
if (decoded.shift_imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data << decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data << (renamed.rs2_data & 0x1f);
}
break;
case SRL:
if (decoded.shift_imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data >> decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = renamed.rs1_data >> (renamed.rs2_data & 0x1f);
}
break;
case SRLW:
if (decoded.shift_imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data >> decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data >> (renamed.rs2_data & 0x1f);
}
break;
case SRA:
if (decoded.shift_imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data >> decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data >> (renamed.rs2_data & 0x1f);
}
break;
case SRAW:
if (decoded.shift_imm) {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data >> decoded.imm;
} else {
ex_to_cmt_sig[1].alu[offset].rd_data = (int32_t)renamed.rs1_data >> (renamed.rs2_data & 0x1f);
}
break;
case LUI:
ex_to_cmt_sig[1].alu[offset].rd_data = decoded.imm;
break;
case AUIPC:
ex_to_cmt_sig[1].alu[offset].rd_data = decoded.imm + decoded.pc;
break;
default:
break;
}
}
extern char* ram;
extern uint16_t *ram_half;
extern uint32_t *ram_word;
extern uint64_t *ram_double;
void LSU_calc(struct decode_info decoded, struct rename_info renamed, int offset) {
// load/store instruction
ex_to_cmt_sig[1].lsu[offset].pc = decoded.pc;
ex_to_cmt_sig[1].lsu[offset].slot_valid = true;
ex_to_cmt_sig[1].lsu[offset].ard = decoded.rd;
if (decoded.is_load) {
ex_to_cmt_sig[1].lsu[offset].rd_valid = true;
}
printf("decode imm %d rs1 %lx\n", decoded.imm, renamed.rs1_data);
switch (decoded.lsu_type) {
case LB:
ex_to_cmt_sig[1].lsu[offset].rd_data = (int8_t)ram[renamed.rs1_data + decoded.imm - 0x80000000];
break;
case LH:
ex_to_cmt_sig[1].lsu[offset].rd_data = (int16_t)ram_half[(renamed.rs1_data + decoded.imm - 0x80000000) / 2];
break;
case LW:
ex_to_cmt_sig[1].lsu[offset].rd_data = (int32_t)ram_word[(renamed.rs1_data + decoded.imm - 0x80000000) / 4];
break;
case LBU:
ex_to_cmt_sig[1].lsu[offset].rd_data = ram[renamed.rs1_data + decoded.imm - 0x80000000];
break;
case LHU:
ex_to_cmt_sig[1].lsu[offset].rd_data = (uint16_t)ram_half[(renamed.rs1_data + decoded.imm - 0x80000000) / 2];
break;
case LWU:
ex_to_cmt_sig[1].lsu[offset].rd_data = (uint32_t)ram_word[(renamed.rs1_data + decoded.imm - 0x80000000) / 4];
break;
case LD:
ex_to_cmt_sig[1].lsu[offset].rd_data = (uint64_t)ram_double[(renamed.rs1_data + decoded.imm - 0x80000000) / 8];
printf("LD\n");
break;
case SB:
ram[renamed.rs1_data + decoded.imm - 0x80000000] = renamed.rs2_data;
break;
case SH:
ram_half[(renamed.rs1_data + decoded.imm - 0x80000000) / 2] = renamed.rs2_data;
break;
case SW:
ram_word[(renamed.rs1_data + decoded.imm - 0x80000000) / 4] = renamed.rs2_data;
break;
case SD:
printf("SD\n");
ram_double[(renamed.rs1_data + decoded.imm - 0x80000000) / 8] = renamed.rs2_data;
break;
default:
break;
}
}
/*
void CSR_calc(struct decode_info decoded, struct rename_info renamed, int offset) {
// CSR instruction
}*/
void MDU_calc(struct decode_info decoded, struct rename_info renamed, int offset) {
// multiply/division instruction
ex_to_cmt_sig[1].mdu[offset].pc = decoded.pc;
ex_to_cmt_sig[1].mdu[offset].slot_valid = true;
ex_to_cmt_sig[1].mdu[offset].rd_valid = true;
ex_to_cmt_sig[1].mdu[offset].ard = decoded.rd;
switch (decoded.mdu_type) {
case MUL:
ex_to_cmt_sig[1].mdu[offset].rd_data = renamed.rs1_data * renamed.rs2_data;
break;
case MULH:
ex_to_cmt_sig[1].mdu[offset].rd_data = (uint64_t)((int32_t)renamed.rs1_data * (int32_t)renamed.rs2_data) >> 32;
break;
case MULHSU:
ex_to_cmt_sig[1].mdu[offset].rd_data = (uint64_t)((int32_t)renamed.rs1_data * (uint64_t)renamed.rs2_data) >> 32;
break;
case MULHU:
ex_to_cmt_sig[1].mdu[offset].rd_data = ((uint64_t)renamed.rs1_data * (uint64_t)renamed.rs2_data) >> 32;
break;
case DIV:
ex_to_cmt_sig[1].mdu[offset].rd_data = (int32_t)renamed.rs1_data / (int32_t)renamed.rs2_data;
break;
case DIVU:
ex_to_cmt_sig[1].mdu[offset].rd_data = renamed.rs1_data / renamed.rs2_data;
break;
case REM:
ex_to_cmt_sig[1].mdu[offset].rd_data = (int32_t)renamed.rs1_data % (int32_t)renamed.rs2_data;
break;
case REMU:
ex_to_cmt_sig[1].mdu[offset].rd_data = renamed.rs1_data % renamed.rs2_data;
break;
default:
assert(1);
}
}
extern struct jmp_redirectInfo jmp_to_is_sig[2];
void JMP_calc(struct decode_info decoded, struct rename_info renamed, int offset) {
ex_to_cmt_sig[1].jmp[offset].pc = decoded.pc;
ex_to_cmt_sig[1].jmp[offset].slot_valid = true;
ex_to_cmt_sig[1].jmp[offset].ard = decoded.rd;
bool taken = false;
// jmp/branch instruction
// by default we proceed without considering branch, so if branch, redirect
switch (decoded.branch_type) {
case BEQ:
if (renamed.rs1_data == renamed.rs2_data) {
taken = true;
}
break;
case BNE:
if (renamed.rs1_data != renamed.rs2_data) {
taken = true;
}
break;
case BLT:
if ((int32_t)renamed.rs1_data < (int32_t)renamed.rs2_data) {
taken = true;
}
break;
case BGE:
if ((int32_t)renamed.rs1_data > (int32_t)renamed.rs2_data) {
taken = true;
}
break;
case BLTU:
if (renamed.rs1_data < renamed.rs2_data) {
taken = true;
}
break;
case BGEU:
if (renamed.rs1_data > renamed.rs2_data) {
taken = true;
}
break;
case JAL:
taken = true;
// with valid rd
ex_to_cmt_sig[1].jmp[offset].rd_valid = true;
ex_to_cmt_sig[1].jmp[offset].rd_data = decoded.pc + 4;
break;
case JALR:
printf("jalr pc %lx rs1 %d rs2 %d\n", decoded.pc, renamed.rs1_phy, renamed.rs2_phy);
taken = true;
// directly update redirect info
jmp_to_is_sig[1].instr_idx = decoded.instr_idx;
printf("imm %lx rs1_data %lx\n", decoded.imm, renamed.rs1_data);
jmp_to_is_sig[1].redirect_pc = renamed.rs1_data + decoded.imm;
// with valid rd
ex_to_cmt_sig[1].jmp[offset].rd_valid = true;
ex_to_cmt_sig[1].jmp[offset].rd_data = decoded.pc + 4;
break;
default:
printf("Unknown branch type %d\n", decoded.branch_type);
assert(0);
break;
}
if (taken) {
jmp_to_is_sig[1].redirect_valid = true;
jmp_to_is_sig[1].instr_idx = decoded.instr_idx;
if (decoded.branch_type != JALR) {
jmp_to_is_sig[1].redirect_pc = decoded.pc + decoded.imm;
}
jmp_to_is_sig[1].current_jmp = renamed.current_jmp;
} else {
jmp_to_is_sig[1].redirect_valid = false;
}
}
void CSR_calc(struct decode_info decoded, struct rename_info renamed, int offset) {
ex_to_cmt_sig[1].csr[offset].pc = decoded.pc;
ex_to_cmt_sig[1].csr[offset].slot_valid = true;
ex_to_cmt_sig[1].csr[offset].rd_valid = true;
ex_to_cmt_sig[1].csr[offset].ard = decoded.rd;
int csr_addr = decoded.imm;
uint64_t *csr_src = get_csr(csr_addr);
uint64_t rs_data, csr_data = *csr_src;
switch (decoded.csr_type) {
case CSRRW:
rs_data = renamed.rs1_data;
*csr_src = rs_data;
printf("CSRRW rs_data %lx csr_src %lx rd_data %lx\n", rs_data, *csr_src, csr_data);
break;
case CSRRC:
rs_data = renamed.rs1_data;
*csr_src = *csr_src ^ rs_data;
printf("CSRRC rs_data %lx csr_src %lx rd_data %lx\n", rs_data, *csr_src, csr_data);
break;
case CSRRS:
rs_data = renamed.rs1_data;
*csr_src = *csr_src | rs_data;
printf("CSRRS rs_data %lx csr_src %lx rd_data %lx\n", rs_data, *csr_src, csr_data);
break;
case CSRRWI:
rs_data = decoded.rs1;
*csr_src = rs_data;
printf("CSRRWI rs_data %lx csr_src %lx rd_data %lx\n", rs_data, *csr_src, csr_data);
break;
case CSRRCI:
rs_data = decoded.rs1;
*csr_src = *csr_src ^ rs_data;
printf("CSRRCI rs_data %lx csr_src %lx rd_data %lx\n", rs_data, *csr_src, csr_data);
break;
case CSRRSI:
rs_data = decoded.rs1;
*csr_src = *csr_src | rs_data;
printf("CSRRSI rs_data %lx csr_src %lx rd_data %lx\n", rs_data, *csr_src, csr_data);
break;
}
ex_to_cmt_sig[1].csr[offset].rd_data = csr_data;
}