-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip9.c
572 lines (484 loc) · 10.8 KB
/
chip9.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <keyboard.h>
#include <mouse.h>
#include <bio.h>
Image *red, *green;
Keyboardctl *kctl;
Mousectl *mctl;
#define MIN(a, b) (a < b ? a : b)
#define scr (screen->r)
#define scrx (int)(screen->r.min.x)
#define scry (int)(screen->r.min.y)
#define scrmx (int)(screen->r.max.x)
#define scrmy (int)(screen->r.max.y)
#define scrw (int)(scrmx - scrx)
#define scrh (int)(scrmy - scry)
#define WIDTH 800
#define HEIGHT 600
#define BORDER_SIZE 6
#define SCREEN_WIDTH 64
#define SCREEN_HEIGHT 32
#define MEMORY_SIZE 4096
#define STK_SIZE 16
#define NUM_REGS 16
#define NUM_KEYS 16
#define START_ADDR 0x200
#define FONTSET_SIZE 80
#define FONT_ADDR 0x50
#define PC_STEP 0x2
#define CF 0xF
#define SCREEN_X (int)((scrw/2) - (SCREEN_WIDTH*emu.scale)/2)
#define SCREEN_Y (int)((scrh/2) - (SCREEN_HEIGHT*emu.scale)/2)
#define KEY_DEBUG 'i'
#define KEY_STEP 'o'
#define KEY_PAUSE 'p'
#define EMULATOR_SPEED 1
typedef struct emulator Emulator;
struct emulator {
Image *screen;
Rectangle r;
int stack[STK_SIZE];
char keys[NUM_KEYS];
char gfx[SCREEN_WIDTH * SCREEN_HEIGHT];
uchar memory[MEMORY_SIZE];
uchar V[NUM_REGS];
int scale, running, debug, step;
uchar DT, ST;
uint PC, I, SP;
};
Emulator emu = {0};
// 1 2 3 C
// 4 5 6 D
// 7 8 9 E
// A 0 B F
//
// 1 2 3 4
// Q W E R
// A S D F
// Z X C V
const char keyset[NUM_KEYS] = {
'x', '1', '2', '3',
'q', 'w', 'e', 'a',
's', 'd', 'z', 'c',
'4', 'r', 'f', 'v'
};
const char fontset[FONTSET_SIZE] = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
};
#define OPCODE (ushort)(emu.memory[emu.PC]<<8 | emu.memory[emu.PC+1])
#define X (uchar)((OPCODE & 0x0F00) >> 8)
#define Y (uchar)((OPCODE & 0x00F0) >> 4)
#define N (uchar )(OPCODE & 0x000F)
#define NN (uchar )(OPCODE & 0x00FF)
#define NNN (ushort)(OPCODE & 0x0FFF)
#define INST (ushort)(OPCODE & 0xF000)
void resetemu(void);
void load_rom(char* path);
void drawpixel(Point p, char color);
void redraw_debug(void);
void redraw(void);
void resize(int x, int y);
void emouse(Mouse* m);
void err(void);
void interpret(void);
void clockproc(void *c);
void usage(void);
void threadmain(int argc, char* argv[]);
void
resetemu(void)
{
memcpy(emu.memory+FONT_ADDR, fontset, FONTSET_SIZE);
emu.PC = START_ADDR;
emu.debug = 1;
emu.running = 1;
emu.scale = MIN(scrw/SCREEN_WIDTH, scrh/SCREEN_HEIGHT);
emu.r = Rect(0, 0, (emu.scale*SCREEN_WIDTH), (emu.scale*SCREEN_HEIGHT));
emu.screen = allocimage(display, emu.r, RGB24, 1, 0x000000FF);
}
void
load_rom(char* path)
{
Biobuf* b;
ulong nr;
vlong siz;
if((b = Bopen(path, OREAD)) == nil)
sysfatal("Bopen: %r");
siz = Bseek(b, 0L, 2);
Bseek(b, 0L, 0);
if((nr = Bread(b, emu.memory+START_ADDR, siz)) != siz)
sysfatal("Bread: %r");
Bterm(b);
USED(nr);
print("loaded: %s\n", path);
}
void
drawpixel(Point p, char color)
{
if(p.x<0 || p.x>=(emu.scale*SCREEN_WIDTH))
sysfatal("draw x out of bounds");
if(p.y<0 || p.y>=(emu.scale*SCREEN_HEIGHT))
sysfatal("draw y out of bounds");
draw(emu.screen,
Rect((p.x*emu.scale), (p.y*emu.scale), (p.x*emu.scale)+emu.scale, (p.y*emu.scale)+emu.scale),
(color ? display->white : display->black), nil, ZP);
}
void
redraw_debug(void)
{
Point p;
char buf[256], b[32];
int i;
p = Pt(scrx+SCREEN_X, scry+font->height);
snprint(buf, sizeof buf, "%.10s opcode: 0x%.4X | inst: 0x%.4X | PC: 0x%.4X (%.4d)",
(!emu.running?"PAUSED":"RUNNING"), OPCODE, INST, emu.PC, emu.PC-START_ADDR);
string(screen, p, display->black, ZP, font, buf);
// | I: 0x%.4X (%.4d) | SP: %.4d | DT: %.4d | ST: %.4d
// emu.I, emu.I, emu.SP, emu.DT, emu.ST
p.y += font->height;
memset(buf, 0, sizeof buf);
for(i=0; i<NUM_REGS; i++){
snprint(b, sizeof b, "V%X=%.3d ", i, emu.V[i]);
strcat(buf, b);
}
string(screen, p, display->black, ZP, font, buf);
}
void
redraw(void)
{
Rectangle r;
r = Rect(scrx+SCREEN_X, scry+SCREEN_Y,
scrx+SCREEN_X+emu.r.max.x, scry+SCREEN_Y+emu.r.max.y);
draw(screen, screen->r, display->white, nil, ZP);
if(emu.debug)
redraw_debug();
draw(screen, r, display->transparent, emu.screen, ZP);
flushimage(display, 1);
}
void
resize(int x, int y)
{
int fd;
if((fd = open("/dev/wctl", OWRITE))){
fprint(fd, "resize -dx %d -dy %d", x, y);
close(fd);
}
}
void
emouse(Mouse* m)
{
USED(m);
// TODO: Add support for a menu, maybe?
}
void
err(void)
{
Biobuf* buf;
if((buf = Bopen("dump", OWRITE|OTRUNC)) == nil)
sysfatal("Bopen: %r");
Bwrite(buf, emu.memory, sizeof emu.memory);
Bterm(buf);
print("unknown opcode: 0x%.4X (instruction: 0x%X) @ PC=%d\n",
OPCODE, INST, emu.PC-START_ADDR);
emu.running=0;
}
void
interpret(void)
{
short i, px, py, row, bit;
short index, p;
switch(INST){
case 0x0000:
switch(NN){
case 0x0000: // NOP [0NNN]
break;
case 0x00E0: // CLEAR [00E0]
memset(emu.gfx, 0, (SCREEN_WIDTH*SCREEN_HEIGHT));
draw(emu.screen, emu.r, display->black, nil, ZP);
break;
case 0x00EE: // RET [00EE]
emu.PC = emu.stack[emu.SP--];
break;
}
break;
case 0x1000: // JMP [1NNN]
emu.PC = NNN;
return;
case 0x2000: // CALL NNN [2NNN]
emu.stack[++emu.SP] = emu.PC;
emu.PC = NNN;
return;
case 0x3000: // SKIP VX == NN [3XNN]
if(emu.V[X] == NN)
emu.PC += PC_STEP;
break;
case 0x4000: // SKIP VX != NN [4XNN]
if(emu.V[X] != NN)
emu.PC += PC_STEP;
break;
case 0x5000: // SKIP VX == VY [5XY0]
if(emu.V[X] == emu.V[Y])
emu.PC += PC_STEP;
break;
case 0x6000: // VX = NN [6XNN]
emu.V[X] = NN;
break;
case 0x7000: // VX += NN [7XNN]
emu.V[X] += NN;
break;
case 0x8000:
switch(N){
case 0x0000: // VX = VY [8XY0]
emu.V[X] = emu.V[Y];
break;
case 0x0001: // VX |= VY [8XY1]
emu.V[X] |= emu.V[Y];
emu.V[CF]=0;
break;
case 0x0002: // VX &= VY [8XY2]
emu.V[X] &= emu.V[Y];
emu.V[CF]=0;
break;
case 0x0003: // VX ^= VY [8XY3]
emu.V[X] ^= emu.V[Y];
emu.V[CF]=0;
break;
case 0x0004: // VX += VY [8XY4]
p = emu.V[X] + emu.V[Y];
emu.V[X] = p;
emu.V[CF] = (p>0xFF);
break;
case 0x0005: // VX -= VY [8XY5]
p = (emu.V[X] >= emu.V[Y]);
emu.V[X] -= emu.V[Y];
emu.V[CF] = p;
break;
case 0x0006: // VX >> 1 [8XY6]
p = (emu.V[Y] & 1);
emu.V[X] >>= 1;
emu.V[CF] = p;
break;
case 0x0007: // VX >>= 1 [8XY7]
emu.V[X] = emu.V[Y] - emu.V[X];
emu.V[CF] = (emu.V[Y] >= emu.V[X]);
break;
case 0x000E: // VX <<= 1 [8XYE]
p = (emu.V[X] >> 7);
emu.V[X] <<= 1;
emu.V[CF] = p;
break;
}
break;
case 0x9000: // SKIP VX != VY [9XY0]
if(emu.V[X] != emu.V[Y])
emu.PC += PC_STEP;
break;
case 0xA000: // I = NNNN [ANNN]
emu.I = NNN;
break;
case 0xB000: // JMP V0 + NNN [BNNN]
emu.PC = NNN + emu.V[0x0];
return;
case 0xC000: // VX = rand() & NN [CXNN]
srand(time(nil));
emu.V[X] = (rand() & NN);
break;
case 0xD000: // DRAW [DXYNN]
emu.V[CF]=0;
for(row=0; row<N; row++){
py = (emu.V[Y] + row) % SCREEN_HEIGHT;
for(bit=0; bit<8; bit++){
px = (emu.V[X] + (7 - bit)) % SCREEN_WIDTH;
p = (emu.memory[emu.I + row] >> bit) & 0x01;
index = px + (py * SCREEN_WIDTH);
emu.V[CF] = (p && emu.gfx[index]);
emu.gfx[index] ^= p;
drawpixel(Pt(px, py), emu.gfx[index]);
}
}
break;
case 0xE000:
switch(NN){
case 0x009E: // SKIP KEY PRESS [EX9E]
if(emu.keys[emu.V[X]]==1)
emu.PC += PC_STEP;
break;
case 0x00A1: // SKIP KEY RELEASE [EXA1]
if(emu.keys[emu.V[X]]==0)
emu.PC += PC_STEP;
break;
}
break;
case 0xF000:
switch(NN){
case 0x0007: // VX = DT [FX07]
emu.V[X] = emu.DT;
break;
case 0x000A: // WAIT KEY [FX0A]
bit = -1;
for(i=0; i<NUM_KEYS; i++){
if(emu.keys[i] != 0){
bit=i;
break;
}
}
if(bit != -1)
emu.V[X] = bit;
else
emu.PC -= PC_STEP;
break;
case 0x0015: // DT = VX [FX15]
emu.DT = emu.V[X];
break;
case 0x0018: // ST = VX [FX18]
emu.ST = emu.V[X];
break;
case 0x001E: // I += VX [FX1E]
emu.I += emu.V[X];
break;
case 0x0029: // I = FONT [FX29]
emu.I = (emu.V[X] * 5) + FONT_ADDR;
break;
case 0x0033: // BCD [FX33]
emu.memory[emu.I] = emu.V[X]/100;
emu.memory[emu.I+1] = (emu.V[X]/10)%10;
emu.memory[emu.I+2] = emu.V[X]%10;
break;
case 0x0055: // STORE V0 - VX [FX55]
for(i=0; i<=X; i++)
emu.memory[emu.I+i] = emu.V[i];
break;
case 0x0065: // LOAD V0 - VX [FX65]
for(i=0; i<=X; i++)
emu.V[i] = emu.memory[emu.I+i];
break;
}
break;
}
emu.PC += PC_STEP;
if(emu.step){
emu.step=0;
emu.running=0;
}
}
void
clockproc(void *c)
{
threadsetname("clock");
for(;;){
sleep(EMULATOR_SPEED);
sendul(c, 0);
if(emu.PC >= MEMORY_SIZE || (!emu.running && !emu.step))
continue;
interpret();
emu.DT -= (emu.DT>0);
emu.ST -= (emu.ST>0);
if(!emu.ST){
// TODO: Beep!
}
memset(emu.keys, 0, NUM_KEYS);
}
}
void
keyboardproc(void *)
{
Rune r;
int i;
threadsetname("keyboardproc");
for(;;){
recv(kctl->c, &r);
switch(r){
case Kdel:
threadexitsall(nil);
break;
case KEY_STEP:
emu.step = 1;
break;
case KEY_PAUSE:
emu.running = !emu.running;
if(!emu.running)
emu.step = 0;
break;
case KEY_DEBUG:
emu.debug = !emu.debug;
break;
}
for(i=0; i<NUM_KEYS; i++)
emu.keys[i] = (keyset[i]==r);
}
}
void
usage(void)
{
fprint(2, "usage: %s rom\n", argv0);
threadexitsall("usage");
}
void
threadmain(int argc, char* argv[])
{
Mouse m;
Rune k;
Alt alts[] = {
{ nil, &m, CHANRCV },
{ nil, &k, CHANRCV },
{ nil, nil, CHANRCV },
{ nil, nil, CHANRCV },
{ nil, nil, CHANEND },
};
ARGBEGIN{
case 'h':
usage();
}ARGEND;
if(initdraw(nil, nil, argv0) < 0)
sysfatal("initdraw: %r");
display->locking = 0;
if((mctl = initmouse(nil, screen)) == nil)
sysfatal("initmouse: %r");
if((kctl = initkeyboard(nil)) == nil)
sysfatal("initkeyboard: %r");
srand(time(nil));
resetemu();
if(argc<1)
usage();
load_rom(argv[0]);
alts[0].c = mctl->c;
alts[1].c = kctl->c;
alts[2].c = mctl->resizec;
alts[3].c = chancreate(sizeof(ulong), 0);
proccreate(clockproc, alts[3].c, 1024);
proccreate(keyboardproc, nil, 1024);
red = allocimage(display, Rect(0,0,1,1), RGB24, 1, 0xE47674FF);
green = allocimage(display, Rect(0,0,1,1), RGB24, 1, 0x7DFDA4FF);
for(;;){
switch(alt(alts)){
case 0:
emouse(&m);
break;
case 2:
if(getwindow(display, Refnone) < 0)
sysfatal("getwindow: %r");
redraw();
break;
case 3:
redraw();
break;
}
}
}