-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8.c
464 lines (404 loc) · 8.99 KB
/
chip8.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <SDL.h>
#define CHIP8_SCALE (10)
#define CHIP8_WIDTH (64)
#define CHIP8_HEIGHT (32)
struct chip8_state {
uint8_t memory[4096];
uint8_t V[16];
uint16_t pc;
uint16_t I;
uint8_t gfx[CHIP8_WIDTH * CHIP8_HEIGHT];
#define CHIP8_TOGGLE_PIXEL(s, x, y)\
((s)->gfx[((x) % CHIP8_WIDTH) + CHIP8_WIDTH * ((y) % CHIP8_HEIGHT)]^=1)
uint16_t stack[16];
uint8_t sp;
uint8_t key[16];
uint16_t delay;
uint16_t sound;
uint8_t flags;
#define CHIP8_DRAW_FLAG (0x1)
SDL_Window *win;
SDL_Renderer *ren;
};
static void chip8_init(struct chip8_state *, const char *);
static void chip8_draw(struct chip8_state *);
static void chip8_decode(struct chip8_state *, uint16_t);
static void chip8_decode_math(struct chip8_state *, uint8_t, uint8_t, uint8_t);
static void chip8_decode_draw(struct chip8_state *, uint8_t, uint8_t, uint8_t);
static void chip8_decode_keys(struct chip8_state *, uint8_t, uint8_t);
static void chip8_decode_misc(struct chip8_state *, uint8_t, uint8_t);
static void chip8_handle_keys(struct chip8_state *, SDL_Event *);
static void chip8_handle_timer(struct chip8_state *);
static void chip8_wait_for_key(struct chip8_state *, uint8_t);
static void chip8_destroy(struct chip8_state *);
int
main(int argc, char **argv)
{
struct chip8_state state;
SDL_Event ev;
uint8_t do_timer = 0;
if (argc != 2)
errx(1, "usage: %s program", argv[0]);
chip8_init(&state, argv[1]);
SDL_Init(SDL_INIT_VIDEO);
state.win = SDL_CreateWindow("CHIP-8", 100, 100,
CHIP8_SCALE * CHIP8_WIDTH, CHIP8_SCALE * CHIP8_HEIGHT,
SDL_WINDOW_SHOWN);
if (state.win == NULL) {
SDL_Quit();
errx(1, "SDL_CreateWindow: %s", SDL_GetError());
}
state.ren = SDL_CreateRenderer(state.win, -1,
SDL_RENDERER_ACCELERATED);
if (state.ren == NULL) {
SDL_DestroyWindow(state.win);
SDL_Quit();
errx(1, "SDL_CreateRenderer: %s", SDL_GetError());
}
for (;;) {
uint16_t opcode;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
goto cleanup;
chip8_handle_keys(&state, &ev);
}
if (state.pc >= 4096)
goto cleanup;
opcode = (state.memory[state.pc] << 8)
| state.memory[state.pc + 1];
state.pc += 2;
chip8_decode(&state, opcode);
if (state.flags & CHIP8_DRAW_FLAG)
chip8_draw(&state);
if ((do_timer = (do_timer + 1) & 1) == 0)
chip8_handle_timer(&state);
SDL_Delay(1);
}
cleanup:
chip8_destroy(&state);
return 0;
}
const static uint8_t chip8_fontset[80] = {
0xF0, 0x90, 0x90, 0x90, 0xF0,
0x20, 0x60, 0x20, 0x20, 0x70,
0xF0, 0x10, 0xF0, 0x80, 0xF0,
0xF0, 0x10, 0xF0, 0x10, 0xF0,
0x90, 0x90, 0xF0, 0x10, 0x10,
0xF0, 0x80, 0xF0, 0x10, 0xF0,
0xF0, 0x80, 0xF0, 0x90, 0xF0,
0xF0, 0x10, 0x20, 0x40, 0x40,
0xF0, 0x90, 0xF0, 0x90, 0xF0,
0xF0, 0x90, 0xF0, 0x10, 0xF0,
0xF0, 0x90, 0xF0, 0x90, 0x90,
0xE0, 0x90, 0xE0, 0x90, 0xE0,
0xF0, 0x80, 0x80, 0x80, 0xF0,
0xE0, 0x90, 0x90, 0x90, 0xE0,
0xF0, 0x80, 0xF0, 0x80, 0xF0,
0xF0, 0x80, 0xF0, 0x80, 0x80
};
static void
chip8_init(struct chip8_state *state, const char *path)
{
FILE *fp;
memset(state, '\0', sizeof(struct chip8_state));
memcpy(state->memory, chip8_fontset, sizeof(chip8_fontset));
state->pc = 0x200;
fp = fopen(path, "r");
if (fp == NULL)
err(1, "fopen");
fread(state->memory + 0x200, sizeof(uint8_t), 3136, fp);
fclose(fp);
}
static void
chip8_draw(struct chip8_state *state)
{
int x, y;
SDL_Rect r;
r.w = CHIP8_SCALE;
r.h = CHIP8_SCALE;
SDL_SetRenderDrawColor(state->ren, 0, 0, 0, 255);
SDL_RenderClear(state->ren);
SDL_SetRenderDrawColor(state->ren, 255, 255, 255, 255);
for (x = 0; x < CHIP8_WIDTH; ++x) {
for (y = 0; y < CHIP8_HEIGHT; ++y) {
if (state->gfx[x + CHIP8_WIDTH * y] == 0)
continue;
r.x = x * CHIP8_SCALE;
r.y = y * CHIP8_SCALE;
SDL_RenderFillRect(state->ren, &r);
}
}
SDL_RenderPresent(state->ren);
state->flags &= ~CHIP8_DRAW_FLAG;
}
static void
chip8_decode(struct chip8_state *state, uint16_t opcode)
{
uint8_t x, y;
x = (opcode & 0x0F00) >> 8;
y = (opcode & 0x00F0) >> 4;
switch (opcode & 0xF000) {
case 0x0000:
if (opcode == 0x00EE)
state->pc = state->stack[--state->sp];
else if (opcode == 0x00E0)
memset(state->gfx, '\0', CHIP8_WIDTH * CHIP8_HEIGHT);
break;
case 0x1000:
state->pc = opcode & 0x0FFF;
break;
case 0x2000:
state->stack[state->sp] = state->pc;
++state->sp;
state->pc = opcode & 0x0FFF;
break;
case 0x3000:
if (state->V[x] == (opcode & 0x00FF))
state->pc += 2;
break;
case 0x4000:
if (state->V[x] != (opcode & 0x00FF))
state->pc += 2;
break;
case 0x5000:
if (state->V[x] == state->V[y])
state->pc += 2;
break;
case 0x6000:
state->V[x] = opcode & 0x00FF;
break;
case 0x7000:
state->V[x] += opcode & 0x00FF;
break;
case 0x8000:
chip8_decode_math(state, opcode & 0x000F, x, y);
break;
case 0x9000:
if (state->V[x] != state->V[y])
state->pc += 2;
break;
case 0xA000:
state->I = opcode & 0x0FFF;
break;
case 0xB000:
state->pc = state->V[0] + (opcode & 0x0FFF);
break;
case 0xC000:
state->V[x] = rand() & opcode & 0x00FF;
break;
case 0xD000:
chip8_decode_draw(state, x, y, opcode & 0x000F);
break;
case 0xE000:
chip8_decode_keys(state, x, opcode & 0x00FF);
break;
case 0xF000:
chip8_decode_misc(state, x, opcode & 0x00FF);
break;
}
}
static void
chip8_decode_math(struct chip8_state *state, uint8_t op, uint8_t x, uint8_t y)
{
int16_t temp;
switch (op) {
case 0x0:
state->V[x] = state->V[y];
break;
case 0x1:
state->V[x] |= state->V[y];
break;
case 0x2:
state->V[x] &= state->V[y];
break;
case 0x3:
state->V[x] ^= state->V[y];
break;
case 0x4:
temp = state->V[x] + state->V[y];
if (temp > 255) {
state->V[0xF] = 1;
temp = temp % 256;
} else {
state->V[0xF] = 0;
}
state->V[x] = temp;
break;
case 0x5:
temp = state->V[x] - state->V[y];
if (temp < 0) {
state->V[0xF] = 0;
temp = (temp % 256) + 256;
} else {
state->V[0xF] = 1;
}
state->V[x] = temp;
break;
case 0x6:
state->V[0xF] = state->V[x] & 0x1;
state->V[x] >>= 1;
break;
case 0x7:
temp = state->V[y] - state->V[x];
if (temp < 0) {
state->V[0xF] = 0;
temp = (temp % 256) + 256;
} else {
state->V[0xF] = 1;
}
state->V[x] = temp;
break;
case 0xE:
temp = state->V[x] << 1;
if (temp > 255) {
state->V[0xF] = 1;
temp = temp % 256;
} else {
state->V[0xF] = 0;
}
state->V[x] = temp;
break;
}
}
static void
chip8_decode_draw(struct chip8_state *state, uint8_t x, uint8_t y, uint8_t h)
{
uint8_t i, j, spr;
state->V[0xF] = 0;
for (j = 0; j < h; ++j) {
spr = state->memory[state->I + j];
for (i = 0; i < 8; ++i) {
if (spr & 0x80) {
if (CHIP8_TOGGLE_PIXEL(state,
state->V[x] + i, state->V[y] + j) == 0)
state->V[0xF] = 1;
}
spr <<= 1;
}
}
state->flags |= CHIP8_DRAW_FLAG;
}
static void
chip8_decode_keys(struct chip8_state *state, uint8_t x, uint8_t op)
{
switch (op) {
case 0x9E:
if (state->key[state->V[x] & 0xF])
state->pc += 2;
break;
case 0xA1:
if (!state->key[state->V[x] & 0xF])
state->pc += 2;
break;
}
}
static void
chip8_decode_misc(struct chip8_state *state, uint8_t x, uint8_t op)
{
uint8_t i, n;
switch (op) {
case 0x07:
state->V[x] = state->delay;
break;
case 0x0A:
chip8_wait_for_key(state, x);
break;
case 0x15:
state->delay = state->V[x];
break;
case 0x18:
state->sound = state->V[x];
break;
case 0x1E:
state->I += state->V[x];
break;
case 0x29:
state->I = state->V[x] * 5;
break;
case 0x33:
n = state->V[x];
for (i = 3; i > 0; --i) {
state->memory[state->I + i - 1] = n % 10;
n /= 10;
}
break;
case 0x55:
for (i = 0; i <= x; ++i)
state->memory[state->I + i] = state->V[i];
break;
case 0x65:
for (i = 0; i <= x; ++i)
state->V[i] = state->memory[state->I + i];
break;
}
}
static void
chip8_handle_keys(struct chip8_state *state, SDL_Event *ev)
{
uint8_t setval;
if (ev->type != SDL_KEYDOWN && ev->type != SDL_KEYUP)
return;
if (ev->key.state == SDL_PRESSED)
setval = 1;
else if (ev->key.state == SDL_RELEASED)
setval = 0;
switch (ev->key.keysym.sym) {
#define KEYMAP(s, n) case (s):\
state->key[(n)] = setval;\
break;
#include "keymap.def"
#undef KEYMAP
}
}
static void
chip8_handle_timer(struct chip8_state *state)
{
if (state->delay > 0)
--state->delay;
if (state->sound > 0) {
if (state->sound == 1) {
/* TODO - beep */
}
--state->sound;
}
}
static void
chip8_wait_for_key(struct chip8_state *state, uint8_t reg)
{
SDL_Event ev;
for (;;) {
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
goto shutdown;
if (ev.type != SDL_KEYDOWN)
continue;
switch (ev.key.keysym.sym) {
#define KEYMAP(s, n) case (s):\
state->V[reg] = (n);\
state->key[(n)] = 1;\
break;
#include "keymap.def"
#undef KEYMAP
default:
continue;
}
return;
}
chip8_draw(state);
}
shutdown:
chip8_destroy(state);
exit(0);
}
static void
chip8_destroy(struct chip8_state *state)
{
SDL_DestroyRenderer(state->ren);
SDL_DestroyWindow(state->win);
SDL_Quit();
}