diff --git a/include/chip8.h b/include/chip8.h index 4d7a3fa..a652459 100644 --- a/include/chip8.h +++ b/include/chip8.h @@ -2,22 +2,26 @@ #define CHIP8_CHIP8_H #include +#include +#include "display.h" class InstructionHandler; -class Display; class Chip8 { private: unsigned char memory[0xFFF] = {}; unsigned char v[0x10] = {}; + std::stack stack{}; unsigned short pc; - Display* display; + Display display; InstructionHandler* instructionHandler; public: Chip8(); ~Chip8(); - void loadRom(const std::string& file); - [[nodiscard]] Display* getDisplay() const; + void loadRom(const std::string&); + [[nodiscard]] Display getDisplay() const; void emulateCycle(); + [[nodiscard]] unsigned short popFromStack(); + void setProgramCounter(unsigned short); }; #endif //CHIP8_CHIP8_H diff --git a/include/display.h b/include/display.h index 50213d2..6d58105 100644 --- a/include/display.h +++ b/include/display.h @@ -5,14 +5,13 @@ #define STATE_OFF 0 #include -#include "chip8.h" class Display { private: bool flagRedraw = false; bool display[64 * 32] = {}; public: - void draw(SDL_Renderer*, const Chip8&); + void draw(SDL_Renderer*); [[nodiscard]] bool needsRedraw() const; void clear(); }; diff --git a/include/instruction_handler.h b/include/instruction_handler.h index 3956f8a..2f36423 100644 --- a/include/instruction_handler.h +++ b/include/instruction_handler.h @@ -7,11 +7,11 @@ class InstructionHandler { private: - typedef void (*Handler)(const Chip8&, Instruction); + typedef void (*Handler)(Chip8&, Instruction); std::map handlers = {}; - static void handle0000(const Chip8&, Instruction); + static void handle0000(Chip8&, Instruction); public: InstructionHandler(); - void handleInstruction(unsigned short, const Chip8&, Instruction); + void handleInstruction(unsigned short, Chip8&, Instruction); }; #endif //CHIP8_INSTRUCTION_HANDLER_H diff --git a/src/chip8.cpp b/src/chip8.cpp index 7b3123d..96fc306 100644 --- a/src/chip8.cpp +++ b/src/chip8.cpp @@ -4,14 +4,12 @@ #include "../include/chip8.h" #include "../include/instruction.h" #include "../include/instruction_handler.h" -#include "../include/display.h" Chip8::Chip8() : instructionHandler(), display() { this->pc = 0x200; } Chip8::~Chip8() { - delete display; delete instructionHandler; } @@ -26,11 +24,22 @@ void Chip8::loadRom(const std::string& file) { fclose(game); } -Display* Chip8::getDisplay() const { +Display Chip8::getDisplay() const { return this->display; } void Chip8::emulateCycle() { unsigned short opcode = (this->memory[this->pc] << 8 | this->memory[this->pc + 1]); + this->pc += 2; this->instructionHandler->handleInstruction(opcode & 0xF000, *this, Instruction(opcode)); } + +unsigned short Chip8::popFromStack() { + unsigned short value = this->stack.top(); + this->stack.pop(); + return value; +} + +void Chip8::setProgramCounter(unsigned short value) { + this->pc = value; +} diff --git a/src/display.cpp b/src/display.cpp index e44da3f..376d3bd 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -1,6 +1,6 @@ #include "../include/display.h" -void Display::draw(SDL_Renderer * renderer, const Chip8& chip8) { +void Display::draw(SDL_Renderer * renderer) { SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); diff --git a/src/instruction_handler.cpp b/src/instruction_handler.cpp index 2cf21f0..235b00b 100644 --- a/src/instruction_handler.cpp +++ b/src/instruction_handler.cpp @@ -6,7 +6,7 @@ InstructionHandler::InstructionHandler() { this->handlers[0x0000] = handle0000; } -void InstructionHandler::handleInstruction(unsigned short instructionNumber, const Chip8& chip8, Instruction instruction) { +void InstructionHandler::handleInstruction(unsigned short instructionNumber, Chip8& chip8, Instruction instruction) { if (!this->handlers.contains(instructionNumber)) { Logger::error("Unregistered instruction", instructionNumber); return; @@ -15,13 +15,13 @@ void InstructionHandler::handleInstruction(unsigned short instructionNumber, con this->handlers[instructionNumber](chip8, instruction); } -void InstructionHandler::handle0000(const Chip8& chip8, Instruction instruction) { +void InstructionHandler::handle0000(Chip8& chip8, Instruction instruction) { switch (instruction.kk()) { case 0x00E0: - chip8.getDisplay()->clear(); + chip8.getDisplay().clear(); break; case 0x00EE: - + chip8.setProgramCounter(chip8.popFromStack()); break; } }