-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathriscv_sim.c
560 lines (455 loc) · 10.9 KB
/
riscv_sim.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// 컴퓨터구조 P2_RISCV_Simulator
// 컴퓨터학부 2020112757 김유진
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
//clock cycles
long long cycles = 0;
// registers
long long int regs[32];
// program counter
unsigned long pc = 0;
// memory
#define INST_MEM_SIZE 32*1024
#define DATA_MEM_SIZE 32*1024
unsigned long inst_mem[INST_MEM_SIZE]; //instruction memory
unsigned long long data_mem[DATA_MEM_SIZE]; //data memory
//misc. function
int init(char* filename);
void print_cycles();
void print_reg();
void print_pc();
// code
char inst[33];
char inst32[33];
char opcode[8];
char funct3[4];
char funct7[8];
char rd[6];
char rs1[6];
char rs2[6];
char I_imm[13];
char S_imm[13];
// control
int memto_reg;
int reg_write;
int mem_read;
int mem_write;
int branch;
int PCSrs;
// index, value, pc
int rd_index;
int rs1_index;
int rs2_index;
int rd_value;
int rs1_value;
int rs2_value;
int imm_value;
int ALUresult;
int prePC;
int branch_pc;
int return_pc;
int read_from_mem;
char type[10];
// 2진수 문자열을 10진수 숫자로 변환하는 함수
int read_bin(char code[])
{
int result = 0;
int code_len = strlen(code);
char convert[13] = { 0, };
// 상수가 음수일 때 2의 보수로 구하기!
if (code_len == 12 && code[0] == '1')
{
// 0->1 , 1->0
for (int i = 0; code[i]; i++)
{
if (code[i] == '0') convert[i] = '1';
else if (code[i] == '1') convert[i] = '0';
}
// 2진수 -> 10진수
for (int i = 0; code[i]; i++)
{
result = (result << 1) + convert[i] - '0';
}
result += 1;
result = -result;
}
// 2진수 -> 10진수
else
{
for (int i = 0; code[i]; i++)
{
result = (result << 1) + code[i] - '0';
}
}
return result;
}
//fetch an instruction from a instruction memory
void fetch() {
//printf("inst_mem[%d] = %d\n", pc/4, inst_mem[pc/4]);
//printf("inst_mem[%d] to bin = %32s\n", pc/4, _itoa(inst_mem[pc/4], inst, 2));
// instruction 가져오기
_itoa(inst_mem[pc / 4], inst, 2);
memset(inst32, '0', 33);
memset(opcode, '\0', 8);
memset(funct3, '\0', 4);
memset(funct7, '\0', 8);
memset(rd, '\0', 6);
memset(rs1, '\0', 6);
memset(rs2, '\0', 6);
memset(I_imm, '\0', 13);
memset(S_imm, '\0', 13);
int len = strlen(inst);
strcpy(inst32 + (32 - len), inst);
//printf("%s\n", inst32);
strncpy(opcode, inst32 + 25, 7);
//printf("opcode: %s\n", opcode);
strncpy(rd, inst32 + 20, 5);
//printf("rd: %s\n", rd);
strncpy(funct3, inst32 + 17, 3);
//printf("funct3: %s\n", funct3);
strncpy(rs1, inst32 + 12, 5);
//printf("rs1: %s\n", rs1);
strncpy(rs2, inst32 + 7, 5);
//printf("rs2: %s\n", rs2);
strncpy(funct7, inst32, 7);
//printf("funct7: %s\n", funct7);
strcat(I_imm, funct7);
strcat(I_imm, rs2);
//printf("I_imm: %s\n", I_imm);
strcat(S_imm, funct7);
strcat(S_imm, rd);
//printf("S_imm: %s\n", S_imm);
prePC = pc;
pc += 4; // PC update
}
//decode the instruction and read data from register file
void decode() {
//add
if (strcmp(opcode, "0110011") == 0 && strcmp(funct7, "0000000") == 0)
{
// rd
rd_index = read_bin(rd);
//printf("rd_index = x%d\n", rd_index);
// rs1
rs1_index = read_bin(rs1);
rs1_value = regs[rs1_index];
//printf("rs1_index, rs1_value = x%d, %d\n", rs1_index, rs1_value);
// rs2
rs2_index = read_bin(rs2);
rs2_value = regs[rs2_index];
//printf("rs2_index, rs2_value = x%d, %d\n", rs2_index, rs2_value);
strcpy(type, "add\0");
memto_reg = 0;
reg_write = 1;
mem_read = 0;
mem_write = 0;
branch = 0;
PCSrs = 0;
}
//addi
if (strcmp(opcode, "0010011") == 0 && strcmp(funct3, "000") == 0)
{
// rd
rd_index = read_bin(rd);
//printf("rd_index = x%d\n", rd_index);
// rs1
rs1_index = read_bin(rs1);
rs1_value = regs[rs1_index];
//printf("rs1_index, rs1_value = x%d, %d\n", rs1_index, rs1_value);
// I_imm
imm_value = read_bin(I_imm);
//printf("imm_value = %d\n", imm_value);
strcpy(type, "addi\0");
memto_reg = 0;
reg_write = 1;
mem_read = 0;
mem_write = 0;
branch = 0;
PCSrs = 0;
}
//beq
if (strcmp(opcode, "1100011") == 0 && strcmp(funct3, "000") == 0)
{
// rs1
rs1_index = read_bin(rs1);
rs1_value = regs[rs1_index];
//printf("rs1_index, rs1_value = x%d, %d\n", rs1_index, rs1_value);
// rs2
rs2_index = read_bin(rs2);
rs2_value = regs[rs2_index];
//printf("rs2_index, rs2_value = x%d, %d\n", rs2_index, rs2_value);
// branch_offset
int beq_imm[13] = { '\0', };
strncat(beq_imm, S_imm, 1);
strncat(beq_imm, S_imm + 11, 1);
strncat(beq_imm, S_imm + 1, 6);
strncat(beq_imm, S_imm + 7, 4);
imm_value = read_bin(beq_imm);
//printf("imm_value = %d\n", imm_value);
strcpy(type, "beq\0");
reg_write = 0;
mem_read = 0;
mem_write = 0;
branch = 1;
PCSrs = 0;
}
//jal
if (strcmp(opcode, "1101111") == 0)
{
// rd
rd_index = read_bin(rd);
//printf("rd_index = x%d\n", rd_index);
// branch_offset
int jal_imm[13] = { '\0', };
strncat(jal_imm, I_imm, 1);
strncat(jal_imm, I_imm + 12, 8);
strncat(jal_imm, I_imm + 11, 1);
strncat(jal_imm, I_imm + 1, 10);
imm_value = read_bin(jal_imm);
//printf("imm_value = %d\n", imm_value);
strcpy(type, "jal\0");
reg_write = 0;
mem_read = 0;
mem_write = 0;
branch = 1;
PCSrs = 0;
}
//jalr
if (strcmp(opcode, "1100111") == 0 && strcmp(funct3, "000") == 0)
{
// rd
rd_index = read_bin(rd);
//printf("rd_index = x%d\n", rd_index);
// rs1
rs1_index = read_bin(rs1);
rs1_value = regs[rs1_index];
//printf("rs1_index, rs1_value = x%d, %d\n", rs1_index, rs1_value);
// I_imm
imm_value = read_bin(I_imm);
//printf("imm_value = %d\n", imm_value);
strcpy(type, "jalr\0");
reg_write = 0;
mem_read = 0;
mem_write = 0;
branch = 1;
PCSrs = 0;
}
//sd
if (strcmp(opcode, "0100011") == 0)
{
// rs1: BaseAddress
rs1_index = read_bin(rs1);
rs1_value = regs[rs1_index];
//printf("rs1_index, rs1_value = x%d, %d\n", rs1_index, rs1_value);
// offset
imm_value = read_bin(S_imm);
//printf("imm_value = %d\n", imm_value);
// rs2: 저장할 register
rs2_index = read_bin(rs2);
rs2_value = regs[rs2_index];
//printf("rs2_index, rs2_value = x%d, %d\n", rs2_index, rs2_value);
strcpy(type, "sd\0");
reg_write = 0;
mem_read = 0;
mem_write = 1;
branch = 0;
PCSrs = 0;
}
// ld
if (strcmp(opcode, "0000011") == 0 && strcmp(funct3, "011") == 0)
{
// rd
rd_index = read_bin(rd);
//printf("rd_index = x%d\n", rd_index);
// rs1
rs1_index = read_bin(rs1);
rs1_value = regs[rs1_index];
//printf("rs1_index, rs1_value = x%d, %d\n", rs1_index, rs1_value);
// I_imm
imm_value = read_bin(I_imm);
//printf("imm_value = %d\n", imm_value);
strcpy(type, "ld\0");
memto_reg = 1;
reg_write = 1;
mem_read = 1;
mem_write = 0;
branch = 0;
PCSrs = 0;
}
//printf("%s\n", type);
}
//perform the appropriate operation
void exe() {
if (strcmp(type, "add") == 0)
{
ALUresult = rs1_value + rs2_value;
}
if (strcmp(type, "addi") == 0)
{
ALUresult = rs1_value + imm_value;
}
if (strcmp(type, "sd") == 0)
{
ALUresult = rs1_value + imm_value; // 주소값 계산
}
if (strcmp(type, "ld") == 0)
{
ALUresult = rs1_value + imm_value; // 주소값 계산
}
if (strcmp(type, "beq") == 0)
{
ALUresult = rs1_value - rs2_value; // rs1과 rs2가 같은지 확인. 같으면 0
branch_pc = prePC + (imm_value << 1); // imm << 1한후 branch 주소 계산
if (!ALUresult && branch) PCSrs = 1;
}
if (strcmp(type, "jal") == 0)
{
return_pc = pc; // 돌아갈 주소
branch_pc = prePC + (imm_value << 1); // imm << 1한후 branch 주소 계산
PCSrs = 1;
}
if (strcmp(type, "jalr") == 0)
{
return_pc = pc; // 돌아갈 주소
branch_pc = rs1_value + imm_value; // branch 주소 계산
PCSrs = 1;
}
}
//access the data memory
void mem() {
if (strcmp(type, "sd") == 0) {
data_mem[ALUresult] = rs2_value; // 메모리에 데이터 savd
}
if (strcmp(type, "ld") == 0) {
read_from_mem = data_mem[ALUresult]; // 메모리부터 데이터 load
}
if (PCSrs == 1) pc = branch_pc; // beq일 경우 여기서 PC update
}
//write result of arithmetic operation or data read from the data memory if required
void wb() {
if (memto_reg == 0 && reg_write == 1) // R type
regs[rd_index] = ALUresult;
if (memto_reg == 1 && reg_write == 1) // ld
regs[rd_index] = read_from_mem;
if (strcmp(type, "jal") == 0)
regs[rd_index] = return_pc;
if (strcmp(type, "jalr") == 0)
regs[rd_index] = return_pc;
regs[0] = 0;
}
int main(int ac, char* av[])
{
if (ac < 3)
{
printf("./riscv_sim filename mode\n");
return -1;
}
char done = 0;
if (init(av[1]) != 0)
return -1;
while (!done)
{
fetch();
decode();
exe();
mem();
wb();
cycles++; //increase clock cycle
//if debug mode, print clock cycle, pc, reg
if (*av[2] == '0') {
print_cycles(); //print clock cycles
print_pc(); //print pc
print_reg(); //print registers
}
// check the exit condition, do not delete!!
if (regs[9] == 10) //if value in $t1 is 10, finish the simulation
done = 1;
}
if (*av[2] == '1')
{
print_cycles(); //print clock cycles
print_pc(); //print pc
print_reg(); //print registers
}
return 0;
}
/* initialize all datapat elements
//fill the instruction and data memory
//reset the registers
*/
int init(char* filename)
{
FILE* fp = fopen(filename, "r");
int i;
long inst;
if (fp == NULL)
{
fprintf(stderr, "Error opening file.\n");
return -1;
}
/* fill instruction memory */
i = 0;
while (fscanf(fp, "%lx", &inst) == 1)
{
inst_mem[i++] = inst;
}
/*reset the registers*/
for (i = 0; i < 32; i++)
{
regs[i] = 0;
}
/*reset pc*/
pc = 0;
/*reset clock cycles*/
cycles = 0;
return 0;
}
void print_cycles()
{
printf("---------------------------------------------------\n");
printf("Clock cycles = %lld\n", cycles);
}
void print_pc()
{
printf("PC = %ld\n\n", pc);
}
void print_reg()
{
printf("x0 = %d\n", regs[0]);
printf("x1 = %d\n", regs[1]);
printf("x2 = %d\n", regs[2]);
printf("x3 = %d\n", regs[3]);
printf("x4 = %d\n", regs[4]);
printf("x5 = %d\n", regs[5]);
printf("x6 = %d\n", regs[6]);
printf("x7 = %d\n", regs[7]);
printf("x8 = %d\n", regs[8]);
printf("x9 = %d\n", regs[9]);
printf("x10 = %d\n", regs[10]);
printf("x11 = %d\n", regs[11]);
printf("x12 = %d\n", regs[12]);
printf("x13 = %d\n", regs[13]);
printf("x14 = %d\n", regs[14]);
printf("x15 = %d\n", regs[15]);
printf("x16 = %d\n", regs[16]);
printf("x17 = %d\n", regs[17]);
printf("x18 = %d\n", regs[18]);
printf("x19 = %d\n", regs[19]);
printf("x20 = %d\n", regs[20]);
printf("x21 = %d\n", regs[21]);
printf("x22 = %d\n", regs[22]);
printf("x23 = %d\n", regs[23]);
printf("x24 = %d\n", regs[24]);
printf("x25 = %d\n", regs[25]);
printf("x26 = %d\n", regs[26]);
printf("x27 = %d\n", regs[27]);
printf("x28 = %d\n", regs[28]);
printf("x29 = %d\n", regs[29]);
printf("x30 = %d\n", regs[30]);
printf("x31 = %d\n", regs[31]);
printf("\n");
}