-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
674 lines (544 loc) · 14.9 KB
/
main.cpp
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
668
669
670
671
672
673
#include <SFML/Graphics.hpp>
#include <fstream>
#include <iostream>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
using namespace std;
#define MAX_MEM 4096
#define MAX_REG 16
#define MAX_STACK 16
#define MAX_KEY 16
#define DISPLAY_WIDTH 64
#define DISPLAY_HEIGHT 32
#define MAX_FONT 16
#define FONT_SIZE 5
#define MEM_START 0x200
#define NB_MASK_1 0xF000
#define NB_MASK_2 0x0F00
#define NB_MASK_3 0x00F0
#define NB_MASK_4 0x000F
const uint8_t font [MAX_FONT][FONT_SIZE]= {
{0xF0, 0x90, 0x90, 0x90, 0xF0},
{0x20, 0x60, 0x20, 0x20, 0x70},
{0xF0, 0x10, 0xF0, 0x80, 0xF0},
{0xF0, 0x10, 0xF0, 0x10, 0xF0},
{0x90, 0x90, 0xF0, 0x10, 0x10},
{0xF0, 0x80, 0xF0, 0x10, 0xF0},
{0xF0, 0x80, 0xF0, 0x90, 0xF0},
{0xF0, 0x10, 0x20, 0x40, 0x40},
{0xF0, 0x90, 0xF0, 0x90, 0xF0},
{0xF0, 0x90, 0xF0, 0x10, 0xF0},
{0xF0, 0x90, 0xF0, 0x90, 0x90},
{0xE0, 0x90, 0xE0, 0x90, 0xE0},
{0xF0, 0x80, 0x80, 0x80, 0xF0},
{0xE0, 0x90, 0x90, 0x90, 0xE0},
{0xF0, 0x80, 0xF0, 0x80, 0xF0},
{0xF0, 0x80, 0xF0, 0x80, 0x80}
};
const int buttons[0xF] = {
sf::Keyboard::Num1, sf::Keyboard::Num2
sf::Keyboard::Num3, sf::Keyboard::Num4
sf::Keyboard::Q, sf::Keyboard::W
sf::Keyboard::E, sf::Keyboard::R
sf::Keyboard::A, sf::Keyboard::S
sf::Keyboard::D, sf::Keyboard::F
sf::Keyboard::Z, sf::Keyboard::X
sf::Keyboard::C, sf::Keyboard::V
}
uint8_t * mem = new uint8_t[MAX_MEM];
uint8_t * registers = new uint8_t[MAX_REG];
uint8_t * soundTimer = new uint8_t;
uint8_t * delayTimer = new uint8_t;
uint16_t * stackPointer = new uint16_t;
uint16_t * programCounter = new uint16_t;
uint16_t * stack = new uint16_t[MAX_STACK];
uint16_t * I = new uint16_t;
bool * keyboard = new bool[MAX_KEY];
uint8_t * display = new uint8_t[DISPLAY_WIDTH * DISPLAY_HEIGHT];
bool * drawflag = new bool;
char defaultRom[] = "MAZE";
sf::RenderWindow window(sf::VideoMode(640, 640), "Chip-8");
void load (char* file) {
ifstream f;
f.open(file);
f.read((char*)(mem+MEM_START), MAX_MEM - MEM_START);
}
void loadFont (void) {
for (int i = 0; i < MAX_FONT; i++) {
for (int j = 0; j < FONT_SIZE; j++) {
mem[i*FONT_SIZE + j] = font[i][j];
}
}
}
void init (char* rom) {
//init drawflag
*drawflag = false;
//init indexes
*programCounter = MEM_START;
*stackPointer = 0;
//initialise display
for (int i = 0; i < DISPLAY_HEIGHT * DISPLAY_WIDTH; i++) {
display[i] = 0;
}
//init mem
for (int i = 0; i < MAX_MEM; i++) {
mem[i] = 0;
}
//init registers
for (int i = 0; i < MAX_REG; i++) {
registers[i] = 0;
}
//init stack
for (int i = 0; i < MAX_STACK; i++) {
stack[i] = 0;
}
loadFont();
load(rom);
srand(time(NULL));
}
uint16_t getOpcode (int index) {
uint16_t opCode = (mem[index] << 8) | mem[index + 1];
return opCode;
}
//clear display
void ex_00E0(){
for (int i = 0; i < DISPLAY_HEIGHT * DISPLAY_WIDTH; i++) {
display[i] = 0;
}
*drawflag = true;
*programCounter += 2;
}
//return from subroutine
void ex_00EE(){
*programCounter = stack[(*stackPointer)--];
*programCounter += 2;
}
//jump to location nnn
void ex_1nnn(uint16_t opCode){
*programCounter = opCode & (0x0FFF);
}
//call subroutine at nnn
void ex_2nnn(uint16_t opCode){
stack[++(*stackPointer)] = *programCounter;
*programCounter = opCode & (0x0FFF);
}
//skip next instruction if register x is equal to kk
void ex_3xkk(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t kk = opCode & (0x00FF);
if (registers[x] == kk) *programCounter += 2;
*programCounter += 2;
}
//skip next instruction if register x is not equal to kk
void ex_4xkk(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t kk = opCode & (0x00FF);
if (registers[x] != kk) *programCounter += 2;
*programCounter += 2;
}
//skip next instruction if register x is equal to register y
void ex_5xy0(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
if (registers[x] == registers[y]) *programCounter += 2;
*programCounter += 2;
}
//put the value kk into register x
void ex_6xkk(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t kk = opCode & (NB_MASK_3 + NB_MASK_4);
registers[x] = kk;
*programCounter += 2;
}
//adds the value in register x to kk and stores result in register x
void ex_7xkk(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
registers[x] += opCode & (NB_MASK_3 + NB_MASK_4);
*programCounter += 2;
}
//stores value in register y in register x
void ex_8xy0(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
registers[x] = registers[y];
*programCounter += 2;
}
//sets register x to register x or register y
void ex_8xy1(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
registers[x] = registers[x] | registers[y];
*programCounter += 2;
}
//sets register x to register x and register y
void ex_8xy2(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
registers[x] = registers[x] & registers[y];
*programCounter += 2;
}
//sets register x to register x xor register y
void ex_8xy3(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
registers[x] = registers[x] ^ registers[y];
*programCounter += 2;
}
//adds registers x and y, if the result is > 255 register F is set to 1, 0 otherwise
//the lowest two nibbles are stored in register x
void ex_8xy4(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
registers[0x000F] = (registers[x] + registers[y]) > 255;
registers[x] = (registers[x] + registers[y]) & (0x00FF);
*programCounter += 2;
}
//if register x > register y register F is set to 1, otherwise 0
//register x is set to register x - register y
void ex_8xy5(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
registers[0x000F] = registers[x] > registers[y];
registers[x] -= registers[y];
*programCounter += 2;
}
//if register x & 0x0001, register F is set to 1 otherwise 0
//register x is set to register x >> 1
void ex_8xy6(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
registers[0x000F] = registers[x] & 0x0001;
registers[x] >>= 1;
*programCounter += 2;
}
//if register y > register x register F is set to 1 otherwise 0,
//register x is set to register y - register x
void ex_8xy7(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
registers[0x000F] = registers[y] > registers[x];
registers[x] = registers[y] - registers[x];
*programCounter += 2;
}
//if register x & 0x8000 != 0 register F is set to 1 otherwise 0
//register x is set to register x * 2
void ex_8xyE(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
registers[0x000F] = ((registers[x] & 0x80) != 0);
registers[x] <<= 1;
*programCounter += 2;
}
//skip next instruction if registers x and y are not equal
void ex_9xy0(uint16_t opCode){
uint8_t x = (opCode & NB_MASK_2) >> 8;
uint8_t y = (opCode & NB_MASK_3) >> 4;
if (registers[x] != registers[y]) *programCounter += 2;
*programCounter += 2;
}
//register I is set to nnn
void ex_Annn(uint16_t opCode){
*I = opCode & (NB_MASK_2 + NB_MASK_3 + NB_MASK_4);
*programCounter += 2;
}
//jump to address nnn + register 0
void ex_Bnnn(uint16_t opCode){
*programCounter = opCode & (0x0FFF);
*programCounter += registers[0];
}
//register x is set to a random byte & kk
void ex_Cxkk(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
registers[x] = (opCode & 0x00FF) & (rand() % 256);
*programCounter += 2;
}
//draw n byte sprite from I onwards at positions determined by registers x and y
//register F is set if there is a collision
void ex_Dxyn(uint16_t opCode){
uint16_t x = registers[(opCode & NB_MASK_2) >> 8] % DISPLAY_WIDTH;
uint16_t y = registers[(opCode & NB_MASK_3) >> 4] % DISPLAY_HEIGHT;
uint16_t n = (opCode & NB_MASK_4);
uint8_t * sprite = new uint8_t [n];
for (int i = 0; i < n; i++) {
sprite[i] = mem[*I + i];
}
registers[0xF] = 0;
for (int byte = 0; byte < n; byte++) {
for (int bit = 0; bit < 8; bit++) {
int xc = (x + bit) % DISPLAY_WIDTH;
int yc = (y + byte) % DISPLAY_HEIGHT;
if ((sprite[byte] & (0x80 >> bit)) != 0) {
if (display[yc * DISPLAY_WIDTH + xc]) {
registers[0xF] = 1;
}
display[yc * DISPLAY_WIDTH + xc] ^= 1;
}
}
}
*drawflag = true;
*programCounter += 2;
}
//skips next instruction if key code in register x is pressed
void ex_Ex9E(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
if (keyboard[registers[x]]) *programCounter += 2;
*programCounter += 2;
}
//skips next instruction if key code in register x is not pressed
void ex_ExA1(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >>8;
if (!keyboard[registers[x]]) *programCounter += 2;
*programCounter += 2;
}
//set register x to the value of the delaytimer
void ex_Fx07(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
registers[x] = *delayTimer;
*programCounter += 2;
}
//wait for key press then stores key code in register x
void ex_Fx0A(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
bool done = false;
for (uint8_t i = 0; i < MAX_KEY; i++) {
if (keyboard[i]) {
done = true;
registers[x] = i;
}
}
if (done) *programCounter += 2;
}
//sets delay timer to value in register x
void ex_Fx15(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
*delayTimer = registers[x];
*programCounter += 2;
}
//set sound timer to value in register x
void ex_Fx18(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
*soundTimer = registers[x];
*programCounter += 2;
}
//set I to I + register x
void ex_Fx1E(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
*I += registers[x];
*programCounter += 2;
}
//sets I to the location of the charatcer sprite for the character in register x
void ex_Fx29(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
*I = FONT_SIZE * registers[x];
*programCounter += 2;
}
//hundreds value of register x
//is stored at address is stored in I
//tens and ones stored in following addresses
void ex_Fx33(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
mem[*I] = (uint8_t)(registers[x]/100);
mem[*I + 1] = (uint8_t)((registers[x] % 100)/10);
mem[*I + 2] = (uint8_t)((registers[x] % 100)%10);
*programCounter += 2;
}
//stores registers 0 through x in memory starting at address stored in I
void ex_Fx55(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
for (int i = 0; i <= x; i++) {
mem[*I + i] = registers[i];
}
*programCounter += 2;
}
//loads values from memory into registers 0 through x starting at address I
void ex_Fx65(uint16_t opCode){
uint16_t x = (opCode & NB_MASK_2) >> 8;
for (int i = 0; i <= x; i++) {
registers[i] = mem[*I + i];
}
*programCounter += 2;
}
void executeOpcode (uint16_t opCode) {
cout << "Executing: " << hex << opCode << endl;
uint16_t nib_1 = opCode & NB_MASK_1;
uint16_t nib_3 = opCode & NB_MASK_3;
uint16_t nib_4 = opCode & NB_MASK_4;
switch (nib_1) {
case 0x0000:
switch (opCode) {
case 0x00E0:
ex_00E0();
break;
case 0x00EE:
ex_00EE();
break;
}
break;
case 0x1000:
ex_1nnn(opCode);
break;
case 0x2000:
ex_2nnn(opCode);
break;
case 0x3000:
ex_3xkk(opCode);
break;
case 0x4000:
ex_4xkk(opCode);
break;
case 0x5000:
ex_5xy0(opCode);
break;
case 0x6000:
ex_6xkk(opCode);
break;
case 0x7000:
ex_7xkk(opCode);
break;
case 0x8000:
switch (nib_4) {
case 0x0000:
ex_8xy0(opCode);
break;
case 0x0001:
ex_8xy1(opCode);
break;
case 0x0002:
ex_8xy2(opCode);
break;
case 0x0003:
ex_8xy3(opCode);
break;
case 0x0004:
ex_8xy4(opCode);
break;
case 0x0005:
ex_8xy5(opCode);
break;
case 0x0006:
ex_8xy6(opCode);
break;
case 0x0007:
ex_8xy7(opCode);
break;
case 0x000E:
ex_8xyE(opCode);
break;
}
break;
case 0x9000:
ex_9xy0(opCode);
break;
case 0xA000:
ex_Annn(opCode);
break;
case 0xB000:
ex_Bnnn(opCode);
break;
case 0xC000:
ex_Cxkk(opCode);
break;
case 0xD000:
ex_Dxyn(opCode);
break;
case 0xE000:
switch (nib_4) {
case 0x000E:
ex_Ex9E(opCode);
break;
case 0x0001:
ex_ExA1(opCode);
break;
}
break;
case 0xF000:
switch (nib_3 + nib_4) {
case 0x0007:
ex_Fx07(opCode);
break;
case 0x000A:
ex_Fx0A(opCode);
break;
case 0x0015:
ex_Fx15(opCode);
break;
case 0x0018:
ex_Fx18(opCode);
break;
case 0x001E:
ex_Fx1E(opCode);
break;
case 0x0029:
ex_Fx29(opCode);
break;
case 0x0033:
ex_Fx33(opCode);
break;
case 0x0055:
ex_Fx55(opCode);
break;
case 0x0065:
ex_Fx65(opCode);
break;
}
break;
}
}
void updateKeys () {
/*
0-1, 8-a
1-2, 9-s
2-3, A-d
3-4, B-f
4-q, C-z
5-w, D-x
6-e, E-c
7-r, F-v
*/
for (int i = 0; i <= 0xF; i++) {
keyboard[i] = sf::Keyboard::isKeyPressed(buttons[i]);
}
}
int main (int argc, char* argv[]) {
if (argc > 1) {
init(argv[1]);
}else{
init(defaultRom);
}
while (*programCounter <= MAX_MEM && window.isOpen())
{
updateKeys();
executeOpcode(getOpcode(*programCounter));
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (*drawflag) {
for (int i = 0; i < DISPLAY_HEIGHT; i++) {
for (int j = 0; j < DISPLAY_WIDTH; j++) {
sf::RectangleShape rectangle(sf::Vector2f(window.getSize().x / DISPLAY_WIDTH,
window.getSize().y / DISPLAY_HEIGHT));
rectangle.setPosition(j * (window.getSize().x / DISPLAY_WIDTH),
i * (window.getSize().y / DISPLAY_HEIGHT));
rectangle.setFillColor(sf::Color::Black);
if (display[i * DISPLAY_WIDTH + j]) {
rectangle.setFillColor(sf::Color::White);
}
window.draw(rectangle);
}
}
window.display();
}
if (*delayTimer > 0) *delayTimer -= 1;
if (*soundTimer > 0) *soundTimer -= 1;
*drawflag = false;
cout << "Program counter: " << hex << 1*(*programCounter) << endl;
for (int i = 0; i < MAX_REG; i++) {
cout << "reg[" << i << "]: " << hex << 1*registers[i] << endl;
}
cout << "reg[I]: " << hex << *I << endl;
}
return 0;
}