From 3aacfdf53750db5bfd58a889ba5ba066a6ebb843 Mon Sep 17 00:00:00 2001 From: Tiger Date: Sun, 10 Sep 2023 09:42:00 +0200 Subject: [PATCH] Implemented 0x2NNN --- include/chip8.h | 8 ++++++++ include/instruction_handler.h | 5 +++++ src/chip8.cpp | 8 ++++++++ src/instruction_handler.cpp | 12 ++++++++++++ 4 files changed, 33 insertions(+) diff --git a/include/chip8.h b/include/chip8.h index 6fc6752..cc5ac2e 100644 --- a/include/chip8.h +++ b/include/chip8.h @@ -68,9 +68,17 @@ public: */ [[nodiscard]] unsigned short popFromStack(); + void pushToStack(unsigned short value); + /** * Sets the program counter (PC) to the given value */ void setProgramCounter(unsigned short value); + + /** + * Gets the program counter (PC) + * @return + */ + [[nodiscard]] unsigned short getProgramCounter() const; }; #endif //CHIP8_CHIP8_H diff --git a/include/instruction_handler.h b/include/instruction_handler.h index f79aabc..30f4545 100644 --- a/include/instruction_handler.h +++ b/include/instruction_handler.h @@ -29,6 +29,11 @@ private: * Handles Chip8 instruction 0x1000 */ static void handle1000(Chip8&, Instruction); + + /** + * Handles Chip8 instruction 0x2000 + */ + static void handle2000(Chip8&, Instruction); public: InstructionHandler(); diff --git a/src/chip8.cpp b/src/chip8.cpp index f35353f..03ff5db 100644 --- a/src/chip8.cpp +++ b/src/chip8.cpp @@ -54,6 +54,14 @@ unsigned short Chip8::popFromStack() { return value; } +void Chip8::pushToStack(unsigned short value) { + this->stack.push(value); +} + void Chip8::setProgramCounter(unsigned short value) { this->pc = value; } + +unsigned short Chip8::getProgramCounter() const { + return this->pc; +} diff --git a/src/instruction_handler.cpp b/src/instruction_handler.cpp index edf46a2..ffa736c 100644 --- a/src/instruction_handler.cpp +++ b/src/instruction_handler.cpp @@ -56,3 +56,15 @@ void InstructionHandler::handle1000(Chip8& chip8, Instruction instruction) { chip8.setProgramCounter(instruction.nnn()); } +/** + * Handles the 0x2NNN Chip8 instruction which is used to call a subroutine at NNN. + * This will increments the stack pointer and puts PC on top of the stack. + * + * @param chip8 The Chip8 instance + * @param instruction The Instruction instance + */ +void InstructionHandler::handle2000(Chip8& chip8, Instruction instruction) { + chip8.pushToStack(chip8.getProgramCounter()); + chip8.setProgramCounter(instruction.nnn()); +} +