From 8de34f6091679b0cc7f7b21547cdabc9a503b24c Mon Sep 17 00:00:00 2001 From: Tiger Date: Sun, 10 Sep 2023 09:48:26 +0200 Subject: [PATCH] Implemented 0x3XKK --- include/chip8.h | 7 +++++++ include/instruction_handler.h | 5 +++++ src/chip8.cpp | 4 ++++ src/instruction_handler.cpp | 16 +++++++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/chip8.h b/include/chip8.h index cc5ac2e..4e7e126 100644 --- a/include/chip8.h +++ b/include/chip8.h @@ -80,5 +80,12 @@ public: * @return */ [[nodiscard]] unsigned short getProgramCounter() const; + + /** + * Gets a value from the register (V) + * @param i The index + * @return + */ + unsigned short getRegister(unsigned char i); }; #endif //CHIP8_CHIP8_H diff --git a/include/instruction_handler.h b/include/instruction_handler.h index 30f4545..af82804 100644 --- a/include/instruction_handler.h +++ b/include/instruction_handler.h @@ -34,6 +34,11 @@ private: * Handles Chip8 instruction 0x2000 */ static void handle2000(Chip8&, Instruction); + + /** + * Handles Chip8 instruction 0x3000 + */ + static void handle3000(Chip8&, Instruction); public: InstructionHandler(); diff --git a/src/chip8.cpp b/src/chip8.cpp index 03ff5db..3560831 100644 --- a/src/chip8.cpp +++ b/src/chip8.cpp @@ -65,3 +65,7 @@ void Chip8::setProgramCounter(unsigned short value) { unsigned short Chip8::getProgramCounter() const { return this->pc; } + +unsigned short Chip8::getRegister(unsigned char reg) { + return this->v[reg]; +} diff --git a/src/instruction_handler.cpp b/src/instruction_handler.cpp index ffa736c..4a27605 100644 --- a/src/instruction_handler.cpp +++ b/src/instruction_handler.cpp @@ -4,6 +4,8 @@ InstructionHandler::InstructionHandler() { this->handlers[0x0000] = handle0000; this->handlers[0x1000] = handle1000; + this->handlers[0x2000] = handle2000; + this->handlers[0x3000] = handle3000; } /** @@ -58,7 +60,7 @@ void InstructionHandler::handle1000(Chip8& chip8, Instruction instruction) { /** * 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. + * This will increments the stack pointer and puts the program counter on top of the stack. * * @param chip8 The Chip8 instance * @param instruction The Instruction instance @@ -68,3 +70,15 @@ void InstructionHandler::handle2000(Chip8& chip8, Instruction instruction) { chip8.setProgramCounter(instruction.nnn()); } +/** + * Handles the 0x3XKK Chip8 instruction which is used to skip the next instruction if V[x] == kk. + * This will compare V[x] to kk and increases the program counter by 2 if they are equal. + * + * @param chip8 The Chip8 instance + * @param instruction The Instruction instance + */ +void InstructionHandler::handle3000(Chip8& chip8, Instruction instruction) { + if (chip8.getRegister(instruction.x()) == instruction.kk()) { + chip8.setProgramCounter(chip8.getProgramCounter() + 2); + } +} \ No newline at end of file