-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.c
476 lines (433 loc) · 12.8 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
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "vm.h"
#include "timing.h"
uint16_t pop(vm_t *vm)
{
if(vm->SP == 0) return UINT16_MAX;
return vm->stack[--vm->SP];
}
void push(vm_t *vm, uint16_t val)
{
if(vm->SP == STACK_SIZE) return;
vm->stack[vm->SP++] = val;
}
char *sttocstr(status_t st)
{
switch(st)
{
case ST_OK:
return "ST_OK";
break;
case ST_STACKOVERFLOW:
return "ST_STACKOVERFLOW";
break;
case ST_STACKUNDERFLOW:
return "ST_STACKUNDERFLOW";
break;
case ST_SEGFAULT:
return "ST_SEGFAULT";
break;
case ST_QUIRK_UNDEFINED:
return "ST_QUIRK_UNDEFINED";
break;
case ST_OP_UNDEFINED:
return "ST_OP_UNDEFINED";
break;
}
return "ST_UNKNOWN";
}
char *exttocstr(extensions_t ext)
{
switch(ext)
{
case CHIP8:
return "CHIP8";
break;
case SCHIP:
return "SCHIP";
break;
case XOCHIP:
return "XOCHIP";
break;
}
return "UNKNOWN";
}
uint8_t testaddoverflow(uint8_t a, uint8_t b)
{
return b > UINT8_MAX - a;
}
uint8_t testsubunderflow(uint8_t a, uint8_t b)
{
return a < b;
}
uint8_t testsegfault(uint16_t memptr, vm_t *vm)
{
if(vm->mem+memptr >= vm->mem + MEMORY_SIZE)
return 0;
return 1;
}
status_t step(vm_t *vm)
{
if(!testsegfault(vm->PC, vm))
return ST_SEGFAULT;
if(!testsegfault(vm->PC+1, vm))
return ST_SEGFAULT;
uint16_t op = (vm->mem[vm->PC] << 8) | (vm->mem[vm->PC+1]);
uint8_t hn = (op & 0xf000) >> 8;
uint8_t ln = (op & 0x00ff);
uint8_t n = (op & 0x000f);
uint16_t nnn = (op & 0x0fff);
uint8_t x = (op & 0x0f00) >> 8;
uint8_t y = (op & 0x00f0) >> 4;
int8_t inp = input();
uint8_t shift;
uint8_t flag = 0;
vm->op = op;
if(inp == HALT_KEYCODE)
{
vm->halt = 1;
return ST_OK;
}
else if(inp == MEMDUMP_KEYCODE)
{
memdump(vm);
return ST_OK;
}
else if(inp == REFRESH_KEYCODE)
{
vm->redrawscreen = 1;
return ST_OK;
}
#ifdef FLOOD_WITH_OPS
fprintf(stderr, "========================\n");
fprintf(stderr, "op: 0x%04x\n", op);
fprintf(stderr, "registers: ");
for(int i = 0; i < 16; i++)
{
fprintf(stderr, "%d ", vm->V[i]);
}
fprintf(stderr, "\nnnn: 0x%04x %d\n", nnn, nnn);
fprintf(stderr, "ln: 0x%02x %d\n", ln, ln);
fprintf(stderr, "PC: 0x%04x\n", vm->PC);
fprintf(stderr, "SP: 0x%04x\n", vm->SP);
fprintf(stderr, "I: 0x%04x\n", vm->I);
#endif
#ifdef TESTNBLINP
showinp(inp);
#endif
switch(hn)
{
case 0x00:
if(ln == 0x00)
{
// do nothing
}
else if(ln == 0xe0)
{
if(vm->graphicsmode == LORES)
memset(vm->screen, 0, sizeof(vm->screen));
else
memset(vm->screenhr, 0, sizeof(vm->screenhr));
vm->redrawscreen = 1;
}
else if(ln == 0xee)
{
if(vm->SP == 0) return ST_STACKUNDERFLOW;
vm->PC = pop(vm);
}
else if(vm->extensions > CHIP8)
{
if(ln == 0xff)
vm->graphicsmode = HIRES;
else if(ln == 0xfe)
vm->graphicsmode = LORES;
else if((ln & 0xf0) == 0xc0);
// TODO: scrolldown n - move screen up by n pixels
else if(ln == 0xfb);
// TODO: scrollright - move screen left by 4 pixels
else if(ln == 0xfc);
// TODO: scrollleft - move screen right by 4 pixels
else if(ln == 0xfd)
vm->halt = 1;
else
return ST_OP_UNDEFINED;
}
else
return ST_OP_UNDEFINED;
break;
case 0x10:
vm->PC = nnn - 2;
break;
case 0x20:
if(vm->SP == STACK_SIZE) return ST_STACKOVERFLOW;
push(vm, vm->PC);
vm->PC = nnn - 2;
break;
case 0x30:
if(vm->V[x] == ln) vm->PC+=2;
break;
case 0x40:
if(vm->V[x] != ln) vm->PC+=2;
break;
case 0x50:
if(vm->V[x] == vm->V[y]) vm->PC+=2;
break;
case 0x60:
vm->V[x] = ln;
break;
case 0x70:
vm->V[x] += ln;
break;
case 0x80:
switch(n)
{
case 0x00:
vm->V[x] = vm->V[y];
break;
case 0x01:
if(vm->extensions == CHIP8) vm->V[15] = 0;
vm->V[x] |= vm->V[y];
break;
case 0x02:
if(vm->extensions == CHIP8) vm->V[15] = 0;
vm->V[x] &= vm->V[y];
break;
case 0x03:
if(vm->extensions == CHIP8) vm->V[15] = 0;
vm->V[x] ^= vm->V[y];
break;
case 0x04:
flag = testaddoverflow(vm->V[x], vm->V[y]);
vm->V[x] += vm->V[y];
vm->V[15] = flag;
break;
case 0x05:
flag = !testsubunderflow(vm->V[x], vm->V[y]);
vm->V[x] -= vm->V[y];
vm->V[15] = flag;
break;
case 0x06:
if(vm->extensions == CHIP8)
shift = vm->V[y];
else if(vm->extensions == SCHIP)
shift = vm->V[x];
else
return ST_QUIRK_UNDEFINED;
flag = shift & 0x01;
vm->V[x] = shift >> 1;
vm->V[15] = flag;
break;
case 0x07:
flag = !testsubunderflow(vm->V[y], vm->V[x]);
vm->V[x] = vm->V[y] - vm->V[x];
vm->V[15] = flag;
break;
case 0x0e:
if(vm->extensions == CHIP8)
shift = vm->V[y];
else if(vm->extensions == SCHIP)
shift = vm->V[x];
else
return ST_QUIRK_UNDEFINED;
flag = shift >> 7;
vm->V[x] = shift << 1;
vm->V[15] = flag;
break;
default:
return ST_OP_UNDEFINED;
break;
}
break;
case 0x90:
if(vm->V[x] != vm->V[y]) vm->PC+=2;
break;
case 0xa0:
vm->I = nnn;
break;
case 0xb0:
// TODO: make this quirk disableable at runtime as some games don't support it
#ifndef DISABLEJUMPINGQUIRK
if(vm->extensions == CHIP8)
vm->PC = vm->V[0] + nnn - 2;
else if(vm->extensions == SCHIP)
vm->PC = vm->V[x] + nnn - 2;
else
return ST_QUIRK_UNDEFINED;
#else
vm->PC = vm->V[0] + nnn - 2;
#endif
break;
case 0xc0:
vm->V[x] = randint() & ln;
break;
case 0xd0:
if(vm->extensions > CHIP8 && n == 0)
n = 16;
if(!testsegfault(vm->I, vm))
return ST_SEGFAULT;
if(!testsegfault(vm->I+n, vm))
return ST_SEGFAULT;
draw(vm, vm->V[x], vm->V[y], n);
break;
case 0xe0:
if(ln == 0x9e)
{
if(inp == vm->V[x]) vm->PC+=2;
}
else if(ln == 0xa1)
{
if(inp != vm->V[x]) vm->PC+=2;
}
else
return ST_OP_UNDEFINED;
break;
case 0xf0:
switch(ln)
{
case 0x07:
vm->V[x] = vm->delay;
break;
case 0x0a:
vm->V[x] = blockinginput();
break;
case 0x15:
vm->delay = vm->V[x];
break;
case 0x18:
vm->sound = vm->V[x];
break;
case 0x1e:
vm->I += vm->V[x];
break;
case 0x29:
vm->I = vm->V[x]*5;
break;
case 0x30:
if(vm->extensions == CHIP8)
return ST_OP_UNDEFINED;
vm->I = vm->V[x]*20 + 80;
break;
case 0x33:
if(!testsegfault(vm->I, vm))
return ST_SEGFAULT;
if(!testsegfault(vm->I+2, vm))
return ST_SEGFAULT;
vm->mem[vm->I+2] = vm->V[x] % 10;
vm->mem[vm->I+1] = vm->V[x] / 10 % 10;
vm->mem[vm->I] = vm->V[x] / 100 % 10;
break;
case 0x55:
if(!testsegfault(vm->I, vm))
return ST_SEGFAULT;
if(!testsegfault(vm->I+x, vm))
return ST_SEGFAULT;
memcpy(vm->mem+vm->I, &vm->V, x+1);
if(vm->extensions == CHIP8)
vm->I += x + 1;
else if(vm->extensions == SCHIP);
else
return ST_QUIRK_UNDEFINED;
break;
case 0x65:
if(!testsegfault(vm->I, vm))
return ST_SEGFAULT;
if(!testsegfault(vm->I+x, vm))
return ST_SEGFAULT;
memcpy(&vm->V, vm->mem+vm->I, x+1);
if(vm->extensions == CHIP8)
vm->I += x + 1;
else if(vm->extensions == SCHIP);
else
return ST_QUIRK_UNDEFINED;
break;
case 0x75:
if(vm->extensions == CHIP8)
return ST_OP_UNDEFINED;
save(vm->V, x);
break;
case 0x85:
if(vm->extensions == CHIP8)
return ST_OP_UNDEFINED;
load(vm->V, x);
break;
default:
return ST_OP_UNDEFINED;
break;
}
break;
default:
return ST_OP_UNDEFINED;
break;
}
timing_t tnow = now();
if(tnow - vm->dstart >= hztotiming(DELAY_HZ))
{
vm->dstart = tnow;
if(vm->sound == 1) pabeep();
if(vm->delay > 0) vm->delay--;
if(vm->sound > 0) vm->sound--;
}
vm->PC+=2;
if(vm->PC > 0x0ffe) vm->halt = 1;
return ST_OK;
}
void draw16(vm_t *vm, uint8_t *screen, int x, int y, int width, int height)
{
uint8_t *sprite = &vm->mem[vm->I];
vm->V[15] = 0;
vm->redrawscreen = 1;
x %= width;
y %= height;
for(int v = 0; v < 32; v+=2)
{
for(int u = 0; u < 16; u++)
{
uint8_t bit;
if(u < 8)
bit = (sprite[v] >> (7 - u)) & 0x01;
else
bit = (sprite[v+1] >> (15 - u)) & 0x01;
int i;
if(!coordtoi(&i, x+u, y+v/2, width, height))
continue;
if(screen[i] && bit) vm->V[15] = 1;
screen[i] ^= bit;
}
}
}
void drawsprite(vm_t *vm, uint8_t *screen, int x, int y, int n, int width, int height)
{
if(vm->extensions > CHIP8 && n == 16)
{
draw16(vm, screen, x, y, width, height);
return;
}
uint8_t *sprite = &vm->mem[vm->I];
vm->V[15] = 0;
vm->redrawscreen = 1;
x %= width;
y %= height;
for(int v = 0; v < n; v++)
{
for(int u = 0; u < 8; u++)
{
uint8_t bit = (sprite[v] >> (7 - u)) & 0x01;
int i;
if(!coordtoi(&i, x+u, y+v, width, height))
continue;
if(screen[i] && bit) vm->V[15] = 1;
screen[i] ^= bit;
}
}
}
void draw(vm_t *vm, int x, int y, int n)
{
if(vm->graphicsmode == LORES)
drawsprite(vm, vm->screen, x, y, n, SCREEN_WIDTH, SCREEN_HEIGHT);
else
drawsprite(vm, vm->screenhr, x, y, n, SCREEN_WIDTH_HIRES, SCREEN_HEIGHT_HIRES);
}