mirror of
https://github.com/pevans/erc-c.git
synced 2025-02-13 12:30:23 +00:00
XOR the orig and result bit 7 for overflow
We should have done this all along. We hadn't accounted for if orig >= 0x80 and result < 0x80, but at that point, it's kind of silly to have a long complicated condition that we can satisfy simply with an XOR. (Which is what the machine would do anyway.)
This commit is contained in:
parent
809e6ca7c6
commit
42c7fcbb47
@ -232,6 +232,8 @@ mos6502_set_status(mos6502 *cpu, vm_8bit status)
|
|||||||
void
|
void
|
||||||
mos6502_modify_status(mos6502 *cpu, vm_8bit status, int orig, int result)
|
mos6502_modify_status(mos6502 *cpu, vm_8bit status, int orig, int result)
|
||||||
{
|
{
|
||||||
|
int bit7o, bit7r;
|
||||||
|
|
||||||
if (status & MOS_NEGATIVE) {
|
if (status & MOS_NEGATIVE) {
|
||||||
cpu->P &= ~MOS_NEGATIVE;
|
cpu->P &= ~MOS_NEGATIVE;
|
||||||
if (result & 0x80) {
|
if (result & 0x80) {
|
||||||
@ -242,12 +244,15 @@ mos6502_modify_status(mos6502 *cpu, vm_8bit status, int orig, int result)
|
|||||||
if (status & MOS_OVERFLOW) {
|
if (status & MOS_OVERFLOW) {
|
||||||
cpu->P &= ~MOS_OVERFLOW;
|
cpu->P &= ~MOS_OVERFLOW;
|
||||||
|
|
||||||
|
bit7o = orig & 0x80;
|
||||||
|
bit7r = result & 0x80;
|
||||||
|
|
||||||
// If the result of the operation is such that the sign bit,
|
// If the result of the operation is such that the sign bit,
|
||||||
// that is to say bit 7, changes, then we have overflowed. E.g.:
|
// that is to say bit 7, changes, then we have overflowed. E.g.:
|
||||||
// 90 + 40 = 130, but that's actually -124 in two's complement.
|
// 90 + 40 = 130, but that's actually -124 in two's complement.
|
||||||
// So if you are paying attention to the sign, you have
|
// So if you are paying attention to the sign, you have
|
||||||
// overflowed from a positive into a negative result.
|
// overflowed from a positive into a negative result.
|
||||||
if (orig < 0x80 && result >= 0x80) {
|
if (bit7o ^ bit7r) {
|
||||||
cpu->P |= MOS_OVERFLOW;
|
cpu->P |= MOS_OVERFLOW;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user