Implementing CPX and CPY

This commit is contained in:
AnonymusRaccoon
2020-02-28 17:23:27 +01:00
parent 7b34473e63
commit 68b82c9cda
5 changed files with 133 additions and 1 deletions
+8
View File
@@ -351,6 +351,14 @@ namespace ComSquare::CPU
case Instructions::INX: this->INX(); return 2;
case Instructions::INY: this->INY(); return 2;
case Instructions::CPX_IM: this->CPX(this->_getImmediateAddrForX()); return 2 + !this->_registers.p.m;
case Instructions::CPX_ABS: this->CPX(this->_getAbsoluteAddr()); return 4 + !this->_registers.p.m;
case Instructions::CPX_DP: this->CPX(this->_getDirectAddr()); return 3 + !this->_registers.p.m + this->_registers.dl != 0;
case Instructions::CPY_IM: this->CPY(this->_getImmediateAddrForX()); return 2 + !this->_registers.p.m;
case Instructions::CPY_ABS: this->CPY(this->_getAbsoluteAddr()); return 4 + !this->_registers.p.m;
case Instructions::CPY_DP: this->CPY(this->_getDirectAddr()); return 3 + !this->_registers.p.m + this->_registers.dl != 0;
default:
throw InvalidOpcode("CPU", opcode);
}
+13 -1
View File
@@ -333,7 +333,15 @@ namespace ComSquare::CPU
TXS = 0x9A,
INX = 0xE8,
INY = 0xC8
INY = 0xC8,
CPX_IM = 0xE0,
CPX_ABS = 0xEC,
CPX_DP = 0xE4,
CPY_IM = 0xC0,
CPY_ABS = 0xCC,
CPY_DP = 0xC4
};
//! @brief The main CPU
@@ -497,6 +505,10 @@ namespace ComSquare::CPU
void INX();
//! @brief Increment the Y register
void INY();
//! @brief Compare the X register with the memory
void CPX(uint24_t valueAddr);
//! @brief Compare the Y register with the memory
void CPY(uint24_t valueAddr);
public:
explicit CPU(std::shared_ptr<Memory::MemoryBus> bus, Cartridge::Header &cartridgeHeader);
CPU(const CPU &) = default;
@@ -188,4 +188,42 @@ namespace ComSquare::CPU
this->_registers.p.z = this->_registers.y == 0;
this->_registers.p.n = this->_registers.y & negativeFlag;
}
void CPU::CPX(uint24_t valueAddr)
{
unsigned value = this->_bus->read(valueAddr++);
if (this->_registers.p.x_b) {
uint8_t x = this->_registers.x;
x -= value;
this->_registers.p.z = x == 0;
this->_registers.p.n = x & 0x80u;
} else {
value += this->_bus->read(valueAddr) << 8u;
uint16_t x = this->_registers.x;
x -= value;
this->_registers.p.z = x == 0;
this->_registers.p.n = x & 0x8000u;
}
this->_registers.p.c = this->_registers.x >= value;
}
void CPU::CPY(uint24_t valueAddr)
{
unsigned value = this->_bus->read(valueAddr++);
this->_registers.p.c = this->_registers.y >= value;
if (this->_registers.p.x_b) {
uint8_t y = this->_registers.y;
y -= value;
this->_registers.p.z = y == 0;
this->_registers.p.n = y & 0x80u;
} else {
value += this->_bus->read(valueAddr) << 8u;
uint16_t y = this->_registers.y;
y -= value;
this->_registers.p.z = y == 0;
this->_registers.p.n = y & 0x8000u;
}
}
}