Hey guys, I've got SBC working correctly, but I don't understand why.
I'm working off of the following document: http://teaching.idallen.com/dat2343/10f ... erflow.txt
Here's pseudocodehere's my implementation
I got this from another emulator, and it's working properly (comparing against nestest.log shows it's correct), but I don't understand why.
1. In the statement 'int16 diff = a - b - !cpu.C', I don't understand why we're subtracting from the opposite of the carry flag.
2. In the statement 'cpu.C = diff >= 0x00', I don't understand why checking if diff >= 0 is sufficient to test for underflow. I replaced it with "cpu.C = (b-!cpu.C) >= a" and it's not doing the right thing, even though that appears to be a restatement of the original.
What am I missing?
I'm working off of the following document: http://teaching.idallen.com/dat2343/10f ... erflow.txt
Here's pseudocodehere's my implementation
Code:
var a = cpu.A;
var b = mem.read(addr);
int16 diff = a - b - !cpu.C
cpu.A = (uint8)diff;
set_zs(cpu.A);
cpu.C = diff >= 0x00;
cpu.V = ((a^b)&0x80) != 0 && ((a^cpu.A)&0x80) != 0;
var b = mem.read(addr);
int16 diff = a - b - !cpu.C
cpu.A = (uint8)diff;
set_zs(cpu.A);
cpu.C = diff >= 0x00;
cpu.V = ((a^b)&0x80) != 0 && ((a^cpu.A)&0x80) != 0;
I got this from another emulator, and it's working properly (comparing against nestest.log shows it's correct), but I don't understand why.
1. In the statement 'int16 diff = a - b - !cpu.C', I don't understand why we're subtracting from the opposite of the carry flag.
2. In the statement 'cpu.C = diff >= 0x00', I don't understand why checking if diff >= 0 is sufficient to test for underflow. I replaced it with "cpu.C = (b-!cpu.C) >= a" and it's not doing the right thing, even though that appears to be a restatement of the original.
What am I missing?