Skip to content

Commit

Permalink
did i just fix math
Browse files Browse the repository at this point in the history
  • Loading branch information
itsvic-dev committed May 3, 2024
1 parent af9d49f commit 3856f84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/opcodes/arith.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion tests/instructions/arith/adc-1-7f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

0 comments on commit 3856f84

Please sign in to comment.