diff --git a/src/processor/instructions/instructions.rs b/src/processor/instructions/instructions.rs index 8683d62..970ad71 100644 --- a/src/processor/instructions/instructions.rs +++ b/src/processor/instructions/instructions.rs @@ -20,25 +20,15 @@ impl CPU { } pub(crate) fn inc(&mut self, reg: Reg8) { - let result = self.reg.get_8(reg).wrapping_add(0x1); - self.clear_flag(Flags::NSubtract); - self.set_or_clear_flag(Flags::Zero, result == 0x0); - self.set_or_clear_flag( - Flags::HalfCarry, - (((self.reg.get_8(reg) & 0xF).wrapping_add(0x1 & 0xF)) & 0x10) == 0x10, - ); - self.reg.set_8(reg, result); + let result = self.reg.get_8(reg); + let val = self.inc_raw(result); + self.reg.set_8(reg, val); } pub(crate) fn dec(&mut self, reg: Reg8) { - let result = self.reg.get_8(reg).wrapping_sub(0x1); - self.set_flag(Flags::NSubtract); - self.set_or_clear_flag(Flags::Zero, result == 0x0); - self.set_or_clear_flag( - Flags::HalfCarry, - (((self.reg.get_8(reg) & 0xF).wrapping_sub(0x1 & 0xF)) & 0x10) == 0x10, - ); - self.reg.set_8(reg, result); + let result = self.reg.get_8(reg); + let val = self.dec_raw(result); + self.reg.set_8(reg, val); } pub(crate) fn inc_pair(&mut self, val: u16) -> u16 { diff --git a/src/processor/instructions/primitives.rs b/src/processor/instructions/primitives.rs index 4faf6cc..94809d9 100644 --- a/src/processor/instructions/primitives.rs +++ b/src/processor/instructions/primitives.rs @@ -79,7 +79,7 @@ impl CPU { } } - pub(crate) fn is_flag(&mut self, flag: Flags) -> bool { + pub(crate) fn is_flag(&self, flag: Flags) -> bool { get_bit(self.reg.af.get_low(), flag as u8) } @@ -167,4 +167,26 @@ impl CPU { ); return result; } + + pub(crate) fn dec_raw(&mut self, val: u8) -> u8 { + let result = val.wrapping_sub(0x1); + self.set_flag(Flags::NSubtract); + self.set_or_clear_flag(Flags::Zero, result == 0x0); + self.set_or_clear_flag( + Flags::HalfCarry, + (((val & 0xF).wrapping_sub(0x1 & 0xF)) & 0x10) == 0x10, + ); + result + } + + pub(crate) fn inc_raw(&mut self, val: u8) -> u8 { + let result = val.wrapping_add(0x1); + self.clear_flag(Flags::NSubtract); + self.set_or_clear_flag(Flags::Zero, result == 0x0); + self.set_or_clear_flag( + Flags::HalfCarry, + (((val & 0xF).wrapping_add(0x1 & 0xF)) & 0x10) == 0x10, + ); + result + } }