-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.cpp
More file actions
621 lines (494 loc) · 15.9 KB
/
chip8.cpp
File metadata and controls
621 lines (494 loc) · 15.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
//
// Created by rusty on 9/11/25.
//
#include "chip8.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <stdexcept>
/*
* Sets pc to 0x200 because thats the first instruction that will execute
*/
Chip8::Chip8() : pc(START_ADDRESS), gen(rd()), dis(0, 255) {
loadFontSet();
// setting up function pointer table
table[0x0] = &Chip8::Table0; // ptr to helper function
table[0x1] = &Chip8::OP_1nnn;
table[0x2] = &Chip8::OP_2nnn;
table[0x3] = &Chip8::OP_3xkk;
table[0x4] = &Chip8::OP_4xkk;
table[0x5] = &Chip8::OP_5xy0;
table[0x6] = &Chip8::OP_6xkk;
table[0x7] = &Chip8::OP_7xkk;
table[0x8] = &Chip8::Table8;
table[0x9] = &Chip8::OP_9xy0;
table[0xA] = &Chip8::OP_Annn;
table[0xB] = &Chip8::OP_Bnnn;
table[0xC] = &Chip8::OP_Cxkk;
table[0xD] = &Chip8::OP_Dxyn;
table[0xE] = &Chip8::TableE;
table[0xF] = &Chip8::TableF;
// initializing secondary tables with null functions
for (size_t i = 0; i <= 0xE; i++) {
table0[i] = &Chip8::OP_NULL;
table8[i] = &Chip8::OP_NULL;
tableE[i] = &Chip8::OP_NULL;
}
table0[0x0] = &Chip8::OP_00E0;
table0[0xE] = &Chip8::OP_00EE;
table8[0x0] = &Chip8::OP_8xy0;
table8[0x1] = &Chip8::OP_8xy1;
table8[0x2] = &Chip8::OP_8xy2;
table8[0x3] = &Chip8::OP_8xy3;
table8[0x4] = &Chip8::OP_8xy4;
table8[0x5] = &Chip8::OP_8xy5;
table8[0x6] = &Chip8::OP_8xy6;
table8[0x7] = &Chip8::OP_8xy7;
table8[0xE] = &Chip8::OP_8xyE;
tableE[0x1] = &Chip8::OP_ExA1;
tableE[0xE] = &Chip8::OP_Ex9E;
for (size_t i = 0; i <= 0x65; i++) {
tableF[i] = &Chip8::OP_NULL;
}
tableF[0x07] = &Chip8::OP_Fx07;
tableF[0x0A] = &Chip8::OP_Fx0A;
tableF[0x15] = &Chip8::OP_Fx15;
tableF[0x18] = &Chip8::OP_Fx18;
tableF[0x1E] = &Chip8::OP_Fx1E;
tableF[0x29] = &Chip8::OP_Fx29;
tableF[0x33] = &Chip8::OP_Fx33;
tableF[0x55] = &Chip8::OP_Fx55;
tableF[0x65] = &Chip8::OP_Fx65;
}
// each byte represents a row of pixels in the character
void Chip8::loadFontSet() {
std::array<uint8_t, 80> fontSet = {
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
};
// Load font set into memory starting at 0x50
for (size_t i = 0; i < fontSet.size(); ++i) {
memory[FONTSET_START_ADDRESS + i] = fontSet[i];
}
}
void Chip8::loadROM(const std::string &filename) {
// Opens file in binary mode with cursor positioned at end (ate) to get file
// size
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
throw std::runtime_error("Failed to open ROM file: " + filename);
}
// get size of file and allocate a buffer to hold its content
std::streampos size = file.tellg();
char *buffer = new char[size];
file.seekg(0, std::ios::beg);
// Validate ROM size fits in available memory (4096 total - 512 reserved =
// 3584 bytes max) lower memory is reserved for the interpreter itself!
if (size > (4096 - 0x200)) {
throw std::runtime_error("ROM too large to fit in memory!");
}
// fill the buffer
file.read(buffer, size);
file.close();
std::cout << "Loaded ROM: " << filename << " (" << size << " bytes)\n";
// Load ROM data into memory starting at 0x200 (standard CHIP-8 program
// location)
for (long i = 0; i < size; ++i) {
memory[START_ADDRESS + i] = buffer[i];
}
// set it freeeee
delete[] buffer;
}
void Chip8::Cycle() {
// fetching
opcode = (memory[pc] << 8u) | memory[pc + 1];
// incrementing the pc before executing the program
pc += 2;
// decode and execute
((*this).*(table[(opcode & 0xF000u) >> 12u]))();
updateTimers();
}
void Chip8::updateTimers() {
if (delay_timer > 0) {
--delay_timer;
}
if (sound_timer > 0) {
--sound_timer;
// #ifdef _WIN32
// Beep(800, 50); // 800 freq and 50ms dur
// #elif __linux__
// system(
// "speaker-test -t sine -f 1000 -l 1 & sleep 0.1 && kill $!"); //
// terminal
// //
// bell
// #elif __APPLE__
// system("afplay /System/Library/Sounds/Ping.aiff &");
// #endif // _WIN32
}
}
// Bit position: 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
// Nibble name: N1 | N2 | N3 | N4
// Common names: Opcode | X | Y | N
//
// So for example:
// For extracting the X nibble (register Vx):
// uint8_t Vx = (opcode & 0x0F00u) >> 8u;
//
// Step by step with example opcode 0x5230:
// Original: 0101 0010 0011 0000 (0x5230)
// Mask 0x0F00: 0000 1111 0000 0000
// Result: 0000 0010 0000 0000 (0x0200)
// Shift >> 8: 0000 0000 0000 0010 (0x02)
// The masks target specific nibble positions:
//
// | Target | Bit Positions | Mask | Binary |
// |-------------|---------------|--------|---------- -----------|
// | Opcode (N1) | 12-15 | 0xF000 | 1111 0000 0000 0000 |
// | X (N2) | 8-11 | 0x0F00 | 0000 1111 0000 0000 |
// | Y (N3) | 4-7 | 0x00F0 | 0000 0000 1111 0000 |
// | N (N4) | 0-3 | 0x000F | 0000 0000 0000 1111 |
//
// Pattern: Each F represents 4 bits (one nibble) we
// want to keep, 0 means ignore those bits.
//
void Chip8::OP_00E0() { display.fill(0); }
void Chip8::OP_00EE() {
// top of the stack has the addr of one instruction past the one that called
// the subroutine so we can put that back into the pc
--sp;
pc = stack[sp];
}
void Chip8::OP_1nnn() {
// a jump doesnt remember its origin so no stack interaction is required
uint16_t addr = opcode & 0x0FFFu;
pc = addr;
}
void Chip8::OP_2nnn() {
// when we call a subroutine, we want to return eventually so we put the
// current pc onto the top of the stack. we did pc += 2 in Cycle() so the
// current pc holds the next instruction after this call.
uint16_t addr = opcode & 0x0FFFu;
stack[sp] = pc;
++sp;
pc = addr;
}
void Chip8::OP_3xkk() {
// since pc has already been incremented by 2 in Cycle(), we just increment by
// 2 again to skip the next instruction
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t byte = opcode & 0x00FFu;
if (registers[Vx] == byte) {
pc += 2;
}
}
void Chip8::OP_4xkk() {
// since pc has already been incremented by 2 in Cycle(), we just increment by
// 2 again to skip the next instruction
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t byte = opcode & 0x00FFu;
if (registers[Vx] != byte) {
pc += 2;
}
}
void Chip8::OP_5xy0() {
// since pc has already been incremented by 2 in Cycle(), we just increment by
// 2 again to skip the next instruction
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
if (registers[Vx] == registers[Vy]) {
pc += 2;
}
}
void Chip8::OP_6xkk() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t byte = opcode & 0x00FFu;
registers[Vx] = byte;
}
void Chip8::OP_7xkk() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t byte = opcode & 0x00FFu;
registers[Vx] += byte;
}
void Chip8::OP_8xy0() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
registers[Vx] = registers[Vy];
}
void Chip8::OP_8xy1() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
registers[Vx] |= registers[Vy];
}
void Chip8::OP_8xy2() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
registers[Vx] &= registers[Vy];
}
void Chip8::OP_8xy3() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
registers[Vx] ^= registers[Vy];
}
void Chip8::OP_8xy4() {
// this is an ADD with an overflow flag. if the sum is greater than what can
// fit into a byte(255), register Vf will be set to 1 as a flag!
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
uint16_t sum = registers[Vx] + registers[Vy];
if (sum > 255u) {
registers[0xF] = 1;
} else {
registers[0xF] = 0;
}
registers[Vx] = sum & 0xFFu;
}
void Chip8::OP_8xy5() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
if (registers[Vx] > registers[Vy]) {
registers[0xF] = 1;
} else {
registers[0xF] = 0;
}
registers[Vx] -= registers[Vy];
}
void Chip8::OP_8xy6() {
// if the least significant bit of Vx is 1, then Vf is set to 1, otherwise 0.
// then Vx is divided by 2!
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
// & 0x1u masks all bits except the LSB (bit 0)
// Stores this bit (0 or 1) in the VF flag register
registers[0xF] = (registers[Vx] & 0x1u);
// Right shift by 1 bit effectively divides by 2
registers[Vx] >>= 1;
}
void Chip8::OP_8xy7() {
// if Vx > Vy then Vf is set to 1 otherwise 0. then Vx is subtracted from Vy
// and results stored in Vx
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
if (registers[Vx] > registers[Vy]) {
registers[0xF] = 1;
} else {
registers[0xF] = 0;
}
registers[Vx] = registers[Vy] - registers[Vx];
}
void Chip8::OP_8xyE() {
// if the most significant bit of Vx is 1, then Vf is set to 1; then Vx is
// multiplied by 2
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
registers[0xF] = (registers[Vx] & 0x80u) >> 7u;
// left shift = x2
registers[Vx] <<= 1;
}
void Chip8::OP_9xy0() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
if (registers[Vx] != registers[Vy]) {
pc += 2;
}
}
void Chip8::OP_Annn() {
// this mask extracts the lower 12 bits (nnn)
// How the mask works:
// Opcode format: Annn where A=instruction type,
// nnn=12-bit address
// Example: opcode 0xA22A
// 0xA22A & 0x0FFF = 0x022A (extracts address 0x22A)
uint16_t addr = opcode & 0x0FFFu;
// the index register needs the full 12-bit address to
// properly access CHIP-8's 4KB memory space.
I = addr;
}
void Chip8::OP_Bnnn() {
uint16_t addr = opcode & 0x0FFFu;
pc = registers[0] + addr;
}
void Chip8::OP_Cxkk() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t kk = opcode & 0x00FFu;
// generates random number. uses existing
// dis(gen) random number generator
// ANDs with kk. performs bitwise AND with the
// extracted byte value
registers[Vx] = dis(gen) & kk;
}
void Chip8::OP_Dxyn() {
// CHIP-8 Draw instruction: Dxyn
// Draws an 8-pixel wide sprite of n rows tall at coordinates (Vx, Vy)
// Sprite data starts at memory location stored in I register
// Each row of the sprite is 1 byte (8 bits = 8 pixels)
// Uses XOR drawing - pixels toggle on/off when drawn over existing pixels
// Extract instruction parameters from opcode nibbles
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t Vy = (opcode & 0x00F0u) >> 4u;
// Sprite height in rows (nibble 4)
uint8_t height = opcode & 0x000Fu;
// Get starting coordinates from registers, wrapping around screen edges
// CHIP-8 screen is 64x32 pixels - coordinates beyond this wrap around
uint8_t xPos = registers[Vx] % VIDEO_WIDTH;
uint8_t yPos = registers[Vy] % VIDEO_HEIGHT;
// Clear collision flag - will be set to 1 if any pixels are erased
registers[0xF] = 0;
for (unsigned int row = 0; row < height; ++row) {
if (yPos + row >= VIDEO_HEIGHT) {
break;
}
if (I + row >= 4096) {
std::cerr << "Error: Sprite data access out of bounds at I=" << std::hex
<< I << " + " << row << std::endl;
break;
}
// get the sprite data for this row from memory
// each byte represents 8 horizontal pixels (1 bit per pixel)
uint8_t spriteByte = memory[I + row];
for (unsigned int col = 0; col < 8; ++col) {
if (xPos + col >= VIDEO_WIDTH) {
break;
}
// extract individual sprite pixel by shifting a mask bit
// 0x80u = 10000000b, shifted right by col gives us each bit position
// result: 1 if pixel should be drawn, 0 if transparent
uint8_t spritePixel = spriteByte & (0x80u >> col);
// calculate screen buffer position for this pixel
// screen is stored as 1D array: row * width + column
uint32_t *screenPixel =
&display[(yPos + row) * VIDEO_WIDTH + (xPos + col)];
// only draw if sprite pixel is "on" (non-zero)
if (spritePixel) {
// check for collision: if screen pixel is already on (white =
// 0xFFFFFFFF) set VF register to 1 to indicate collision occurred
if (*screenPixel == 0xFFFFFFFF) {
registers[0xF] = 1;
}
// XOR the screen pixel to toggle it on/off
// 0x00000000 (black) XOR 0xFFFFFFFF = 0xFFFFFFFF (white)
// 0xFFFFFFFF (white) XOR 0xFFFFFFFF = 0x00000000 (black)
// This creates the "erasing" effect when sprites overlap
*screenPixel ^= 0xFFFFFFFF;
}
}
}
}
void Chip8::OP_Ex9E() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t key = registers[Vx];
if (keypad[key]) {
pc += 2;
}
}
void Chip8::OP_ExA1() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t key = registers[Vx];
if (!keypad[key]) {
pc += 2;
}
}
void Chip8::OP_Fx07() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
registers[Vx] = delay_timer;
}
void Chip8::OP_Fx0A() {
// easiest way to wait is to decrement the pc by 2 whenever a keypad value is
// not detected. this will be like running the same instruction repeatedly
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
if (keypad[0]) {
registers[Vx] = 0;
} else if (keypad[1]) {
registers[Vx] = 1;
} else if (keypad[2]) {
registers[Vx] = 2;
} else if (keypad[3]) {
registers[Vx] = 3;
} else if (keypad[4]) {
registers[Vx] = 4;
} else if (keypad[5]) {
registers[Vx] = 5;
} else if (keypad[6]) {
registers[Vx] = 6;
} else if (keypad[7]) {
registers[Vx] = 7;
} else if (keypad[8]) {
registers[Vx] = 8;
} else if (keypad[9]) {
registers[Vx] = 9;
} else if (keypad[10]) {
registers[Vx] = 10;
} else if (keypad[11]) {
registers[Vx] = 11;
} else if (keypad[12]) {
registers[Vx] = 12;
} else if (keypad[13]) {
registers[Vx] = 13;
} else if (keypad[14]) {
registers[Vx] = 14;
} else if (keypad[15]) {
registers[Vx] = 15;
} else {
pc -= 2;
}
}
void Chip8::OP_Fx15() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
delay_timer = registers[Vx];
}
void Chip8::OP_Fx18() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
sound_timer = registers[Vx];
}
void Chip8::OP_Fx1E() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
I += registers[Vx];
}
void Chip8::OP_Fx29() {
// the fond chars are located at 0x50 and they are five bytes each. so we
// could get the addr of the first byte of any char by taking an offset from
// the start addr
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t digit = registers[Vx];
I = FONTSET_START_ADDRESS + (5 * digit);
}
void Chip8::OP_Fx33() {
// the interpreter takes the decimal value of Vx and places the hundreds digit
// in memory at location in I!. the tens digit in location I+1 and the one
// digit at location I+2.
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
uint8_t value = registers[Vx];
memory[I + 2] = value % 10;
value /= 10;
memory[I + 1] = value % 10;
value /= 10;
memory[I] = value % 10;
}
void Chip8::OP_Fx55() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
for (uint8_t i = 0; i <= Vx; ++i) {
memory[I + i] = registers[i];
}
}
void Chip8::OP_Fx65() {
uint8_t Vx = (opcode & 0x0F00u) >> 8u;
for (uint8_t i = 0; i <= Vx; ++i) {
registers[i] = memory[I + i];
}
}