Implemented 0x2NNN

main
Tiger 2023-09-10 09:42:00 +02:00
parent fce922f6c6
commit 3aacfdf537
4 changed files with 33 additions and 0 deletions

View File

@ -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

View File

@ -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();

View File

@ -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;
}

View File

@ -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());
}