-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.c
235 lines (212 loc) · 6.07 KB
/
state.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
#include <limits.h>
#include <string.h>
#include "state.h"
#include "logging.h"
#include "exec.h"
#include "symbols.h"
// uart utils
//////////////
int get_uart_status(state_t *state){
int c = state->ifp == NULL? EOF : fgetc(state->ifp);
if (c != EOF)
fseek(state->ifp, -1, SEEK_CUR);
return c == EOF? 0 : 1;
}
// controller of registers
//////////////
void write_reg(state_t *state, int dest, int value){
if (dest == 0){
debug("Someone tried to write a value to zero register(x0). It will be ignored.");
} else {
state->reg[dest] = value;
state->reg_min[dest] = state->reg_min[dest] <= value? state->reg_min[dest] : value;
state->reg_max[dest] = value <= state->reg_max[dest]? state->reg_max[dest] : value;
}
}
void write_freg(state_t *state, int dest, float value){
state->freg[dest].f = value;
}
// controller of memory
//////////////
uint8_t read_mem_uint8(state_t *state, int addr){
if (0 <= addr && addr < state->memsize) {
return *(uint8_t *)(state->mem + addr);
} else {
error("SIGSEGV was thrown.");
state->is_running = 0;
return 0;
}
}
uint16_t read_mem_uint16(state_t *state, int addr){
if (0 <= addr && addr < state->memsize) {
return *(uint16_t *)(state->mem + addr);
} else {
error("SIGSEGV was thrown.");
state->is_running = 0;
return 0;
}
}
uint32_t read_mem_uint32(state_t *state, int addr){
if (0 <= addr && addr < state->memsize) {
return *(uint32_t *)(state->mem + addr);
} else {
error("SIGSEGV was thrown.");
state->is_running = 0;
return 0;
}
}
float read_mem_float(state_t *state, int addr){
if (0 <= addr && addr < state->memsize) {
return *(float *)(state->mem + addr);
} else {
error("SIGSEGV was thrown.");
state->is_running = 0;
return 0;
}
}
void write_mem(state_t *state, int addr, char value){
if (0 <= addr && addr < state->memsize) {
state->mem[addr] = value;
} else {
error("SIGSEGV was thrown.");
state->is_running = 0;
}
}
// controller of the state of simulator
//////////////
void init_state(state_t *state, int argc, char* argv[]){
state->pc = 0;
state->is_running = 1;
for(int i=0; i<32; i++){
state->reg[i] = 0;
state->freg[i].i = 0;
}
for(int i=0; i < HIST_SIZE; i++)
state->history[i] = 0;
// last return address
state->reg[1] = INITIAL_X1;
state->memsize = MEM_SIZE;
state->mem = malloc(MEM_SIZE * sizeof(uint8_t));
for(int i=0; i<MEM_SIZE; i++)
state->mem[i] = 0;
state->blist = NULL;
state->slist = NULL;
state->prog = NULL;
state->is_first_output_done = 0;
state->step_num = 0;
state->ifp = NULL;
state->ofp = NULL;
state->sfp = NULL;
set_execution_mode(CONTINUOUS);
// process the arguments
for (int i=1; i < argc; i++){
if (strcmp(argv[i], "--input") == 0
|| strcmp(argv[i], "-i")==0){
// set source of uart input
///////////////
if (i == argc-1){
error("No input file is specified.");
exit(1);
}
state->ifp = fopen(argv[++i], "rb");
if (state->ifp == NULL){
error("Cannot open input destination.");
exit(1);
}
} else if (strcmp(argv[i], "--output") == 0
|| strcmp(argv[i], "-o")==0){
// set target of uart output
///////////////
if (i == argc-1){
error("No output file is specified.");
exit(1);
}
state->ofp = fopen(argv[++i], "wb");
if (state->ofp == NULL){
error("Cannot open output destination.");
exit(1);
}
} else if (strcmp(argv[i], "--statout")==0){
// set output file of statistics
///////////////
if (i == argc-1){
error("No statout file is specified.");
exit(1);
}
state->sfp = fopen(argv[++i], "w");
if (state->sfp == NULL){
error("Cannot open statout destination.");
exit(1);
}
} else if (strcmp(argv[i], "--memsize")==0){
// set output file of statistics
///////////////
if (i == argc-1){
error("No memory is specified.");
exit(1);
}
free(state->mem);
state->memsize = atoi(argv[++i]);
state->mem = malloc(sizeof(uint8_t) * state->memsize);
if(state->mem == NULL){
error("Cannot allocate memory. (size=%d)", state->memsize);
exit(1);
}
} else if (strcmp(argv[i], "--debug")==0){
// set logging level to debug
///////////////
set_logging_level(DEBUG);
} else if (strcmp(argv[i], "--debugger")==0){
// launch debugger at startup
///////////////
set_execution_mode(STEP);
} else if (strcmp(argv[i], "--info")==0){
// set logging level to info
///////////////
set_logging_level(INFO);
} else if (strcmp(argv[i], "--symbols") == 0){
// loading symbol information
///////////////
if (i == argc-1){
error("No symbol list is specified.");
exit(1);
}
FILE *slistfp = fopen(argv[++i], "r");
if (slistfp == NULL){
error("Cannot open the file you given: %s", argv[i]);
exit(1);
}
char symbol_buf[80];
int addr_buf;
while (fscanf(slistfp, "%s %d", symbol_buf, &addr_buf) == 2 ) {
insert_new_symbol(&(state->slist), symbol_buf, addr_buf);
}
} else {
// argument should be .bin filepath
///////////////
if(state->prog != NULL){
error("Two or more executables were specified.");
exit(1);
}
state->filename = malloc(strlen(argv[i])+1);
strcpy(state->filename, argv[i]);
FILE *pfp = fopen(argv[i], "rb");
if(pfp == NULL){
error("Failed to open specified executable.");
exit(1);
}
// load program & set program length
fseek(pfp, 0L, SEEK_END);
state->length = ftell(pfp);
fseek(pfp, 0, SEEK_SET);
state->prog = malloc(state->length * sizeof(int));
for(int i=0; i < (int)(state->length/4); i++)
fread(&(state->prog[i]), 4, 1, pfp);
}
}
// validate
if (state->prog == NULL){
error("No executable was specified.");
exit(1);
}
}