-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
667 lines (586 loc) · 15.6 KB
/
main.cpp
File metadata and controls
667 lines (586 loc) · 15.6 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
664
665
666
667
#include <iostream>
#include <cstdint>
#include <array>
#include <string>
#include <stack>
#include <ctime>
#include <chrono>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <fstream>
#include <vector>
using namespace std;
using namespace sf::Keyboard;
class Chip8
{
public:
Chip8()
{
memory.fill(0);
V.fill(0);
display.fill(0);
keypad.fill(0);
I = 0;
DTR = 0;
STR = 0;
SP = 0;
PC = 0x200; // Program start point
// Load the 4x5 hex font into memory (0x000 to 0x050)
uint8_t 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
};
for (int i = 0; i < 80; i++)
memory[i] = fontSet[i];
}
void memoryDataDebug()
{
for (int i = 0; i < sizeof(memory); i++)
{
if (memory[i] == 0) // ==0 is the same as ==null
{
continue;
}
string str;
str = to_string(i);
cout << str << endl;
}
}
void run()
{
std::chrono::time_point<std::chrono::system_clock> last_frame_time = std::chrono::system_clock::now();
sf::RenderWindow window(sf::VideoMode({640, 320}), "SFML window");
bool program_running = true;
while (program_running)
{
auto current_time = std::chrono::system_clock::now();
auto elapsed = current_time - last_frame_time;
// 1. Run the CPU at its own speed (e.g., 500Hz)
// You might run 8-10 instructions per "frame"
execute_instruction();
// 2. Only tick timers if 1/60th of a second (~16.67ms) has passed
if (elapsed.count() >= 16.67)
{
if (DTR > 0)
DTR--;
if (STR > 0)
{
STR--;
}
else
{
}
last_frame_time = current_time;
}
debugRender(window);
}
}
void loadRom(std::array<uint8_t, 4096> rom)
{
// in a interpreter, bytes: 0-512 are reserved for interpeter ram. Obviously im not using that but we will start at 0x200(512) to emulate that.
const int start = 0x200;
for (int i = 0; i < 3584; i++)
{
memory[0x200 + i] = rom[i];
}
}
std::array<uint8_t, 4096> loadFromFile(const std::string &filename, Chip8 &system, std::array<uint8_t, 4096> rom)
{
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (file.is_open())
{
std::streampos size = file.tellg();
std::vector<uint8_t> buffer(size);
file.seekg(0, std::ios::beg);
file.read((char *)buffer.data(), size);
rom.fill(0);
for (int i = 0; i < size && i < 3584; i++)
{
rom[i] = buffer[i];
}
}
return rom;
}
private:
std::array<uint8_t, 4096> memory; // the 4096 bytes of ram.
uint16_t I; // special purpose register
std::array<uint8_t, 16> V; // V0–VF general purpose registers.
uint8_t DTR; // delay register
uint8_t STR; // sound register
uint16_t PC; // program counter
uint8_t SP; // Stack pointer (points to top of stack) SO Stack.top()
stack<uint16_t> Stack; // WARNING, cpp has no size limit on stacks. Have to make a method for protection.
std::array<uint8_t, 16> keypad; // keypad
std::array<uint32_t, 64 * 32> display; // standard size of chip-8 displays is 64 by 32 (2048)
Key *keys_array = new Key[16]{
Key::X, // 0
Key::Num1, // 1
Key::Num2, // 2
Key::Num3, // 3
Key::Q, // 4
Key::W, // 5
Key::E, // 6
Key::A, // 7
Key::S, // 8
Key::D, // 9
Key::Z, // A
Key::C, // B
Key::Num4, // C
Key::R, // D
Key::F, // E
Key::V // F
};
int A = 10;
int B = 11;
int C = 12;
int D = 13;
int E = 14;
int F = 15;
void execute_instruction()
{
uint16_t opcode = (memory[PC] << 8) | memory[PC + 1]; // combine the 2 bytes chip-8 opcodes are 16bit
PC += 2; // fetch cycle complete.
int x = (opcode & 0x0F00) >> 8;
int y = (opcode & 0x00F0) >> 4;
int nnn = (opcode & 0x0FFF);
int kk = (opcode & 0x00FF);
int n = (opcode & 0x000F);
switch (opcode & 0xF000)
{
case 0x0000:
if (opcode == 0x00E0)
cls();
else if (opcode == 0x00EE)
ret();
else
cout << "unknown opcode" << opcode << endl;
break;
case 0x1000:
jp(nnn);
break;
case 0x2000:
call(nnn);
break;
case 0x3000:
se(x, kk);
break;
case 0x4000:
sne(x, kk);
break;
case 0x5000:
seRegister(x, y);
break;
case 0x6000:
ld(x, kk);
break;
case 0x7000:
add(x, kk);
break;
case 0x8000:
switch (n)
{
case 0x0:
ldRegister(x, y);
break;
case 0x1:
orBwise(x, y);
break;
case 0x2:
andBwise(x, y);
break;
case 0x3:
xorBwise(x, y);
break;
case 0x4:
addRegister(x, y);
break;
case 0x5:
subRegister(x, y);
break;
case 0x6:
shr(x);
break;
case 0x7:
subn(x, y);
break;
case 0xE:
shl(x);
break;
}
break;
case 0x9000:
sneRegister(x, y);
break;
case 0xA000:
si(nnn);
break;
case 0xB000:
jpRegister(nnn);
break;
case 0xC000:
rnd(x, kk);
break;
case 0xD000:
draw(x, y, n);
break;
case 0xE000:
if (kk == 0x9E)
kbrdSKP(x);
else if (kk == 0xA1)
kbrdSKNP(x);
break;
case 0xF000:
switch (kk)
{
case 0x07:
ldD(x);
break;
case 0x0A:
ldP(x);
break;
case 0x15:
setDRegister(x);
break;
case 0x18:
setSRegister(x);
break;
case 0x1E:
addIRegister(x);
break;
case 0x29:
ldF(x);
break;
case 0x33:
bcd(x);
break;
case 0x55:
ldIRegisterMultiple(x);
break;
case 0x65:
rdIRegisterMultiple(x);
break;
}
break;
default:
break;
}
}
void cls()
{
display.fill(0);
}
void ret()
{
PC = Stack.top();
Stack.pop();
}
void jp(int addr)
{
PC = addr;
}
void call(int addr)
{
// we dont update SP because we use stack so its redundant, stack.top()
Stack.push(PC);
PC = addr;
}
void se(int Vx, int value)
{
if (V[Vx] == value)
{
PC += 2;
}
}
void sne(int Vx, int value)
{
if (V[Vx] != value)
{
PC += 2;
}
}
void seRegister(int Vx, int Vy) // compares the vals in two registers
{
if (V[Vx] == V[Vy])
{
PC += 2;
}
}
void ld(int Vx, int value)
{
V[Vx] = value;
}
void add(int Vx, int value)
{
V[Vx] += value;
}
void ldRegister(int Vx, int Vy) // copies the value of register y to register x
{
V[Vx] = V[Vy];
}
void orBwise(int Vx, int Vy) // bitwise or operation result stored in Vx
{
V[Vx] = V[Vx] | V[Vy];
}
void andBwise(int Vx, int Vy) // bitwise and operation result stored in Vx
{
V[Vx] = V[Vx] & V[Vy];
}
void xorBwise(int Vx, int Vy) // bitwise XOR operation result stored in VX
{
V[Vx] = V[Vx] ^ V[Vy];
}
void addRegister(int Vx, int Vy)
{
uint16_t sum = V[Vx] + V[Vy];
if (sum > 255)
{
V[F] = 1; // essentially, carry around back for binary addition
}
else
{
V[F] = 0; // no carry
}
V[Vx] = (uint8_t)(sum & 0xFF);
}
void subRegister(int Vx, int Vy)
{
V[F] = (V[Vx] >= V[Vy]) ? 1 : 0; // sets the flag
V[Vx] -= V[Vy];
}
void shr(int Vx)
{
V[F] = (V[Vx] & 0x1);
// 2. Perform the actual shift
V[Vx] >>= 1;
}
void subn(int Vx, int Vy)
{
V[F] = (V[Vy] >= V[Vx]) ? 1 : 0;
V[Vx] = V[Vy] - V[Vx];
}
void shl(int Vx)
{
V[F] = (V[Vx] & 0x80) >> 7;
V[Vx] <<= 1;
}
void sneRegister(int Vx, int Vy)
{
if (V[Vx] != V[Vy])
{
PC += 2;
}
}
void si(int addr)
{
I = addr;
}
void jpRegister(int addr) // sets PC to addr +Register 0
{
PC = V[0] + addr;
}
void rnd(int Vx, int value)
{
uint8_t randomByte = rand() % 256;
V[Vx] = randomByte & value;
}
void draw(int Vx, int Vy, int height)
{
// start coords from reg
uint8_t xStart = V[Vx] % 64;
uint8_t yStart = V[Vy] % 32;
V[F] = 0; // reset collision flag
// go thru each row
for (int row = 0; row < height; row++)
{
uint8_t spriteByte = memory[I + row];
// loop through 8 bits in row
for (int col = 0; col < 8; col++)
{
if ((spriteByte & (0x80 >> col)) != 0)
{
// calc pos 1D display array
int x = (xStart + col) % 64;
int y = (yStart + row) % 32;
int index = x + (y * 64);
// collision Check
if (display[index] == 1)
{
V[F] = 1;
}
// XOR the pixel
display[index] ^= 1;
}
}
}
}
void kbrdSKP(int Vx)
{
if (keypad[V[Vx]])
{
PC += 2;
}
}
void kbrdSKNP(int Vx)
{
if (!keypad[V[Vx]])
{
PC += 2;
}
}
void ldD(int Vx) // loads delay timer to vx
{
V[Vx] = DTR;
}
void ldP(int Vx) // halts all program execution until ANY key is pressed. KEY pressed stored in specified register.
{
bool anyPressed = false;
for (int i = 0; i < sizeof(keypad); i++)
{
if (keypad[i] != 0)
{
V[Vx] = i;
anyPressed = true;
break;
}
}
// If no key was found this cycle, move PC back
// so we execute this SAME instruction again next time.
if (!anyPressed)
{
PC -= 2;
}
}
void setDRegister(int Vx)
{
DTR = V[Vx];
}
void setSRegister(int Vx)
{
STR = V[Vx];
}
void addIRegister(int Vx)
{
I = I + V[Vx];
}
void ldF(int Vx)
{
// V[Vx] contains the digit (0-15)
// We point I to the start of that digit's sprite in memory
I = V[Vx] * 5;
}
void bcd(int Vx)
{
uint8_t value = V[Vx];
memory[I] = value / 100; // Get the hundreds
memory[I + 1] = (value / 10) % 10; // Get the tens
memory[I + 2] = value % 10; // Get the ones
}
void ldIRegisterMultiple(int Vx)
{
for (int i = 0; i <= Vx; i++)
{
memory[I + i] = V[i];
}
}
void rdIRegisterMultiple(int Vx)
{
for (int i = 0; i <= Vx; i++)
{
V[i] = memory[I + i];
}
}
void debugRender(sf::RenderWindow &window)
{
window.clear();
sf::VertexArray vertexArray(sf::PrimitiveType::Points);
sf::View view(sf::FloatRect({0.f, 0.f}, {64.f, 32.f}));
while (const std::optional event = window.pollEvent())
{
// Window closed or escape key pressed: exit
if (event->is<sf::Event::Closed>() ||
(event->is<sf::Event::KeyPressed>() &&
event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape))
{
cout << "Bye! :)" << endl;
exit(0);
}
}
window.setView(view);
sf::Vertex point;
for (int y = 0; y < 32; y++)
{
for (int x = 0; x < 64; x++)
{
// Print a '#' for ON and a '.' for OFF
if (display[x + (y * 64)] != 0)
{
point.position = sf::Vector2f(x, y);
point.color = sf::Color::White;
vertexArray.append(point);
}
else
cout << " ";
}
cout << endl;
}
for (int i = 0; i < 16; i++)
{
if (isKeyPressed(keys_array[i]))
{
keypad[i] = 1;
}
else
keypad[i] = 0;
}
window.draw(vertexArray);
window.display();
}
};
int main(int argc, char *argv[])
{
std::cout << "CHIP-8 Emulator Started!" << std::endl;
Chip8 system = Chip8();
std::array<uint8_t, 4096> rom;
rom.fill(0);
if (argc > 1)
{
rom = system.loadFromFile(argv[1], system, rom);
system.loadRom(rom);
system.run();
}
else
{
// 1. F029 -> Point I to the sprite for '0' (located in your font set)
rom[0] = 0xF0;
rom[1] = 0x29;
// 2. 600A -> Set V0 to 10 (X coordinate)
rom[2] = 0x60;
rom[3] = 0x0A;
// 3. 610A -> Set V1 to 10 (Y coordinate)
rom[4] = 0x61;
rom[5] = 0x0A;
// 4. D015 -> Draw 5 rows of the sprite at (V0, V1)
rom[6] = 0xD0;
rom[7] = 0x15;
// 5. 1208 -> Infinite loop so it doesn't crash
rom[8] = 0x12;
rom[9] = 0x08;
system.loadRom(rom);
system.run();
}
return 0;
}