diff --git a/src/opcodes/arith.cpp b/src/opcodes/arith.cpp index 146bb39..8d21fa0 100644 --- a/src/opcodes/arith.cpp +++ b/src/opcodes/arith.cpp @@ -6,16 +6,16 @@ OPCODE(adc) { if (FLAG_IS_SET(cpu->status, STATUS_DECIMAL)) { v6502Internal::logWarning("ADC: decimal mode is not supported yet"); } - int16_t result = cpu->a + cpu->fetchByteWithMode(mode) + - FLAG_IS_SET(cpu->status, STATUS_CARRY); + uint8_t m = cpu->fetchByteWithMode(mode); + int16_t result = cpu->a + m + FLAG_IS_SET(cpu->status, STATUS_CARRY); cpu->clearFlags(STATUS_CARRY | STATUS_OVERFLOW); - if ((result & 0x80) == 0x80 && (cpu->a & 0x80) != 0x80) { - cpu->setFlags(STATUS_OVERFLOW); - } if (result > 0xff) { cpu->setFlags(STATUS_CARRY); } + if ((m ^ result) & (cpu->a ^ result) & 0x80) { + cpu->setFlags(STATUS_OVERFLOW); + } cpu->a = result & 0xFF; cpu->evaluateFlags(cpu->a); @@ -55,14 +55,14 @@ OPCODE(sbc) { if (FLAG_IS_SET(cpu->status, STATUS_DECIMAL)) { v6502Internal::logWarning("SBC: decimal mode is not supported yet"); } - int16_t result = cpu->a - cpu->fetchByteWithMode(mode) - - !FLAG_IS_SET(cpu->status, STATUS_CARRY); + uint8_t m = cpu->fetchByteWithMode(mode); + int16_t result = cpu->a - m - !FLAG_IS_SET(cpu->status, STATUS_CARRY); cpu->clearFlags(STATUS_CARRY | STATUS_OVERFLOW); if (result >= 0) { cpu->setFlags(STATUS_CARRY); } - if ((result & 0x80) != 0x80 && (cpu->a & 0x80) == 0x80) { + if ((~m ^ result) & (cpu->a ^ result) & 0x80) { cpu->setFlags(STATUS_OVERFLOW); } diff --git a/tests/instructions/arith/adc-1-7f.cpp b/tests/instructions/arith/adc-1-7f.cpp index b9def42..03a82bf 100644 --- a/tests/instructions/arith/adc-1-7f.cpp +++ b/tests/instructions/arith/adc-1-7f.cpp @@ -11,5 +11,8 @@ TEST { cpu->executeStep(); assert(cpu->a == 0x80); - STATUS_ASSERT(STATUS_NEGATIVE | STATUS_OVERFLOW); + assert(FLAG_IS_SET(cpu->status, STATUS_NEGATIVE)); + assert(FLAG_IS_SET(cpu->status, STATUS_OVERFLOW)); + assert(FLAG_IS_CLEAR(cpu->status, STATUS_ZERO)); + assert(FLAG_IS_CLEAR(cpu->status, STATUS_CARRY)); }