From a6066d1fdf00689922b6f8979ac81c1071640d3e Mon Sep 17 00:00:00 2001 From: John Date: Tue, 9 Jul 2024 01:05:21 -0500 Subject: [PATCH] Implement DAA --- src/cpu.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/cpu.rs b/src/cpu.rs index 912f0e5..a63536a 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -414,7 +414,26 @@ mod instructions { /// Funky instructions impl CPU { pub fn daa(&mut self) -> IResult { - Err(UnimplementedInsn(self.ir))? + let mut carry = false; + let mut adjusted = self[R8::A]; + let previous = adjusted.0; + let flags = self.flags(); + // ones digit: if there was a carry out, or the value *grew* too large + if flags.h() || (!flags.n() && previous & 0xf > 9) { + adjusted += 0x06; + } + // tens digit: if there was a carry out, or the value *grew* too large + if flags.c() || (!flags.n() && previous >> 4 > 9) { + adjusted += 0x60; + carry = true; + } + adjusted += previous; + self[R8::A] = adjusted; + + let flags = self.flags_mut(); + *flags = flags.with_z(adjusted.0 == 0).with_h(false).with_c(carry); + + Ok(()) } pub fn cpl(&mut self) -> IResult { self[R8::A] ^= 0xff;