-
Notifications
You must be signed in to change notification settings - Fork 2
/
riscv.c
145 lines (117 loc) · 3.17 KB
/
riscv.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
#include "riscv.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <assert.h>
/* WARNING: DO NOT CHANGE THIS FILE.
YOU PROBABLY DON'T EVEN NEED TO LOOK AT IT... */
// Pointer to simulator memory
Byte *memory;
void execute(Processor *processor,int prompt,int print) {
/* fetch an instruction */
uint32_t instruction_bits = load(memory, processor->PC, LENGTH_WORD);
/* interactive-mode prompt */
if(prompt) {
if(prompt==1) {
printf("simulator paused,enter to continue...");
while(getchar()!='\n');
}
printf("%08x: ",processor->PC);
decode_instruction(instruction_bits);
}
execute_instruction(instruction_bits, processor, memory);
// enforce $0 being hard-wired to 0
processor->R[0] = 0;
// print trace
if(print) {
int i,j;
for(i=0;i<8;i++) {
for(j=0;j<4;j++) {
printf("r%2d=%08x ",i*4+j,processor->R[i*4+j]);
}
puts("");
}
printf("\n");
}
}
void load_program(uint8_t *mem, size_t memsize, int startaddr, const char *filename, int disasm) {
FILE *file = fopen(filename, "r");
const int MAX_SIZE = 50;
char line[MAX_SIZE];
int instruction, offset = 0;
while (fgets(line, MAX_SIZE, file) != NULL) {
instruction = (int32_t) strtol(line, NULL, 16);
mem[startaddr + offset] = instruction & 0xFF;
mem[startaddr + offset + 1] = (instruction >> 8) & 0xFF;
mem[startaddr + offset + 2] = (instruction >> 16) & 0xFF;
mem[startaddr + offset + 3] = (instruction >> 24) & 0xFF;
if (disasm) {
printf("%08x: ", startaddr + offset);
decode_instruction((uint32_t) instruction);
}
offset += 4;
}
}
int main(int argc,char** argv) {
/* options */
int opt_disasm = 0,opt_regdump = 0,opt_interactive = 0;
/* the architectural state of the CPU */
Processor processor;
/* parse the command-line args */
int c;
while((c=getopt(argc,argv,"drit"))!=-1) {
switch (c) {
case 'd':
opt_disasm = 1;
break;
case 'r':
opt_regdump = 1;
break;
case 'i':
opt_interactive = 1;
break;
case 't':
opt_interactive = 2;
break;
default:
fprintf(stderr,"Bad option %c\n",c);
return -1;
}
}
/* make sure we got an executable filename on the command line */
if(argc<=optind) {
fprintf(stderr,"Give me an executable file to run!\n");
return -1;
}
/* load the executable into memory */
assert(memory == NULL);
memory = calloc(MEMORY_SPACE,sizeof(uint8_t)); // allocate zeroed memory
assert(memory != NULL);
/* SEt the PC to 0x1000 */
processor.PC = 0x1000;
load_program(memory, MEMORY_SPACE, processor.PC, argv[optind], opt_disasm);
/* if we're just disassembling,exit here */
if(opt_disasm) {
return 0;
}
/* initialize the CPU */
/* zero out all registers */
int i;
for(i=0;i<32;i++) {
processor.R[i] = 0;
}
/* Set the global pointer to 0x3000. We arbitrarily call this the middle of the static data segment */
processor.R[3] = 0x3000;
/* Set the stack pointer near the top of the memory array */
processor.R[2] = 0xEFFFF;
/* simulate forever! */
while (1)
execute(&processor,opt_interactive,opt_regdump);
return 0;
}