-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.cpp
715 lines (515 loc) · 12.9 KB
/
src.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// CHIP 8 Emulator/Intrepter
#include "pch.h"
#include <iostream>
#include <chrono>
#include <thread>
#include "stdint.h"
#include <stdio.h>
#include "SDL.h"
#undef main
//fontset -
unsigned char chip8_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
};
class Opcodes
{
public:
//Declarations
unsigned short opcode; //Chip 8 has 35 opcodes, all two bytes long (an unsigned short has a length of 2 bytes.)
unsigned char memory[4096]; // Chip has 4k memory in total.
unsigned char V[16]; // Chip 8 has 16 registers in all, ranging from V0 to VE, the 16th register is a 'carry flag'
unsigned short I; // Index register
unsigned short pc; // Program Counter
bool drawFlag;
/*
-------------------------MEMORY MAP----------------------------------------------------
0x000 - 0x1FF -> Memory for the emulator/intrepter |
0x050 - 0x0A0 -> For the 4x5 pixel font set (built in 0-F) |
0x200 - 0xFFF -> For ROM and RAM |
|
--------------------------------------------------------------------------------------*/
unsigned char gfx[64 * 32]; //Screen array.
//Chip 8 has 2 timers
unsigned char delay_timer;
unsigned char sound_timer;
/*
Chip 8's sepcification doesn't mention a stack,however it needs to be interpreted for the intrepter/emulator
*/
unsigned short stack[16];
unsigned short sp; //'sp' is the stack pointer.
//Chip 8 has a hex based keypad
unsigned char key[16];
void initialize()
{
//In this method all registers and memory are initialized once.
//Initializations
pc = 0x200; //Chip-8 doesn't have a BIOS, however it loads the program at memory location 0x200.
opcode = 0; //Current Opcode reset.
I = 0; //Index register reset.
sp = 0; //Stack pointer reset.
srand(time(NULL));
for (int i = 0; i < 2048; ++i)
{
gfx[i] = 0; //Clearing video buffer
}
//Clearing the stack, keypad, and V registers
for (int i = 0; i < 16; ++i)
{
stack[i] = 0;
key[i] = 0;
V[i] = 0;
}
//Clearing memory
for (int i = 0; i < 4096; ++i) {
memory[i] = 0;
}
//Load font set into memory
for (int i = 0; i < 80; ++i) {
memory[i] = chip8_fontset[i];
}
//Reseting timers
delay_timer = 0;
sound_timer = 0;
}
void emulateCycle()
{
//This function is called whenever a cycle is execyted, this method Fetches-Decodes-Executes one opcode.
/*
Opcodes Fetch-Decode-Execute will be performed here.
The 'memory' here is a unsigned char array, an char is one Byte long.
A Chip-8 opcode is 2 bytes long.
So we need to combine 2 elements in the 'memory' array in order to get the memory location of the opcode.
This is done by using the following algorithm -
opcode = memory[pc] << 8 | memory[pc + 1];
EXPLANATION -
(assume the opcodes to be in binary for now, in actuality memory locations are represented as hex.)
('pc' is the program counter,it will point to the memory location of the next opcode in this case.)
>First we perform a Left Shift on the first element of the 'memory' array which adds eight 0's to the right of the binary number.
>Then we perform a logical OR operation to the new element with the newly aquired binary number which gives us the actual memory location of the desired opcode.
*/
//Fetch.
opcode = memory[pc] << 8 | memory[pc + 1];
//Decode.
switch (opcode & 0xF000)
{
case 0x0000:
switch (opcode & 0x000F)
{
case 0x0000:
for (int i = 0; i < 2048; ++i) {
gfx[i] = 0;
}
drawFlag = true;
pc += 2;
break;
// 00EE - Return from subroutine
case 0x000E:
--sp;
pc = stack[sp];
pc += 2;
break;
default:
printf("\nUnknown op code: %.4X\n", opcode);
exit(3);
}
break;
case 0x1000:
pc = opcode & 0x0FFF;
break;
case 0x2000:
stack[sp] = pc;
++sp;
pc = opcode & 0x0FFF;
break;
case 0x3000:
if (V[(opcode & 0x0F00) >> 8] == (opcode & 0x00FF))
{
pc += 4;
}
else
pc += 2;
break;
case 0x4000:
if (V[(opcode & 0x0F00) >> 8] != (opcode & 0x00FF))
{
pc += 4;
}
else
pc += 2;
break;
case 0x5000:
if (V[(opcode & 0x0F00) >> 8] == V[(opcode & 0x00F0) >> 4])
{
pc += 4;
}
else
pc += 2;
break;
case 0x6000:
V[(opcode & 0x0F00) >> 8] = opcode & 0x00FF;
pc += 2;
break;
case 0x7000:
V[(opcode & 0x0F00) >> 8] += (opcode & 0x00FF);
pc += 2;
break;
case 0x8000:
switch (opcode & 0xF00F)
{
case 0x8000:
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4];
pc += 2;
break;
case 0x8001:
//8XY1 BitOp Vx=Vx|Vy
V[opcode & 0x0F00 >> 8] = V[(opcode & 0x0F00) >> 8] | V[(opcode & 0x00F0) >> 4];
pc += 2;
break;
case 0x8002:
//8XY2 BitOp Vx=Vx&Vy
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x0F00) >> 8] & V[(opcode & 0x00F0) >> 4];
pc += 2;
break;
case 0x8003:
//8XY3 BitOp Vx = Vx ^ Vy
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x0F00) >> 8] ^ V[(opcode & 0x00F0) >> 4];
pc += 2;
break;
case 0x8004:
//8XY4 Math Vx += Vy
if (V[(opcode & 0x00F0) >> 4] > (0xFF - V[(opcode & 0x0F00) >> 8]))
V[0xF] = 1; //carry
else
V[0xF] = 0;
V[(opcode & 0x0F00) >> 8] += V[(opcode & 0x00F0) >> 4];
pc += 2;
break;
case 0x8005:
//8XY5 Math Vx -= Vy
if (V[(opcode & 0x00F0) >> 4] > V[(opcode & 0x0F00) >> 8])
V[0xF] = 0; // there is a borrow
else
V[0xF] = 1;
V[(opcode & 0x0F00) >> 8] -= V[(opcode & 0x00F0) >> 4];
pc += 2;
break;
case 0x8006:
//8XY6 BitOp Vx>>=1
V[(opcode & 0x0F00) >> 8] >>= 1;
pc += 2;
break;
case 0x8007:
//8XY7 Math Vx=Vy-Vx
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x0F00) >> 8] - V[(opcode & 0x00F0) >> 4];
pc += 2;
break;
case 0x800E:
//8XYE BitOp Vx<<=1
V[(opcode & 0x0F00) >> 8] <<= 1;
pc += 2;
break;
default:
std::cout << "Unknown opcode 0x" << opcode << std::endl;
break;
}
break;
case 0x9000:
//9XY0 Cond if (Vx != Vy)
if (V[(opcode & 0x0F00) >> 8] != V[(opcode & 0x00F0) >> 4])
{
pc += 2;
}
break;
case 0xA000:
I = opcode & 0x0FFF;
pc += 2;
break;
case 0xB000:
//BNNN Flow PC=V0+NNN
pc = V[0x0000] + (opcode & 0x0FFF);
pc += 2;
break;
case 0xC000:
//CXNN Rand Vx=rand()&NN
V[(opcode & 0x0F00) >> 8] = (rand() % (0xFF + 1)) & (opcode & 0x00FF);
pc += 2;
break;
case 0xD000:
{
unsigned short x = V[(opcode & 0x0F00) >> 8];
unsigned short y = V[(opcode & 0x00F0) >> 4];
unsigned short height = opcode & 0x000F;
unsigned short pixel;
V[0xF] = 0;
for (int yline = 0; yline < height; yline++)
{
pixel = memory[I + yline];
for (int xline = 0; xline < 8; xline++)
{
if ((pixel & (0x80 >> xline)) != 0)
{
if (gfx[(x + xline + ((y + yline) * 64))] == 1)
V[0xF] = 1;
gfx[x + xline + ((y + yline) * 64)] ^= 1;
}
}
}
drawFlag = true;
pc += 2;
}
break;
case 0xE000:
switch (opcode & 0x00FF)
{
case 0x009E:
if (key[V[(opcode & 0x0F00) >> 8]] != 0)
pc += 4;
else
pc += 2;
break;
case 0x00A1:
if (key[V[(opcode & 0x0F00) >> 8]] == 0)
pc += 4;
else
pc += 2;
break;
default:
printf("Debug console:Unknown opcode [0xF000]: 0x%X\n", opcode);
break;
}
case 0xF000:
switch (opcode & 0x00FF)
{
case 0x0007:
V[(opcode & 0x0F00) >> 8] = delay_timer;
pc += 2;
break;
case 0x000A:
{
bool key_pressed = false;
for (int i = 0; i < 16; ++i)
{
if (key[i] != 0)
{
V[(opcode & 0x0F00) >> 8] = i;
key_pressed = true;
}
}
if (!key_pressed)
return;
pc += 2;
}
break;
case 0x0015:
delay_timer = V[(opcode & 0x0F00) >> 8];
pc += 2;
break;
case 0x0018:
sound_timer = V[(opcode & 0x0F00) >> 8];
pc += 2;
break;
case 0x001E:
if (I + V[(opcode & 0x0F00) >> 8] > 0xFFF)
V[0xF] = 1;
else
V[0xF] = 0;
I += V[(opcode & 0x0F00) >> 8];
pc += 2;
break;
case 0x0029:
I = V[(opcode & 0x0F00) >> 8] * 0x5;
pc += 2;
break;
case 0x0033:
memory[I] = V[(opcode & 0x0F00) >> 8] / 100;
memory[I + 1] = (V[(opcode & 0x0F00) >> 8] / 10) % 10;
memory[I + 2] = V[(opcode & 0x0F00) >> 8] % 10;
pc += 2;
break;
case 0x0055:
for (int i = 0; i <= ((opcode & 0x0F00) >> 8); ++i)
memory[I + i] = V[i];
I += ((opcode & 0x0F00) >> 8) + 1;
pc += 2;
break;
case 0x0065:
for (int i = 0; i <= ((opcode & 0x0F00) >> 8); ++i)
V[i] = memory[I + i];
I += ((opcode & 0x0F00) >> 8) + 1;
pc += 2;
break;
}
}
if (delay_timer > 0)
--delay_timer;
if (sound_timer > 0)
if (sound_timer == 1)
{
std::cout << "BEEP!!" << std::endl;
--sound_timer;
}
}
bool loadApplication(const char * filename)
{
initialize();
printf("Loading: %s\n", filename);
// Open file
FILE * pFile = fopen(filename, "rb");
if (pFile == NULL)
{
fputs("File error", stderr);
return false;
}
// Check file size
fseek(pFile, 0, SEEK_END);
long lSize = ftell(pFile);
rewind(pFile);
printf("Filesize: %d\n", (int)lSize);
// Allocate memory to contain the whole file
char * buffer = (char*)malloc(sizeof(char) * lSize);
if (buffer == NULL)
{
fputs("Memory error", stderr);
return false;
}
// Copy the file into the buffer
size_t result = fread(buffer, 1, lSize, pFile);
if (result != lSize)
{
fputs("Reading error", stderr);
return false;
}
// Copy buffer to Chip8 memory
if ((4096 - 512) > lSize)
{
for (int i = 0; i < lSize; ++i)
memory[i + 512] = buffer[i];
}
else
printf("Error: ROM too big for memory");
// Close file, free buffer
fclose(pFile);
free(buffer);
return true;
}
};
// Keypad keymap
uint8_t keymap[16] = {
SDLK_x,
SDLK_1,
SDLK_2,
SDLK_3,
SDLK_q,
SDLK_w,
SDLK_e,
SDLK_a,
SDLK_s,
SDLK_d,
SDLK_z,
SDLK_c,
SDLK_4,
SDLK_r,
SDLK_f,
SDLK_v,
};
int main(int argc, char **argv) {
// Command usage
if (argc != 2) {
std::cout << "Usage: chip8 <ROM file>" << std::endl;
return 1;
}
Opcodes chip8 = Opcodes(); // Initialise Chip8
chip8.initialize();
int w = 1024; // Window width
int h = 512; // Window height
SDL_Window* window = NULL;
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
// Create window
window = SDL_CreateWindow(
"CHIP-8 Emulator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
w, h, SDL_WINDOW_SHOWN
);
if (window == NULL) {
printf("Window could not be created! SDL_Error: %s\n",
SDL_GetError());
exit(2);
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_RenderSetLogicalSize(renderer, w, h);
SDL_Texture* sdlTexture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
64, 32);
// Temporary pixel buffer
uint32_t pixels[2048];
load:
// Attempt to load ROM
if (!chip8.loadApplication(argv[1]))
return 2;
// Emulation loop
while(true)
{
chip8.emulateCycle();
// Process SDL events
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) exit(0);
// Process keydown events
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE)
exit(0);
if (e.key.keysym.sym == SDLK_F1)
goto load;
for (int i = 0; i < 16; ++i) {
if (e.key.keysym.sym == keymap[i]) {
chip8.key[i] = 1;
}
}
}
// Process keyup events
if (e.type == SDL_KEYUP) {
for (int i = 0; i < 16; ++i) {
if (e.key.keysym.sym == keymap[i]) {
chip8.key[i] = 0;
}
}
}
}
// If draw occurred, redraw SDL screen
if (chip8.drawFlag) {
chip8.drawFlag = false;
for (int i = 0; i < 2048; ++i) {
uint8_t pixel = chip8.gfx[i];
pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000;
}
SDL_UpdateTexture(sdlTexture, NULL, pixels, 64 * sizeof(Uint32));
// Clear screen and render
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(renderer);
}
//To slow down emulation speed -
std::this_thread::sleep_for(std::chrono::microseconds(4000));
}
}