|
|
|
@ -9,6 +9,7 @@ InstructionHandler::InstructionHandler() {
|
|
|
|
|
this->handlers[0x4000] = handle4000;
|
|
|
|
|
this->handlers[0x5000] = handle5000;
|
|
|
|
|
this->handlers[0x6000] = handle6000;
|
|
|
|
|
this->handlers[0x7000] = handle7000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -121,3 +122,90 @@ void InstructionHandler::handle5000(Chip8& chip8, Instruction instruction) {
|
|
|
|
|
void InstructionHandler::handle6000(Chip8& chip8, Instruction instruction) {
|
|
|
|
|
chip8.setRegister(instruction.x(), instruction.kk());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles the 0x7XKK Chip8 instruction which is used to add the value KK to register V[x] and set the result in register V[x].
|
|
|
|
|
*
|
|
|
|
|
* @param chip8 The Chip8 instance
|
|
|
|
|
* @param instruction The Instruction instance
|
|
|
|
|
*/
|
|
|
|
|
void InstructionHandler::handle7000(Chip8& chip8, Instruction instruction) {
|
|
|
|
|
chip8.setRegister(instruction.x(), chip8.getRegister(instruction.x()) + instruction.kk());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles the 0x8XY* opcodes (0x8XY0 to 0x8XY7 + 0x8XYE).
|
|
|
|
|
* The following opcodes are handled:
|
|
|
|
|
* - 0x8XY0 - Stores the value of register V[y] into register V[x]
|
|
|
|
|
* - 0x8XY1 - Stores the bitwise OR result of register V[x] and register V[y] into register V[x]
|
|
|
|
|
* - 0x8XY2 - Stores the bitwise AND result of register V[x] and register V[y] into register V[x]
|
|
|
|
|
* - 0x8XY3 - Stores the bitwise XOR result of register V[x] and register V[y] into register V[x]
|
|
|
|
|
* - 0x8XY4 - Sum of register V[y] and register V[x] is checked, if the result is greater than 8 bits, register V[F] is
|
|
|
|
|
* set to 1, otherwise 0. Only lowest 8 bits of the result are kept and stored into register V[x]
|
|
|
|
|
* - 0x8XY5 - If register V[x] is higher than register V[y], set register V[F] to 1, otherwise 0.
|
|
|
|
|
* Store the result of register V[x] - register V[y] into register V[x].
|
|
|
|
|
* - 0x8XY6 - If the least significant bit of register V[x] is 1, set register V[F] to 1, otherwise 0.
|
|
|
|
|
* Divide register V[x] by 2 (shift right with 1) and store the result in register V[x].
|
|
|
|
|
* - 0x8XY7 - If register V[y] is higher than register V[x], set register V[F] to 1, otherwise 0.
|
|
|
|
|
* Store the result of register V[y] - register V[x] into register V[x].
|
|
|
|
|
* - 0x8XYE - If the most significant bit of register V[x] is 1, set register V[F] to 1, otherwise 0.
|
|
|
|
|
* Multiply register V[x] by 2 (shift left with 1) and store the result in register V[x].
|
|
|
|
|
*
|
|
|
|
|
* @param chip8 The Chip8 instance
|
|
|
|
|
* @param instruction The Instruction instance
|
|
|
|
|
*/
|
|
|
|
|
void InstructionHandler::handle8000(Chip8& chip8, Instruction instruction) {
|
|
|
|
|
switch (instruction.n()) {
|
|
|
|
|
case 0: // 0x8XY0
|
|
|
|
|
chip8.setRegister(instruction.x(), chip8.getRegister(instruction.y()));
|
|
|
|
|
break;
|
|
|
|
|
case 1: // 0x8XY1
|
|
|
|
|
chip8.setRegister(instruction.x(), chip8.getRegister(instruction.x()) | chip8.getRegister(instruction.y()));
|
|
|
|
|
break;
|
|
|
|
|
case 2: // 0x8XY2
|
|
|
|
|
chip8.setRegister(instruction.x(), chip8.getRegister(instruction.x()) & chip8.getRegister(instruction.y()));
|
|
|
|
|
break;
|
|
|
|
|
case 3: // 0x8XY3
|
|
|
|
|
chip8.setRegister(instruction.x(), chip8.getRegister(instruction.x()) ^ chip8.getRegister(instruction.y()));
|
|
|
|
|
break;
|
|
|
|
|
case 4: // 0x8XY4
|
|
|
|
|
{
|
|
|
|
|
unsigned short sum = chip8.getRegister(instruction.y()) + chip8.getRegister(instruction.x());
|
|
|
|
|
chip8.setRegister(0xF, sum > 0xFF ? 1 : 0);
|
|
|
|
|
chip8.setRegister(instruction.x(), sum & 0xFF);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 5: // 0x8XY5
|
|
|
|
|
{
|
|
|
|
|
unsigned short regX = chip8.getRegister(instruction.x());
|
|
|
|
|
unsigned short regY = chip8.getRegister(instruction.y());
|
|
|
|
|
|
|
|
|
|
chip8.setRegister(0xF, regX > regY ? 1 : 0);
|
|
|
|
|
chip8.setRegister(instruction.x(), (regX - regY) & 0xFF);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 6: // 0x8XY6
|
|
|
|
|
{
|
|
|
|
|
unsigned short regX = chip8.getRegister(instruction.x());
|
|
|
|
|
chip8.setRegister(0xF, regX & 0x1);
|
|
|
|
|
chip8.setRegister(instruction.x(), regX >> 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 7: // 0x8XY7
|
|
|
|
|
{
|
|
|
|
|
unsigned short regX = chip8.getRegister(instruction.x());
|
|
|
|
|
unsigned short regY = chip8.getRegister(instruction.y());
|
|
|
|
|
|
|
|
|
|
chip8.setRegister(0xF, regY > regX ? 1 : 0);
|
|
|
|
|
chip8.setRegister(instruction.x(), (regY - regX) & 0xFF);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0xE: // 0x8XYE
|
|
|
|
|
{
|
|
|
|
|
unsigned short regX = chip8.getRegister(instruction.x());
|
|
|
|
|
chip8.setRegister(0xF, ((regX >> 7) & 0x1) == 1);
|
|
|
|
|
chip8.setRegister(instruction.x(), (regX << 1) & 0xFF);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|