|
1
2
3
4
|
//
// Created by imanol on 12/25/16.
//
|
|
5
|
#include <stdlib.h>
|
|
6
7
|
#include "cpu.h"
|
|
8
|
void mov(uint16_t dst, uint16_t src)
|
|
9
10
11
|
{
if(src > MAX_INT)
{
|
|
12
|
regs[dst % (MAX_INT+1)] = regs[src % (MAX_INT+1)];
|
|
13
14
15
|
}
else
{
|
|
16
|
regs[dst % (MAX_INT+1)] = src;
|
|
17
18
19
|
}
}
|
|
20
|
void push(uint16_t src)
|
|
21
22
23
24
25
26
27
28
29
30
31
|
{
if(src > MAX_INT)
{
stack_push(regs[src % (MAX_INT+1)]);
}
else
{
stack_push(src);
}
}
|
|
32
|
void pop(uint16_t dst)
|
|
33
|
{
|
|
34
|
uint16_t value = stack_pop();
|
|
35
36
37
38
39
|
if(STACK_FAULT)
{
fprintf(stderr,"CRITICAL: STACK VIOLATION\n");
core_dump();
}
|
|
40
|
regs[dst % (MAX_INT+1)] = value;
|
|
41
42
|
}
|
|
43
|
void teq(uint16_t dst, uint16_t a, uint16_t b)
|
|
44
45
46
47
48
49
50
51
52
53
54
55
|
{
if(a > MAX_INT)
{
a = regs[a % (MAX_INT+1)];
}
if(b > MAX_INT)
{
b = regs[b % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = (a == b);
}
|
|
56
|
void tgt(uint16_t dst, uint16_t a, uint16_t b)
|
|
57
58
59
60
61
62
63
64
65
66
67
68
|
{
if(a > MAX_INT)
{
a = regs[a % (MAX_INT+1)];
}
if(b > MAX_INT)
{
b = regs[b % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = (a > b);
}
|
|
69
|
void jmp(uint16_t dst)
|
|
70
71
72
73
74
75
76
77
78
79
80
|
{
if(dst > MAX_INT)
{
pc = regs[dst % (MAX_INT+1)];
}
else
{
pc = dst;
}
}
|
|
81
|
void jnz(uint16_t cond, uint16_t dst)
|
|
82
|
{
|
|
83
84
|
if(cond > MAX_INT)
{
|
|
85
|
cond = regs[cond % (MAX_INT+1)];
|
|
86
|
}
|
|
87
88
89
90
91
92
|
if(cond)
{
jmp(dst);
}
}
|
|
93
|
void jz(uint16_t cond, uint16_t dst)
|
|
94
|
{
|
|
95
96
|
if(cond > MAX_INT)
{
|
|
97
|
cond = regs[cond % (MAX_INT+1)];
|
|
98
|
}
|
|
99
100
101
102
103
104
|
if(!cond)
{
jmp(dst);
}
}
|
|
105
|
void add(uint16_t dst, uint16_t a, uint16_t b)
|
|
106
107
108
109
110
111
112
113
114
115
116
117
|
{
if(a > MAX_INT)
{
a = regs[a % (MAX_INT+1)];
}
if(b > MAX_INT)
{
b = regs[b % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = (a + b) % (MAX_INT+1);
}
|
|
118
|
void mul(uint16_t dst, uint16_t a, uint16_t b)
|
|
119
120
121
122
123
124
125
126
127
128
129
130
|
{
if(a > MAX_INT)
{
a = regs[a % (MAX_INT+1)];
}
if(b > MAX_INT)
{
b = regs[b % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = (a * b) % (MAX_INT+1);
}
|
|
131
|
void mod(uint16_t dst, uint16_t a, uint16_t b)
|
|
132
133
134
135
136
137
138
139
140
141
142
143
|
{
if(a > MAX_INT)
{
a = regs[a % (MAX_INT+1)];
}
if(b > MAX_INT)
{
b = regs[b % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = (a % b);
}
|
|
144
|
void and(uint16_t dst, uint16_t a, uint16_t b)
|
|
145
146
147
148
149
150
151
152
153
154
155
156
|
{
if(a > MAX_INT)
{
a = regs[a % (MAX_INT+1)];
}
if(b > MAX_INT)
{
b = regs[b % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = (a & b);
}
|
|
157
|
void or(uint16_t dst, uint16_t a, uint16_t b)
|
|
158
159
160
161
162
163
164
165
166
167
168
169
|
{
if(a > MAX_INT)
{
a = regs[a % (MAX_INT+1)];
}
if(b > MAX_INT)
{
b = regs[b % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = (a | b);
}
|
|
170
|
void not(uint16_t dst, uint16_t src)
|
|
171
172
173
174
175
176
177
178
|
{
if(src > MAX_INT)
{
src = regs[src % (MAX_INT+1)];
}
regs[dst % (MAX_INT+1)] = ~src & 0x7FFF;
}
|
|
179
|
void load(uint16_t dst, uint16_t src)
|
|
180
181
182
183
184
|
{
if(src > MAX_INT)
{
src = regs[src % (MAX_INT+1)];
}
|
|
185
|
regs[dst % (MAX_INT+1)] = mem[src];
|
|
186
187
|
}
|
|
188
|
void stor(uint16_t dst, uint16_t src)
|
|
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
{
if(src > MAX_INT)
{
src = regs[src % (MAX_INT+1)];
}
if(dst > MAX_INT)
{
mem[regs[dst % (MAX_INT+1)]] = src;
}
else
{
mem[dst] = src;
}
}
|
|
204
|
void call(uint16_t dst)
|
|
205
206
207
208
209
210
211
212
213
214
215
|
{
stack_push(pc);
jmp(dst);
}
uint8_t ret()
{
pc = stack_pop();
return STACK_FAULT;
}
|
|
216
|
void out(uint16_t src)
|
|
217
|
{
|
|
218
219
220
221
222
|
if(src > MAX_INT)
{
src = regs[src % (MAX_INT+1)];
}
putchar(src);
|
|
223
224
|
}
|
|
225
|
void in(uint16_t dst)
|
|
226
227
|
{
int c = getchar();
|
|
228
|
regs[dst % (MAX_INT + 1)] = (uint16_t)c;
|
|
229
230
|
}
|
|
231
232
233
234
235
|
void nop()
{
return;
}
|
|
236
|
uint16_t fetch()
|
|
237
|
{
|
|
238
|
uint16_t value = mem[pc++];
|
|
239
240
241
|
return value;
}
|
|
242
243
244
245
246
247
|
void decode_instruction(uint16_t opcode, uint16_t *arg1, uint16_t *arg2, uint16_t *arg3)
{
switch(opcode)
{
case HALT:
|
|
248
249
|
case RET:
case NOP:
|
|
250
251
252
253
|
break;
case PUSH:
case POP:
case JMP:
|
|
254
255
256
|
case CALL:
case OUT:
case IN:
|
|
257
258
|
*arg1 = fetch();
break;
|
|
259
|
case MOV:
|
|
260
261
|
case JNZ:
case JZ:
|
|
262
263
264
|
case NOT:
case LOAD:
case STOR:
|
|
265
266
267
|
*arg1 = fetch();
*arg2 = fetch();
break;
|
|
268
269
|
case TEQ:
case TGT:
|
|
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
|
case ADD:
case MUL:
case MOD:
case AND:
case OR:
*arg1 = fetch();
*arg2 = fetch();
*arg3 = fetch();
break;
default:
break;
}
}
uint8_t execute_instruction(uint16_t opcode, uint16_t arg1, uint16_t arg2, uint16_t arg3)
{
switch(opcode)
{
case HALT:
return 1;
case MOV:
mov(arg1,arg2);
break;
case PUSH:
push(arg1);
break;
case POP:
pop(arg1);
break;
case TEQ:
teq(arg1,arg2,arg3);
break;
case TGT:
tgt(arg1,arg2,arg3);
break;
case JMP:
jmp(arg1);
break;
case JNZ:
jnz(arg1,arg2);
break;
case JZ:
jz(arg1,arg2);
break;
case ADD:
add(arg1,arg2,arg3);
break;
case MUL:
mul(arg1,arg2,arg3);
break;
case MOD:
mod(arg1,arg2,arg3);
break;
case AND:
and(arg1,arg2,arg3);
break;
case OR:
or(arg1,arg2,arg3);
break;
case NOT:
not(arg1,arg2);
break;
case LOAD:
load(arg1,arg2);
break;
case STOR:
stor(arg1,arg2);
break;
case CALL:
call(arg1);
break;
case RET:
if(ret())
{
return 1;
}
break;
case OUT:
out(arg1);
break;
case IN:
in(arg1);
break;
case NOP:
nop();
break;
default:
fprintf(stderr,"CRITICAL: UNKNOWN OPCODE %2x FOUND IN %2x\n",opcode,pc);
core_dump();
break;
}
return 0;
}
|
|
364
|
void start_execution()
|
|
365
|
{
|
|
366
367
368
|
uint16_t arg1;
uint16_t arg2;
uint16_t arg3;
|
|
369
|
|
|
370
371
|
for(;;)
{
|
|
372
|
uint16_t opcode = fetch();
|
|
373
374
|
decode_instruction(opcode, &arg1, &arg2, &arg3);
if(execute_instruction(opcode, arg1, arg2, arg3))
|
|
375
|
{
|
|
376
|
return;
|
|
377
378
|
}
}
|
|
379
380
|
}
|
|
381
382
383
|
void core_dump()
{
print_regs();
|
|
384
385
386
387
388
389
390
391
392
393
394
395
|
uint16_t memdump[MEMSIZE];
uint16_t *stackdump;
dump_memory(memdump);
uint32_t stacksize = stack_dump(&stackdump);
FILE *fpm = fopen("synacor.mem","w");
FILE *fps = fopen("synacor.stack","w");
fwrite(memdump,MEMSIZE,sizeof(uint16_t),fpm);
fwrite(stackdump,stacksize,sizeof(uint16_t),fps);
fclose(fpm);
fclose(fps);
|
|
396
|
exit(1);
|
|
397
|
}
|
|
398
399
|
|