Added ASL, BCS, BIT, BMI, BPL, BRA

BRK, BVC, BVS, CPX, CPY, DEX, DEY
INX, INY, LDX, LDY, LSR, ORA, PEA, PEI
PER ROL, ROR, RTL, RTS, STA, STX, STY
TRB, TSB, XBA,
This commit is contained in:
scawful
2023-08-20 00:27:05 -04:00
parent 536136d8c9
commit 905f81d60e
3 changed files with 922 additions and 172 deletions

View File

@@ -86,12 +86,11 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
ADC(FetchByteDirectPage(PC));
break;
case 0x67: // ADC DP Indirect Long
operand = memory.ReadByte(DirectPageIndirectLong());
operand = memory.ReadWord(DirectPageIndirectLong());
ADC(operand);
break;
case 0x69: // ADC Immediate
operand = memory.ReadByte(Immediate());
ADC(operand);
ADC(Immediate());
break;
case 0x6D: // ADC Absolute
operand = memory.ReadWord(Absolute());
@@ -118,15 +117,14 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
ADC(operand);
break;
case 0x77: // ADC DP Indirect Long Indexed, Y
operand = memory.ReadByte(DirectPageIndirectLongIndexedY());
ADC(operand);
ADC(DirectPageIndirectLongIndexedY());
break;
case 0x79: // ADC Absolute Indexed, Y
operand = memory.ReadByte(AbsoluteIndexedY());
operand = memory.ReadWord(AbsoluteIndexedY());
ADC(operand);
break;
case 0x7D: // ADC Absolute Indexed, X
operand = memory.ReadByte(AbsoluteIndexedX());
operand = memory.ReadWord(AbsoluteIndexedX());
ADC(operand);
break;
case 0x7F: // ADC Absolute Long Indexed, X
@@ -151,7 +149,7 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
AND(operand);
break;
case 0x29: // AND Immediate
AND(Immediate());
AND(Immediate(), true);
break;
case 0x2D: // AND Absolute
AND(Absolute());
@@ -190,19 +188,23 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0x06: // ASL Direct Page
// ASL();
ASL(DirectPage());
break;
case 0x0A: // ASL Accumulator
// ASL();
A <<= 1;
A &= 0xFE;
SetCarryFlag(A & 0x80);
SetNegativeFlag(A);
SetZeroFlag(!A);
break;
case 0x0E: // ASL Absolute
// ASL();
ASL(Absolute());
break;
case 0x16: // ASL DP Indexed, X
// ASL();
ASL(DirectPageIndexedX());
break;
case 0x1E: // ASL Absolute Indexed, X
// ASL();
ASL(AbsoluteIndexedX());
break;
case 0x90: // BCC Branch if carry clear
@@ -211,7 +213,7 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0xB0: // BCS Branch if carry set
// BCS();
BCS(memory.ReadByte(PC));
break;
case 0xF0: // BEQ Branch if equal (zero set)
@@ -220,39 +222,39 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0x24: // BIT Direct Page
// BIT();
BIT(DirectPage());
break;
case 0x2C: // BIT Absolute
// BIT();
BIT(Absolute());
break;
case 0x34: // BIT DP Indexed, X
// BIT();
BIT(DirectPageIndexedX());
break;
case 0x3C: // BIT Absolute Indexed, X
// BIT();
BIT(AbsoluteIndexedX());
break;
case 0x89: // BIT Immediate
// BIT();
BIT(Immediate());
break;
case 0x30: // BMI Branch if minus (negative set)
// BMI();
BMI(ReadByte(PC));
break;
case 0xD0: // BNE Branch if not equal (zero clear)
// BNE();
BNE(ReadByte(PC));
break;
case 0x10: // BPL Branch if plus (negative clear)
// BPL();
BPL(ReadByte(PC));
break;
case 0x80: // BRA Branch always
// BRA();
BRA(ReadByte(PC));
break;
case 0x00: // BRK Break
// BRK();
BRK();
break;
case 0x82: // BRL Branch always long
@@ -334,20 +336,20 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0xE0: // CPX Immediate
CPX(Immediate());
CPX(Immediate(), true);
break;
case 0xE4: // CPX Direct Page
// CPX();
CPX(DirectPage());
break;
case 0xEC: // CPX Absolute
CPX(Absolute());
break;
case 0xC0: // CPY Immediate
CPY(Immediate());
CPY(Immediate(), true);
break;
case 0xC4: // CPY Direct Page
// CPY();
CPY(DirectPage());
break;
case 0xCC: // CPY Absolute
CPY(Absolute());
@@ -424,19 +426,19 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0x1A: // INC Accumulator
// INC();
INC(A);
break;
case 0xE6: // INC Direct Page
// INC();
INC(DirectPage());
break;
case 0xEE: // INC Absolute
// INC();
INC(Absolute());
break;
case 0xF6: // INC DP Indexed, X
// INC();
INC(DirectPageIndexedX());
break;
case 0xFE: // INC Absolute Indexed, X
// INC();
INC(AbsoluteIndexedX());
break;
case 0xE8: // INX Increment X register
@@ -457,10 +459,10 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
JMP(AbsoluteIndirect());
break;
case 0x7C: // JMP Absolute Indexed Indirect, X
// JMP();
JMP(AbsoluteIndexedIndirect());
break;
case 0xDC: // JMP Absolute Indirect Long
// JMP();
JMP(AbsoluteIndirectLong());
break;
case 0x20: // JSR Absolute
@@ -472,53 +474,53 @@ void CPU::ExecuteInstruction(uint8_t opcode) {
break;
case 0xFC: // JSR Absolute Indexed Indirect, X
// JSR();
JSR(AbsoluteIndexedIndirect());
break;
case 0xA1: // LDA DP Indexed Indirect, X
// LDA();
LDA(DirectPageIndexedIndirectX());
break;
case 0xA3: // LDA Stack Relative
// LDA();
LDA(StackRelative());
break;
case 0xA5: // LDA Direct Page
// LDA();
LDA(DirectPage());
break;
case 0xA7: // LDA DP Indirect Long
// LDA();
LDA(DirectPageIndirectLong());
break;
case 0xA9: // LDA Immediate
LDA();
LDA(PC + 1, true);
break;
case 0xAD: // LDA Absolute
// LDA();
LDA(Absolute());
break;
case 0xAF: // LDA Absolute Long
// LDA();
LDA(AbsoluteLong());
break;
case 0xB1: // LDA DP Indirect Indexed, Y
// LDA();
LDA(DirectPageIndirectIndexedY());
break;
case 0xB2: // LDA DP Indirect
// LDA();
LDA(DirectPageIndirect());
break;
case 0xB3: // LDA SR Indirect Indexed, Y
// LDA();
LDA(StackRelativeIndirectIndexedY());
break;
case 0xB5: // LDA DP Indexed, X
// LDA();
LDA(DirectPageIndexedX());
break;
case 0xB7: // LDA DP Indirect Long Indexed, Y
// LDA();
LDA(DirectPageIndirectLongIndexedY());
break;
case 0xB9: // LDA Absolute Indexed, Y
// LDA();
LDA(DirectPageIndirectLongIndexedY());
break;
case 0xBD: // LDA Absolute Indexed, X
// LDA();
LDA(DirectPageIndirectLongIndexedY());
break;
case 0xBF: // LDA Absolute Long Indexed, X
// LDA();
LDA(DirectPageIndirectLongIndexedY());
break;
case 0xA2: // LDX Immediate
@@ -995,15 +997,15 @@ void CPU::ADC(uint8_t operand) {
}
}
void CPU::AND(uint16_t address) {
uint8_t operand;
void CPU::AND(uint16_t value, bool isImmediate) {
uint16_t operand;
if (E == 0) { // 16-bit mode
uint16_t operand16 = memory.ReadWord(address);
A &= operand16;
operand = isImmediate ? value : memory.ReadWord(value);
A &= operand;
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x8000);
} else { // 8-bit mode
operand = memory.ReadByte(address);
operand = isImmediate ? value : memory.ReadByte(value);
A &= operand;
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x80);
@@ -1018,6 +1020,39 @@ void CPU::ANDAbsoluteLong(uint32_t address) {
SetNegativeFlag(A & 0x80000000);
}
void CPU::SBC(uint16_t value, bool isImmediate) {
uint16_t operand;
if (!GetAccumulatorSize()) { // 16-bit mode
operand = isImmediate ? value : memory.ReadWord(value);
uint32_t result = A - operand - (GetCarryFlag() ? 0 : 1);
SetCarryFlag(!(result > 0xFFFF)); // Update the carry flag
// Update the overflow flag
bool overflow = ((A ^ operand) & (A ^ result) & 0x8000) != 0;
SetOverflowFlag(overflow);
// Update the accumulator
A = result & 0xFFFF;
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x8000);
} else { // 8-bit mode
operand = isImmediate ? value : memory.ReadByte(value);
uint16_t result = A - operand - (GetCarryFlag() ? 0 : 1);
SetCarryFlag(!(result > 0xFF)); // Update the carry flag
// Update the overflow flag
bool overflow = ((A ^ operand) & (A ^ result) & 0x80) != 0;
SetOverflowFlag(overflow);
// Update the accumulator
A = result & 0xFF;
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x80);
}
}
} // namespace emu
} // namespace app
} // namespace yaze

View File

@@ -196,7 +196,7 @@ class CPU : public Memory {
// register in bank zero.
//
// LDA [dp]
uint16_t DirectPageIndirectLong() {
uint32_t DirectPageIndirectLong() {
uint8_t dp = FetchByte();
uint16_t effective_address = D + dp;
return memory.ReadWordLong(effective_address);
@@ -227,8 +227,8 @@ class CPU : public Memory {
// LDA (dp), Y
uint16_t DirectPageIndirectLongIndexedY() {
uint8_t dp = FetchByte();
uint16_t effective_address = D + dp;
return memory.ReadWordLong(effective_address) + Y;
uint16_t effective_address = D + dp + Y;
return memory.ReadWordLong(effective_address);
}
// 8-bit data: Data Operand Byte
@@ -237,7 +237,13 @@ class CPU : public Memory {
// Data Low: First Operand Byte
//
// LDA #const
uint16_t Immediate() { return PC++; }
uint16_t Immediate() {
if (GetAccumulatorSize()) {
return FetchByte();
} else {
return FetchWord();
}
}
uint16_t StackRelative() {
uint8_t sr = FetchByte();
@@ -246,7 +252,7 @@ class CPU : public Memory {
uint16_t StackRelativeIndirectIndexedY() {
uint8_t sr = FetchByte();
return memory.ReadWord(SP() + sr) + Y;
return memory.ReadWord(SP() + sr + Y);
}
// ==========================================================================
@@ -254,10 +260,8 @@ class CPU : public Memory {
uint8_t A = 0; // Accumulator
uint8_t B = 0; // Accumulator (High)
uint8_t X = 0; // X index register
uint8_t X2 = 0; // X index register (High)
uint8_t Y = 0; // Y index register
uint8_t Y2 = 0; // Y index register (High)
uint16_t X = 0; // X index register
uint16_t Y = 0; // Y index register
uint16_t D = 0; // Direct Page register
uint16_t DB = 0; // Data Bank register
uint8_t PB = 0; // Program Bank register
@@ -280,6 +284,8 @@ class CPU : public Memory {
// Setting flags in the status register
int GetAccumulatorSize() const { return status & 0x20; }
int GetIndexSize() const { return status & 0x10; }
void SetAccumulatorSize(bool set) { SetFlag(0x20, set); }
void SetIndexSize(bool set) { SetFlag(0x10, set); }
// Set individual flags
void SetNegativeFlag(bool set) { SetFlag(0x80, set); }
@@ -301,16 +307,24 @@ class CPU : public Memory {
// ==========================================================================
// Instructions
/// ``` Unimplemented
// ADC: Add with carry
void ADC(uint8_t operand);
void ANDAbsoluteLong(uint32_t address);
// AND: Logical AND
void AND(uint16_t address);
void AND(uint16_t address, bool isImmediate = false);
// ASL: Arithmetic shift left ```
// ASL: Arithmetic shift left
void ASL(uint16_t address) {
uint8_t value = memory.ReadByte(address);
SetCarryFlag(!(value & 0x80)); // Set carry flag if bit 7 is set
value <<= 1; // Shift left
value &= 0xFE; // Clear bit 0
memory.WriteByte(address, value);
SetNegativeFlag(!value);
SetZeroFlag(value);
}
// BCC: Branch if carry clear
void BCC(int8_t offset) {
@@ -319,7 +333,12 @@ class CPU : public Memory {
}
}
// BCS: Branch if carry set ```
// BCS: Branch if carry set
void BCS(int8_t offset) {
if (GetCarryFlag()) { // If the carry flag is set
PC += offset; // Add the offset to the program counter
}
}
// BEQ: Branch if equal (zero set)
void BEQ(int8_t offset) {
@@ -328,20 +347,65 @@ class CPU : public Memory {
}
}
// BIT: Bit test ```
// BMI: Branch if minus (negative set) ```
// BNE: Branch if not equal (zero clear) ```
// BPL: Branch if plus (negative clear) ```
// BRA: Branch always ```
// BRK: Break ```
// BIT: Bit test
void BIT(uint16_t address) {
uint8_t value = memory.ReadByte(address);
SetNegativeFlag(value & 0x80);
SetOverflowFlag(value & 0x40);
SetZeroFlag((A & value) == 0);
}
// BMI: Branch if minus (negative set)
void BMI(int8_t offset) {
if (GetNegativeFlag()) { // If the negative flag is set
PC += offset; // Add the offset to the program counter
}
}
// BNE: Branch if not equal (zero clear)
void BNE(int8_t offset) {
if (!GetZeroFlag()) { // If the zero flag is clear
PC += offset; // Add the offset to the program counter
}
}
// BPL: Branch if plus (negative clear)
void BPL(int8_t offset) {
if (!GetNegativeFlag()) { // If the negative flag is clear
PC += offset; // Add the offset to the program counter
}
}
// BRA: Branch always
void BRA(int8_t offset) { PC += offset; }
// BRK: Break
void BRK() {
PC += 2; // Increment the program counter by 2
memory.PushWord(PC);
memory.PushByte(status);
SetInterruptFlag(true);
PC = memory.ReadWord(0xFFFE);
}
// BRL: Branch always long
void BRL(int16_t offset) {
PC += offset; // Add the offset to the program counter
}
// BVC: Branch if overflow clear ```
// BVS: Branch if overflow set ```
// BVC: Branch if overflow clear
void BVC(int8_t offset) {
if (!GetOverflowFlag()) { // If the overflow flag is clear
PC += offset; // Add the offset to the program counter
}
}
// BVS: Branch if overflow set
void BVS(int8_t offset) {
if (GetOverflowFlag()) { // If the overflow flag is set
PC += offset; // Add the offset to the program counter
}
}
// CLC: Clear carry flag
void CLC() { status &= ~0x01; }
@@ -359,16 +423,21 @@ class CPU : public Memory {
// COP: Coprocessor ```
// CPX: Compare X register
void CPX(uint16_t address) {
uint16_t memory_value =
E ? memory.ReadByte(address) : memory.ReadWord(address);
// CPX: Compare X register
void CPX(uint16_t value, bool isImmediate = false) {
uint16_t memory_value = isImmediate
? value
: (GetIndexSize() ? memory.ReadByte(value)
: memory.ReadWord(value));
compare(X, memory_value);
}
// CPY: Compare Y register
void CPY(uint16_t address) {
uint16_t memory_value =
E ? memory.ReadByte(address) : memory.ReadWord(address);
void CPY(uint16_t value, bool isImmediate = false) {
uint16_t memory_value = isImmediate
? value
: (GetIndexSize() ? memory.ReadByte(value)
: memory.ReadWord(value));
compare(Y, memory_value);
}
@@ -376,56 +445,73 @@ class CPU : public Memory {
// DEX: Decrement X register
void DEX() {
X--;
SetZeroFlag(X == 0);
SetNegativeFlag(X & 0x80);
if (GetIndexSize()) { // 8-bit
X = static_cast<uint8_t>(X - 1);
SetZeroFlag(X == 0);
SetNegativeFlag(X & 0x80);
} else { // 16-bit
X = static_cast<uint16_t>(X - 1);
SetZeroFlag(X == 0);
SetNegativeFlag(X & 0x8000);
}
}
// DEY: Decrement Y register
void DEY() {
Y--;
SetZeroFlag(Y == 0);
SetNegativeFlag(Y & 0x80);
if (GetIndexSize()) { // 8-bit
Y = static_cast<uint8_t>(Y - 1);
SetZeroFlag(Y == 0);
SetNegativeFlag(Y & 0x80);
} else { // 16-bit
Y = static_cast<uint16_t>(Y - 1);
SetZeroFlag(Y == 0);
SetNegativeFlag(Y & 0x8000);
}
}
// EOR: Exclusive OR ```
// INC: Increment
// TODO: Check if this is correct
void INC(uint16_t address) {
if (GetAccumulatorSize()) {
uint8_t value = ReadByte(address);
uint8_t value = memory.ReadByte(address);
value++;
if (value == static_cast<uint8_t>(0x100)) {
value = 0x00; // Wrap around in 8-bit mode
}
WriteByte(address, value);
memory.WriteByte(address, value);
SetNegativeFlag(value & 0x80);
SetZeroFlag(value == 0);
} else {
uint16_t value = ReadWord(address);
uint16_t value = memory.ReadWord(address);
value++;
if (value == static_cast<uint16_t>(0x10000)) {
value = 0x0000; // Wrap around in 16-bit mode
}
WriteByte(address, value);
SetNegativeFlag(value & 0x80);
memory.WriteWord(address, value);
SetNegativeFlag(value & 0x8000);
SetZeroFlag(value == 0);
}
}
// INX: Increment X register
void INX() {
X++;
SetNegativeFlag(X & 0x80);
SetZeroFlag(X == 0);
if (GetIndexSize()) { // 8-bit
X = static_cast<uint8_t>(X + 1);
SetZeroFlag(X == 0);
SetNegativeFlag(X & 0x80);
} else { // 16-bit
X = static_cast<uint16_t>(X + 1);
SetZeroFlag(X == 0);
SetNegativeFlag(X & 0x8000);
}
}
// INY: Increment Y register
void INY() {
Y++;
SetNegativeFlag(Y & 0x80);
SetZeroFlag(Y == 0);
if (GetIndexSize()) { // 8-bit
Y = static_cast<uint8_t>(Y + 1);
SetZeroFlag(Y == 0);
SetNegativeFlag(Y & 0x80);
} else { // 16-bit
Y = static_cast<uint16_t>(Y + 1);
SetZeroFlag(Y == 0);
SetNegativeFlag(Y & 0x8000);
}
}
// JMP: Jump
@@ -457,16 +543,54 @@ class CPU : public Memory {
}
// LDA: Load accumulator
void LDA() {
A = memory[PC];
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x80);
PC++;
void LDA(uint16_t address, bool isImmediate = false) {
if (GetAccumulatorSize()) {
A = isImmediate ? address : memory.ReadByte(address);
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x80);
} else {
A = isImmediate ? address : memory.ReadWord(address);
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x8000);
}
}
// LDX: Load X register
void LDX(uint16_t address, bool isImmediate = false) {
if (GetIndexSize()) {
X = isImmediate ? address : memory.ReadByte(address);
SetZeroFlag(X == 0);
SetNegativeFlag(X & 0x80);
} else {
X = isImmediate ? address : memory.ReadWord(address);
SetZeroFlag(X == 0);
SetNegativeFlag(X & 0x8000);
}
}
// LDY: Load Y register
void LDY(uint16_t address, bool isImmediate = false) {
if (GetIndexSize()) {
Y = isImmediate ? address : memory.ReadByte(address);
SetZeroFlag(Y == 0);
SetNegativeFlag(Y & 0x80);
} else {
Y = isImmediate ? address : memory.ReadWord(address);
SetZeroFlag(Y == 0);
SetNegativeFlag(Y & 0x8000);
}
}
// LSR: Logical shift right
void LSR(uint16_t address) {
uint8_t value = memory.ReadByte(address);
SetCarryFlag(value & 0x01);
value >>= 1;
memory.WriteByte(address, value);
SetNegativeFlag(false);
SetZeroFlag(value == 0);
}
// LDX: Load X register ```
// LDY: Load Y register ```
// LSR: Logical shift right ```
// MVN: Move negative ```
// MVP: Move positive ```
@@ -475,10 +599,36 @@ class CPU : public Memory {
// Do nothing
}
// ORA: Logical OR ```
// PEA: Push effective address ```
// PEI: Push effective indirect address ```
// PER: Push effective PC-relative address ```
// ORA: Logical OR
void ORA(uint16_t address, bool isImmediate = false) {
if (GetAccumulatorSize()) {
A |= isImmediate ? address : memory.ReadByte(address);
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x80);
} else {
A |= isImmediate ? address : memory.ReadWord(address);
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x8000);
}
}
// PEA: Push effective address
void PEA() {
uint16_t address = FetchWord();
memory.PushWord(address);
}
// PEI: Push effective indirect address
void PEI() {
uint16_t address = FetchWord();
memory.PushWord(memory.ReadWord(address));
}
// PER: Push effective PC-relative address
void PER() {
uint16_t address = FetchWord();
memory.PushWord(PC + address);
}
// PHA: Push Accumulator on Stack
void PHA() { memory.PushByte(A); }
@@ -546,12 +696,47 @@ class CPU : public Memory {
status &= ~byte;
}
// ROL: Rotate left ```
// ROR: Rotate right ```
// RTI: Return from interrupt ```
// RTL: Return from subroutine long ```
// RTS: Return from subroutine ```
// SBC: Subtract with carry ```
// ROL: Rotate left
void ROL(uint16_t address) {
uint8_t value = memory.ReadByte(address);
uint8_t carry = GetCarryFlag() ? 0x01 : 0x00;
SetCarryFlag(value & 0x80);
value <<= 1;
value |= carry;
memory.WriteByte(address, value);
SetNegativeFlag(value & 0x80);
SetZeroFlag(value == 0);
}
// ROR: Rotate right
void ROR(uint16_t address) {
uint8_t value = memory.ReadByte(address);
uint8_t carry = GetCarryFlag() ? 0x80 : 0x00;
SetCarryFlag(value & 0x01);
value >>= 1;
value |= carry;
memory.WriteByte(address, value);
SetNegativeFlag(value & 0x80);
SetZeroFlag(value == 0);
}
// RTI: Return from interrupt
void RTI() {
status = memory.PopByte();
PC = memory.PopWord();
}
// RTL: Return from subroutine long
void RTL() {
PC = memory.PopWord();
PB = memory.PopByte();
}
// RTS: Return from subroutine
void RTS() { PC = memory.PopWord() + 1; }
// SBC: Subtract with carry
void SBC(uint16_t operand, bool isImmediate = false);
// SEC: Set carry flag
void SEC() { status |= 0x01; }
@@ -569,11 +754,43 @@ class CPU : public Memory {
status |= byte;
}
// STA: Store accumulator ```
// STA: Store accumulator
void STA(uint16_t address) {
if (GetAccumulatorSize()) {
memory.WriteByte(address, static_cast<uint8_t>(A));
} else {
memory.WriteWord(address, A);
}
}
// STP: Stop the clock ```
// STX: Store X register ```
// STY: Store Y register ```
// STZ: Store zero ```
// STX: Store X register
void STX(uint16_t address) {
if (GetIndexSize()) {
memory.WriteByte(address, static_cast<uint8_t>(X));
} else {
memory.WriteWord(address, X);
}
}
// STY: Store Y register
void STY(uint16_t address) {
if (GetIndexSize()) {
memory.WriteByte(address, static_cast<uint8_t>(Y));
} else {
memory.WriteWord(address, Y);
}
}
// STZ: Store zero
void STZ(uint16_t address) {
if (GetAccumulatorSize()) {
memory.WriteByte(address, 0x00);
} else {
memory.WriteWord(address, 0x0000);
}
}
// TAX: Transfer accumulator to X
void TAX() {
@@ -606,8 +823,21 @@ class CPU : public Memory {
SetNegativeFlag(A & 0x80);
}
// TRB: Test and reset bits ```
// TSB: Test and set bits ```
// TRB: Test and reset bits
void TRB(uint16_t address) {
uint8_t value = memory.ReadByte(address);
SetZeroFlag((A & value) == 0);
value &= ~A;
memory.WriteByte(address, value);
}
// TSB: Test and set bits
void TSB(uint16_t address) {
uint8_t value = memory.ReadByte(address);
SetZeroFlag((A & value) == 0);
value |= A;
memory.WriteByte(address, value);
}
// TSC: Transfer stack pointer to accumulator
void TSC() {
@@ -655,7 +885,15 @@ class CPU : public Memory {
}
// WAI: Wait for interrupt ```
// XBA: Exchange B and A accumulator ```
// XBA: Exchange B and A accumulator
void XBA() {
uint8_t temp = A;
A = B;
B = temp;
SetZeroFlag(A == 0);
SetNegativeFlag(A & 0x80);
}
// XCE: Exchange Carry and Emulation Flags
void XCE() {
@@ -667,10 +905,20 @@ class CPU : public Memory {
private:
void compare(uint16_t register_value, uint16_t memory_value) {
uint16_t result = register_value - memory_value;
SetNegativeFlag(result & (E ? 0x8000 : 0x80)); // Negative flag
SetZeroFlag(result == 0); // Zero flag
SetCarryFlag(register_value >= 0); // Carry flag
uint16_t result;
if (GetIndexSize()) {
// 8-bit mode
uint8_t result8 = static_cast<uint8_t>(register_value) -
static_cast<uint8_t>(memory_value);
result = result8;
SetNegativeFlag(result & 0x80); // Negative flag for 8-bit
} else {
// 16-bit mode
result = register_value - memory_value;
SetNegativeFlag(result & 0x8000); // Negative flag for 16-bit
}
SetZeroFlag(result == 0); // Zero flag
SetCarryFlag(register_value >= memory_value); // Carry flag
}
// Helper function to set or clear a specific flag bit