Skip to content

Commit

Permalink
Merge pull request #2 from deathscreton/Code_Refactoring
Browse files Browse the repository at this point in the history
Merging commits from separate branch. This branch is a duplicate of code-refactor, without the commits for the unfinished disassembler.
  • Loading branch information
deathscreton authored Mar 13, 2020
2 parents 58d126f + 6373b2d commit 7f4733a
Show file tree
Hide file tree
Showing 107 changed files with 646 additions and 302 deletions.
2 changes: 2 additions & 0 deletions Chip8 Emulator.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -136,6 +137,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
Empty file removed empty.txt
Empty file.
104 changes: 88 additions & 16 deletions include/Chip8.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,108 @@
class Chip8
{
public:
Chip8(); //Constructor
~Chip8(); //Destructor

///////////////////////
//PUBLIC METHODS
///////////////////////

bool loadROM(const int argc, const char* rom); //Function responsible for loading program into memory. Takes argc and a string pointer to determine path and file name.
void emulateCycle(); //Function responsible for a single emulated CPU cycle.
//Constructor
Chip8();

short fetch(); //Fetches and stores the Opcode in class variable 'opcode'.
//Destructor
~Chip8();

void debugRender(); //Draws graphics buffer 'gfx[x][y]' to the console.
//Function responsible for loading program into memory. Takes argc and a string pointer to determine path and file name.
bool loadROM(const int argc, const char* rom);

//Function responsible for a single emulated CPU cycle.
void emulateCycle();

bool drawFlag; //Determines if something was written to the display buffer and needs to be pushed.
//Draws graphics buffer 'gfx[x][y]' to the console.
void debugRender();

char unsigned gfx[64][32]; //Screen buffer, 2048 pixels total. Switched to a 2D array, last buffer used 1D array with a 64 multiple offset for the y value.
char unsigned key[16]; //Key buffer that stores the state of a pressed key; the value does not matter. Anything other than 0 indicates a pressed state.
///////////////////////
///PUBLIC MEMBER VARIABLES
///////////////////////

bool drawFlag; //Determines if something was written to the display buffer and needs to be pushed.

char unsigned gfx[64][32]{0,0}; //Screen buffer, 2048 pixels total. Switched to a 2D array, last buffer used 1D array with a 64 multiple offset for the y value.
char unsigned key[16]{0}; //Key buffer that stores the state of a pressed key; the value does not matter. Anything other than 0 indicates a pressed state.

private:

/*Used as a typedef for opFunc and stores data passed to it by the initializer list in vector 'parentFuncTable'.*/
struct opFunc
{
std::string opName;
void (Chip8::*opCo)(void) = nullptr;
};

//Supplies an offset for child opcodes that are derived from a parent opcode like 8XXX. This offset is used to determine which opcode function is ran from within vector childFuncTable.
enum childFuncOffset { noOffset = 0, opCode8 = 2, opCodeE = 11, opCodeF = 13, } offset;

//Stores intializer lists of type 'opFunc' that contain pointers to a certain function.
std::vector<opFunc> parentFuncTable;

//Stores initializer lists of type 'opFunc' that contain pointers to opcode functions (child functions) derived from a parent opcode, such as 8XXX.
std::vector<opFunc> childFuncTable;

///////////////////////
//PRIVATE HELPER FUNCTIONS
///////////////////////

//Fetches and stores the Opcode in class variable 'opcode'; sets hi and lo nibble for usage in findFunc. Also sets the offset for child opcode functions in childFuncTable.
void fetch();

//Find the initial opcode function using the hinibble. If validOP returns true, the nibble is passed to parentFuncTable to initiate the function. If false, it hands off the program to the catch-all function XXXX.
void findFunc();

//Checks to make sure the opcode being generated is a legal instruction.
bool isValidOp(const char &nibble);

//Function responsible for returning the offset value based on the child opcode function requested by the loNibble.
enum childFuncOffset getOffset();

///////////////////////
///OPCODE FUNCTION DECLARATIONS
///////////////////////

void zeroOp(); void CLS(); void RET();
void JMP(); void CALL(); void SEXB();
void SNEXB(); void SEXY(); void LDXB();
void ADXB(); void eightOp(); void LDXY();
void ORXY(); void ANDXY(); void XORXY();
void ADDXY(); void SUBXY(); void SHRX();
void SUBNXY(); void SHLXY(); void SNEXY();
void LDI(); void JP0B(); void RNDXB();
void DRWXY(); void hexEOp(); void SKPX();
void SKNPX(); void fOp(); void LDXDT();
void LDXK(); void LDDTX(); void LDSTX();
void ADDIX(); void LDFX(); void LDBX();
void LDIX(); void LDXI(); void XXXX();

///////////////////////
///PRIVATE MEMBER VARIABLES
///////////////////////

char unsigned memory[4096]; //4KB of memory used to store the ROM and other bits of important data.
short unsigned emuStack[16]; //Array that emulate the Stack; capable of storing sixteen levels of indirections.

char unsigned V[16]; //registers from V0 to VF, with VF being used for flags

short unsigned I; //Address Register used as an index to keep track of placement in memory.
short unsigned pc; //Program Counter used to keep track of where we are in the memory.
short unsigned sp; //Stack Pointer used to store the pc in the stack for temporary jumps.
short unsigned opcode; //Variable used to store the current opcode being executed.
short unsigned I = 0; //Address Register used as an index to keep track of placement in memory.
short unsigned pc = 0; //Program Counter used to keep track of where we are in the memory.
short unsigned sp = 0; //Stack Pointer used to store the pc in the stack for temporary jumps.
short unsigned opcode = 0x0000; //Variable used to store the current opcode being executed.

char unsigned hiNibble = 0x00; //Carries the high nibble of the opcode. Used to determine group function.
char unsigned loNibble = 0x00; //Carries the low nibble of the opcode. Used to determine specific function.

char unsigned delayTimer = 0; //Timer variable. Counts down at 60 Hz.
char unsigned soundTimer = 0; //Sound variable. Counts down at 60 Hz. Rings at non-zero.

char unsigned delayTimer; //Timer variable. Counts down at 60 Hz.
char unsigned soundTimer; //Sound variable. Counts down at 60 Hz. Rings at non-zero.

};
}; //Class Chip8

#endif // CHIP8_HPP
49 changes: 28 additions & 21 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

#define SCALE_FACTOR 12.5f

void keyStatePressed(sf::Event); //function encapsulating key states; moved from emulationLoop for legibility reasons
void keyStateReleased(sf::Event); //function encapsulating key states; moved from emulationLoop for legibility reasons
Chip8 chip8; //creates emulator object and initializes class state using constructor
sf::Event event;
sf::RenderWindow mainWindow(sf::VideoMode(800, 600), "Chip 8 Emulator"); //create and declare window
sf::RectangleShape chip8SpriteRect(sf::Vector2f(SCALE_FACTOR, SCALE_FACTOR)); //create RectangleShape object with a size of 12.5f, which is also the scale factor

Chip8 chip8; //creates object and initializes class state using constructor

void keyStateReleased(sf::Event keyState) //moved outside of the emulationLoop for legibility
void keyStateReleased(sf::Event keyState)
{
switch (keyState.key.code)
{
Expand Down Expand Up @@ -71,7 +71,7 @@ void keyStateReleased(sf::Event keyState) //moved outside of the emulationLoop f
}
}

void keyStatePressed(sf::Event keyState) //moved outside of the emulationLoop for legibility
void keyStatePressed(sf::Event keyState)
{
switch (keyState.key.code)
{
Expand Down Expand Up @@ -128,11 +128,21 @@ void keyStatePressed(sf::Event keyState) //moved outside of the emulationLoop fo
}
}

void pushBuffer() //fills the SFML window buffer with the gfx buffer from the chip8 then draws it to the screen.
{
for (int y = 0; y < 32; ++y)
{
for (int x = 0; x < 64; ++x)
{
if (chip8.gfx[x][y] == 1)
chip8SpriteRect.setPosition(x * SCALE_FACTOR, y * SCALE_FACTOR);//multiply the position value by the scale factor 12.5 so nothing overlaps
mainWindow.draw(chip8SpriteRect);
}
}
}

void emulationLoop()
{
sf::Event event;
sf::RenderWindow mainWindow(sf::VideoMode(800, 600), "Chip 8 Emulator"); //create and declare window
sf::RectangleShape chip8SpriteRect(sf::Vector2f(SCALE_FACTOR, SCALE_FACTOR));//create RectangleShape object with a size of 12.5, which is also the scale factor
mainWindow.setFramerateLimit(60);

while (mainWindow.isOpen())
Expand All @@ -154,25 +164,21 @@ void emulationLoop()
break;
}
}

chip8.emulateCycle();

if (chip8.drawFlag)
{
mainWindow.clear(sf::Color::Black);

for (int y = 0; y < 32; ++y)
{
for (int x = 0; x < 64; ++x)
{
if (chip8.gfx[x][y] == 1)
chip8SpriteRect.setPosition(x * SCALE_FACTOR, y * SCALE_FACTOR);//multiply the position value by the scale factor 12.5 so nothing overlaps
mainWindow.draw(chip8SpriteRect);
}
}
#ifdef DEBUG
pushBuffer();

#ifdef _DEBUG
chip8.debugRender();
#endif // DEBUG
#endif // _DEBUG

mainWindow.display();

chip8.drawFlag = false;
}
}
Expand All @@ -184,7 +190,8 @@ int main(int argc, char* argv[])
emulationLoop();
else
{
std::cout << "Error: Something failed with loading the ROM." << std::endl;
std::cerr << "Error: Something failed with loading the ROM." << std::endl;
std::cin.get();
return 1;
}
return 0;
Expand Down
Binary file added roms/15 Puzzle [Roger Ivie] (alt).c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Addition Problems [Paul C. Moews].c8
Binary file not shown.
Binary file added roms/Airplane.c8
Binary file not shown.
Binary file added roms/Animal Race [Brian Astle].c8
Binary file not shown.
Binary file added roms/Astro Dodge [Revival Studios, 2008].c8
Binary file not shown.
Binary file added roms/BC_test.ch8
Binary file not shown.
Binary file added roms/BMP Viewer - Hello (C8 example) [Hap, 2005].c8
Binary file not shown.
Binary file added roms/Biorhythm [Jef Winsor].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Blinky [Hans Christian Egeberg] (alt).c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Bowling [Gooitzen van der Wal].c8
Binary file not shown.
Binary file added roms/Breakout (Brix hack) [David Winter, 1997].c8
Binary file not shown.
Binary file added roms/Breakout [Carmelo Cortez, 1979].c8
Binary file not shown.
Binary file added roms/Brick (Brix hack, 1990).c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Cave.c8
Binary file not shown.
Binary file added roms/Chip8 Picture.c8
Binary file not shown.
Binary file added roms/Chip8 emulator Logo [Garstyciuks].c8
Binary file not shown.
Binary file added roms/Clock Program [Bill Fisher, 1981].c8
Binary file not shown.
Binary file added roms/Coin Flipping [Carmelo Cortez, 1978].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Craps [Camerlo Cortez, 1978].c8
Binary file not shown.
Binary file added roms/Deflection [John Fort].c8
Binary file not shown.
Binary file added roms/Delay Timer Test [Matthew Mikolay, 2010].c8
Binary file not shown.
Binary file added roms/Division Test [Sergey Naydenov, 2010].c8
Binary file not shown.
Binary file added roms/Figures.c8
Binary file not shown.
Binary file added roms/Filter.c8
Binary file not shown.
Binary file added roms/Fishie [Hap, 2005].c8
Binary file not shown.
Binary file added roms/Framed MK1 [GV Samways, 1980].c8
Binary file not shown.
Binary file added roms/Framed MK2 [GV Samways, 1980].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Guess [David Winter].c8
Binary file not shown.
Binary file added roms/Hi-Lo [Jef Winsor, 1978].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/IBM Logo.c8
Binary file not shown.
Binary file added roms/Jumping X and O [Harry Kleinberg, 1977].c8
Binary file not shown.
Binary file not shown.
Binary file added roms/Keypad Test [Hap, 2006].c8
Binary file not shown.
Binary file added roms/Landing.c8
Binary file not shown.
Binary file added roms/Life [GV Samways, 1980].c8
Binary file not shown.
Binary file added roms/Lunar Lander (Udo Pernisz, 1979).c8
Binary file not shown.
Binary file added roms/Mastermind FourRow (Robert Lindley, 1978).c8
Binary file not shown.
Binary file added roms/Maze (alt) [David Winter, 199x].c8
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file added roms/Minimal game [Revival Studios, 2007].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Most Dangerous Game [Peter Maruhnic].c8
Binary file not shown.
Binary file added roms/Nim [Carmelo Cortez, 1978].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Paddles.c8
Binary file not shown.
Binary file added roms/Particle Demo [zeroZshadow, 2008].c8
Binary file not shown.
Binary file added roms/Pong (1 player).c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Pong 2 (Pong hack) [David Winter, 1997].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Programmable Spacefighters [Jef Winsor].c8
Binary file not shown.
Binary file added roms/Random Number Test [Matthew Mikolay, 2010].c8
Binary file not shown.
Binary file added roms/Reversi [Philip Baltzer].c8
Binary file not shown.
Binary file added roms/Rocket Launch [Jonas Lindstedt].c8
Binary file not shown.
Binary file added roms/Rocket Launcher.c8
Binary file not shown.
Binary file added roms/Rocket [Joseph Weisbecker, 1978].c8
Binary file not shown.
Binary file added roms/Rush Hour [Hap, 2006] (alt).c8
Binary file not shown.
Binary file added roms/Rush Hour [Hap, 2006].c8
Binary file not shown.
Binary file added roms/Russian Roulette [Carmelo Cortez, 1978].c8
Binary file not shown.
Binary file added roms/SQRT Test [Sergey Naydenov, 2010].c8
Binary file not shown.
Binary file added roms/Sequence Shoot [Joyce Weisbecker].c8
Binary file not shown.
Binary file added roms/Shooting Stars [Philip Baltzer, 1978].c8
Binary file not shown.
Binary file added roms/Sierpinski [Sergey Naydenov, 2010].c8
Binary file not shown.
Binary file added roms/Sirpinski [Sergey Naydenov, 2010].c8
Binary file not shown.
Binary file added roms/Slide [Joyce Weisbecker].c8
Binary file not shown.
Binary file added roms/Soccer.c8
Binary file not shown.
Binary file added roms/Space Flight.c8
Binary file not shown.
Binary file added roms/Space Intercept [Joseph Weisbecker, 1978].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Space Invaders [David Winter].c8
Binary file not shown.
Binary file added roms/Spooky Spot [Joseph Weisbecker, 1978].c8
Binary file not shown.
Binary file added roms/Squash [David Winter].c8
Binary file not shown.
Binary file added roms/Stars [Sergey Naydenov, 2010].c8
Binary file not shown.
Binary file added roms/Submarine [Carmelo Cortez, 1978].c8
Binary file not shown.
Binary file added roms/Sum Fun [Joyce Weisbecker].c8
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file added roms/Tapeworm [JDR, 1999].c8
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file added roms/Timebomb.c8
Binary file not shown.
Binary file added roms/Trip8 Demo (2008) [Revival Studios].c8
Binary file not shown.
Binary file added roms/Tron.c8
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added roms/Wall [David Winter].c8
Binary file not shown.
File renamed without changes.
Binary file added roms/Worm V4 [RB-Revival Studios, 2007].ch8
Binary file not shown.
Binary file added roms/X-Mirror.ch8
Binary file not shown.
11 changes: 11 additions & 0 deletions roms/Zero Demo [zeroZshadow, 2007].ch8
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
`
ef
ghabcd`
�x�Vp
�~�fp
���vp
��Іj�`
�x�VEa�Ea��Vp
�~�fFb�Fb�$�fp
���vGc�Gc�4�vp
��ІHd�Hd�DІ*� 0��������������<����<
Binary file added roms/ZeroPong [zeroZshadow, 2007].c8
Binary file not shown.
1 change: 1 addition & 0 deletions roms/jason.c8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`�ao�
Binary file added roms/test_opcode.ch8
Binary file not shown.
Loading

0 comments on commit 7f4733a

Please sign in to comment.