Fix DAA instruction

This commit is contained in:
Franco Colmenarez 2021-10-19 09:22:23 -05:00
parent f1a035d5ac
commit 3f46ab4a54
2 changed files with 14 additions and 15 deletions

View File

@ -25,7 +25,7 @@ impl Console {
self.cpu.run(&mut self.bus); self.cpu.run(&mut self.bus);
// thread::sleep(time::Duration::from_millis(100)); // thread::sleep(time::Duration::from_millis(100));
exit = self.cpu.get_exec_calls_count() > 1258895; exit = self.cpu.get_exec_calls_count() >= 1258895;
} }
} }
} }

View File

@ -698,26 +698,25 @@ impl CPU {
Opcode::DAA => { Opcode::DAA => {
self.registers.increment(Register::PC, 1); self.registers.increment(Register::PC, 1);
let mut val = self.registers.get_8bit(Register::A); let mut val = self.registers.get_8bit(Register::A);
// self.registers.set_flag(FlagRegister::Carry, false);
self.registers.set_flag(FlagRegister::HalfCarry, false); if !self.registers.get_flag(FlagRegister::Substract) {
if self.registers.get_flag(FlagRegister::Substract) {
if self.registers.get_flag(FlagRegister::Carry) {
val = val.wrapping_add(0x60);
}
if self.registers.get_flag(FlagRegister::HalfCarry) {
val = val.wrapping_add(0x06);
}
} else {
if self.registers.get_flag(FlagRegister::Carry) || val > 0x99 { if self.registers.get_flag(FlagRegister::Carry) || val > 0x99 {
val = val.wrapping_add(0x60); val = val.wrapping_add(0x60);
self.registers.set_flag(FlagRegister::Carry, true); self.registers.set_flag(FlagRegister::Carry, true);
} }
if self.registers.get_flag(FlagRegister::HalfCarry) || (val & 0x0F) > 0x09 { if self.registers.get_flag(FlagRegister::HalfCarry) || ((val & 0x0F) > 0x09) {
val = val.wrapping_add(0x06); val = val.wrapping_add(0x6);
}
} else {
if self.registers.get_flag(FlagRegister::Carry) {
val = val.wrapping_sub(0x60);
}
if self.registers.get_flag(FlagRegister::HalfCarry) {
val = val.wrapping_sub(0x6);
} }
} }
// println!("{:02X} {:02X}", self.registers.get_8bit(Register::A), val);
self.registers.set(Register::A, val as u16); self.registers.set(Register::A, val as u16);
self.registers.set_flag(FlagRegister::HalfCarry, false);
self.registers.set_flag(FlagRegister::Zero, val == 0); self.registers.set_flag(FlagRegister::Zero, val == 0);
}, },
// Jump to address // Jump to address
@ -2846,11 +2845,11 @@ mod tests {
cpu.registers.set_flag(FlagRegister::HalfCarry, true); cpu.registers.set_flag(FlagRegister::HalfCarry, true);
cpu.registers.set_flag(FlagRegister::Carry, false); cpu.registers.set_flag(FlagRegister::Carry, false);
cpu.exec(Opcode::DAA, &mut bus); cpu.exec(Opcode::DAA, &mut bus);
assert_eq!(cpu.registers.get(Register::A), 0x06);
assert_eq!(cpu.registers.get_flag(FlagRegister::Zero), false); assert_eq!(cpu.registers.get_flag(FlagRegister::Zero), false);
assert_eq!(cpu.registers.get_flag(FlagRegister::Substract), false); assert_eq!(cpu.registers.get_flag(FlagRegister::Substract), false);
assert_eq!(cpu.registers.get_flag(FlagRegister::HalfCarry), false); assert_eq!(cpu.registers.get_flag(FlagRegister::HalfCarry), false);
assert_eq!(cpu.registers.get_flag(FlagRegister::Carry), false); assert_eq!(cpu.registers.get_flag(FlagRegister::Carry), false);
assert_eq!(cpu.registers.get(Register::A), 0x06);
assert_eq!(cpu.registers.get(Register::PC), 0x101); assert_eq!(cpu.registers.get(Register::PC), 0x101);
} }