Simplify half carry checking logic

jsmoo
Nick Krichevsky 2022-04-23 13:33:59 -04:00
parent dcf13061c6
commit 418a659e07
1 changed files with 4 additions and 3 deletions

View File

@ -28,15 +28,16 @@ impl CarryingAdd<u8, u16> for u16 {
// NOTE: These generics are not as generic as they could be. The Into<u16> is just a shortcut because we
// only use up to u16s
fn did_8bit_half_carry<L: Into<u16>, R: Into<u16>>(lhs: L, rhs: R) -> bool {
// https://robdor.com/2016/08/10/gameboy-emulator-half-carry-flag/
((lhs.into() & 0xf) + (rhs.into() & 0xf)) & 0x10 == 0x10
// https://stackoverflow.com/a/7261149/1103734
(lhs.into() & 0xf) + (rhs.into() & 0xf) > 0xf
}
/// `did_8_bit_full_carry` checks whether or not the given addition would have done an 8 bit carry.
// NOTE: These generics are not as generic as they could be. The Into<u16> is just a shortcut because we
// only use up to u16s
fn did_8bit_full_carry<L: Into<u16>, R: Into<u16>>(lhs: L, rhs: R) -> bool {
(((lhs.into() & 0xff) + (rhs.into() & 0xff)) & 0x100) == 0x100
// https://stackoverflow.com/a/7261149/1103734
((lhs.into() & 0xff) + (rhs.into() & 0xff)) > 0xff
}
#[cfg(test)]