From f9a95827f549df6614a1acbe397aa7a673fd9fe9 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Sun, 21 Jan 2018 12:47:36 -0600 Subject: [PATCH] Change carry flag strategy according to docs --- src/mos6502.arith.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/mos6502.arith.c b/src/mos6502.arith.c index 1323902..8874392 100644 --- a/src/mos6502.arith.c +++ b/src/mos6502.arith.c @@ -32,7 +32,14 @@ DEFINE_INST(adc) */ DEFINE_INST(cmp) { - mos6502_modify_status(cpu, MOS_NZC, cpu->A, cpu->A - oper); + mos6502_modify_status(cpu, MOS_NZ, cpu->A, cpu->A - oper); + + // Carry works slightly different with the cmp-style instructions; + // it's set if A >= oper. + cpu->P &= ~MOS_CARRY; + if (cpu->A >= oper) { + cpu->P |= MOS_CARRY; + } } /* @@ -41,7 +48,12 @@ DEFINE_INST(cmp) */ DEFINE_INST(cpx) { - mos6502_modify_status(cpu, MOS_NZC, cpu->X, cpu->X - oper); + mos6502_modify_status(cpu, MOS_NZ, cpu->X, cpu->X - oper); + + cpu->P &= ~MOS_CARRY; + if (cpu->X >= oper) { + cpu->P |= MOS_CARRY; + } } /* @@ -50,7 +62,12 @@ DEFINE_INST(cpx) */ DEFINE_INST(cpy) { - mos6502_modify_status(cpu, MOS_NZC, cpu->Y, cpu->Y - oper); + mos6502_modify_status(cpu, MOS_NZ, cpu->Y, cpu->Y - oper); + + cpu->P &= ~MOS_CARRY; + if (cpu->Y >= oper) { + cpu->P |= MOS_CARRY; + } } /*