-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.c
executable file
·71 lines (57 loc) · 1.23 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
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "load_program.h"
#include "processor.h"
#include "disassemble.h"
int main(int argc, char** argv)
{
/* options */
int opt_disasm = 0, opt_regdump = 0, opt_interactive = 0;
/* the architectural state of the CPU */
processor_t p;
/* 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 */
load_program(init_mem(), MEM_SIZE, argv[optind], opt_disasm);
/* if we're just disassembling, exit here */
if (opt_disasm)
{
return 0;
}
/* initialize the CPU */
init_processor(&p);
/* simulate forever! */
while (1)
{
execute_one_inst(&p, opt_interactive, opt_regdump);
}
return 0;
}