Quote:
why in an emulator, can we implement SBC(value) as SBC(value ^ 0xFF)...
I think tokumaru's post did answer your question.
The carry is traditionally clear before an addition and set before a subtraction. (By the 6502 program, not the NES or the emulator.)
Here's an adc function in C. (Apologies if it has poor syntax, C isn't really what I do)
Code:
void adc(uint8 value){
if(carry){
a = a + value + 1;
}else{
a = a + value;
}
}
Let's look at this 6502 code.
Code:
sec
sbc #$00
The result will be whatever was in A.
adc(0x00^0xFF)
The carry is SET before this is run, so this is the part of the function is used:
a = a + value + 1;
a = a + 0xFF + 1;
a = a;
Let's take a look at the carry being clear.
Code:
clc
sbc #$00
Because the carry is clear, the result will be whatever was in A minus 1.
adc(0x00^0xFF)
The carry is clear before this is run, so this part of the function is used:
a = a + value;
a = a + 0xFF;
0xFF = -1, so a = a-1 as expected.
Does this make sense?
Edit: To use your example:
45 Dec = 2D hex = 0010 1101 Bin XOR that with FF
-46 Dec = D2 Hex = 1101 0010 Bin
lda #$2D
sec
sbc #$2D
Result should be 0.
a = a + value + 1;
a = 0x2D + 0xD2 + 1
a = 0xFF + 1
a = 0.
lda #$2D
clc
sbc #$2D
Result should be -1 (0xFF).
a = a + value;
a = 0x2D + 0xD2
a = 0xFF