From 666cd9cd99484f769b563218214433d37faa1d87 Mon Sep 17 00:00:00 2001 From: kris Date: Thu, 11 May 2017 22:43:08 +0100 Subject: [PATCH] Use the decimally adjusted aluresult to compute the value of flags. This fixes various bugs with ADC/SBC in decimal mode found by Klaus Dormann's test suite. --- py65/devices/mpu6502.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/py65/devices/mpu6502.py b/py65/devices/mpu6502.py index 62a0647..3ca4cef 100644 --- a/py65/devices/mpu6502.py +++ b/py65/devices/mpu6502.py @@ -315,11 +315,14 @@ class MPU: # the ALU outputs are not decimally adjusted nibble0 = nibble0 & 0xf nibble1 = nibble1 & 0xf - aluresult = (nibble1 << 4) + nibble0 # the final A contents will be decimally adjusted nibble0 = (nibble0 + adjust0) & 0xf nibble1 = (nibble1 + adjust1) & 0xf + + # Update result for use in setting flags below + aluresult = (nibble1 << 4) + nibble0 + self.p &= ~(self.CARRY | self.OVERFLOW | self.NEGATIVE | self.ZERO) if aluresult == 0: self.p |= self.ZERO @@ -420,6 +423,9 @@ class MPU: nibble0 = (aluresult + adjust0) & 0xf nibble1 = ((aluresult + adjust1) >> 4) & 0xf + # Update result for use in setting flags below + aluresult = (nibble1 << 4) + nibble0 + self.p &= ~(self.CARRY | self.ZERO | self.NEGATIVE | self.OVERFLOW) if aluresult == 0: self.p |= self.ZERO