mirror of
https://github.com/pevans/erc-c.git
synced 2025-01-29 21:30:51 +00:00
Rewrite SBC logic
The carry handling was incorrect; carry is set if we went below zero, but bit 7 is not enough to determine that. It follows that negative handling needed to be modified accordingly. Overflow handling was also incorrect.
This commit is contained in:
parent
07a8a385f5
commit
0ccabda71e
@ -225,18 +225,30 @@ DEFINE_INST(sbc)
|
|||||||
|
|
||||||
MOS_CARRY_BIT();
|
MOS_CARRY_BIT();
|
||||||
|
|
||||||
vm_8bit result = cpu->A - oper - (carry ? 0 : 1);
|
int result32 = cpu->A - oper - (carry ? 0 : 1);
|
||||||
MOS_CHECK_NVZ(cpu->A, result);
|
vm_8bit result8 = cpu->A - oper - (carry ? 0 : 1);
|
||||||
|
|
||||||
|
MOS_CHECK_Z(result8);
|
||||||
|
|
||||||
// Carry is handled slightly differently in SBC; it's set if the
|
// Carry is handled slightly differently in SBC; it's set if the
|
||||||
// value is non-negative, and unset if negative. (It's essentially a
|
// value is non-negative, and unset if negative. (It's essentially a
|
||||||
// mirror of the N flag in that sense.)
|
// mirror of the N flag in that sense.)
|
||||||
cpu->P |= MOS_CARRY;
|
cpu->P |= MOS_CARRY;
|
||||||
if (result >= 0x80) {
|
cpu->P &= ~MOS_NEGATIVE;
|
||||||
|
|
||||||
|
if (result32 < 0) {
|
||||||
cpu->P &= ~MOS_CARRY;
|
cpu->P &= ~MOS_CARRY;
|
||||||
|
cpu->P |= MOS_NEGATIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
cpu->A = result;
|
cpu->P &= ~MOS_OVERFLOW;
|
||||||
|
if ((cpu->A & 0x80) != (oper & 0x80) &&
|
||||||
|
(cpu->A & 0x80) != (result8 & 0x80)
|
||||||
|
) {
|
||||||
|
cpu->P |= MOS_OVERFLOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
cpu->A = result8;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user