-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2.c
More file actions
executable file
·391 lines (368 loc) · 14.4 KB
/
Copy pathpart2.c
File metadata and controls
executable file
·391 lines (368 loc) · 14.4 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <stdio.h> // for stderr
#include <stdlib.h> // for exit()
#include "types.h"
#include "utils.h"
#include "riscv.h"
void execute_rtype(Instruction, Processor *);
void execute_itype_except_load(Instruction, Processor *);
void execute_branch(Instruction, Processor *);
void execute_jal(Instruction, Processor *);
void execute_jalr(Instruction, Processor *);
void execute_load(Instruction, Processor *, Byte *);
void execute_store(Instruction, Processor *, Byte *);
void execute_ecall(Processor *, Byte *);
void execute_lui(Instruction, Processor *);
void execute_auipc(Instruction, Processor *);
void execute_instruction(uint32_t instruction_bits, Processor *processor,Byte *memory) {
Instruction instruction = parse_instruction(instruction_bits); // Look in utils.c
switch(instruction.opcode) {
case 0x33:
execute_rtype(instruction, processor);
break;
case 0x13:
execute_itype_except_load(instruction, processor);
break;
case 0x3:
execute_load(instruction, processor, memory);
break;
case 0x67:
execute_jalr(instruction, processor);
break;
case 0x23:
execute_store(instruction, processor, memory);
break;
case 0x63:
execute_branch(instruction, processor);
break;
case 0x37:
execute_lui(instruction, processor);
break;
case 0x17:
execute_auipc(instruction, processor);
break;
case 0x6F:
execute_jal(instruction, processor);
break;
case 0x73:
execute_ecall(processor, memory);
break;
default: // undefined opcode
handle_invalid_instruction(instruction);
break;
}
}
void execute_rtype(Instruction instruction, Processor *processor) {
switch(instruction.rtype.funct3) {
case 0x0:
if (instruction.rtype.funct7 == 0x00) {
// add
processor->R[instruction.rtype.rd] = (Word) (((sWord)processor->R[instruction.rtype.rs1]) + ((sWord)
processor->R[instruction.rtype.rs2]));
} else if (instruction.rtype.funct7 == 0x01) {
// mul
processor->R[instruction.rtype.rd] = (Word) (((sWord)processor->R[instruction.rtype.rs1]) * ((sWord)
processor->R[instruction.rtype.rs2]));
} else {
// sub
processor->R[instruction.rtype.rd] = (Word) (((sWord)processor->R[instruction.rtype.rs1]) - ((sWord)
processor->R[instruction.rtype.rs2]));
}
break;
case 0x1:
if (instruction.rtype.funct7 == 0x00) {
// sll
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] << processor->R[instruction.rtype.rs2];
} else {
// mulh
processor->R[instruction.rtype.rd] = (Word) ((sDouble) processor->R[instruction.rtype.rs1]) + ((sDouble)
processor->R[instruction.rtype.rs2]);
}
break;
case 0x2:
// slt
processor->R[instruction.rtype.rd] = (((sWord) processor->R[instruction.rtype.rs1]) < ((sWord) processor->R[instruction.rtype.rs2]))? 1 : 0;
break;
case 0x4:
if (instruction.rtype.funct7 == 0x00) {
// xor
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] ^ processor->R[instruction.rtype.rs2];
} else {
// div
processor->R[instruction.rtype.rd] = (Word) (((sWord)processor->R[instruction.rtype.rs1]) / ((sWord)
processor->R[instruction.rtype.rs2]));
}
break;
case 0x5:
if (instruction.rtype.funct7 == 0x00) {
// srl
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] >> processor->R[instruction.rtype.rs2];
} else {
// sra
processor->R[instruction.rtype.rd] = (Word) (((sWord) processor->R[instruction.rtype.rs1]) >> ((Word)
processor->R[instruction.rtype.rs2]));
}
break;
case 0x6:
if (instruction.rtype.funct7 == 0x00) {
// or
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] | processor->R[instruction.rtype.rs2];
} else {
// rem
processor->R[instruction.rtype.rd] = (Word) (((sWord)processor->R[instruction.rtype.rs1]) % ((sWord)
processor->R[instruction.rtype.rs2]));
}
break;
case 0x7:
// and
processor->R[instruction.rtype.rd] = processor->R[instruction.rtype.rs1] & processor->R[instruction.rtype.rs2];
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
processor->PC += 4;
}
void execute_itype_except_load(Instruction instruction, Processor *processor) {
int shift = -1;
switch(instruction.itype.funct3) {
case 0x0:
// addi
processor->R[instruction.itype.rd] = (Word) (((sWord)processor->R[instruction.itype.rs1]) + sign_extend_number(instruction.itype.imm, 12));
break;
case 0x1:
// slli
processor->R[instruction.itype.rd] = processor->R[instruction.itype.rs1] << sign_extend_number(instruction.itype.imm, 12);
break;
case 0x2:
// slti
processor->R[instruction.itype.rd] = (((sWord) processor->R[instruction.itype.rs1]) < sign_extend_number(instruction.itype.imm, 12))? 1 : 0;
break;
case 0x3:
//sltiu
processor->R[instruction.itype.rd] = ((processor->R[instruction.itype.rs1]) < sign_extend_number(instruction.itype.imm, 12))? 1 : 0;
break;
case 0x4:
// xori
processor->R[instruction.itype.rd] = processor->R[instruction.itype.rs1] ^ sign_extend_number(instruction.itype.imm, 12);
break;
case 0x5:
shift = (instruction.itype.imm & 0x01f);
if ((instruction.itype.imm & 0xfe0) == 0x00) {
// srli
processor->R[instruction.itype.rd] = processor->R[instruction.itype.rs1] >> shift;
} else {
// srai
processor->R[instruction.itype.rd] = (Word) (((sWord)processor->R[instruction.itype.rs1]) >> shift);
}
break;
case 0x6:
// ori
processor->R[instruction.itype.rd] = processor->R[instruction.itype.rs1] | sign_extend_number(instruction.itype.imm, 12);
break;
case 0x7:
// andi
processor->R[instruction.itype.rd] = processor->R[instruction.itype.rs1] & sign_extend_number(instruction.itype.imm, 12);
break;
default:
handle_invalid_instruction(instruction);
break;
}
processor->PC += 4;
}
void execute_ecall(Processor *p, Byte *memory) {
Register i;
// What do we switch on?
switch(p->R[10]) {
case 1: // print an integer
printf("%d",p->R[11]);
p->PC += 4;
break;
case 4: // print a string
for(i = p->R[11]; i < MEMORY_SPACE && load(memory, i, LENGTH_BYTE); i++) {
printf("%c",load(memory,i,LENGTH_BYTE));
}
break;
case 10: // exit
printf("exiting the simulator\n");
exit(0);
break;
case 11: // print a character
printf("%c",p->R[11]);
break;
default: // undefined ecall
printf("Illegal ecall number %d\n", p->R[10]);
exit(-1);
break;
}
}
void execute_branch(Instruction instruction, Processor *processor) {
int branchAddr = 4;
switch(instruction.sbtype.funct3) {
case 0x0:
//beq
if (((sWord)processor->R[instruction.sbtype.rs1]) == ((sWord)processor->R[instruction.sbtype.rs2])) {
branchAddr = (sWord) get_branch_offset(instruction);
}
break;
case 0x1:
//bne
if (((sWord)processor->R[instruction.sbtype.rs1]) != ((sWord)processor->R[instruction.sbtype.rs2])) {
branchAddr = (sWord) get_branch_offset(instruction);
}
break;
case 0x4:
//blt
if (((sWord)processor->R[instruction.sbtype.rs1]) < ((sWord)processor->R[instruction.sbtype.rs2])){
branchAddr = (sWord)get_branch_offset(instruction);
}
break;
case 0x5:
//bge
if (((sWord)processor->R[instruction.sbtype.rs1]) >= ((sWord)processor->R[instruction.sbtype.rs2])){
branchAddr = (sWord)get_branch_offset(instruction);
}
break;
case 0x6:
//bltu
if ((processor->R[instruction.sbtype.rs1]) < processor->R[instruction.sbtype.rs2]){
branchAddr = (sWord)get_branch_offset(instruction);
}
break;
case 0x7:
//bgeu
if ((processor->R[instruction.sbtype.rs1]) >= processor->R[instruction.sbtype.rs2]){
branchAddr = (sWord)get_branch_offset(instruction);
}
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
processor->PC += branchAddr;
}
void execute_load(Instruction instruction, Processor *processor, Byte *memory) {
switch(instruction.itype.funct3) {
case 0x0:
// lb
processor->R[instruction.itype.rd] = sign_extend_number(load(memory, ((sWord)
processor->R[instruction.itype.rs1]) + ((sWord)
sign_extend_number(instruction.itype.imm, 12)), LENGTH_BYTE), 8);
break;
case 0x1:
// lh
processor->R[instruction.itype.rd] = sign_extend_number(load(memory, ((sWord)
processor->R[instruction.itype.rs1]) + ((sWord)
sign_extend_number(instruction.itype.imm, 12)), LENGTH_HALF_WORD), 16);
break;
case 0x2:
// lw
processor->R[instruction.itype.rd] = load(memory, ((sWord)
processor->R[instruction.itype.rs1]) + ((sWord)
sign_extend_number(instruction.itype.imm, 12)), LENGTH_WORD);
break;
case 0x4:
//lbu
processor->R[instruction.itype.rd] = load(memory, (processor->R[instruction.itype.rs1]) + (sign_extend_number(instruction.itype.imm, 12)), LENGTH_BYTE);
break;
case 0x5:
//lhu
processor->R[instruction.itype.rd] = load(memory, (processor->R[instruction.itype.rs1]) + (sign_extend_number(instruction.itype.imm, 12)), LENGTH_HALF_WORD);
break;
default:
handle_invalid_instruction(instruction);
break;
}
processor->PC += 4;
}
void execute_store(Instruction instruction, Processor *processor, Byte *memory) {
switch(instruction.stype.funct3) {
case 0x0:
// sb
store(memory, ((sWord) processor->R[instruction.stype.rs1]) + ((sWord)get_store_offset(instruction)), LENGTH_BYTE,
processor->R[instruction.stype.rs2]);
break;
case 0x1:
// sh
store(memory, ((sWord) processor->R[instruction.stype.rs1]) + ((sWord)get_store_offset(instruction)), LENGTH_HALF_WORD,
processor->R[instruction.stype.rs2]);
break;
case 0x2:
// sw
store(memory, ((sWord) processor->R[instruction.stype.rs1]) + ((sWord)get_store_offset(instruction)), LENGTH_WORD,
processor->R[instruction.stype.rs2]);
break;
default:
handle_invalid_instruction(instruction);
exit(-1);
break;
}
processor->PC += 4;
}
void execute_jal(Instruction instruction, Processor *processor) {
int nextPC = processor->PC + (sWord) get_jump_offset(instruction);
processor->R[instruction.ujtype.rd] = processor->PC + 4;
processor->PC = nextPC;
}
void execute_jalr(Instruction instruction, Processor *processor) {
int nextPC = processor->R[instruction.itype.rs1] + sign_extend_number(instruction.itype.imm, 12);
processor->R[instruction.itype.rd] = processor->PC + 4;
processor->PC = nextPC;
}
void execute_lui(Instruction instruction, Processor *processor) {
int imm = (instruction.utype.imm << 12);
processor->R[instruction.utype.rd] = imm;
processor->PC += 4;
}
void execute_auipc(Instruction instruction, Processor *processor) {
processor->R[instruction.utype.rd] = processor->PC + (instruction.utype.imm << 12);
processor->PC += 4;
}
void store(Byte *memory,Address address,Alignment alignment,Word value) {
if((address >= MEMORY_SPACE)) {
handle_invalid_write(address);
}
switch(alignment) {
case LENGTH_BYTE:
memory[address] = (Byte)(value & 0x000000ff);
break;
case LENGTH_HALF_WORD:
memory[address] = (Byte)(value & 0x000000ff);
memory[address+1] = (Byte) ((value & 0x0000ff00) >> 8);
break;
case LENGTH_WORD:
memory[address] = (Byte)(value & 0x000000ff);
memory[address+1] = (Byte)((value & 0x0000ff00) >> 8);
memory[address+2] = (Byte)((value & 0x00ff0000) >> 16);
memory[address+3] = (Byte)((value & 0xff000000) >> 24);
break;
default:
return;
break;
}
}
Word load(Byte *memory,Address address,Alignment alignment) {
if((address >= MEMORY_SPACE)) {
handle_invalid_read(address);
}
uint32_t value = 0x00000000;
switch(alignment) {
case LENGTH_BYTE:
value = value | memory[address];
break;
case LENGTH_HALF_WORD:
value = value | memory[address];
value = value | (memory[address+1] << 8);
break;
case LENGTH_WORD:
value = value | memory[address];
value = value | (memory[address+1] << 8);
value = value | (memory[address+2] << 16);
value = value | (memory[address+3] << 24);
break;
default:
break;
}
return value;
}