diff --git a/src/py65/devices/mpu6502.py b/src/py65/devices/mpu6502.py index ab613e1..b2bb306 100644 --- a/src/py65/devices/mpu6502.py +++ b/src/py65/devices/mpu6502.py @@ -375,7 +375,7 @@ class MPU: else: self.p |= self.NEGATIVE data +=100 - self.a = convert_to_bcd(data) + self.a = convert_to_bcd(data) # throws exception if result is over 99 decimal else: if self.p & self.CARRY: borrow = 0 diff --git a/src/py65/tests/devices/test_mpu6502.py b/src/py65/tests/devices/test_mpu6502.py index ba54bab..c31bc34 100644 --- a/src/py65/tests/devices/test_mpu6502.py +++ b/src/py65/tests/devices/test_mpu6502.py @@ -333,6 +333,20 @@ class Common6502Tests: self.assertEquals(mpu.OVERFLOW, mpu.p & mpu.OVERFLOW) self.assertEquals(0, mpu.p & mpu.ZERO) + def test_adc_bcd_on_immediate_79_plus_00_carry_set(self): + mpu = self._make_mpu() + mpu.p |= mpu.DECIMAL + mpu.p |= mpu.CARRY + mpu.a = 0x79 + self._write(mpu.memory, 0x0000, (0x69, 0x00)) #=> $0000 ADC #$00 + mpu.step() + self.assertEquals(0x0002, mpu.pc) + self.assertEquals(0x80, mpu.a) + self.assertEquals(mpu.NEGATIVE, mpu.p & mpu.NEGATIVE) + self.assertEquals(mpu.OVERFLOW, mpu.p & mpu.OVERFLOW) + self.assertEquals(0, mpu.p & mpu.ZERO) + self.assertEquals(0, mpu.p & mpu.CARRY) + # ADC Absolute, X-Indexed def test_adc_bcd_off_abs_x_carry_clear_in_accumulator_zeroes(self): @@ -4084,6 +4098,34 @@ class Common6502Tests: self.assertEquals(0, mpu.p & mpu.ZERO) self.assertEquals(mpu.CARRY, mpu.CARRY) + def test_sbc_bcd_on_immediate_0a_minus_00_carry_set(self): + mpu = self._make_mpu() + mpu.p |= mpu.DECIMAL + mpu.p |= mpu.CARRY + mpu.a = 0x0a + self._write(mpu.memory, 0x0000, (0xe9, 0x00)) #=> $0000 SBC #$00 + mpu.step() + self.assertEquals(0x0002, mpu.pc) + self.assertEquals(0x0a, mpu.a) + self.assertEquals(0, mpu.p & mpu.NEGATIVE) + self.assertEquals(0, mpu.p & mpu.OVERFLOW) + self.assertEquals(0, mpu.p & mpu.ZERO) + self.assertEquals(mpu.CARRY, mpu.p & mpu.CARRY) + + def test_sbc_bcd_on_immediate_9a_minus_00_carry_set(self): + mpu = self._make_mpu() + mpu.p |= mpu.DECIMAL + mpu.p |= mpu.CARRY + mpu.a = 0x9a + self._write(mpu.memory, 0x0000, (0xe9, 0x00)) #=> $0000 SBC #$00 + mpu.step() + self.assertEquals(0x0002, mpu.pc) + self.assertEquals(0x9a, mpu.a) + self.assertEquals(mpu.NEGATIVE, mpu.p & mpu.NEGATIVE) + self.assertEquals(0, mpu.p & mpu.OVERFLOW) + self.assertEquals(0, mpu.p & mpu.ZERO) + self.assertEquals(mpu.CARRY, mpu.p & mpu.CARRY) + # SBC Absolute, X-Indexed def test_sbc_abs_x_all_zeros_and_no_borrow_is_zero(self):