1
0
mirror of https://github.com/mre/mos6502.git synced 2024-11-28 07:49:19 +00:00

get overflow calcs to look at boolean algebra instead of comparison to magic number

This commit is contained in:
Sam M W 2024-04-20 16:30:42 +01:00
parent 11499b6bc8
commit 5ecdae2d25

View File

@ -747,8 +747,7 @@ impl<M: Bus, V: Variant> CPU<M, V> {
|| (a_after == 0 && c_before == 0x01) || (a_after == 0 && c_before == 0x01)
|| (value == 0xff && c_before == 0x01); || (value == 0xff && c_before == 0x01);
let did_overflow = (a_before > 127 && value > 127 && a_after < 128) let did_overflow = (a_after ^ a_before) & (a_after ^ value) & 0x80 != 0;
|| (a_before < 128 && value < 128 && a_after > 127);
let mask = Status::PS_CARRY | Status::PS_OVERFLOW; let mask = Status::PS_CARRY | Status::PS_OVERFLOW;
@ -779,8 +778,7 @@ impl<M: Bus, V: Variant> CPU<M, V> {
|| (a_after == 0 && c_before == 0x01) || (a_after == 0 && c_before == 0x01)
|| (value == 0xff && c_before == 0x01); || (value == 0xff && c_before == 0x01);
let did_overflow = (a_before > 127 && value > 127 && a_after < 128) let did_overflow = (a_after ^ a_before) & (a_after ^ value) & 0x80 != 0;
|| (a_before < 128 && value < 128 && a_after > 127);
let mask = Status::PS_CARRY | Status::PS_OVERFLOW; let mask = Status::PS_CARRY | Status::PS_OVERFLOW;
@ -817,18 +815,7 @@ impl<M: Bus, V: Variant> CPU<M, V> {
let a_after = a_before.wrapping_sub(value).wrapping_sub(nc); let a_after = a_before.wrapping_sub(value).wrapping_sub(nc);
// The overflow flag is set on two's-complement overflow. let did_overflow = (a_after ^ a_before) & (a_after ^ value) & 0x80 == 0;
//
// range of A is -128 to 127
// range of - M - (1 - C) is -128 to 128
// -(127 + 1) to -(-128 + 0)
//
let over = (nc == 0 && value > 127) && a_before < 128 && a_after > 127;
let under =
(a_before > 127) && (0u8.wrapping_sub(value).wrapping_sub(nc) > 127) && a_after < 128;
let did_overflow = over || under;
let mask = Status::PS_CARRY | Status::PS_OVERFLOW; let mask = Status::PS_CARRY | Status::PS_OVERFLOW;
@ -863,18 +850,7 @@ impl<M: Bus, V: Variant> CPU<M, V> {
let a_after = a_before.wrapping_sub(value).wrapping_sub(nc); let a_after = a_before.wrapping_sub(value).wrapping_sub(nc);
// The overflow flag is set on two's-complement overflow. let did_overflow = (a_after ^ a_before) & (a_after ^ value) & 0x80 == 0;
//
// range of A is -128 to 127
// range of - M - (1 - C) is -128 to 128
// -(127 + 1) to -(-128 + 0)
//
let over = (nc == 0 && value > 127) && a_before < 128 && a_after > 127;
let under =
(a_before > 127) && (0u8.wrapping_sub(value).wrapping_sub(nc) > 127) && a_after < 128;
let did_overflow = over || under;
let mask = Status::PS_CARRY | Status::PS_OVERFLOW; let mask = Status::PS_CARRY | Status::PS_OVERFLOW;