-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.c
More file actions
638 lines (591 loc) · 19.3 KB
/
Copy pathchip8.c
File metadata and controls
638 lines (591 loc) · 19.3 KB
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
// This interpreter was written primarily using Cowgod's reference
// Several references are made to sections of Cowgod's document throughout this program
// You can find one mirror here: http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#2.1
// Behavior was honed in after implementing Cowgod's instructions by using this test suite:
// https://github.com/Timendus/chip8-test-suite?tab=readme-ov-file
#define INSTRUCTIONS_PER_FRAME 500
#define DISPLAY_WIDTH 64
#define DISPLAY_HEIGHT 32
#define SCALE_FACTOR 16
// Static Sprites (2.4)
#define SPRITE_0_OFFSET 0 // where in memory this sprite goes
#define SPRITE_1_OFFSET 5
#define SPRITE_2_OFFSET 10
#define SPRITE_3_OFFSET 15
#define SPRITE_4_OFFSET 20
#define SPRITE_5_OFFSET 25
#define SPRITE_6_OFFSET 30
#define SPRITE_7_OFFSET 35
#define SPRITE_8_OFFSET 40
#define SPRITE_9_OFFSET 45
#define SPRITE_A_OFFSET 50
#define SPRITE_B_OFFSET 55
#define SPRITE_C_OFFSET 60
#define SPRITE_D_OFFSET 65
#define SPRITE_E_OFFSET 70
#define SPRITE_F_OFFSET 75
const uint8_t SPRITE_0[5] = {
0b11110000,
0b10010000,
0b10010000,
0b10010000,
0b11110000
};
const uint8_t SPRITE_1[5] = {
0b00100000,
0b01100000,
0b00100000,
0b00100000,
0b01110000
};
const uint8_t SPRITE_2[5] = {
0b11110000,
0b00010000,
0b11110000,
0b10000000,
0b11110000
};
const uint8_t SPRITE_3[5] = {
0b11110000,
0b00010000,
0b11110000,
0b00010000,
0b11110000
};
const uint8_t SPRITE_4[5] = {
0b10010000,
0b10010000,
0b11110000,
0b00010000,
0b00010000
};
const uint8_t SPRITE_5[5] = {
0b11110000,
0b10000000,
0b11110000,
0b00010000,
0b11110000
};
const uint8_t SPRITE_6[5] = {
0b11110000,
0b10000000,
0b11110000,
0b10010000,
0b11110000
};
const uint8_t SPRITE_7[5] = {
0b11110000,
0b00010000,
0b00100000,
0b01000000,
0b01000000
};
const uint8_t SPRITE_8[5] = {
0b11110000,
0b10010000,
0b11110000,
0b10010000,
0b11110000
};
const uint8_t SPRITE_9[5] = {
0b11110000,
0b10010000,
0b11110000,
0b00010000,
0b11110000
};
const uint8_t SPRITE_A[5] = {
0b11110000,
0b10010000,
0b11110000,
0b10010000,
0b10010000
};
const uint8_t SPRITE_B[5] = {
0b11100000,
0b10010000,
0b11100000,
0b10010000,
0b11100000
};
const uint8_t SPRITE_C[5] = {
0b11110000,
0b10000000,
0b10000000,
0b10000000,
0b11110000
};
const uint8_t SPRITE_D[5] = {
0b11100000,
0b10010000,
0b10010000,
0b10010000,
0b11100000
};
const uint8_t SPRITE_E[5] = {
0b11110000,
0b10000000,
0b11110000,
0b10000000,
0b11110000
};
const uint8_t SPRITE_F[5] = {
0b11110000,
0b10000000,
0b11110000,
0b10000000,
0b10000000
};
typedef struct
{
uint8_t memory[4096]; // 2.1
uint8_t V[16]; // 2.2
uint16_t I;
uint8_t delay_timer;
uint8_t sound_timer;
uint16_t PC;
uint8_t SP;
uint16_t stack[16];
uint8_t keyboard[16]; // 2.3
uint8_t previous_keyboard[16]; // Fx0A
uint8_t display[DISPLAY_WIDTH][DISPLAY_HEIGHT]; // 2.4 (column major)
} chip8;
chip8 C8 = {0};
void execute_instruction() {
// 3.0
// if (C8.PC % 2 != 0) {
// printf("Failed to execute instruction at 0x%03X; misaligned PC\n", C8.PC);
// exit(1);
// }
uint16_t opcode = (C8.memory[C8.PC] << 8) + (C8.memory[C8.PC + 1]);
uint16_t nnn = opcode & 0x0FFF;
uint8_t nibble = opcode & 0x000F;
uint8_t x = (opcode & 0x0F00) >> 8;
uint8_t y = (opcode & 0x00F0) >> 4;
uint8_t kk = opcode & 0x00FF;
uint8_t tmp;
uint8_t previous_keyboard[16];
// printf("Executing instruction 0x%04X from location 0x%03X; nnn=0x%03X, nibble=%d, x=%d, y=%d, kk=%d\n", opcode, C8.PC, nnn, nibble, x, y, kk);
C8.PC += 2;
switch (opcode & 0xF000) { // switch over first nibble
case 0x0000:
switch (opcode) {
case 0x00E0: // CLS
memset(&C8.display, 0, DISPLAY_WIDTH * DISPLAY_HEIGHT);
break;
case 0x00EE: // RET
// Diverges from Cowgod by decrementing SP first. Makes stack[0] usable
C8.PC = C8.stack[--C8.SP];
break;
default:
printf("Attempted to execute unknown instruction %04X\n", opcode);
exit(1);
break;
}
break;
case 0x1000: // JP addr
C8.PC = nnn;
break;
case 0x2000: // CALL addr
// Diverges from Cowgod by incrementing SP after pushing PC. Makes stack[0] usable
C8.stack[C8.SP++] = C8.PC;
C8.PC = nnn;
break;
case 0x3000: // SE Vx, byte
if (C8.V[x] == kk) C8.PC += 2;
break;
case 0x4000: // SNE Vx, byte
if (C8.V[x] != kk) C8.PC += 2;
break;
case 0x5000: // SE Vx, Vy
if (opcode & 0x000F != 0) {
printf("Attempted to execute unknown instruction %04X\n", opcode);
exit(1);
}
if (C8.V[x] == C8.V[y]) C8.PC += 2;
break;
case 0x6000: // LD Vx, byte
C8.V[x] = kk;
break;
case 0x7000: // ADD Vx, byte
C8.V[x] += kk;
break;
case 0x8000: // Register arithmetic
switch (opcode & 0x000F) {
case 0: // LD Vx, Vy
C8.V[x] = C8.V[y];
break;
case 1: // OR Vx, Vy
C8.V[x] |= C8.V[y];
C8.V[0xF] = 0;
break;
case 2: // AND Vx, Vy
C8.V[x] &= C8.V[y];
C8.V[0xF] = 0;
break;
case 3: // XOR Vx, Vy
C8.V[x] ^= C8.V[y];
C8.V[0xF] = 0;
break;
case 4: // ADD Vx, Vy
if ((uint16_t)C8.V[x] + C8.V[y] > 255) tmp = 1;
else tmp = 0;
C8.V[x] += C8.V[y];
C8.V[0xF] = tmp; // take care to write VF AFTER the operation
break;
case 5: // SUB Vx, Vy
if (C8.V[x] >= C8.V[y]) tmp = 1; // Cowgod's guide incorrectly states this should be > and not >=
else tmp = 0;
C8.V[x] -= C8.V[y];
C8.V[0xF] = tmp;
break;
case 6: // SHR Vx {, Vy}
if (C8.V[y] % 2 == 1) tmp = 1;
else tmp = 0;
C8.V[x] = C8.V[y] >> 1;
C8.V[0xF] = tmp;
break;
case 7: // SUBN Vx, Vy
if (C8.V[y] >= C8.V[x]) tmp = 1;
else tmp = 0;
C8.V[x] = C8.V[y] - C8.V[x];
C8.V[0xF] = tmp;
break;
case 0xE: // SHL Vx {, Vy}
if (C8.V[y] >= 128) tmp = 1;
else tmp = 0;
C8.V[x] = C8.V[y] << 1;
C8.V[0xF] = tmp;
break;
default:
printf("Attempted to execute unknown instruction %04X\n", opcode);
exit(1);
}
break;
case 0x9000: // SNE Vx, Vy
if (C8.V[x] != C8.V[y]) C8.PC += 2;
break;
case 0xA000: // LD I, addr
C8.I = nnn;
break;
case 0xB000: // JP V0, addr
C8.PC = nnn + C8.V[0];
break;
case 0xC000: // RND Vx, byte
C8.V[x] = (rand() % 256) & kk;
break;
case 0xD000: // DRW Vx, Vy, nibble
// Draws sprites left to right, top to bottom
uint8_t draw_buffer[16];
memcpy(draw_buffer, &C8.memory[C8.I], nibble);
uint8_t x_offset = C8.V[x] % DISPLAY_WIDTH;
uint8_t y_offset = C8.V[y] % DISPLAY_HEIGHT;
C8.V[0xF] = 0;
for (int row = 0; row < nibble; row++) {
for (int column = 0; column < 8; column++) { // All sprites are 8 bits wide
// Skip drawing this sprite if it's off screen
if (x_offset + column >= DISPLAY_WIDTH || y_offset + row >= DISPLAY_HEIGHT) continue;
uint8_t sprite_bit = (draw_buffer[row] >> (7 - column)) & 1;
uint8_t* write_location = &C8.display[x_offset + column][y_offset + row];
// If both the sprite bit and existing bit here are on (1), set VF to 1
if (*write_location == 1 && sprite_bit == 1) C8.V[0xF] = 1;
*write_location ^= sprite_bit;
}
}
break;
case 0xE000:
switch (opcode & 0x00FF) {
case 0x9E: // SKP Vx
if (C8.V[x] > 15) {
printf("Attempted to check down state of %X key (does not exist)\n", C8.V[x]);
exit(1);
}
if (C8.keyboard[C8.V[x]] == 1) C8.PC += 2;
break;
case 0xA1: // SKNP Vx
if (C8.V[x] > 15) {
printf("Attempted to check down state of %X key (does not exist)\n", C8.V[x]);
exit(1);
}
if (C8.keyboard[C8.V[x]] == 0) C8.PC += 2;
break;
default:
printf("Attempted to execute unknown instruction %04X\n", opcode);
exit(1);
}
break;
case 0xF000:
switch (opcode & 0x00FF) {
case 0x07: // LD Vx, DT
C8.V[x] = C8.delay_timer;
break;
case 0x0A: // LD Vx, K
uint8_t key_released = 0;
for (int i = 0; i < 16; i++) {
if (C8.keyboard[i] == 0 && C8.previous_keyboard[i] == 1) { // key released between frames
C8.V[x] = i;
key_released = 1;
break;
}
}
// If no key release was detected, re-run this instruction
if (!key_released) C8.PC -= 2;
break;
case 0x15: // LD DT, Vx
C8.delay_timer = C8.V[x];
break;
case 0x18: // LD ST, Vx
C8.sound_timer = C8.V[x];
break;
case 0x1E: // ADD I, Vx
C8.I += C8.V[x];
break;
case 0x29: // LD F, Vx
switch (C8.V[x]) {
case 0:
C8.I = SPRITE_0_OFFSET;
break;
case 1:
C8.I = SPRITE_1_OFFSET;
break;
case 2:
C8.I = SPRITE_2_OFFSET;
break;
case 3:
C8.I = SPRITE_3_OFFSET;
break;
case 4:
C8.I = SPRITE_4_OFFSET;
break;
case 5:
C8.I = SPRITE_5_OFFSET;
break;
case 6:
C8.I = SPRITE_6_OFFSET;
break;
case 7:
C8.I = SPRITE_7_OFFSET;
break;
case 8:
C8.I = SPRITE_8_OFFSET;
break;
case 9:
C8.I = SPRITE_9_OFFSET;
break;
case 0xA:
C8.I = SPRITE_A_OFFSET;
break;
case 0xB:
C8.I = SPRITE_B_OFFSET;
break;
case 0xC:
C8.I = SPRITE_C_OFFSET;
break;
case 0xD:
C8.I = SPRITE_D_OFFSET;
break;
case 0xE:
C8.I = SPRITE_E_OFFSET;
break;
case 0xF:
C8.I = SPRITE_F_OFFSET;
break;
default:
printf("Attempted to load nonexistent sprite for digit %X\n", C8.V[x]);
exit(1);
}
break;
case 0x33: // LD B, Vx
C8.memory[C8.I] = C8.V[x] / 100;
C8.memory[C8.I+1] = (C8.V[x] % 100) / 10;
C8.memory[C8.I+2] = C8.V[x] % 10;
break;
case 0x55: // LD [I], Vx
memcpy(&C8.memory[C8.I], C8.V, x + 1); // x = n -> copy n + 1 regs to mem
C8.I += x + 1;
break;
case 0x65: // LD Vx, [I]
memcpy(C8.V, &C8.memory[C8.I], x + 1); // x = n -> copy mem into n + 1 regs
C8.I += x + 1;
break;
default:
printf("Attempted to execute unknown instruction %04X\n", opcode);
exit(1);
}
break;
default:
printf("Attempted to execute unknown instruction %04X\n", opcode);
exit(1);
}
}
// Loads sprite data for 0-F into the interpreter area of Chip-8 memory
void load_static_sprites() {
memcpy(&C8.memory[SPRITE_0_OFFSET], SPRITE_0, 5);
memcpy(&C8.memory[SPRITE_1_OFFSET], SPRITE_1, 5);
memcpy(&C8.memory[SPRITE_2_OFFSET], SPRITE_2, 5);
memcpy(&C8.memory[SPRITE_3_OFFSET], SPRITE_3, 5);
memcpy(&C8.memory[SPRITE_4_OFFSET], SPRITE_4, 5);
memcpy(&C8.memory[SPRITE_5_OFFSET], SPRITE_5, 5);
memcpy(&C8.memory[SPRITE_6_OFFSET], SPRITE_6, 5);
memcpy(&C8.memory[SPRITE_7_OFFSET], SPRITE_7, 5);
memcpy(&C8.memory[SPRITE_8_OFFSET], SPRITE_8, 5);
memcpy(&C8.memory[SPRITE_9_OFFSET], SPRITE_9, 5);
memcpy(&C8.memory[SPRITE_A_OFFSET], SPRITE_A, 5);
memcpy(&C8.memory[SPRITE_B_OFFSET], SPRITE_B, 5);
memcpy(&C8.memory[SPRITE_C_OFFSET], SPRITE_C, 5);
memcpy(&C8.memory[SPRITE_D_OFFSET], SPRITE_D, 5);
memcpy(&C8.memory[SPRITE_E_OFFSET], SPRITE_E, 5);
memcpy(&C8.memory[SPRITE_F_OFFSET], SPRITE_F, 5);
}
void render(SDL_Renderer* renderer) {
// Black background
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
// White foreground
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for (int col = 0; col < DISPLAY_WIDTH * SCALE_FACTOR; col++) {
for (int row = 0; row < DISPLAY_HEIGHT * SCALE_FACTOR; row++) {
if (C8.display[col / SCALE_FACTOR][row / SCALE_FACTOR]) {
SDL_RenderDrawPoint(renderer, col, row);
}
}
}
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
int map_sdl_to_chip8(SDL_Keycode key) {
// Maps keyboard as shown here: https://code.benco.io/chip8/web/
switch (key) {
case SDLK_1:
return 0x1;
case SDLK_2:
return 0x2;
case SDLK_3:
return 0x3;
case SDLK_4:
return 0xC;
case SDLK_q:
return 0x4;
case SDLK_w:
return 0x5;
case SDLK_e:
return 0x6;
case SDLK_r:
return 0xD;
case SDLK_a:
return 0x7;
case SDLK_s:
return 0x8;
case SDLK_d:
return 0x9;
case SDLK_f:
return 0xE;
case SDLK_z:
return 0xA;
case SDLK_x:
return 0x0;
case SDLK_c:
return 0xB;
case SDLK_v:
return 0xF;
}
return -1;
}
int poll_input() {
// Save previous keyboard state for accurate execution of Fx0A instruction
memcpy(C8.previous_keyboard, C8.keyboard, 16);
SDL_Event event;
while (SDL_PollEvent(&event)) {
int key_index;
switch (event.type) {
case SDL_QUIT:
return 1;
case SDL_KEYDOWN:
key_index = map_sdl_to_chip8(event.key.keysym.sym);
if (key_index == -1) continue; // key down on non-keypad key
C8.keyboard[key_index] = 1;
break;
case SDL_KEYUP:
key_index = map_sdl_to_chip8(event.key.keysym.sym);
if (key_index == -1) continue; // key up on non-keypad key
C8.keyboard[key_index] = 0;
break;
default:
break;
}
}
return 0;
}
int play_beep(Mix_Chunk* sound) {
return Mix_FadeInChannel(-1, sound, -1, 30); // short fade in to reduce popping
}
void stop_beep(int channelID) {
Mix_FadeOutChannel(channelID, 30); // short fade out to reduce popping
}
int main(int argc, char** argv) {
if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
return 1;
}
// Read sprites into memory
load_static_sprites();
// Read program into memory
FILE *fptr;
fptr = fopen(argv[1], "rb");
if (fptr == NULL) {
printf("Unable to open file '%s'\n", argv[1]);
return 1;
}
fread(&C8.memory[0x200], sizeof(uint8_t), sizeof(C8.memory) - 0x200, fptr);
C8.PC = 0x200;
// Init rendering engine
if (SDL_Init(SDL_INIT_VIDEO || SDL_INIT_AUDIO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 1;
}
SDL_Renderer *renderer;
SDL_Window* window;
SDL_CreateWindowAndRenderer(DISPLAY_WIDTH * SCALE_FACTOR, DISPLAY_HEIGHT * SCALE_FACTOR, 0, &window, &renderer);
if (window == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// Init sound engine
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 2048) < 0) {
printf("Audio mixer could not be created! Mixer Error: %s\n", Mix_GetError());
SDL_Quit();
return 1;
}
Mix_Chunk* sound = Mix_LoadWAV("beep.wav");
if (!sound) {
printf("Could not load beep.wav! Mixer Error: %s\n", Mix_GetError());
SDL_Quit();
return 1;
}
uint8_t running = 1;
int audio_channel_id = 0;
while (running) {
// Execute instructions
for (int i = 0; i < INSTRUCTIONS_PER_FRAME; i++) {
execute_instruction();
}
// Play sound
if (C8.sound_timer > 0) {
audio_channel_id = play_beep(sound);
} else {
stop_beep(audio_channel_id);
}
// Update timers (2.5)
if (C8.delay_timer > 0) C8.delay_timer--;
if (C8.sound_timer > 0) C8.sound_timer--;
if (poll_input()) running = 0; // quit event occurred
// Draw frame
render(renderer);
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}