Implemented 0x3XKK

main
Tiger 2023-09-10 09:48:26 +02:00
parent 3aacfdf537
commit 8de34f6091
4 changed files with 31 additions and 1 deletions

View File

@ -80,5 +80,12 @@ public:
* @return * @return
*/ */
[[nodiscard]] unsigned short getProgramCounter() const; [[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 #endif //CHIP8_CHIP8_H

View File

@ -34,6 +34,11 @@ private:
* Handles Chip8 instruction 0x2000 * Handles Chip8 instruction 0x2000
*/ */
static void handle2000(Chip8&, Instruction); static void handle2000(Chip8&, Instruction);
/**
* Handles Chip8 instruction 0x3000
*/
static void handle3000(Chip8&, Instruction);
public: public:
InstructionHandler(); InstructionHandler();

View File

@ -65,3 +65,7 @@ void Chip8::setProgramCounter(unsigned short value) {
unsigned short Chip8::getProgramCounter() const { unsigned short Chip8::getProgramCounter() const {
return this->pc; return this->pc;
} }
unsigned short Chip8::getRegister(unsigned char reg) {
return this->v[reg];
}

View File

@ -4,6 +4,8 @@
InstructionHandler::InstructionHandler() { InstructionHandler::InstructionHandler() {
this->handlers[0x0000] = handle0000; this->handlers[0x0000] = handle0000;
this->handlers[0x1000] = handle1000; 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. * 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 chip8 The Chip8 instance
* @param instruction The Instruction instance * @param instruction The Instruction instance
@ -68,3 +70,15 @@ void InstructionHandler::handle2000(Chip8& chip8, Instruction instruction) {
chip8.setProgramCounter(instruction.nnn()); 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);
}
}