From 058b45e7d11a6cab554ce81de73022559bce39af Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Thu, 15 Feb 2018 19:32:01 -0600 Subject: [PATCH] Use 9-bit rotation, not 8-bit --- src/mos6502.bits.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/mos6502.bits.c b/src/mos6502.bits.c index 20efec5..5eca0bd 100644 --- a/src/mos6502.bits.c +++ b/src/mos6502.bits.c @@ -141,13 +141,19 @@ DEFINE_INST(rol) MOS_CARRY_BIT(); SET_RESULT(oper << 1); + // Rotations are effectively _9-bit_. So we aren't rotating bit 7 + // into bit 0; we're rotating bit 7 into the carry bit, and we're + // rotating the _previous value of the carry bit_ into bit 0. + if (cpu->P & MOS_CARRY) { + result |= 0x1; + } + if (oper & 0x80) { carry = 1; } cpu->P &= ~MOS_CARRY; if (carry) { - result |= 0x01; cpu->P |= MOS_CARRY; } @@ -169,13 +175,17 @@ DEFINE_INST(ror) MOS_CARRY_BIT(); SET_RESULT(oper >> 1); + // See the code for ROL for my note on 9-bit rotation (vs. 8-bit). + if (cpu->P & MOS_CARRY) { + result |= 0x80; + } + if (oper & 0x01) { carry = 1; } cpu->P &= ~MOS_CARRY; if (carry) { - result |= 0x80; cpu->P |= MOS_CARRY; }