Hello I'm writing an emulator for Game Boy.
And I'm struggling with SUB intruction
SUB a, 0x92
given a = 0x90
.
What I am doing is :
0x90 + (-0x92)
I use 2 complement method for substraction.
-0x92 <=>
2_complement(0x92) <=>
2_complement(10010010) <=>
01101101 + 1 <=>
01101110
So the substraction is equivalent to the following addition:
1001 0000 (0x90)
+0110 1110 (-0x92)
---------
1111 1110 (0xFE)
During the process, there is no carry and no half carry so I don't set the flags. And I think it's a mistake because other emulators (like BGB does. Note that the result is correct, only the flags are incorrect.
So I suppose real processor doesn't use 2 complement method because there is no free way to retrieve carry and half-carry.
Still, can I use two complement for emulating the SUB instruction with flag handling, or should I rely on "classic" substraction logic ?