Skip to content

Commit f3b392d

Browse files
author
Luk3
committed
Load the fontset correctly
The fontset was not being loaded to the emulator, resulting in the scores in pong now showing.
1 parent 1aabbca commit f3b392d

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

AlphaChip/chip8.cpp

+37-23
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
#include <time.h> /* time */
44
#include "chip8.h"
55

6+
unsigned char chip8_fontset[80] =
7+
{
8+
0xF0, 0x90, 0x90, 0x90, 0xF0, //0
9+
0x20, 0x60, 0x20, 0x20, 0x70, //1
10+
0xF0, 0x10, 0xF0, 0x80, 0xF0, //2
11+
0xF0, 0x10, 0xF0, 0x10, 0xF0, //3
12+
0x90, 0x90, 0xF0, 0x10, 0x10, //4
13+
0xF0, 0x80, 0xF0, 0x10, 0xF0, //5
14+
0xF0, 0x80, 0xF0, 0x90, 0xF0, //6
15+
0xF0, 0x10, 0x20, 0x40, 0x40, //7
16+
0xF0, 0x90, 0xF0, 0x90, 0xF0, //8
17+
0xF0, 0x90, 0xF0, 0x10, 0xF0, //9
18+
0xF0, 0x90, 0xF0, 0x90, 0x90, //A
19+
0xE0, 0x90, 0xE0, 0x90, 0xE0, //B
20+
0xF0, 0x80, 0x80, 0x80, 0xF0, //C
21+
0xE0, 0x90, 0x90, 0x90, 0xE0, //D
22+
0xF0, 0x80, 0xF0, 0x80, 0xF0, //E
23+
0xF0, 0x80, 0xF0, 0x80, 0x80 //F
24+
};
625

726
chip8::chip8()
8-
{
9-
unsigned char chip8_fontset[80] =
10-
{
11-
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
12-
0x20, 0x60, 0x20, 0x20, 0x70, // 1
13-
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
14-
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
15-
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
16-
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
17-
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
18-
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
19-
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
20-
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
21-
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
22-
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
23-
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
24-
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
25-
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
26-
0xF0, 0x80, 0xF0, 0x80, 0x80 // F
27-
};
27+
{
2828
}
2929

3030

@@ -78,7 +78,7 @@ void chip8::initialize()
7878
void chip8::emulateCycle()
7979
{
8080
// Fetch opcode
81-
this->opcode = this->memory[this->pc] << 8 | this->memory[this->pc + 1];
81+
opcode = memory[pc] << 8 | memory[pc + 1];
8282

8383
// Decode opcode
8484
switch (opcode & 0xF000)
@@ -474,8 +474,10 @@ void chip8::emulateCycle()
474474
if (sound_timer > 0)
475475
{
476476
if (sound_timer == 1)
477+
{
477478
printf("BEEP!\n");
478479
printf("\7");
480+
}
479481
--sound_timer;
480482
}
481483
}
@@ -498,7 +500,19 @@ void chip8::loadGame(std::string new_filename)
498500
std::memcpy(memory + 0x200, buffer, 4096 - 0x200); // Rom data starts at 0x200
499501
}
500502

501-
void chip8::setKeys()
503+
void chip8::debugDraw()
502504
{
503-
//throw std::exception("not implemented");
505+
// Draw
506+
for (int y = 0; y < 32; ++y)
507+
{
508+
for (int x = 0; x < 64; ++x)
509+
{
510+
if (gfx[(y * 64) + x] == 0)
511+
printf("O");
512+
else
513+
printf(" ");
514+
}
515+
printf("\n");
516+
}
517+
printf("\n");
504518
}

AlphaChip/chip8.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class chip8
77
void initialize();
88
void emulateCycle();
99
void loadGame(std::string filename);
10-
void setKeys();
10+
void debugDraw();
1111

1212
bool drawFlag;
1313

@@ -25,9 +25,6 @@ class chip8
2525
unsigned short stack[16];
2626
unsigned short sp;
2727
unsigned char key[16];
28-
29-
unsigned char chip8_fontset[80];
30-
3128
/* 0x000-0x1FF - Chip 8 interpreter (contains font set in emu) [0 - 511]
3229
0x050-0x0A0 - Used for the built in 4x5 pixel font set (0-F) [80 - 160]
3330
0x200-0xFFF - Program ROM and work RAM [512 - 7777]*/

AlphaChip/main.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(int argc, char ** argv)
3939
setupInput();
4040

4141
emulator.initialize();
42-
emulator.loadGame("C:\\chip8-roms\\PONG");
42+
emulator.loadGame("C:\\chip8-roms\\pong");
4343

4444

4545
while (!quit)
@@ -50,6 +50,7 @@ int main(int argc, char ** argv)
5050

5151
if (emulator.drawFlag)
5252
{
53+
//emulator.debugDraw();
5354
for (int i = 0; i < 64 * 32; i++)
5455
{
5556
if(emulator.gfx[i] == 1)
@@ -127,6 +128,9 @@ int main(int argc, char ** argv)
127128
case SDLK_v:
128129
emulator.key[0xF] = 1;
129130
break;
131+
case SDLK_ESCAPE:
132+
quit = true;
133+
break;
130134
default:
131135
break;
132136
}

0 commit comments

Comments
 (0)