-
Notifications
You must be signed in to change notification settings - Fork 57
/
chip8.c
406 lines (372 loc) · 12.9 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
/**
* @file chip8.c
*
* @author David Matlack
*/
#include "chip8.h"
#define unknown_opcode(op) \
do { \
fprintf(stderr, "Unknown opcode: 0x%x\n", op); \
fprintf(stderr, "kk: 0x%02x\n", kk); \
exit(42); \
} while (0)
#ifdef DEBUG
#define p(...) printf(__VA_ARGS__);
#else
#define p(...)
#endif
#define IS_BIT_SET(byte, bit) (((0x80 >> (bit)) & (byte)) != 0x0)
#define FONTSET_ADDRESS 0x00
#define FONTSET_BYTES_PER_CHAR 5
unsigned char chip8_fontset[80] =
{
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
};
uint16_t opcode;
uint8_t memory[MEM_SIZE];
uint8_t V[16];
uint16_t I;
uint16_t PC;
uint8_t gfx[GFX_ROWS][GFX_COLS];
uint8_t delay_timer;
uint8_t sound_timer;
uint16_t stack[STACK_SIZE];
uint16_t SP;
uint8_t key[KEY_SIZE];
bool chip8_draw_flag;
static inline uint8_t randbyte() { return (rand() % 256); }
void draw_sprite(uint8_t x, uint8_t y, uint8_t n) {
unsigned row = y, col = x;
unsigned byte_index;
unsigned bit_index;
// set the collision flag to 0
V[0xF] = 0;
for (byte_index = 0; byte_index < n; byte_index++) {
uint8_t byte = memory[I + byte_index];
for (bit_index = 0; bit_index < 8; bit_index++) {
// the value of the bit in the sprite
uint8_t bit = (byte >> bit_index) & 0x1;
// the value of the current pixel on the screen
uint8_t *pixelp = &gfx[(row + byte_index) % GFX_ROWS]
[(col + (7 - bit_index)) % GFX_COLS];
// if drawing to the screen would cause any pixel to be erased,
// set the collision flag to 1
if (bit == 1 && *pixelp ==1) V[0xF] = 1;
// draw this pixel by XOR
*pixelp = *pixelp ^ bit;
}
}
}
static void debug_draw() {
int x, y;
for (y = 0; y < GFX_ROWS; y++) {
for (x = 0; x < GFX_COLS; x++) {
if (gfx[y][x] == 0) printf("0");
else printf(" ");
}
printf("\n");
}
printf("\n");
}
static void print_state() {
printf("------------------------------------------------------------------\n");
printf("\n");
printf("V0: 0x%02x V4: 0x%02x V8: 0x%02x VC: 0x%02x\n",
V[0], V[4], V[8], V[12]);
printf("V1: 0x%02x V5: 0x%02x V9: 0x%02x VD: 0x%02x\n",
V[1], V[5], V[9], V[13]);
printf("V2: 0x%02x V6: 0x%02x VA: 0x%02x VE: 0x%02x\n",
V[2], V[6], V[10], V[14]);
printf("V3: 0x%02x V7: 0x%02x VB: 0x%02x VF: 0x%02x\n",
V[3], V[7], V[11], V[15]);
printf("\n");
printf("PC: 0x%04x\n", PC);
printf("\n");
printf("\n");
}
void chip8_initialize() {
int i;
PC = 0x200;
opcode = 0;
I = 0;
SP = 0;
memset(memory, 0, sizeof(uint8_t) * MEM_SIZE);
memset(V, 0, sizeof(uint8_t) * 16);
memset(gfx, 0, sizeof(uint8_t) * GFX_SIZE);
memset(stack, 0, sizeof(uint16_t) * STACK_SIZE);
memset(key, 0, sizeof(uint8_t) * KEY_SIZE);
for (i = 0; i < 80; i++) {
memory[FONTSET_ADDRESS + i] = chip8_fontset[i];
}
chip8_draw_flag = true;
delay_timer = 0;
sound_timer = 0;
srand(time(NULL));
}
void chip8_loadgame(char *game) {
FILE *fgame;
fgame = fopen(game, "rb");
if (NULL == fgame) {
fprintf(stderr, "Unable to open game: %s\n", game);
exit(42);
}
fread(&memory[0x200], 1, MAX_GAME_SIZE, fgame);
fclose(fgame);
}
void chip8_emulatecycle() {
int i;
uint8_t x, y, n;
uint8_t kk;
uint16_t nnn;
// fetch
opcode = memory[PC] << 8 | memory[PC + 1];
x = (opcode >> 8) & 0x000F; // the lower 4 bits of the high byte
y = (opcode >> 4) & 0x000F; // the upper 4 bits of the low byte
n = opcode & 0x000F; // the lowest 4 bits
kk = opcode & 0x00FF; // the lowest 8 bits
nnn = opcode & 0x0FFF; // the lowest 12 bits
#ifdef DEBUG
printf("PC: 0x%04x Op: 0x%04x\n", PC, opcode);
#endif
// decode & execute: case on the highest order byte
switch (opcode & 0xF000) {
case 0x0000:
switch (kk) {
case 0x00E0: // clear the screen
p("Clear the screen\n");
memset(gfx, 0, sizeof(uint8_t) * GFX_SIZE);
chip8_draw_flag = true;
PC += 2;
break;
case 0x00EE: // ret
p("ret\n");
PC = stack[--SP];
break;
default:
unknown_opcode(opcode);
}
break;
case 0x1000: // 1nnn: jump to address nnn
p("Jump to address 0x%x\n", nnn);
PC = nnn;
break;
case 0x2000: // 2nnn: call address nnn
p("Call address 0x%x\n", nnn);
stack[SP++] = PC + 2;
PC = nnn;
break;
case 0x3000: // 3xkk: skip next instr if V[x] = kk
p("Skip next instruction if 0x%x == 0x%x\n", V[x], kk);
PC += (V[x] == kk) ? 4 : 2;
break;
case 0x4000: // 4xkk: skip next instr if V[x] != kk
p("Skip next instruction if 0x%x != 0x%x\n", V[x], kk);
PC += (V[x] != kk) ? 4 : 2;
break;
case 0x5000: // 5xy0: skip next instr if V[x] == V[y]
p("Skip next instruction if 0x%x == 0x%x\n", V[x], V[y]);
PC += (V[x] == V[y]) ? 4 : 2;
break;
case 0x6000: // 6xkk: set V[x] = kk
p("Set V[0x%x] to 0x%x\n", x, kk);
V[x] = kk;
PC += 2;
break;
case 0x7000: // 7xkk: set V[x] = V[x] + kk
p("Set V[0x%d] to V[0x%d] + 0x%x\n", x, x, kk);
V[x] += kk;
PC += 2;
break;
case 0x8000: // 8xyn: Arithmetic stuff
switch (n) {
case 0x0:
p("V[0x%x] = V[0x%x] = 0x%x\n", x, y, V[y]);
V[x] = V[y];
break;
case 0x1:
p("V[0x%x] |= V[0x%x] = 0x%x\n", x, y, V[y]);
V[x] = V[x] | V[y];
break;
case 0x2:
p("V[0x%x] &= V[0x%x] = 0x%x\n", x, y, V[y]);
V[x] = V[x] & V[y];
break;
case 0x3:
p("V[0x%x] ^= V[0x%x] = 0x%x\n", x, y, V[y]);
V[x] = V[x] ^ V[y];
break;
case 0x4:
p("V[0x%x] = V[0x%x] + V[0x%x] = 0x%x + 0x%x\n", x, x, y, V[x], V[y]);
V[0xF] = ((int) V[x] + (int) V[y]) > 255 ? 1 : 0;
V[x] = V[x] + V[y];
break;
case 0x5:
p("V[0x%x] = V[0x%x] - V[0x%x] = 0x%x - 0x%x\n", x, x, y, V[x], V[y]);
V[0xF] = (V[x] > V[y]) ? 1 : 0;
V[x] = V[x] - V[y];
break;
case 0x6:
p("V[0x%x] = V[0x%x] >> 1 = 0x%x >> 1\n", x, x, V[x]);
V[0xF] = V[x] & 0x1;
V[x] = (V[x] >> 1);
break;
case 0x7:
p("V[0x%x] = V[0x%x] - V[0x%x] = 0x%x - 0x%x\n", x, y, x, V[y], V[x]);
V[0xF] = (V[y] > V[x]) ? 1 : 0;
V[x] = V[y] - V[x];
break;
case 0xE:
p("V[0x%x] = V[0x%x] << 1 = 0x%x << 1\n", x, x, V[x]);
V[0xF] = (V[x] >> 7) & 0x1;
V[x] = (V[x] << 1);
break;
default:
unknown_opcode(opcode);
}
PC += 2;
break;
case 0x9000: // 9xy0: skip instruction if Vx != Vy
switch (n) {
case 0x0:
p("Skip next instruction if 0x%x != 0x%x\n", V[x], V[y]);
PC += (V[x] != V[y]) ? 4 : 2;
break;
default:
unknown_opcode(opcode);
}
break;
case 0xA000: // Annn: set I to address nnn
p("Set I to 0x%x\n", nnn);
I = nnn;
PC += 2;
break;
case 0xB000: // Bnnn: jump to location nnn + V[0]
p("Jump to 0x%x + V[0] (0x%x)\n", nnn, V[0]);
PC = nnn + V[0];
break;
case 0xC000: // Cxkk: V[x] = random byte AND kk
p("V[0x%x] = random byte\n", x);
V[x] = randbyte() & kk;
PC += 2;
break;
case 0xD000: // Dxyn: Display an n-byte sprite starting at memory
// location I at (Vx, Vy) on the screen, VF = collision
p("Draw sprite at (V[0x%x], V[0x%x]) = (0x%x, 0x%x) of height %d",
x, y, V[x], V[y], n);
draw_sprite(V[x], V[y], n);
PC += 2;
chip8_draw_flag = true;
break;
case 0xE000: // key-pressed events
switch (kk) {
case 0x9E: // skip next instr if key[Vx] is pressed
p("Skip next instruction if key[%d] is pressed\n", x);
PC += (key[V[x]]) ? 4 : 2;
break;
case 0xA1: // skip next instr if key[Vx] is not pressed
p("Skip next instruction if key[%d] is NOT pressed\n", x);
PC += (!key[V[x]]) ? 4 : 2;
break;
default:
unknown_opcode(opcode);
}
break;
case 0xF000: // misc
switch (kk) {
case 0x07:
p("V[0x%x] = delay timer = %d\n", x, delay_timer);
V[x] = delay_timer;
PC += 2;
break;
case 0x0A:
i = 0;
printf("Wait for key instruction\n");
while (true) {
for (i = 0; i < KEY_SIZE; i++) {
if (key[i]) {
V[x] = i;
goto got_key_press;
}
}
}
got_key_press:
PC += 2;
break;
case 0x15:
p("delay timer = V[0x%x] = %d\n", x, V[x]);
delay_timer = V[x];
PC += 2;
break;
case 0x18:
p("sound timer = V[0x%x] = %d\n", x, V[x]);
sound_timer = V[x];
PC += 2;
break;
case 0x1E:
p("I = I + V[0x%x] = 0x%x + 0x%x\n", x, I, V[x]);
V[0xF] = (I + V[x] > 0xfff) ? 1 : 0;
I = I + V[x];
PC += 2;
break;
case 0x29:
p("I = location of font for character V[0x%x] = 0x%x\n", x, V[x]);
I = FONTSET_BYTES_PER_CHAR * V[x];
PC += 2;
break;
case 0x33:
p("Store BCD for %d starting at address 0x%x\n", V[x], I);
memory[I] = (V[x] % 1000) / 100; // hundred's digit
memory[I+1] = (V[x] % 100) / 10; // ten's digit
memory[I+2] = (V[x] % 10); // one's digit
PC += 2;
break;
case 0x55:
p("Copy sprite from registers 0 to 0x%x into memory at address 0x%x\n", x, I);
for (i = 0; i <= x; i++) { memory[I + i] = V[i]; }
I += x + 1;
PC += 2;
break;
case 0x65:
p("Copy sprite from memory at address 0x%x into registers 0 to 0x%x\n", x, I);
for (i = 0; i <= x; i++) { V[i] = memory[I + i]; }
I += x + 1;
PC += 2;
break;
default:
unknown_opcode(opcode);
}
break;
default:
unknown_opcode(opcode);
}
#ifdef DEBUG
print_state();
#endif
}
void chip8_tick() {
// update timers
if (delay_timer > 0) {
--delay_timer;
}
if (sound_timer > 0) {
--sound_timer;
if (sound_timer == 0) {
printf("BEEP!\n");
}
}
}