-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsim.c
521 lines (477 loc) · 14.4 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include "debug.h"
#include "fpu.h"
#define HALT_CODE 0xffffffff
#define IRQ_PSEUDO 0
#define IRQ_TIMER 1
#define IRQ_SERIAL 2
#define IRQ_SYSENTER 3
#define PGCOLOR(va) ((va) & 0x3000)
uint32_t reg[32];
uint32_t *mem;
uint32_t mem_size = 0x400000;
uint32_t entry_point = 0x2000;
uint32_t pc;
uint32_t prog_size;
uint32_t intr_addr, intr_enabled, epc, irq_num, irq_bits;
uint32_t mmu_enabled, pd_addr;
int debug_enabled;
long long inst_cnt;
struct termios original_ttystate;
char infile[128];
int show_stat, boot_test, sim_intr_disabled, use_maswag_fpu;
uint32_t to_physical(uint32_t);
void restore_term();
void print_env(int show_vpc)
{
fprintf(stderr, "\x1b[1m*** Simulator Status ***\x1b[0m\n");
if (show_stat) {
fprintf(stderr, "<register>\n");
for (int i = 0; i < 16; ++i)
fprintf(stderr, " r%-2d: %11d (0x%08x) / r%-2d: %11d (0x%08x)\n",
i, reg[i], reg[i], i + 16, reg[i + 16], reg[i + 16]);
}
if (mmu_enabled) {
fprintf(stderr, "<Current Virtual PC>: 0x%08x\n", pc);
if (show_vpc)
fprintf(stderr, "<Current Physical PC>: 0x%06x\n", to_physical(pc));
} else {
fprintf(stderr, "<Current PC>: 0x%06x\n", pc);
}
fprintf(stderr, "<Number of executed instructions>: %lld\n", inst_cnt);
}
void error(char *, ...) __attribute__((noreturn));
void error(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "\x1b[1;31mruntime error: \x1b[39m");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\x1b[0m\n\n");
print_env(strncmp("to_physical: ", fmt, strlen("to_physical: ")));
restore_term();
dump_e_i();
va_end(ap);
exit(1);
}
uint32_t bitint(float x)
{
union { uint32_t i; float f; } u;
u.f = x;
return u.i;
}
float bitfloat(uint32_t x)
{
union { uint32_t i; float f; } u;
u.i = x;
return u.f;
}
uint32_t alu(int tag, int ra, int rb, uint32_t lit)
{
uint32_t t = reg[rb] + lit;
switch (tag) {
case 0: return reg[ra] + t;
case 1: return reg[ra] - t;
case 2: return reg[ra] << t;
case 3: return reg[ra] >> t;
case 4: return (int32_t)reg[ra] >> t;
case 5: return reg[ra] & t;
case 6: return reg[ra] | t;
case 7: return reg[ra] ^ t;
case 8: return reg[ra] + 4 * t;
case 22: return reg[ra] < t;
case 23: return reg[ra] <= t;
case 24: return reg[ra] != t;
case 25: return reg[ra] == t;
case 26: return (int32_t)reg[ra] < (int32_t)t;
case 27: return (int32_t)reg[ra] <= (int32_t)t;
// case 28: return bitfloat(reg[ra]) != bitfloat(reg[rb]);
// case 29: return bitfloat(reg[ra]) == bitfloat(reg[rb]);
case 30: return bitfloat(reg[ra]) < bitfloat(reg[rb]);
case 31: return bitfloat(reg[ra]) <= bitfloat(reg[rb]);
default: error("instruction decode error (ALU)");
}
}
uint32_t fpu(int tag, int ra, int rb)
{
switch (tag) {
case 0: return bitint(bitfloat(reg[ra]) + bitfloat(reg[rb]));
case 1: return bitint(bitfloat(reg[ra]) - bitfloat(reg[rb]));
case 2: return bitint(bitfloat(reg[ra]) * bitfloat(reg[rb]));
// case 3: return bitint(bitfloat(reg[ra]) / bitfloat(reg[rb]));
case 4: return bitint(1.0 / bitfloat(reg[ra]));
case 5: return bitint(sqrtf(bitfloat(reg[ra])));
case 6: return (int32_t)roundf(bitfloat(reg[ra]));
case 7: return bitint((float)(int32_t)reg[ra]);
case 8: return bitint(floorf(bitfloat(reg[ra])));
default: error("instruction decode error (FPU)");
}
}
uint32_t fpu_maswag(int tag, int ra, int rb)
{
switch (tag) {
case 0: return fadd(reg[ra], reg[rb]);
case 1: return fsub(reg[ra], reg[rb]);
case 2: return fmul(reg[ra], reg[rb]);
// case 3: return fdiv(reg[ra], reg[rb]);
case 4: return finv(reg[ra]);
case 5: return fsqrt(reg[ra]);
case 6: return h_f2i(reg[ra]);
case 7: return h_i2f(reg[ra]);
case 8: return h_floor(reg[ra]);
default: error("instruction decode error (FPU)");
}
}
uint32_t fpu_sign(uint32_t x, int mode)
{
switch (mode) {
case 1: return x ^ 0x80000000;
case 2: return x & 0x7fffffff;
case 3: return x | 0x80000000;
default: return x;
}
}
uint32_t care_minus_zero(uint32_t x)
{
return x == 0x80000000 ? 0 : x;
}
// Warning: Error messages in "to_physical" MUST starts with "to_physical: " to prevent
// infinite loop in "error" function, which may call "to_physical" again.
uint32_t to_physical(uint32_t addr)
{
uint32_t tmp;
if (!mmu_enabled) return addr;
tmp = pd_addr | ((addr >> 22) << 2);
if (tmp & 3 || tmp >= mem_size)
error("to_physical: PDE address error: 0x%08x, Requested virtual address: 0x%08x", tmp, addr);
tmp = mem[tmp >> 2];
if ((tmp & 1) == 0)
error("to_physical: invalid PDE, Requested virtual address: 0x%08x", addr);
tmp = (tmp & ~0x0fff) | (((addr >> 12) & 0x03ff) << 2);;
if (tmp >= mem_size)
error("to_physical: PTE address error: 0x%08x, Requested virtual address: 0x%08x", tmp, addr);
tmp = mem[tmp >> 2];
if ((tmp & 1) == 0)
error("to_physical: invalid PTE, Requested virtual address: 0x%08x", addr);
tmp = (tmp & ~0x0fff) | (addr & 0x0fff);
if (PGCOLOR(tmp) != PGCOLOR(addr))
error("to_physical: invalid page color: Physical adrress: 0x%08x, Requested virtual address: 0x%08x", tmp, addr);
return tmp;
}
int has_input()
{
int c = getchar();
if (c == EOF)
return 0;
ungetc(c, stdin);
return 1;
}
uint32_t serial_read()
{
return getchar();
}
void serial_write(uint32_t x)
{
putchar(x);
fflush(stdout);
}
uint32_t load(int ra, uint32_t disp)
{
uint32_t addr = to_physical(reg[ra] + (disp << 2));
if (addr & 3)
error("load: address must be a multiple of 4: 0x%08x", addr);
if (addr < mem_size) {
return mem[addr >> 2];
} else {
switch (addr) {
case 0x80001000: return serial_read();
case 0x80001004: return 1; // Tx ready bit is already high in simulation
case 0x80001100: return intr_addr;
case 0x80001104: return intr_enabled;
case 0x80001108: return epc;
case 0x8000110c: return irq_num;
case 0x80001200: return mmu_enabled;
case 0x80001204: return pd_addr;
default: error("load: exceeded %dMB limit: 0x%08x", mem_size >> 20, addr);
}
}
}
uint32_t load_byte(int ra, uint32_t disp)
{
uint32_t addr = to_physical(reg[ra] + disp);
if (addr >= mem_size)
error("load_byte: exceeded %dMB limit: 0x%08x", mem_size >> 20, addr);
return *((int8_t *)mem + addr);
}
void store(int ra, uint32_t disp, uint32_t x)
{
uint32_t addr = to_physical(reg[ra] + (disp << 2));
if (addr & 3)
error("store: address must be a multiple of 4: 0x%08x", addr);
if (addr < mem_size) {
mem[addr >> 2] = x;
} else {
switch (addr) {
case 0x80001000: serial_write(x); break;
case 0x80001100: intr_addr = x; break;
case 0x80001104: intr_enabled = x; break;
case 0x80001108: epc = x; break;
case 0x8000110c: irq_num = x; break;
case 0x80001200: mmu_enabled = x; break;
case 0x80001204: pd_addr = x; break;
default: error("store: exceeded %dMB limit: 0x%08x", mem_size >> 20, addr);
}
}
}
void store_byte(int ra, uint32_t disp, uint32_t x)
{
uint32_t addr = to_physical(reg[ra] + disp);
if (addr >= mem_size)
error("store_byte: exceeded %dMB limit: 0x%08x", mem_size >> 20, addr);
*((uint8_t *)mem + addr) = x;
}
void exec_alu(uint32_t inst)
{
int tag = inst & 31;
int rx = (inst >> 23) & 31;
int ra = (inst >> 18) & 31;
int rb = (inst >> 13) & 31;
uint32_t lit = (inst >> 5) & 255;
if (lit >= 128) lit -= 256;
reg[rx] = alu(tag, ra, rb, lit);
}
void exec_fpu(uint32_t inst)
{
int tag = inst & 31;
int rx = (inst >> 23) & 31;
int ra = (inst >> 18) & 31;
int rb = (inst >> 13) & 31;
int sign = (inst >> 5) & 3;
uint32_t res = use_maswag_fpu ? fpu_maswag(tag, ra, rb) : fpu(tag, ra, rb);
reg[rx] = care_minus_zero(fpu_sign(res, sign));
}
void exec_misc(uint32_t inst)
{
int opcode = inst >> 28;
int rx = (inst >> 23) & 31;
int ra = (inst >> 18) & 31;
uint32_t disp = inst & 0xffff, tmp;
if (disp >= 0x8000) disp -= 0x10000;
switch (opcode) {
case 2:
reg[rx] = disp;
return;
case 3:
reg[rx] = (disp << 16) | (reg[ra] & 0xffff);
return;
case 4:
reg[rx] = pc + 4;
pc += disp << 2;
return;
case 5:
if (reg[ra] & 3)
error("jr: register corrupted: r%d", ra);
if (to_physical(reg[ra]) >= mem_size)
error("jr: jump destination out of range: r%d", ra);
tmp = reg[ra];
reg[rx] = pc + 4;
pc = tmp - 4;
return;
case 6:
reg[rx] = load(ra, disp);
return;
case 7:
reg[rx] = load_byte(ra, disp);
return;
case 8:
store(ra, disp, reg[rx]);
return;
case 9:
store_byte(ra, disp, reg[rx]);
return;
case 10:
exec_debug(rx, disp);
return;
case 12:
intr_enabled = 0;
irq_num = IRQ_SYSENTER; // IRQ number
epc = pc + 4; // GAIA cpus store the correct interrupted address for sysenter exception.
pc = intr_addr - 4;
return;
case 13:
pc = epc - 4;
intr_enabled = 1;
return;
case 14:
if (reg[rx] != reg[ra]) pc += disp << 2;
return;
case 15:
if (reg[rx] == reg[ra]) pc += disp << 2;
return;
default:
error("instruction decode error");
}
}
void exec(uint32_t inst)
{
int opcode = inst >> 28;
switch (opcode) {
case 0: exec_alu(inst); break;
case 1: exec_fpu(inst); break;
default: exec_misc(inst); break;
}
}
void update_irqbits()
{
static int tick;
// TIMER
if (++tick >= (int)(93.33e6 / 100)) {
irq_bits |= 1 << IRQ_TIMER;
tick = 0;
}
// SERIAL
if (has_input())
irq_bits |= 1 << IRQ_SERIAL;
}
void interrupt()
{
update_irqbits();
if (irq_bits && intr_enabled) {
intr_enabled = 0;
epc = pc + 4; // GAIA cpus store interrupted address + 4.
irq_num = __builtin_ctz(irq_bits); // IRQ number
pc = intr_addr;
irq_bits &= ~(1 << irq_num); // auto EOI
}
}
void init_env()
{
free(mem);
mem = malloc(mem_size);
if (!boot_test)
reg[30] = reg[31] = mem_size;
pc = entry_point;
inst_cnt = 0;
irq_bits = 0;
}
void init_term()
{
struct termios ttystate;
if (!isatty(fileno(stdin)) || sim_intr_disabled)
return;
tcgetattr(fileno(stdin), &ttystate);
original_ttystate = ttystate;
cfmakeraw(&ttystate);
ttystate.c_lflag |= ISIG;
ttystate.c_oflag |= OPOST;
ttystate.c_cc[VMIN] = 0;
ttystate.c_cc[VTIME] = 0;
tcsetattr(fileno(stdin), TCSANOW, &ttystate);
}
void restore_term()
{
if (!isatty(fileno(stdin)) || sim_intr_disabled)
return;
tcsetattr(fileno(stdin), TCSANOW, &original_ttystate);
}
void load_file()
{
FILE *fp = fopen(infile, "r");
if (fp == NULL)
error(strerror(errno));
prog_size = 0;
for (int i = 0; i < 32; i += 8)
prog_size += fgetc(fp) << i;
for (uint32_t i = 0; i < prog_size; ++i) {
int c = fgetc(fp);
if (c == EOF)
error("load_file: reached EOF (actual size is less than header)");
*((uint8_t*)mem + entry_point + i) = c;
}
if (fgetc(fp) != EOF)
error("load_file: input file remained (actual size is more than header)");
fclose(fp);
}
void runsim()
{
init_env();
load_file();
while (1) {
uint32_t phys_pc;
if (! sim_intr_disabled)
interrupt();
if (debug_enabled)
debug_hook();
phys_pc = to_physical(pc);
if (phys_pc >= mem_size)
error("program counter out of range");
if (mem[phys_pc >> 2] == HALT_CODE)
break;
exec(mem[phys_pc >> 2]);
pc += 4;
++inst_cnt;
}
}
void print_help(char *prog)
{
fprintf(stderr, "usage: %s [options] file\n", prog);
fprintf(stderr, "options:\n");
fprintf(stderr, " -boot-test bootloader test mode\n");
fprintf(stderr, " -debug enable debugging feature\n");
fprintf(stderr, " -fpu-maswag use MasWag's FPU\n");
fprintf(stderr, " -msize <integer> change memory size (MB)\n");
fprintf(stderr, " -no-interrupt disable interrupt feature\n");
fprintf(stderr, " -simple same as -no-interrupt\n");
fprintf(stderr, " -stat show simulator status\n");
exit(1);
}
void parse_cmd(int argc, char *argv[])
{
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-boot-test") == 0) {
entry_point = 0;
boot_test = 1;
} else if (strcmp(argv[i], "-debug") == 0) {
debug_enabled = 1;
} else if (strcmp(argv[i], "-fpu-maswag") == 0) {
use_maswag_fpu = 1;
} else if (strcmp(argv[i], "-msize") == 0) {
if (i == argc - 1) print_help(argv[0]);
mem_size = atoi(argv[++i]) << 20;
} else if (strcmp(argv[i], "-no-interrupt") == 0) {
sim_intr_disabled = 1;
} else if (strcmp(argv[i], "-simple") == 0) {
sim_intr_disabled = 1;
} else if (strcmp(argv[i], "-stat") == 0) {
show_stat = 1;
} else if (infile[0] != '\0') {
fprintf(stderr, "error: multiple input files are specified\n");
print_help(argv[0]);
} else {
strcpy(infile, argv[i]);
}
}
}
int main(int argc, char *argv[])
{
parse_cmd(argc, argv);
if (infile[0] == '\0') print_help(argv[0]);
init_term();
runsim();
if (show_stat) {
print_env(1);
dump_e_i();
}
restore_term();
return 0;
}