Implement DAA

This commit is contained in:
John 2024-07-09 01:05:21 -05:00
parent 1a29412c85
commit a6066d1fdf

View File

@ -414,7 +414,26 @@ mod instructions {
/// Funky instructions /// Funky instructions
impl CPU { impl CPU {
pub fn daa(&mut self) -> IResult { 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 { pub fn cpl(&mut self) -> IResult {
self[R8::A] ^= 0xff; self[R8::A] ^= 0xff;