I've been working on an emulator for the MOS 6502, but I just can't seem to get ADC and SBC working right. I'm testing my emulator with the AllSuiteA program loaded at 0x4000 in emulated memory, and for test09, my current ADC and SBC implementations just aren't getting the right flags. I've tried changing their algorithms countless times, but every time, the carry flag and overflow flag are just enough off to matter, and cause the test to branch/not branch.
Both of my functions are based off this.
memory[0x10000] is the Accumulator. It's stored outside of the memory range so I can have a separate addressing switch statement.
This is one of my implementations of these functions:
case "ADC":
var t = memory[0x10000] + memory[address] + getFlag(flag_carry);
(memory[0x10000] & 0x80) != (t & 0x80) ? setFlag(flag_overflow) : clearFlag(flag_overflow);
signCalc(memory[0x10000]);
zeroCalc(t);
t > 255 ? setFlag(flag_carry) : clearFlag(flag_carry);
memory[0x10000] = t & 0xFF;
break;
case "SBC":
var t = memory[0x10000] - memory[address] - (!getFlag(flag_carry));
(t > 127 || t < -128) ? setFlag(flag_overflow) : clearFlag(flag_overflow);
t >= 0 ? setFlag(flag_carry) : clearFlag(flag_carry);
signCalc(t);
zeroCalc(t);
memory[0x10000] = t & 0xFF;
break;
I'm all out of ideas at this point, but I did also run into the same problem with the data offered here. So it isn't just one implementation plan failing me here.