-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.cpp
More file actions
663 lines (611 loc) · 20.9 KB
/
chip8.cpp
File metadata and controls
663 lines (611 loc) · 20.9 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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <random>
#include <string_view>
#include <SDL3/SDL.h>
constexpr int SCREEN_WIDTH = 64;
constexpr int SCREEN_HEIGHT = 32;
constexpr int PROGRAM_ADDRESS = 0x200;
constexpr int FONTSET_ADDRESS = 0x0;
constexpr int PROGRAM_ROM_SIZE = 0xE00;
constexpr int FONT_SIZE = 0x5;
constexpr double FRAME_TIME = 1000.0 / 60.0;
using s8 = std::int8_t;
using u8 = std::uint8_t;
using s16 = std::int16_t;
using u16 = std::uint16_t;
using s32 = std::int32_t;
using u32 = std::uint32_t;
using s64 = std::int64_t;
using u64 = std::uint64_t;
using f32 = float;
using f64 = double;
struct RegisterSet
{
std::array<u8, 16> v;
u16 pc = 0;
u16 sp = 0;
u16 i = 0;
};
struct Machine
{
std::array<u8, 4096> memory;
std::array<u8, 2048> screen;
std::array<u16, 16> stack;
std::array<u8, 16> keypad;
RegisterSet registers;
u8 delay_timer = 0;
u8 sound_timer = 0;
bool should_draw = false;
};
struct IODevice
{
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
const bool* keyboard = nullptr;
bool should_close = false;
};
static std::random_device s_random_device;
static std::mt19937 s_random_generator(s_random_device());
static std::uniform_int_distribution<u8> s_random_distributor(0, 255);
static u8 s_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
};
static u8 GetRandomByte();
static bool MachineLoadProgram(Machine& machine, std::string_view filepath);
static u16 MachineFetchOpcode(Machine& machine);
static void MachineExecuteOpcode(Machine& machine, u16 opcode);
static void MachineUpdateTimers(Machine& machine);
static void MachineEmulateCycle(Machine& machine);
// Always call DestroyIODevice after InitIODevice, even in
// case of failure, so that allocated resources are released.
static bool InitIODevice(IODevice& device);
static void DestroyIODevice(IODevice& device);
static void IODevicePollEvents(IODevice& device);
static void IODeviceUpdateKeypad(IODevice& device, std::array<u8, 16>& keypad);
static void IODeviceDrawScreen(IODevice& device, const std::array<u8, 2048>& screen);
int main(int argc, char** argv)
{
if (argc != 2)
{
std::printf("Usage: chip8 <program-file>\n");
return 1;
}
Machine machine;
if (!MachineLoadProgram(machine, argv[1]))
{
std::printf("chip8: failed to load program %s\n", argv[1]);
return 1;
}
IODevice io_device;
if (!InitIODevice(io_device))
{
std::printf("chip8: failed to init IO device\n");
return 1;
}
f64 time_accumulator = 0.0;
u64 last_ticks = SDL_GetTicks();
while (!io_device.should_close)
{
u64 current_ticks = SDL_GetTicks();
time_accumulator += (current_ticks - last_ticks);
last_ticks = current_ticks;
while (time_accumulator >= FRAME_TIME)
{
IODevicePollEvents(io_device);
IODeviceUpdateKeypad(io_device, machine.keypad);
for (int i = 0; i < 10; ++i)
MachineEmulateCycle(machine);
MachineUpdateTimers(machine);
if (machine.should_draw)
{
IODeviceDrawScreen(io_device, machine.screen);
machine.should_draw = false;
}
time_accumulator -= FRAME_TIME;
}
}
DestroyIODevice(io_device);
}
static u8 GetRandomByte()
{
u8 result = s_random_distributor(s_random_generator);
return result;
}
static bool MachineLoadProgram(Machine& machine, std::string_view filepath)
{
std::ifstream stream(filepath.data(), std::ios::binary);
if (!stream.is_open())
return false;
stream.seekg(0, std::ios::end);
std::streampos size = stream.tellg();
stream.seekg(0, std::ios::beg);
if (size > PROGRAM_ROM_SIZE)
return false;
std::array<char, PROGRAM_ROM_SIZE> buffer;
stream.read(buffer.data(), size);
std::memset(&machine, 0, sizeof(Machine));
std::memcpy(machine.memory.data() + FONTSET_ADDRESS, s_fontset, sizeof(s_fontset));
std::memcpy(machine.memory.data() + PROGRAM_ADDRESS, buffer.data(), size);
machine.registers.pc = PROGRAM_ADDRESS;
return true;
}
static u16 MachineFetchOpcode(Machine& machine)
{
u16 opcode = ((machine.memory[machine.registers.pc] << 8) |
machine.memory[machine.registers.pc + 1]);
machine.registers.pc += 2;
return opcode;
}
static void MachineExecuteOpcode(Machine& machine, u16 opcode)
{
switch (opcode & 0xF000)
{
case 0x0000:
{
switch (opcode & 0x000F)
{
// 00E0 - CLS
// Clear the display.
case 0x0000:
{
std::memset(machine.screen.data(), 0, machine.screen.size());
break;
}
// 00EE - RET
// Return from a subroutine.
case 0x000E:
{
machine.registers.pc = machine.stack[--machine.registers.sp];
break;
}
}
break;
}
// 1nnn - JP addr
// Jump to location nnn.
case 0x1000:
{
u16 address = opcode & 0x0FFF;
machine.registers.pc = address;
break;
}
// 2nnn - CALL addr
// Call subroutine at nnn.
case 0x2000:
{
u16 address = opcode & 0x0FFF;
machine.stack[machine.registers.sp++] = machine.registers.pc;
machine.registers.pc = address;
break;
}
// 3xkk - SE Vx, byte
// Skip next instruction if Vx = kk.
case 0x3000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 byte = (opcode & 0x00FF);
if (machine.registers.v[x] == byte)
machine.registers.pc += 2;
break;
}
// 4xkk - SNE Vx, byte
// Skip next instruction if Vx != kk.
case 0x4000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 byte = (opcode & 0x00FF);
if (machine.registers.v[x] != byte)
machine.registers.pc += 2;
break;
}
// 5xy0 - SE Vx, Vy
// Skip next instruction if Vx = Vy.
case 0x5000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
if (machine.registers.v[x] == machine.registers.v[y])
machine.registers.pc += 2;
break;
}
// 6xkk - LD Vx, byte
// Set Vx = kk.
case 0x6000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 byte = opcode & 0x00FF;
machine.registers.v[x] = byte;
break;
}
// 7xkk - ADD Vx, byte
// Set Vx = Vx + kk.
case 0x7000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 byte = opcode & 0x00FF;
machine.registers.v[x] += byte;
break;
}
case 0x8000:
{
switch (opcode & 0x000F)
{
// 8xy0 - LD Vx, Vy
// Set Vx = Vy.
case 0x0000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
machine.registers.v[x] = machine.registers.v[y];
break;
}
// 8xy1 - OR Vx, Vy
// Set Vx = Vx OR Vy.
case 0x0001:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
machine.registers.v[x] |= machine.registers.v[y];
break;
}
// 8xy2 - AND Vx, Vy
// Set Vx = Vx AND Vy.
case 0x0002:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
machine.registers.v[x] &= machine.registers.v[y];
break;
}
// 8xy3 - XOR Vx, Vy
// Set Vx = Vx XOR Vy.
case 0x0003:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
machine.registers.v[x] ^= machine.registers.v[y];
break;
}
// 8xy4 - ADD Vx, Vy
// Set Vx = Vx + Vy, set VF = carry.
case 0x0004:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
u16 result = machine.registers.v[x] + machine.registers.v[y];
machine.registers.v[x] = result & 0x00FF;
machine.registers.v[0xF] = (result > 0xFF);
break;
}
// 8xy5 - SUB Vx, Vy
// Set Vx = Vx - Vy, set VF = NOT borrow.
case 0x0005:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
machine.registers.v[0xF] = (machine.registers.v[x] >= machine.registers.v[y]);
machine.registers.v[x] -= machine.registers.v[y];
break;
}
// 8xy6 - SHR Vx {, Vy}
// Set Vx = Vx SHR 1.
case 0x0006:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.registers.v[0xF] = machine.registers.v[x] & 0x01;
machine.registers.v[x] >>= 1;
break;
}
// 8xy7 - SUBN Vx, Vy
// Set Vx = Vy - Vx, set VF = NOT borrow.
case 0x0007:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
machine.registers.v[0xF] = (machine.registers.v[y] >= machine.registers.v[x]);
machine.registers.v[x] = machine.registers.v[y] - machine.registers.v[x];
break;
}
// 8xyE - SHL Vx {, Vy}
// Set Vx = Vx SHL 1.
case 0x000E:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.registers.v[0xF] = (machine.registers.v[x] & 0x80) >> 7;
machine.registers.v[x] <<= 1;
break;
}
}
break;
}
// 9xy0 - SNE Vx, Vy
// Skip next instruction if Vx != Vy.
case 0x9000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
if (machine.registers.v[x] != machine.registers.v[y])
machine.registers.pc += 2;
break;
}
// Annn - LD I, addr
// Set I = nnn.
case 0xA000:
{
u16 address = opcode & 0x0FFF;
machine.registers.i = address;
break;
}
// Bnnn - JP V0, addr
// Jump to location nnn + V0.
case 0xB000:
{
u16 address = opcode & 0x0FFF;
machine.registers.pc = address + machine.registers.v[0x0];
break;
}
// Cxkk - RND Vx, byte
// Set Vx = random byte AND kk.
case 0xC000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 byte = opcode & 0x00FF;
machine.registers.v[x] = GetRandomByte() & byte;
break;
}
// Dxyn - DRW Vx, Vy, nibble
// Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.
case 0xD000:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 y = (opcode & 0x00F0) >> 4;
u8 height = opcode & 0x000F;
u8 start_x = machine.registers.v[x] % SCREEN_WIDTH;
u8 start_y = machine.registers.v[y] % SCREEN_HEIGHT;
machine.registers.v[0xF] = 0;
machine.should_draw = true;
for (int row = 0; row < height; ++row)
{
u8 sprite = machine.memory[machine.registers.i + row];
for (int col = 0; col < 8; ++col)
{
u8 pos_x = start_x + col;
u8 pos_y = start_y + row;
if (pos_x >= SCREEN_WIDTH || pos_y >= SCREEN_HEIGHT)
continue;
u8 pixel = sprite & (0x80 >> col);
if (pixel != 0)
{
u16 screen_index = pos_x + pos_y * SCREEN_WIDTH;
if (machine.screen[screen_index] == 1)
machine.registers.v[0xF] = 1;
machine.screen[screen_index] ^= 1;
}
}
}
break;
}
case 0xE000:
{
switch (opcode & 0x00FF)
{
// Ex9E - SKP Vx
// Skip next instruction if key with the value of Vx is pressed.
case 0x009E:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 key = machine.registers.v[x];
if (machine.keypad[key])
machine.registers.pc += 2;
break;
}
// ExA1 - SKNP Vx
// Skip next instruction if key with the value of Vx is not pressed.
case 0x00A1:
{
u8 x = (opcode & 0x0F00) >> 8;
u8 key = machine.registers.v[x];
if (!machine.keypad[key])
machine.registers.pc += 2;
break;
}
}
break;
}
case 0xF000:
{
switch (opcode & 0x00FF)
{
// Fx07 - LD Vx, DT
// Set Vx = delay timer value.
case 0x0007:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.registers.v[x] = machine.delay_timer;
break;
}
// Fx0A - LD Vx, K
// Wait for a key press, store the value of the key in Vx.
case 0x000A:
{
bool key_pressed = false;
for (int key = 0; key < 16; ++key)
{
if (machine.keypad[key] != 0)
{
u8 x = (opcode & 0x0F00) >> 8;
machine.registers.v[x] = key;
key_pressed = true;
}
}
if (!key_pressed)
{
machine.registers.pc -= 2;
return;
}
break;
}
// Fx15 - LD DT, Vx
// Set delay timer = Vx.
case 0x0015:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.delay_timer = machine.registers.v[x];
break;
}
// Fx18 - LD ST, Vx
// Set sound timer = Vx.
case 0x0018:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.sound_timer = machine.registers.v[x];
break;
}
// Fx1E - ADD I, Vx
// Set I = I + Vx.
case 0x001E:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.registers.i += machine.registers.v[x];
break;
}
// Fx29 - LD F, Vx
// Set I = location of sprite for digit Vx.
case 0x0029:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.registers.i = FONTSET_ADDRESS + machine.registers.v[x] * FONT_SIZE;
break;
}
// Fx33 - LD B, Vx
// Store BCD representation of Vx in memory locations I, I+1, and I+2.
case 0x0033:
{
u8 x = (opcode & 0x0F00) >> 8;
machine.memory[machine.registers.i] = machine.registers.v[x] / 100;
machine.memory[machine.registers.i + 1] = (machine.registers.v[x] / 10) % 10;
machine.memory[machine.registers.i + 2] = machine.registers.v[x] % 10;
break;
}
// Fx55 - LD [I], Vx
// Store registers V0 through Vx in memory starting at location I.
case 0x0055:
{
u8 x = (opcode & 0x0F00) >> 8;
for (int i = 0; i <= x; ++i)
machine.memory[machine.registers.i + i] = machine.registers.v[i];
break;
}
// Fx65 - LD Vx, [I]
// Read registers V0 through Vx from memory starting at location I.
case 0x0065:
{
u8 x = (opcode & 0x0F00) >> 8;
for (int i = 0; i <= x; ++i)
machine.registers.v[i] = machine.memory[machine.registers.i + i];
break;
}
}
break;
}
}
}
static void MachineUpdateTimers(Machine& machine)
{
if (machine.delay_timer > 0)
--machine.delay_timer;
if (machine.sound_timer > 0)
--machine.sound_timer;
}
static void MachineEmulateCycle(Machine& machine)
{
u16 opcode = MachineFetchOpcode(machine);
MachineExecuteOpcode(machine, opcode);
}
static bool InitIODevice(IODevice& device)
{
if (!SDL_Init(SDL_INIT_VIDEO))
return false;
device.window = SDL_CreateWindow("CHIP-8", 1024, 512, SDL_WINDOW_RESIZABLE);
if (!device.window)
return false;
device.renderer = SDL_CreateRenderer(device.window, nullptr);
if (!device.renderer)
return false;
SDL_SetRenderLogicalPresentation(device.renderer, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_LOGICAL_PRESENTATION_STRETCH);
device.keyboard = SDL_GetKeyboardState(nullptr);
device.should_close = false;
return true;
}
static void DestroyIODevice(IODevice& device)
{
if (device.renderer)
SDL_DestroyRenderer(device.renderer);
if (device.window)
SDL_DestroyWindow(device.window);
SDL_Quit();
device.should_close = true;
}
static void IODevicePollEvents(IODevice& device)
{
SDL_Event event{};
while (SDL_PollEvent(&event))
{
if (event.type == SDL_EVENT_QUIT)
device.should_close = true;
}
}
static void IODeviceUpdateKeypad(IODevice& device, std::array<u8, 16>& keypad)
{
keypad[0x0] = device.keyboard[SDL_SCANCODE_X];
keypad[0x1] = device.keyboard[SDL_SCANCODE_1];
keypad[0x2] = device.keyboard[SDL_SCANCODE_2];
keypad[0x3] = device.keyboard[SDL_SCANCODE_3];
keypad[0x4] = device.keyboard[SDL_SCANCODE_Q];
keypad[0x5] = device.keyboard[SDL_SCANCODE_W];
keypad[0x6] = device.keyboard[SDL_SCANCODE_E];
keypad[0x7] = device.keyboard[SDL_SCANCODE_A];
keypad[0x8] = device.keyboard[SDL_SCANCODE_S];
keypad[0x9] = device.keyboard[SDL_SCANCODE_D];
keypad[0xA] = device.keyboard[SDL_SCANCODE_Z];
keypad[0xB] = device.keyboard[SDL_SCANCODE_C];
keypad[0xC] = device.keyboard[SDL_SCANCODE_4];
keypad[0xD] = device.keyboard[SDL_SCANCODE_R];
keypad[0xE] = device.keyboard[SDL_SCANCODE_F];
keypad[0xF] = device.keyboard[SDL_SCANCODE_V];
}
static void IODeviceDrawScreen(IODevice& device, const std::array<u8, 2048>& screen)
{
SDL_SetRenderDrawColor(device.renderer, 0, 0, 0, 255);
SDL_RenderClear(device.renderer);
SDL_SetRenderDrawColor(device.renderer, 255, 255, 255, 255);
for (int y = 0; y < SCREEN_HEIGHT; ++y)
{
for (int x = 0; x < SCREEN_WIDTH; ++x)
{
u8 pixel = screen[x + y * SCREEN_WIDTH];
if (pixel != 0)
SDL_RenderPoint(device.renderer, x, y);
}
}
SDL_RenderPresent(device.renderer);
}