-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
181 lines (163 loc) · 4.26 KB
/
vm.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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#define MEM_SIZE 32768
#define STACK_SIZE 100000
#define NEXT mem[pc++]
#define OPERAND operand(NEXT)
#define REGISTER register_(NEXT)
typedef uint16_t num;
num mem[MEM_SIZE];
num reg[8];
num stack[STACK_SIZE];
num pc = 0;
size_t sp = 0;
int halt = 0;
num operand(num op) {
if (op < 32768)
return op;
else if (op < 32768 + 8)
return reg[op - 32768];
else {
fprintf(stderr, "invalid operand %d\n", op);
exit(1);
}
}
size_t register_(num r) {
if (32768 <= r && r < 32768 + 8)
return r - 32768;
else {
fprintf(stderr, "invalid register %d\n", r);
exit(1);
}
}
void push(num val) {
if (sp >= STACK_SIZE) {
fprintf(stderr, "stack overflow\n");
exit(1);
}
stack[sp++] = val;
}
num pop() {
if (sp == 0) {
fprintf(stderr, "empty stack\n");
exit(1);
}
return stack[--sp];
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s PROGRAM\n", argv[0]);
return 1;
}
FILE *program = fopen(argv[1], "rb");
if (!program) {
perror(argv[1]);
return 1;
}
unsigned char b[2];
num i = 0;
while (fread(b, 2, 1, program) != 0)
mem[i++] = b[0] | (b[1] << 8);
while (!halt) {
num op = NEXT, a, b, c;
int ch;
switch (op) {
case 0: // halt
halt = 1;
break;
case 1: // set
a = REGISTER, b = OPERAND;
reg[a] = b;
break;
case 2: // push
a = OPERAND;
push(a);
break;
case 3: // pop
a = REGISTER;
reg[a] = pop();
break;
case 4: // eq
a = REGISTER, b = OPERAND, c = OPERAND;
reg[a] = b == c;
break;
case 5: // gt
a = REGISTER, b = OPERAND, c = OPERAND;
reg[a] = b > c;
break;
case 6: // jmp
a = OPERAND;
pc = a;
break;
case 7: // jt
a = OPERAND, b = OPERAND;
if (a)
pc = b;
break;
case 8: // jf
a = OPERAND, b = OPERAND;
if (!a)
pc = b;
break;
case 9: // add
a = REGISTER, b = OPERAND, c = OPERAND;
reg[a] = (b + c) % 32768;
break;
case 10: // mult
a = REGISTER, b = OPERAND, c = OPERAND;
reg[a] = (b * c) % 32768;
break;
case 11: // mod
a = REGISTER, b = OPERAND, c = OPERAND;
reg[a] = b % c;
break;
case 12: // and
a = REGISTER, b = OPERAND, c = OPERAND;
reg[a] = b & c;
break;
case 13: // or
a = REGISTER, b = OPERAND, c = OPERAND;
reg[a] = b | c;
break;
case 14: // not
a = REGISTER, b = OPERAND;
reg[a] = ~b & ~(1 << 15);
break;
case 15: // rmem
a = REGISTER, b = OPERAND;
reg[a] = mem[b];
break;
case 16: // wmem
a = OPERAND, b = OPERAND;
mem[a] = b;
break;
case 17: // call
a = OPERAND;
push(pc);
pc = a;
break;
case 18: // ret
pc = pop();
break;
case 19: // out
a = OPERAND;
putchar(a);
break;
case 20: // in
a = REGISTER;
ch = getchar();
if (ch == EOF)
halt = 1;
else
reg[a] = (num) ch;
break;
case 21: // noop
break;
default:
fprintf(stderr, "invalid opcode %d\n", op);
return 1;
}
}
return 0;
}