Share (hopefully!) correct overflow implementations

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2018-10-14 21:19:03 +01:00
parent 707a742899
commit 0c07d39250

View File

@ -205,12 +205,16 @@ namespace EightBit {
void adjustBorrow(uint32_t datum) { clearFlag(CC(), CF, datum & Bit16); } // 16-bit subtraction void adjustBorrow(uint32_t datum) { clearFlag(CC(), CF, datum & Bit16); } // 16-bit subtraction
void adjustBorrow(register16_t datum) { adjustBorrow(datum.word); } void adjustBorrow(register16_t datum) { adjustBorrow(datum.word); }
void adjustOverflow(uint8_t before, uint8_t data, uint8_t after) { void adjustOverflow(uint8_t before, uint8_t data, register16_t after) {
setFlag(CC(), VF, ~(before ^ data) & (before ^ after) & Bit7); const uint8_t lowAfter = after.low;
const uint8_t highAfter = after.high;
setFlag(CC(), VF, (before ^ data ^ lowAfter ^ (highAfter << 7)) & Bit7);
} }
void adjustOverflow(uint16_t before, uint16_t data, uint16_t after) { void adjustOverflow(uint16_t before, uint16_t data, uint32_t after) {
setFlag(CC(), VF, ~(before ^ data) & (before ^ after) & Bit15); const uint16_t lowAfter = after & Mask16;
const uint16_t highAfter = after >> 16;
setFlag(CC(), VF, (before ^ data ^ lowAfter ^ (highAfter << 15)) & Bit15);
} }
void adjustOverflow(register16_t before, register16_t data, register16_t after) { void adjustOverflow(register16_t before, register16_t data, register16_t after) {
@ -225,7 +229,7 @@ namespace EightBit {
const auto result = after.low; const auto result = after.low;
adjustNZ(result); adjustNZ(result);
adjustCarry(after); adjustCarry(after);
adjustOverflow(before, data, result); adjustOverflow(before, data, after);
adjustHalfCarry(before, data, result); adjustHalfCarry(before, data, result);
} }
@ -233,7 +237,7 @@ namespace EightBit {
const register16_t result = after & Mask16; const register16_t result = after & Mask16;
adjustNZ(result); adjustNZ(result);
adjustCarry(after); adjustCarry(after);
adjustOverflow(before, data, result); adjustOverflow(before, data, after);
} }
void adjustAddition(register16_t before, register16_t data, uint32_t after) { void adjustAddition(register16_t before, register16_t data, uint32_t after) {
@ -244,14 +248,14 @@ namespace EightBit {
const auto result = after.low; const auto result = after.low;
adjustNZ(result); adjustNZ(result);
adjustCarry(after); adjustCarry(after);
setFlag(CC(), VF, (before ^ data ^ result ^ (after.high << 7)) & Bit7); adjustOverflow(before, data, after);
} }
void adjustSubtraction(uint16_t before, uint16_t data, uint32_t after) { void adjustSubtraction(uint16_t before, uint16_t data, uint32_t after) {
const register16_t result = after & Mask16; const register16_t result = after & Mask16;
adjustNZ(result); adjustNZ(result);
adjustCarry(after); adjustCarry(after);
adjustOverflow(before, data, result); adjustOverflow(before, data, after);
} }
void adjustSubtraction(register16_t before, register16_t data, uint32_t after) { void adjustSubtraction(register16_t before, register16_t data, uint32_t after) {