Compare commits

..

No commits in common. "8df287832a408ed5e37385a3eee840e738884ad8" and "80053eeda720123123983068dc917e8fc7702f3a" have entirely different histories.

4 changed files with 4 additions and 102 deletions

View File

@ -86,7 +86,7 @@ public:
* @param i The index
* @return
*/
unsigned char getRegister(unsigned char i);
unsigned short getRegister(unsigned char i);
/**
* Sets a value in the register (V)
@ -94,6 +94,6 @@ public:
* @param i The index of the register to set
* @param value The value to be set
*/
void setRegister(unsigned char i, unsigned char value);
void setRegister(unsigned char i, unsigned short value);
};
#endif //CHIP8_CHIP8_H

View File

@ -54,16 +54,6 @@ private:
* Handles Chip8 instruction 0x6000
*/
static void handle6000(Chip8&, Instruction);
/**
* Handles Chip8 instruction 0x7000
*/
static void handle7000(Chip8&, Instruction);
/**
* Handles Chip8 instruction 0x8000
*/
static void handle8000(Chip8&, Instruction);
public:
InstructionHandler();

View File

@ -66,10 +66,10 @@ unsigned short Chip8::getProgramCounter() const {
return this->pc;
}
unsigned char Chip8::getRegister(unsigned char reg) {
unsigned short Chip8::getRegister(unsigned char reg) {
return this->v[reg];
}
void Chip8::setRegister(unsigned char reg, unsigned char value) {
void Chip8::setRegister(unsigned char reg, unsigned short value) {
this->v[reg] = value;
}

View File

@ -9,7 +9,6 @@ InstructionHandler::InstructionHandler() {
this->handlers[0x4000] = handle4000;
this->handlers[0x5000] = handle5000;
this->handlers[0x6000] = handle6000;
this->handlers[0x7000] = handle7000;
}
/**
@ -122,90 +121,3 @@ 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;
}
}
}