-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.c
117 lines (96 loc) · 2.34 KB
/
simulator.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
/*
The XSM simulator starts here.
*/
#include "simulator.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static const int XSM_TIMER_DURATION = XSM_SIMULATOR_DEFTIMER;
static const int XSM_DISK_DURATION = XSM_SIMULATOR_DEFDISK;
static const int XSM_CONSOLE_DURATION = XSM_SIMULATOR_DEFCONSOLE;
/* Start the XSM machine */
int simulator_run()
{
// Ready
disk_init(XSM_DEFAULT_DISK);
// Set
if (!machine_init(&_options))
return XSM_FAILURE;
// Go
if (!machine_run())
return XSM_FAILURE;
printf("Machine is halting.\n");
// Finish
machine_destroy();
disk_close();
return XSM_SUCCESS;
}
/* Parse the parameters */
int simulator_parse_args(int argc, char **argv)
{
int val;
argv++;
argc--;
_options.timer = XSM_TIMER_DURATION;
_options.console = XSM_CONSOLE_DURATION;
_options.disk = XSM_DISK_DURATION;
while (argc > 0)
{
if (!strcmp(*argv, "--debug"))
{
_options.debug = TRUE;
argv++;
argc--;
}
else if (!strcmp(*argv, "--timer"))
{
argv++;
argc--;
val = atoi(*argv);
if (val < 0 || val > 1024)
{
printf("--timer takes value in the range 0-1024\n");
exit(0);
}
_options.timer = val + 1;
if (val == 0)
_options.timer = 0;
argv++;
argc--;
}
else if (!strcmp(*argv, "--console"))
{
argv++;
argc--;
val = atoi(*argv);
if (val < 20 || val > 1024)
{
printf("--console takes value in the range 20-1024\n");
exit(0);
}
_options.console = val + 1;
argv++;
argc--;
}
else if (!strcmp(*argv, "--disk"))
{
argv++;
argc--;
val = atoi(*argv);
if (val < 20 || val > 1024)
{
printf("--disk takes value in the range 20-1024\n");
exit(0);
}
_options.disk = val + 1;
argv++;
argc--;
}
else
{
// Unrecognised option.
return XSM_FAILURE;
}
}
return XSM_SUCCESS;
}