I'm debugging my 8080 emulator and I've gone the route of comparing it to the output of another tested emulator to see where I went wrong. When calculating the auxiliary carry flag, I used this algorithm for subtraction:
// a and b are uint8_t, cf_.ac is the auxiliary flag
a &= 0x0F; // lower nibble
b &= 0x0F; // lower nibble
cf_.ac = (a < b); // there must be borrow if minuend is less than subtrahend
On the other hand, the emulator I'm comparing with uses this algorithm:
// hf is the auxiliary flag, a and val are uint8_t
const uint16_t result = c->a - val;
c->hf = ~(c->a ^ result ^ val) & 0x10;
Our emulators are mismatching after this instruction:
Instructions Ran: 32
Program Counter: 022A
Memory Immediate: 0xFE
Instruction: CPI D8 0
Registers (B/C/D/E/H/L/A): 00 00 00 00 00 00 F5
Flags (Z/S/P/C/AC): 0 1 1 1 1
Stack Pointer: 7AD
Cycles: 305
where my auxiliary flag is not set (0xF5 - 0x00
shouldn't set the AF flag?) while theirs is set. I don't completely understand their algorithm so I am not able logically tell if it's wrong. Is my algorithm faulty or is theirs?