diff options
-rw-r--r-- | src/chip8.cpp | 77 | ||||
-rw-r--r-- | src/chip8.hpp | 28 | ||||
-rw-r--r-- | src/main.cpp | 16 | ||||
-rw-r--r-- | src/table.cpp | 76 | ||||
-rw-r--r-- | src/table.hpp | 31 |
5 files changed, 131 insertions, 97 deletions
diff --git a/src/chip8.cpp b/src/chip8.cpp index e813a61..dc63239 100644 --- a/src/chip8.cpp +++ b/src/chip8.cpp @@ -31,7 +31,7 @@ uint8_t fontset[FONTSET_SIZE] = { 0xF0, 0x80, 0xF0, 0x80, 0x80 // F }; -Chip8::Chip8() { +Chip8::Chip8() : table(nullptr) { pc = ROM_START_ADDRESS; for (uint32_t i = 0; i < FONTSET_SIZE; i++) { @@ -44,79 +44,10 @@ Chip8::Chip8() { .count() ); randByte = std::uniform_int_distribution<uint8_t>(0, 255U); - - SetupTable(); -} - -void Chip8::SetupTable() { - for (size_t i = 0; i <= sizeof(table0) / sizeof(table0[0]); i++) { - table0[i] = &Chip8::OP_NULL; - table8[i] = &Chip8::OP_NULL; - tableE[i] = &Chip8::OP_NULL; - } - - for (size_t i = 0; i <= sizeof(tableF) / sizeof(tableF[0]); i++) { - tableF[i] = &Chip8::OP_NULL; - } - - table[0x0] = &Chip8::Table0; - table[0x1] = &Chip8::OP_1nnn; - table[0x2] = &Chip8::OP_2nnn; - table[0x3] = &Chip8::OP_3xkk; - table[0x4] = &Chip8::OP_4xkk; - table[0x5] = &Chip8::OP_5xy0; - table[0x6] = &Chip8::OP_6xkk; - table[0x7] = &Chip8::OP_7xkk; - table[0x8] = &Chip8::Table8; - table[0x9] = &Chip8::OP_9xy0; - table[0xA] = &Chip8::OP_Annn; - table[0xB] = &Chip8::OP_Bnnn; - table[0xC] = &Chip8::OP_Cxkk; - table[0xD] = &Chip8::OP_Dxyn; - table[0xE] = &Chip8::TableE; - table[0xF] = &Chip8::TableF; - - table0[0x0] = &Chip8::OP_00E0; - table0[0xE] = &Chip8::OP_00EE; - - table8[0x0] = &Chip8::OP_8xy0; - table8[0x1] = &Chip8::OP_8xy1; - table8[0x2] = &Chip8::OP_8xy2; - table8[0x3] = &Chip8::OP_8xy3; - table8[0x4] = &Chip8::OP_8xy4; - table8[0x5] = &Chip8::OP_8xy5; - table8[0x6] = &Chip8::OP_8xy6; - table8[0x7] = &Chip8::OP_8xy7; - table8[0xE] = &Chip8::OP_8xyE; - - tableE[0x1] = &Chip8::OP_ExA1; - tableE[0xE] = &Chip8::OP_Ex9E; - - tableF[0x07] = &Chip8::OP_Fx07; - tableF[0x0A] = &Chip8::OP_Fx0A; - tableF[0x15] = &Chip8::OP_Fx15; - tableF[0x18] = &Chip8::OP_Fx18; - tableF[0x1E] = &Chip8::OP_Fx1E; - tableF[0x29] = &Chip8::OP_Fx29; - tableF[0x33] = &Chip8::OP_Fx33; - tableF[0x55] = &Chip8::OP_Fx55; - tableF[0x65] = &Chip8::OP_Fx65; -} - -void Chip8::Table0() { - ((*this).*(table0[opcode & 0x000Fu]))(); -} - -void Chip8::Table8() { - ((*this).*(table8[opcode & 0x000Fu]))(); -} - -void Chip8::TableE() { - ((*this).*(tableE[opcode & 0x000Fu]))(); } -void Chip8::TableF() { - ((*this).*(tableF[opcode & 0x00FFu]))(); +void Chip8::SetTable(Table* opTable) { + table = opTable; } void Chip8::Cycle() { @@ -125,7 +56,7 @@ void Chip8::Cycle() { pc += 2; // Decode and Execute - ((*this).*(table[(opcode & 0xF000u) >> 12u]))(); + table->table[(opcode & 0xF000u) >> 12u](); } void Chip8::TimerUpdateThread(Platform* platform) { diff --git a/src/chip8.hpp b/src/chip8.hpp index 01db296..ccb3489 100644 --- a/src/chip8.hpp +++ b/src/chip8.hpp @@ -1,5 +1,5 @@ -#ifndef CHIP8_H_ -#define CHIP8_H_ +#ifndef CHIP8_HPP_ +#define CHIP8_HPP_ #include <cstdint> #include <random> @@ -7,6 +7,9 @@ #include <mutex> #include "platform.hpp" +#include "table.hpp" + +class Table; const uint32_t VIDEO_HEIGHT = 32; const uint32_t VIDEO_WIDTH = 64; @@ -27,6 +30,7 @@ class Chip8 { uint8_t delayTimer {}; uint8_t soundTimer {}; + public: uint16_t opcode; public: @@ -38,27 +42,15 @@ class Chip8 { std::uniform_int_distribution<uint8_t> randByte; std::mutex timerMutex; + Table* table; + public: + void SetTable(Table* table); void LoadROM(const char* filename); void Cycle(); void TimerUpdateThread(Platform* platform); - private: - void SetupTable(); - - private: - typedef void (Chip8::*OpcodeFunction)(); - OpcodeFunction table[0x10u]; - OpcodeFunction table0[0xFu]; - OpcodeFunction table8[0xFu]; - OpcodeFunction tableE[0xFu]; - OpcodeFunction tableF[0x65u]; - - void Table0(); - void Table8(); - void TableE(); - void TableF(); - + public: void OP_NULL(); void OP_00E0(); void OP_00EE(); void OP_1nnn(); void OP_2nnn(); void OP_3xkk(); void OP_4xkk(); void OP_5xy0(); void OP_6xkk(); diff --git a/src/main.cpp b/src/main.cpp index 9d6e277..94663d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,8 +17,10 @@ int main(int argc, char** argv) { int cycleDelay = std::stoi(argv[2]); char const* filename = argv[3]; - Chip8 chip8; - chip8.LoadROM(filename); + Chip8* chip8 = new Chip8(); + Table* table = new Table(chip8); + + chip8->SetTable(table); Platform platform( WINDOW_TITLE, @@ -27,16 +29,18 @@ int main(int argc, char** argv) { VIDEO_WIDTH, VIDEO_HEIGHT ); - std::thread timerThread(&Chip8::TimerUpdateThread, &chip8, &platform); + std::thread timerThread(&Chip8::TimerUpdateThread, chip8, &platform); + + chip8->LoadROM(filename); bool quit = false; while (!quit) { - quit = platform.ProcessInput(&chip8.keypad); + quit = platform.ProcessInput(&chip8->keypad); usleep(cycleDelay * 1000); - chip8.Cycle(); - platform.Update(chip8.video, videoScale); + chip8->Cycle(); + platform.Update(chip8->video, videoScale); } return 0; diff --git a/src/table.cpp b/src/table.cpp new file mode 100644 index 0000000..f39dd7e --- /dev/null +++ b/src/table.cpp @@ -0,0 +1,76 @@ +#include "table.hpp" +#include <iostream> + +Table::Table(Chip8* chip8) : chip8(chip8) { + for (size_t i = 0; i < sizeof(table0) / sizeof(table0[0]); i++) { + table0[i] = [chip8] { chip8->OP_NULL(); }; + table8[i] = [chip8] { chip8->OP_NULL(); }; + tableE[i] = [chip8] { chip8->OP_NULL(); }; + } + + for (size_t i = 0; i < sizeof(tableF) / sizeof(tableF[0]); i++) { + tableF[i] = [chip8] { chip8->OP_NULL(); }; + } + + table[0x0] = [this] { Table0(); }; + table[0x1] = [chip8] { chip8->OP_1nnn(); }; + table[0x2] = [chip8] { chip8->OP_2nnn(); }; + table[0x3] = [chip8] { chip8->OP_3xkk(); }; + table[0x4] = [chip8] { chip8->OP_4xkk(); }; + table[0x5] = [chip8] { chip8->OP_5xy0(); }; + table[0x6] = [chip8] { chip8->OP_6xkk(); }; + table[0x7] = [chip8] { chip8->OP_7xkk(); }; + + table[0x8] = [this] { Table8(); }; + + table[0x9] = [chip8] { chip8->OP_9xy0(); }; + table[0xA] = [chip8] { chip8->OP_Annn(); }; + table[0xB] = [chip8] { chip8->OP_Bnnn(); }; + table[0xC] = [chip8] { chip8->OP_Cxkk(); }; + table[0xD] = [chip8] { chip8->OP_Dxyn(); }; + + table[0xE] = [this] { TableE(); }; + table[0xF] = [this] { TableF(); }; + + table0[0x0] = [chip8] { chip8->OP_00E0(); }; + table0[0xE] = [chip8] { chip8->OP_00EE(); }; + + table8[0x0] = [chip8] { chip8->OP_8xy0(); }; + table8[0x1] = [chip8] { chip8->OP_8xy1(); }; + table8[0x2] = [chip8] { chip8->OP_8xy2(); }; + table8[0x3] = [chip8] { chip8->OP_8xy3(); }; + table8[0x4] = [chip8] { chip8->OP_8xy4(); }; + table8[0x5] = [chip8] { chip8->OP_8xy5(); }; + table8[0x6] = [chip8] { chip8->OP_8xy6(); }; + table8[0x7] = [chip8] { chip8->OP_8xy7(); }; + table8[0xE] = [chip8] { chip8->OP_8xyE(); }; + + tableE[0x1] = [chip8] { chip8->OP_ExA1(); }; + tableE[0xE] = [chip8] { chip8->OP_Ex9E(); }; + + tableF[0x07] = [chip8] { chip8->OP_Fx07(); }; + tableF[0x0A] = [chip8] { chip8->OP_Fx0A(); }; + tableF[0x15] = [chip8] { chip8->OP_Fx15(); }; + tableF[0x18] = [chip8] { chip8->OP_Fx18(); }; + tableF[0x1E] = [chip8] { chip8->OP_Fx1E(); }; + tableF[0x29] = [chip8] { chip8->OP_Fx29(); }; + tableF[0x33] = [chip8] { chip8->OP_Fx33(); }; + tableF[0x55] = [chip8] { chip8->OP_Fx55(); }; + tableF[0x65] = [chip8] { chip8->OP_Fx65(); }; +} + +void Table::Table0() { + table0[chip8->opcode & 0x000Fu](); +} + +void Table::Table8() { + table8[chip8->opcode & 0x000Fu](); +} + +void Table::TableE() { + tableE[chip8->opcode & 0x000Fu](); +} + +void Table::TableF() { + tableF[chip8->opcode & 0x00FFu](); +} diff --git a/src/table.hpp b/src/table.hpp new file mode 100644 index 0000000..f1eaeb7 --- /dev/null +++ b/src/table.hpp @@ -0,0 +1,31 @@ +#ifndef TABLE_HPP_ +#define TABLE_HPP_ + +#include <functional> + +#include "chip8.hpp" + +class Chip8; + +class Table { + public: + Table(Chip8* chip8); + + private: + Chip8* chip8; + + public: + using OpcodeFunction = std::function<void()>; + OpcodeFunction table[0x10u]; + OpcodeFunction table0[0xFu]; + OpcodeFunction table8[0xFu]; + OpcodeFunction tableE[0xFu]; + OpcodeFunction tableF[0x66u]; + + void Table0(); + void Table8(); + void TableE(); + void TableF(); +}; + +#endif |