From cf4e31454b417b361b70fcbf82edf6a1daf62eb8 Mon Sep 17 00:00:00 2001 From: transistor Date: Sat, 13 May 2023 10:36:50 -0700 Subject: [PATCH] Fixed some Z80 tests related to the overflow flag --- emulator/cpus/z80/src/execute.rs | 25 ++++++++-------- tests/rad_tests/latest.txt | 50 ++++++++++++++++---------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/emulator/cpus/z80/src/execute.rs b/emulator/cpus/z80/src/execute.rs index 3c93f80..462b368 100644 --- a/emulator/cpus/z80/src/execute.rs +++ b/emulator/cpus/z80/src/execute.rs @@ -106,7 +106,7 @@ impl Z80 { let (result1, carry1, overflow1, half_carry1) = add_bytes(acc, self.get_flag(Flags::Carry) as u8); let (result2, carry2, overflow2, half_carry2) = add_bytes(result1, src); - self.set_arithmetic_op_flags(result2 as u16, Size::Byte, false, carry1 | carry2, overflow1 | overflow2, half_carry1 | half_carry2); + self.set_arithmetic_op_flags(result2 as u16, Size::Byte, false, carry1 | carry2, overflow1 ^ overflow2, half_carry1 | half_carry2); self.set_register_value(Register::A, result2); }, @@ -114,9 +114,9 @@ impl Z80 { let src = self.get_register_pair_value(src_pair); let dest = self.get_register_pair_value(dest_pair); - let (result1, carry1, overflow1, half_carry1) = add_words(dest, self.get_flag(Flags::Carry) as u16); - let (result2, carry2, overflow2, half_carry2) = add_words(result1, src); - self.set_arithmetic_op_flags(result2, Size::Word, false, carry1 | carry2, overflow1 | overflow2, half_carry1 | half_carry2); + let (result1, carry1, overflow1, half_carry1) = add_words(dest, src); + let (result2, carry2, overflow2, half_carry2) = add_words(result1, self.get_flag(Flags::Carry) as u16); + self.set_arithmetic_op_flags(result2, Size::Word, false, carry1 | carry2, overflow1 ^ overflow2, half_carry1 | half_carry2); self.set_register_pair_value(dest_pair, result2); }, @@ -484,9 +484,9 @@ impl Z80 { let src = self.get_target_value(target)?; let acc = self.get_register_value(Register::A); - let (result1, carry1, overflow1) = sub_bytes(acc, self.get_flag(Flags::Carry) as u8); - let (result2, carry2, overflow2) = sub_bytes(result1, src); - self.set_arithmetic_op_flags(result2 as u16, Size::Byte, true, carry1 | carry2, overflow1 | overflow2, (result2 & 0x10) != 0); + let (result1, carry1, overflow1) = sub_bytes(acc, src); + let (result2, carry2, overflow2) = sub_bytes(result1, self.get_flag(Flags::Carry) as u8); + self.set_arithmetic_op_flags(result2 as u16, Size::Byte, true, carry1 | carry2, overflow1 ^ overflow2, (result2 & 0x10) != 0); self.set_register_value(Register::A, result2); }, @@ -496,7 +496,7 @@ impl Z80 { let (result1, carry1, overflow1) = sub_words(dest, self.get_flag(Flags::Carry) as u16); let (result2, carry2, overflow2) = sub_words(result1, src); - self.set_arithmetic_op_flags(result2, Size::Word, true, carry1 | carry2, overflow1 | overflow2, (result2 & 0x10) != 0); + self.set_arithmetic_op_flags(result2, Size::Word, true, carry1 | carry2, overflow1 ^ overflow2, (result2 & 0x10) != 0); self.set_register_pair_value(dest_pair, result2); }, @@ -858,27 +858,28 @@ impl Z80 { fn add_bytes(operand1: u8, operand2: u8) -> (u8, bool, bool, bool) { let (result, carry) = operand1.overflowing_add(operand2); - let (_, overflow) = (operand1 as i8).overflowing_add(operand2 as i8); + let overflow = (operand1 & 0x80) == (operand2 & 0x80) && (operand1 & 0x80) != (result & 0x80); let half_carry = (operand1 & 0x10) != 0 && (result & 0x10) == 0; (result, carry, overflow, half_carry) } fn add_words(operand1: u16, operand2: u16) -> (u16, bool, bool, bool) { let (result, carry) = operand1.overflowing_add(operand2); - let (_, overflow) = ((operand1 as i8) as i16).overflowing_add((operand2 as i8) as i16); + let overflow = (operand1 & 0x8000) == (operand2 & 0x8000) && (operand1 & 0x8000) != (result & 0x8000); + // TODO this is probably still wrong let half_carry = (operand1 & 0x10) != 0 && (result & 0x10) == 0; (result, carry, overflow, half_carry) } fn sub_bytes(operand1: u8, operand2: u8) -> (u8, bool, bool) { let (result, carry) = operand1.overflowing_sub(operand2); - let (_, overflow) = (operand1 as i8).overflowing_sub(operand2 as i8); + let overflow = (operand1 & 0x80) != (operand2 & 0x80) && (operand1 & 0x80) != (result & 0x80); (result, carry, overflow) } fn sub_words(operand1: u16, operand2: u16) -> (u16, bool, bool) { let (result, carry) = operand1.overflowing_sub(operand2); - let (_, overflow) = ((operand1 as i8) as i16).overflowing_sub((operand2 as i8) as i16); + let overflow = (operand1 & 0x8000) != (operand2 & 0x8000) && (operand1 & 0x8000) != (result & 0x8000); (result, carry, overflow) } diff --git a/tests/rad_tests/latest.txt b/tests/rad_tests/latest.txt index 2257a33..cf56936 100644 --- a/tests/rad_tests/latest.txt +++ b/tests/rad_tests/latest.txt @@ -1,4 +1,4 @@ -Last run on 2023-05-12 at commit b2b9d73375d0ae7822cd3b1376b706f94b0b4456 +Last run on 2023-05-13 at commit d8638c6f82f45f6235784f96d8f5eb1a766bad52 00.json completed, all passed! 01.json completed, all passed! @@ -138,12 +138,12 @@ Last run on 2023-05-12 at commit b2b9d73375d0ae7822cd3b1376b706f94b0b4456 85.json completed, all passed! 86.json completed, all passed! 87.json completed, all passed! -88.json completed: 999 passed, 1 FAILED +88.json completed, all passed! 89.json completed, all passed! 8a.json completed, all passed! 8b.json completed, all passed! 8c.json completed, all passed! -8d.json completed: 999 passed, 1 FAILED +8d.json completed, all passed! 8e.json completed, all passed! 8f.json completed, all passed! 90.json completed, all passed! @@ -154,14 +154,14 @@ Last run on 2023-05-12 at commit b2b9d73375d0ae7822cd3b1376b706f94b0b4456 95.json completed, all passed! 96.json completed, all passed! 97.json completed, all passed! -98.json completed: 998 passed, 2 FAILED -99.json completed: 999 passed, 1 FAILED -9a.json completed: 998 passed, 2 FAILED -9b.json completed: 999 passed, 1 FAILED -9c.json completed: 999 passed, 1 FAILED -9d.json completed: 999 passed, 1 FAILED -9e.json completed: 998 passed, 2 FAILED -9f.json completed: 996 passed, 4 FAILED +98.json completed, all passed! +99.json completed, all passed! +9a.json completed, all passed! +9b.json completed, all passed! +9c.json completed, all passed! +9d.json completed, all passed! +9e.json completed, all passed! +9f.json completed, all passed! a0.json completed, all passed! a1.json completed, all passed! a2.json completed, all passed! @@ -455,7 +455,7 @@ cb fe.json completed, all passed! cb ff.json completed, all passed! cc.json completed, all passed! cd.json completed, all passed! -ce.json completed: 998 passed, 2 FAILED +ce.json completed, all passed! cf.json completed, all passed! d0.json completed, all passed! d1.json completed, all passed! @@ -489,7 +489,7 @@ dd 74.json completed, all passed! dd 75.json completed, all passed! dd 77.json completed, all passed! dd 7e.json completed, all passed! -dd 8e.json completed: 998 passed, 2 FAILED +dd 8e.json completed, all passed! dd 9e.json completed, all passed! dd ae.json completed, all passed! dd b6.json completed, all passed! @@ -506,7 +506,7 @@ dd e9.json completed, all passed! dd ee.json completed: 0 passed, 1000 FAILED dd f9.json completed, all passed! dd fe.json completed: 0 passed, 1000 FAILED -de.json completed: 998 passed, 2 FAILED +de.json completed, all passed! df.json completed, all passed! e0.json completed, all passed! e1.json completed, all passed! @@ -523,7 +523,7 @@ eb.json completed, all passed! ec.json completed, all passed! ed 40.json completed: 0 passed, 1000 FAILED ed 41.json completed: 0 passed, 1000 FAILED -ed 42.json completed: 765 passed, 235 FAILED +ed 42.json completed, all passed! ed 43.json completed, all passed! ed 44.json completed, all passed! ed 45.json completed: 0 passed, 1000 FAILED @@ -531,7 +531,7 @@ ed 46.json completed, all passed! ed 47.json completed, all passed! ed 48.json completed: 0 passed, 1000 FAILED ed 49.json completed: 0 passed, 1000 FAILED -ed 4a.json completed: 723 passed, 277 FAILED +ed 4a.json completed, all passed! ed 4b.json completed, all passed! ed 4c.json completed, all passed! ed 4d.json completed: 0 passed, 1000 FAILED @@ -539,7 +539,7 @@ ed 4e.json completed, all passed! ed 4f.json completed, all passed! ed 50.json completed: 0 passed, 1000 FAILED ed 51.json completed: 0 passed, 1000 FAILED -ed 52.json completed: 746 passed, 254 FAILED +ed 52.json completed, all passed! ed 53.json completed, all passed! ed 54.json completed, all passed! ed 55.json completed: 0 passed, 1000 FAILED @@ -547,7 +547,7 @@ ed 56.json completed, all passed! ed 57.json completed: 0 passed, 1000 FAILED ed 58.json completed: 0 passed, 1000 FAILED ed 59.json completed: 0 passed, 1000 FAILED -ed 5a.json completed: 743 passed, 257 FAILED +ed 5a.json completed, all passed! ed 5b.json completed, all passed! ed 5c.json completed, all passed! ed 5d.json completed: 0 passed, 1000 FAILED @@ -563,7 +563,7 @@ ed 66.json completed, all passed! ed 67.json completed: 0 passed, 1000 FAILED ed 68.json completed: 0 passed, 1000 FAILED ed 69.json completed: 0 passed, 1000 FAILED -ed 6a.json completed: 486 passed, 514 FAILED +ed 6a.json completed, all passed! ed 6b.json completed, all passed! ed 6c.json completed, all passed! ed 6d.json completed: 0 passed, 1000 FAILED @@ -571,7 +571,7 @@ ed 6e.json completed, all passed! ed 6f.json completed: 0 passed, 1000 FAILED ed 70.json completed: 0 passed, 1000 FAILED ed 71.json completed: 0 passed, 1000 FAILED -ed 72.json completed: 744 passed, 256 FAILED +ed 72.json completed, all passed! ed 73.json completed, all passed! ed 74.json completed, all passed! ed 75.json completed: 0 passed, 1000 FAILED @@ -579,7 +579,7 @@ ed 76.json completed, all passed! ed 77.json completed, all passed! ed 78.json completed: 0 passed, 1000 FAILED ed 79.json completed: 0 passed, 1000 FAILED -ed 7a.json completed: 760 passed, 240 FAILED +ed 7a.json completed, all passed! ed 7b.json completed, all passed! ed 7c.json completed, all passed! ed 7d.json completed: 0 passed, 1000 FAILED @@ -635,8 +635,8 @@ fd 74.json completed, all passed! fd 75.json completed, all passed! fd 77.json completed, all passed! fd 7e.json completed, all passed! -fd 8e.json completed: 999 passed, 1 FAILED -fd 9e.json completed: 999 passed, 1 FAILED +fd 8e.json completed, all passed! +fd 9e.json completed, all passed! fd ae.json completed, all passed! fd b6.json completed, all passed! fd be.json completed, all passed! @@ -655,5 +655,5 @@ fd fe.json completed: 0 passed, 1000 FAILED fe.json completed, all passed! ff.json completed, all passed! -passed: 543908, failed: 110092, total 83% -completed in 12m 50s +passed: 545965, failed: 108035, total 83% +completed in 10m 35s