-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1157 lines (937 loc) · 42.6 KB
/
main.c
File metadata and controls
1157 lines (937 loc) · 42.6 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
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Note:
Sometimes you will find expressions like these:
(void)func();
casting to void is used to silence warnings from clang-tidy
*/
#define _GNU_SOURCE
#include <elf.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct {
uint8_t opcode;
uint8_t rd;
uint8_t funct3;
uint8_t rs1;
uint8_t rs2;
uint8_t funct7;
} r_instruction_t;
typedef struct {
uint8_t opcode;
uint8_t rd;
uint8_t funct3;
uint8_t rs1;
uint16_t imm;
} i_instruction_t;
typedef struct {
uint8_t opcode;
uint8_t funct3;
uint8_t rs1;
uint8_t rs2;
uint16_t imm;
} s_instruction_t;
typedef struct {
uint8_t opcode;
uint8_t rd;
uint32_t imm;
} u_instruction_t;
typedef struct {
uint64_t x[31];
uint64_t pc;
} hart_state_t;
typedef struct {
char file_name[256];
bool debug;
size_t ram_size;
} flags_t;
typedef struct {
void *vm_memory;
uint64_t translation_offset;
uint64_t memory_size;
uint64_t code_segment_min;
uint64_t code_segment_max;
} memory_config_t;
r_instruction_t fetch_r(uint32_t instruction) {
r_instruction_t new_instruction;
new_instruction.rd = (instruction >> 7) & 0x1F;
new_instruction.funct3 = (instruction >> 12) & 0x07;
new_instruction.rs1 = (instruction >> 15) & 0x1F;
new_instruction.rs2 = (instruction >> 20) & 0x1F;
new_instruction.funct7 = (instruction >> 25) & 0x7F;
return new_instruction;
}
i_instruction_t fetch_i(uint32_t instruction) {
i_instruction_t new_instruction;
new_instruction.rd = (instruction >> 7) & 0x1F;
new_instruction.funct3 = (instruction >> 12) & 0x07;
new_instruction.rs1 = (instruction >> 15) & 0x1F;
new_instruction.imm = (instruction >> 20) & 0xFFF;
return new_instruction;
}
s_instruction_t fetch_s(uint32_t instruction) {
s_instruction_t new_instruction;
new_instruction.funct3 = (instruction >> 12) & 0x07;
new_instruction.rs1 = (instruction >> 15) & 0x1F;
new_instruction.rs2 = (instruction >> 20) & 0x1F;
new_instruction.imm = ((instruction >> 7) & 0x1F) | ((instruction >> 20) & 0xFE0);
return new_instruction;
}
u_instruction_t fetch_u(uint32_t instruction) {
u_instruction_t new_instruction;
new_instruction.rd = (instruction >> 7) & 0x1F;
new_instruction.imm = (instruction >> 12) & 0xFFFFF;
return new_instruction;
}
/* Basically, J instructions are just drunk U instructions */
/* Mission of this function is to sober J instr */
u_instruction_t fetch_j(uint32_t instruction) {
u_instruction_t new_instruction;
new_instruction.rd = (instruction >> 7) & 0x1F;
new_instruction.imm = 0;
// wth
new_instruction.imm |= ((instruction >> 31) & 0x1) << 20; // imm[20]
new_instruction.imm |= ((instruction >> 12) & 0xFF) << 12; // imm[19:12]
new_instruction.imm |= ((instruction >> 20) & 0x1) << 11; // imm[11]
new_instruction.imm |= ((instruction >> 21) & 0x3FF) << 1; // imm[10:1]
return new_instruction;
}
/* the same thing as fetch_j */
s_instruction_t fetch_b(uint32_t instruction) {
s_instruction_t new_instruction;
new_instruction.funct3 = (instruction >> 12) & 0x07;
new_instruction.rs1 = (instruction >> 15) & 0x1F;
new_instruction.rs2 = (instruction >> 20) & 0x1F;
new_instruction.imm = ((instruction >> 7) & 0x1E) | // imm[4:1]
((instruction >> 20) & 0x7E0) | // imm[10:5]
((instruction >> 31) & 0x1) << 12 | // imm[12]
((instruction << 4) & 0x800); // imm[11]
return new_instruction;
}
inline static void debug_fn(hart_state_t hart) {
static int to_skip = 0;
if (to_skip > 0) {
to_skip--;
return;
}
while (true) {
char buffer[128];
printf("debug> ");
if (fgets(buffer, sizeof buffer, stdin) == NULL) {
printf("Fatal: fread has returned NULL\n");
return;
}
if (buffer[0] == '\n') {
continue;
}
buffer[strcspn(buffer, "\n")] = 0;
char *endptr;
char *command = strtok(buffer, " ");
char *token = NULL;
if (strcmp(command, "reg") == 0) {
while ((token = strtok(NULL, " ")) != NULL) {
if (strcmp(token, "pc") == 0) {
printf("pc = 0x%lx\n", hart.pc);
} else if (token[0] == 'x') {
char *reg_num_ptr = &token[1];
int reg_num = (int) strtol(reg_num_ptr, &endptr, 10);
printf("x%d = %ld\n", reg_num, hart.x[reg_num]);
} else {
printf("Usage: \"reg xN\" or \"reg pc\"\n"
"Example 1: reg x0\n"
"Example 2: reg pc\n");
}
}
} else if (strcmp(command, "step") == 0) {
token = strtok(NULL, " ");
if (token != NULL) {
to_skip = (int) strtol(token, &endptr, 10) - 1;
}
return;
} else if (strcmp(command, "exit") == 0) {
exit(0);
} else {
printf("Fatal: Invalid command: %s\n", command);
}
}
}
uint64_t extend_sign(uint64_t value, size_t in_size) {
if (((uint64_t) 0b1 << (in_size - 1)) & value) {
return value | (UINT64_MAX << in_size);
}
return value;
}
inline static flags_t parse_flags(int argc, char **argv) {
char *endptr;
flags_t flags;
flags.ram_size = 0;
bool found_filename = false;
bool found_ram_size = false;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0) {
flags.debug = true;
} else if (strcmp(argv[i], "-n") == 0) {
if (argc - 1 == i) {
printf("Fatal: Expected filename after -n option\n");
exit(1);
}
i++;
strncpy(flags.file_name, argv[i], 256);
found_filename = true;
} else if (strcmp(argv[i], "-m") == 0) {
if (argc - 1 == i) {
printf("Fatal: Expected ram size after -m option\n");
exit(1);
}
i++;
flags.ram_size = strtol(argv[i], &endptr, 10);
found_ram_size = true;
} else {
printf("Fatal: Invalid command line argument: %s\n", argv[i]);
exit(1);
}
}
if (!(found_filename)) {
printf("Fatal: no filename specified\n");
exit(1);
}
if (!(found_ram_size)) {
printf("Warning: no ram size was specified. If the code uses stack or "
"heap it will lead to fatal error\n");
}
return flags;
}
inline static memory_config_t setup_memory(flags_t flags) {
// what function will return
memory_config_t memory_config;
memory_config.code_segment_max = 0;
// open file
FILE *file = fopen(flags.file_name, "rb");
if (file == NULL) {
printf("Fatal: Can't open file: %s\n", flags.file_name);
exit(1);
}
// load & parse elf header
Elf64_Ehdr elf_header;
if (fread(&elf_header, sizeof elf_header, 1, file) != 1) {
printf("Fatal: Can't read elf header\n");
exit(1);
}
uint64_t entry_point = elf_header.e_entry;
// load & parse program headers
Elf64_Phdr *program_headers = malloc((long) elf_header.e_phnum * elf_header.e_phentsize);
if (fseek(file, (long) elf_header.e_phoff, SEEK_SET) != 0) {
printf("Fatal: fseek return non zero value\n");
exit(1);
}
if (fread(program_headers, (long) elf_header.e_phnum * elf_header.e_phentsize, 1, file) != 1) {
printf("Fatal: fread retrun error\n");
exit(1);
}
// find the right ammount of memory for vm
uint64_t min_address = UINT64_MAX; // vaddr // this is also offset for translation
uint64_t max_address = 0; // vaddr + memsz
for (int i = 0; i < elf_header.e_phnum; i++) {
Elf64_Phdr phdr = program_headers[i];
if (phdr.p_type == PT_LOAD) {
if (phdr.p_vaddr < min_address) {
min_address = phdr.p_vaddr;
}
if (phdr.p_vaddr + phdr.p_memsz > max_address) {
max_address = phdr.p_vaddr + phdr.p_memsz;
}
// find the code segment size
if (phdr.p_flags & 1) {
memory_config.code_segment_max = phdr.p_vaddr + phdr.p_memsz;
}
}
}
if (memory_config.code_segment_max == 0) {
printf("Fatal: no entry in ELF file\n");
exit(1);
}
memory_config.code_segment_min = entry_point - min_address;
memory_config.code_segment_max = memory_config.code_segment_max - min_address;
// allocate enough memory
void *vm_memory = malloc(max_address - min_address + flags.ram_size);
if (vm_memory == NULL) {
printf("Fatal: malloc returned null\n");
exit(1);
}
memset(vm_memory, 0, max_address - min_address + flags.ram_size);
// load segments in memory
for (int i = 0; i < elf_header.e_phnum; i++) {
Elf64_Phdr phdr = program_headers[i];
if (phdr.p_type == PT_LOAD) {
if (fseek(file, (long) phdr.p_offset, SEEK_SET) != 0) {
printf("Fatal: fseek returned non zero value");
exit(1);
}
int fread_code = fread(&((uint8_t *) vm_memory)[phdr.p_vaddr - min_address], phdr.p_filesz, 1, file);
if ((fread_code != 1) && (fread_code != 0)) {
printf("Fatal: fread returned error: %d\n", fread_code);
exit(1);
}
}
}
// clean
free(program_headers);
// return
memory_config.memory_size = max_address - min_address + flags.ram_size;
memory_config.vm_memory = vm_memory;
memory_config.translation_offset = min_address;
return memory_config;
}
void print_memory_error(
uint64_t temp, memory_config_t memory_config, hart_state_t main_hart, uint32_t instruction, i_instruction_t i_instr,
char *function_name
) {
printf(
"Fatal: virtual memory address is out of bounds: 0x%lx\n"
"Memory size: %lu, emulated memory size: %lu, the real address: %lu\n"
"Difference between memory size and the address: %ld\n"
"Instruction that caused error: 0x%x (%s)\n"
"PC = %lu (transl. PC = %lu, t. PC / 4 = %lu)\n"
"Entering post-crash debugging\n",
temp, memory_config.memory_size, memory_config.memory_size + memory_config.translation_offset,
extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1], memory_config.memory_size - temp, instruction,
function_name, main_hart.pc, main_hart.pc - memory_config.code_segment_min,
(main_hart.pc - memory_config.code_segment_min) / 4
);
}
int main(int argc, char **argv) {
flags_t flags = parse_flags(argc, argv);
memory_config_t memory_config = setup_memory(flags);
hart_state_t main_hart;
main_hart.pc = memory_config.code_segment_min;
memset(main_hart.x, 0, sizeof main_hart.x);
r_instruction_t r_instr;
i_instruction_t i_instr;
s_instruction_t s_instr;
u_instruction_t u_instr;
uint64_t temp;
main_hart.x[2] = memory_config.memory_size + memory_config.translation_offset - 1;
while (main_hart.pc < memory_config.code_segment_max) {
uint32_t instruction = ((uint32_t *) memory_config.vm_memory)[main_hart.pc / 4];
uint8_t opcode = instruction & 0x7F;
if (flags.debug) {
printf("opcode: %x\n", opcode);
}
switch (opcode & 0b11) {
/* This case is respobsible for all 32-bit lenght instructions */
case 0b11:
/* because the lower 2 bits of an opcode determine the instruction
lenght, We don't need them anymore - we are inside of case, where
all of the instructions are 32-bit in length. Thus, code below
don't need these bits */
switch (opcode >> 2) {
/* The lui instruction */
case 0b01101:
u_instr = fetch_u(instruction);
if (u_instr.rd == 0) {
break;
}
main_hart.x[u_instr.rd] = u_instr.imm << 12;
break;
/* The auipc instruction */
case 0b00101:
u_instr = fetch_u(instruction);
main_hart.x[u_instr.rd] = (u_instr.imm << 12) + main_hart.pc + memory_config.translation_offset;
break;
/* The next case is responsible for a lot of I-type instructions */
/* They differ by the "funct" parameter */
case 0b00100:
i_instr = fetch_i(instruction);
if (i_instr.rd == 0) {
break;
}
switch (i_instr.funct3 & 0b111) {
/* The addi instruction */
case 0b000:
main_hart.x[i_instr.rd] = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1];
break;
/* The slti instruction */
case 0b010:
if ((int64_t) main_hart.x[i_instr.rs1] < (int64_t) extend_sign(i_instr.imm, 12)) {
main_hart.x[i_instr.rd] = 1;
} else {
main_hart.x[i_instr.rd] = 0;
}
break;
/* The sltiu instruction */
case 0b011:
if (main_hart.x[i_instr.rs1] < i_instr.imm) {
main_hart.x[i_instr.rd] = 1;
} else {
main_hart.x[i_instr.rd] = 0;
}
break;
/* The xori instruction */
case 0b100:
main_hart.x[i_instr.rd] = i_instr.imm ^ main_hart.x[i_instr.rs1];
break;
/* The ori instruction */
case 0b110:
main_hart.x[i_instr.rd] = i_instr.imm | main_hart.x[i_instr.rs1];
break;
/* The andi instruction */
case 0b111:
main_hart.x[i_instr.rd] = i_instr.imm & main_hart.x[i_instr.rs1];
break;
/* The slli instruction */
case 0b001:
main_hart.x[i_instr.rd] = main_hart.x[i_instr.rs1] << (i_instr.imm & 0x1F);
break;
/* The next case is responsible for two instructions: srli and
* srai */
/* they differ by the highest 5 bits of "imm" value */
case 0b101:
switch (i_instr.imm >> 7) {
/* The srli instruction */
case 0b00000:
main_hart.x[i_instr.rd] = main_hart.x[i_instr.rs1] >> (i_instr.imm & 0x1F);
break;
/* The srai instruction */
case 0b01000:
main_hart.x[i_instr.rd] = (int64_t) main_hart.x[i_instr.rs1] >> (i_instr.imm & 0x1F);
break;
}
break;
}
/* The end of case that is responsible for a lot of I-type
* instructions */
break;
/* This case is responsible for a lot of R-type instructions */
case 0b01100:
r_instr = fetch_r(instruction);
if (r_instr.rd == 0) {
break;
}
switch (r_instr.funct7) {
case 0b0000000:
switch (r_instr.funct3) {
/* The add instruction */
case 0b000:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] + main_hart.x[r_instr.rs2];
break;
/* The sll instrucion */
case 0b001:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] << (main_hart.x[r_instr.rs2] & 0b11111);
break;
/* The slt instruction*/
case 0b010:
if ((int64_t) main_hart.x[r_instr.rs1] < (int64_t) main_hart.x[r_instr.rs2]) {
main_hart.x[r_instr.rd] = 1;
} else {
main_hart.x[r_instr.rd] = 0;
}
break;
/* The sltu instruction */
case 0b011:
if (main_hart.x[r_instr.rs1] < main_hart.x[r_instr.rs2]) {
main_hart.x[r_instr.rd] = 1;
} else {
main_hart.x[r_instr.rd] = 0;
}
break;
/* The xor instruction*/
case 0b100:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] ^ main_hart.x[r_instr.rs2];
break;
/* The srl instruction*/
case 0b101:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] >> (main_hart.x[r_instr.rs2] & 0b11111);
break;
/* The or instruction */
case 0b110:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] | main_hart.x[r_instr.rs2];
break;
/* The and instruction */
case 0b111:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] & main_hart.x[r_instr.rs2];
break;
}
break;
case 0b100000:
switch (r_instr.funct3) {
/* The sub instruction */
case 0b000:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] - main_hart.x[r_instr.rs2];
break;
/* The sra instruction */
case 0b101:
main_hart.x[r_instr.rd] = (int32_t) main_hart.x[r_instr.rs1] >>
((int32_t) main_hart.x[r_instr.rs2] & 0b11111);
break;
}
break;
case 0b0000001:
/* RV64M/RV32M instructions */
switch (r_instr.funct3) {
case 0b000:
main_hart.x[r_instr.rd] = (uint64_t) ((__int128_t) main_hart.x[r_instr.rs1] *
(__int128_t) main_hart.x[r_instr.rs2]);
break;
case 0b001:
main_hart.x[r_instr.rd] = (uint64_t) (((__int128_t) main_hart.x[r_instr.rs1] *
(__int128_t) main_hart.x[r_instr.rs2]) >>
64);
break;
case 0b010:
main_hart.x[r_instr.rd] = (uint64_t) (((__int128_t) main_hart.x[r_instr.rs1] *
(__uint128_t) main_hart.x[r_instr.rs2]) >>
64);
break;
case 0b011:
main_hart.x[r_instr.rd] = (uint64_t) (((__uint128_t) main_hart.x[r_instr.rs1] *
(__uint128_t) main_hart.x[r_instr.rs2]) >>
64);
break;
case 0b100:
main_hart.x[r_instr.rd] = (int32_t) main_hart.x[r_instr.rs1] /
(int32_t) main_hart.x[r_instr.rs2];
break;
case 0b101:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] / main_hart.x[r_instr.rs2];
break;
case 0b110:
main_hart.x[r_instr.rd] = (int64_t) main_hart.x[r_instr.rs1] %
(int64_t) main_hart.x[r_instr.rs2];
break;
case 0b111:
main_hart.x[r_instr.rd] = main_hart.x[r_instr.rs1] % main_hart.x[r_instr.rs2];
break;
}
break;
}
break;
/* This case is responsible for loading things from memory */
case 0b00000:
i_instr = fetch_i(instruction);
switch (i_instr.funct3) {
/* The lb instruction */
case 0b000:
temp = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1] - memory_config.translation_offset;
if (temp >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "lb");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
main_hart.x[i_instr.rd] = extend_sign(((uint8_t *) memory_config.vm_memory)[temp], 8);
break;
/* The lh instruction */
case 0b001:
temp = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1] - memory_config.translation_offset;
if (temp + 1 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "lh");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
main_hart.x[i_instr.rd] = extend_sign(
((uint8_t *) memory_config.vm_memory)[temp] |
(((uint8_t *) memory_config.vm_memory)[temp + 1] << 8),
16
);
break;
/* The lw instruction */
case 0b010:
temp = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1] - memory_config.translation_offset;
if (temp + 3 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "lw");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
main_hart.x[i_instr.rd] = extend_sign(
((uint64_t) ((uint8_t *) memory_config.vm_memory)[temp]) |
((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 1]) << 8) |
((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 2]) << 16) |
((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 3]) << 24),
32
);
break;
/* The lbu instruction */
case 0b100:
temp = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1] - memory_config.translation_offset;
if (temp >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "lbu");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
main_hart.x[i_instr.rd] = ((uint8_t *) memory_config.vm_memory)[temp];
break;
/* The lhu instruction */
case 0b101:
temp = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1] - memory_config.translation_offset;
if (temp + 1 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "lhu");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
main_hart.x[i_instr.rd] = ((uint8_t *) memory_config.vm_memory)[temp] |
(((uint8_t *) memory_config.vm_memory)[temp + 1] << 8);
break;
/* The lwu instruction */
case 0b110:
temp = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1] - memory_config.translation_offset;
if (temp + 3 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "lwu");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
main_hart.x[i_instr.rd] = ((uint64_t) ((uint8_t *) memory_config.vm_memory)[temp]) |
((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 1]) << 8) |
((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 2]) << 16) |
((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 3]) << 24);
break;
/* The ld instruction */
case 0b011:
temp = extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1] - memory_config.translation_offset;
if (temp + 7 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "ld");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
main_hart.x[i_instr.rd] = (((uint64_t) ((uint8_t *) memory_config.vm_memory)[temp])) |
(((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 1])) << 8) |
(((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 2])) << 16) |
(((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 3])) << 24) |
(((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 4])) << 32) |
(((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 5])) << 40) |
(((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 6])) << 48) |
(((uint64_t) (((uint8_t *) memory_config.vm_memory)[temp + 7])) << 56);
break;
}
break;
/* This case is responsible for loading things into memory */
case 0b01000:
s_instr = fetch_s(instruction);
switch (s_instr.funct3) {
/* sb */
case 0b000:
temp = extend_sign(s_instr.imm, 12) + main_hart.x[s_instr.rs1] - memory_config.translation_offset;
if (temp >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "sb");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
((uint8_t *) memory_config.vm_memory)[temp] = (uint8_t) (main_hart.x[s_instr.rs2]);
break;
/* sh */
case 0b001:
temp = extend_sign(s_instr.imm, 12) + main_hart.x[s_instr.rs1] - memory_config.translation_offset;
if (temp + 1 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "sh");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
((uint8_t *) memory_config.vm_memory)[temp] = (uint8_t) (main_hart.x[s_instr.rs2]);
((uint8_t *) memory_config.vm_memory)[temp + 1] = (uint8_t) (main_hart.x[s_instr.rs2] >> 8);
break;
/* sw */
case 0b010:
temp = extend_sign(s_instr.imm, 12) + main_hart.x[s_instr.rs1] - memory_config.translation_offset;
if (temp + 3 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "sw");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
((uint8_t *) memory_config.vm_memory)[temp] = (uint8_t) (main_hart.x[s_instr.rs2]);
((uint8_t *) memory_config.vm_memory)[temp + 1] = (uint8_t) (main_hart.x[s_instr.rs2] >> 8);
((uint8_t *) memory_config.vm_memory)[temp + 2] = (uint8_t) (main_hart.x[s_instr.rs2] >> 16);
((uint8_t *) memory_config.vm_memory)[temp + 3] = (uint8_t) (main_hart.x[s_instr.rs2] >> 24);
break;
/* sd */
case 0b011:
temp = extend_sign(s_instr.imm, 12) + main_hart.x[s_instr.rs1] - memory_config.translation_offset;
if (temp + 7 >= memory_config.memory_size) {
print_memory_error(temp, memory_config, main_hart, instruction, i_instr, "sd");
debug_fn(main_hart);
free(memory_config.vm_memory);
return 1;
}
// whatever this is
((uint8_t *) memory_config.vm_memory)[temp] = (uint8_t) (main_hart.x[s_instr.rs2]);
((uint8_t *) memory_config.vm_memory)[temp + 1] = (uint8_t) (main_hart.x[s_instr.rs2] >> 8);
((uint8_t *) memory_config.vm_memory)[temp + 2] = (uint8_t) (main_hart.x[s_instr.rs2] >> 16);
((uint8_t *) memory_config.vm_memory)[temp + 3] = (uint8_t) (main_hart.x[s_instr.rs2] >> 24);
((uint8_t *) memory_config.vm_memory)[temp + 4] = (uint8_t) (main_hart.x[s_instr.rs2] >> 24);
((uint8_t *) memory_config.vm_memory)[temp + 5] = (uint8_t) (main_hart.x[s_instr.rs2] >> 24);
((uint8_t *) memory_config.vm_memory)[temp + 6] = (uint8_t) (main_hart.x[s_instr.rs2] >> 24);
((uint8_t *) memory_config.vm_memory)[temp + 7] = (uint8_t) (main_hart.x[s_instr.rs2] >> 24);
break;
}
break;
/* The jal instrcution, aka unconditional jump, or just function call */
case 0b11011:
u_instr = fetch_j(instruction);
main_hart.pc += extend_sign(u_instr.imm, 20) - 4;
if (u_instr.rd != 0) {
main_hart.x[u_instr.rd] = main_hart.pc + 4;
}
break;
/* The jalr instruction */
case 0b11001:
i_instr = fetch_i(instruction);
temp = main_hart.pc + 4;
main_hart.pc = (main_hart.x[i_instr.rs1] + extend_sign(i_instr.imm, 12));
if (i_instr.rd != 0) {
main_hart.x[i_instr.rd] = temp;
}
break;
/* This case is responsible for branching */
case 0b11000:
s_instr = fetch_b(instruction);
switch (s_instr.funct3) {
/* The beq instructtion */
case 0b000:
if (main_hart.x[s_instr.rs1] == main_hart.x[s_instr.rs2]) {
main_hart.pc += extend_sign(s_instr.imm, 12) - 4;
}
break;
/* The bne instructtion */
case 0b001:
if (main_hart.x[s_instr.rs1] != main_hart.x[s_instr.rs2]) {
main_hart.pc += extend_sign(s_instr.imm, 12) - 4;
}
break;
/* The blt instructtion */
case 0b100:
if ((int64_t) (main_hart.x[s_instr.rs1]) < (int64_t) (main_hart.x[s_instr.rs2])) {
main_hart.pc += extend_sign(s_instr.imm, 12) - 4;
}
break;
/* The bge instructtion */
case 0b101:
if ((int64_t) (main_hart.x[s_instr.rs1]) >= (int64_t) (main_hart.x[s_instr.rs2])) {
main_hart.pc += extend_sign(s_instr.imm, 12) - 4;
}
break;
/* The bltu instructtion */
case 0b110:
if (main_hart.x[s_instr.rs1] < main_hart.x[s_instr.rs2]) {
main_hart.pc += extend_sign(s_instr.imm, 12) - 4;
}
break;
/* The bgeu instructtion */
case 0b111:
if (main_hart.x[s_instr.rs1] >= main_hart.x[s_instr.rs2]) {
main_hart.pc += extend_sign(s_instr.imm, 12) - 4;
}
break;
}
break;
// new cases here
/* instruction for rv32 support */
/* These are I type*/
case 0b00110:
i_instr = fetch_i(instruction);
switch (i_instr.funct3) {
case 0b000:
main_hart.x[i_instr.rd] =
extend_sign((extend_sign(i_instr.imm, 12) + main_hart.x[i_instr.rs1]) & UINT32_MAX, 32);
break;
case 0b001:
main_hart.x[i_instr.rd] =
extend_sign((main_hart.x[i_instr.rs1] << (i_instr.imm & 0b11111)) & UINT32_MAX, 32);
break;
case 0b101:
switch ((i_instr.imm >> 7) & 0b11111) {
case 0b00000:
main_hart.x[i_instr.rd] =
extend_sign((main_hart.x[i_instr.rs1] >> (i_instr.imm & 0b11111)) & UINT32_MAX, 32);
break;
case 0b01000:
main_hart.x[i_instr.rd] = extend_sign(
((int64_t) (main_hart.x[i_instr.rs1]) >> (i_instr.imm & 0b11111)) & UINT32_MAX, 32
);
break;
}
break;
}
break;
/* More instrctions for rv32 support */
/* These are R type*/
case 0b01110:
r_instr = fetch_r(instruction);
switch (r_instr.funct7) {
case 0b0000000:
switch (r_instr.funct3) {
case 0b000:
main_hart.x[r_instr.rd] =
extend_sign((main_hart.x[r_instr.rs1] + main_hart.x[r_instr.rs2]) & UINT32_MAX, 32);
break;
case 0b001:
main_hart.x[r_instr.rd] = extend_sign(
(main_hart.x[r_instr.rs1] << (main_hart.x[r_instr.rs2] & 0b11111)) & UINT32_MAX, 32
);
break;
case 0b101:
main_hart.x[r_instr.rd] = extend_sign(
(main_hart.x[r_instr.rs1] >> (main_hart.x[r_instr.rs2] & 0b11111)) & UINT32_MAX, 32
);
break;
}
break;
case 0b0100000:
switch (r_instr.funct3) {
case 0b000:
main_hart.x[r_instr.rd] =
extend_sign((main_hart.x[r_instr.rs1] - main_hart.x[r_instr.rs2]) & UINT32_MAX, 32);
break;
case 0b101:
main_hart.x[r_instr.rd] = extend_sign(
((int32_t) main_hart.x[r_instr.rs1] >> (main_hart.x[r_instr.rs2] & 0b11111)) & UINT32_MAX,
32
);
break;
}
break;
case 0b0000001:
switch (r_instr.funct3) {
/* mulw */
case 0b000:
main_hart.x[r_instr.rd] = extend_sign(
((int32_t) main_hart.x[r_instr.rs1] * (int32_t) main_hart.x[r_instr.rs2]) & UINT32_MAX, 32