-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
295 lines (259 loc) · 10.6 KB
/
util.c
File metadata and controls
295 lines (259 loc) · 10.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
/***************************************************************/
/* */
/* MIPS-32 Instruction Level Simulator */
/* */
/* CS311 KAIST */
/* util.c */
/* */
/***************************************************************/
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
/* DO NOT MODIFY THIS FILE! */
/* You should only the parse.c and run.c files! */
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
#include "util.h"
/***************************************************************/
/* Main memory. */
/***************************************************************/
/* memory will be dynamically allocated at initialization */
mem_region_t MEM_REGIONS[] = {
{ MEM_TEXT_START, MEM_TEXT_SIZE, NULL },
{ MEM_DATA_START, MEM_DATA_SIZE, NULL },
};
#define MEM_NREGIONS (sizeof(MEM_REGIONS)/sizeof(mem_region_t))
/***************************************************************/
/* CPU State info. */
/***************************************************************/
CPU_State CURRENT_STATE;
int RUN_BIT; /* run bit */
int INSTRUCTION_COUNT;
/***************************************************************/
/* CPU State info. */
/***************************************************************/
instruction *INST_INFO;
int NUM_INST;
/***************************************************************/
/* */
/* Procedure: str_split */
/* */
/* Purpose: To parse main function argument */
/* */
/***************************************************************/
char** str_split(char *a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
* knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
/***************************************************************/
/* */
/* Procedure: fromBinary */
/* */
/* Purpose: From binary to integer */
/* */
/***************************************************************/
int fromBinary(char *s)
{
return (int) strtol(s, NULL, 2);
}
/***************************************************************/
/* */
/* Procedure: mem_read_32 */
/* */
/* Purpose: Read a 32-bit word from memory */
/* */
/***************************************************************/
uint32_t mem_read_32(uint32_t address)
{
int i;
for (i = 0; i < MEM_NREGIONS; i++) {
if (address >= MEM_REGIONS[i].start &&
address < (MEM_REGIONS[i].start + MEM_REGIONS[i].size)) {
uint32_t offset = address - MEM_REGIONS[i].start;
return
(MEM_REGIONS[i].mem[offset+3] << 24) |
(MEM_REGIONS[i].mem[offset+2] << 16) |
(MEM_REGIONS[i].mem[offset+1] << 8) |
(MEM_REGIONS[i].mem[offset+0] << 0);
}
}
return 0;
}
/***************************************************************/
/* */
/* Procedure: mem_write_32 */
/* */
/* Purpose: Write a 32-bit word to memory */
/* */
/***************************************************************/
void mem_write_32(uint32_t address, uint32_t value)
{
int i;
for (i = 0; i < MEM_NREGIONS; i++) {
if (address >= MEM_REGIONS[i].start &&
address < (MEM_REGIONS[i].start + MEM_REGIONS[i].size)) {
uint32_t offset = address - MEM_REGIONS[i].start;
MEM_REGIONS[i].mem[offset+3] = (value >> 24) & 0xFF;
MEM_REGIONS[i].mem[offset+2] = (value >> 16) & 0xFF;
MEM_REGIONS[i].mem[offset+1] = (value >> 8) & 0xFF;
MEM_REGIONS[i].mem[offset+0] = (value >> 0) & 0xFF;
return;
}
}
}
/***************************************************************/
/* */
/* Procedure : cycle */
/* */
/* Purpose : Execute a cycle */
/* */
/***************************************************************/
void cycle() {
process_instruction();
INSTRUCTION_COUNT++;
//for debug
//printf("%2d - Current PC: %x\n", INSTRUCTION_COUNT, CURRENT_STATE.PC);
}
/***************************************************************/
/* */
/* Procedure : run n */
/* */
/* Purpose : Simulate MIPS for n cycles */
/* */
/***************************************************************/
void run(int num_cycles) {
int i;
if (RUN_BIT == FALSE) {
printf("Can't simulate, Simulator is halted\n\n");
return;
}
printf("Simulating for %d cycles...\n\n", num_cycles);
for (i = 0; i < num_cycles; i++) {
if (RUN_BIT == FALSE) {
printf("Simulator halted\n\n");
break;
}
cycle();
}
}
/***************************************************************/
/* */
/* Procedure : go */
/* */
/* Purpose : Simulate MIPS until HALTed */
/* */
/***************************************************************/
void go() {
if (RUN_BIT == FALSE) {
printf("Can't simulate, Simulator is halted\n\n");
return;
}
printf("Simulating...\n\n");
while (RUN_BIT)
cycle();
printf("Simulator halted\n\n");
}
/***************************************************************/
/* */
/* Procedure : mdump */
/* */
/* Purpose : Dump a word-aligned region of memory to the */
/* output file. */
/* */
/***************************************************************/
void mdump(int start, int stop) {
int address;
printf("Memory content [0x%08x..0x%08x] :\n", start, stop);
printf("-------------------------------------\n");
for (address = start; address <= stop; address += 4)
printf("0x%08x: 0x%08x\n", address, mem_read_32(address));
printf("\n");
}
/***************************************************************/
/* */
/* Procedure : rdump */
/* */
/* Purpose : Dump current register and bus values to the */
/* output file. */
/* */
/***************************************************************/
void rdump() {
int k;
printf("Current register values :\n");
printf("-------------------------------------\n");
printf("PC: 0x%08x\n", CURRENT_STATE.PC);
printf("Registers:\n");
for (k = 0; k < MIPS_REGS; k++)
printf("R%d: 0x%08x\n", k, CURRENT_STATE.REGS[k]);
printf("\n");
}
/***************************************************************/
/* */
/* Procedure : init_memory */
/* */
/* Purpose : Allocate and zero memory */
/* */
/***************************************************************/
void init_memory() {
int i;
for (i = 0; i < MEM_NREGIONS; i++) {
MEM_REGIONS[i].mem = malloc(MEM_REGIONS[i].size);
memset(MEM_REGIONS[i].mem, 0, MEM_REGIONS[i].size);
}
}
/***************************************************************/
/* */
/* Procedure : init_inst_info */
/* */
/* Purpose : Initialize instruction info */
/* */
/***************************************************************/
void init_inst_info()
{
int i;
for(i = 0; i < NUM_INST; i++)
{
INST_INFO[i].value = 0;
INST_INFO[i].opcode = 0;
INST_INFO[i].func_code = 0;
INST_INFO[i].r_t.r_i.rs = 0;
INST_INFO[i].r_t.r_i.rt = 0;
INST_INFO[i].r_t.r_i.r_i.r.rd = 0;
INST_INFO[i].r_t.r_i.r_i.imm = 0;
INST_INFO[i].r_t.r_i.r_i.r.shamt = 0;
INST_INFO[i].r_t.target = 0;
}
}