Like so many others, I am writing a Game Boy emulator and I have a couple of questions regarding the instruction 0xE8 (ADD SP, n
with an 8-bit immediate).
I
TL;DR: For ADD SP,n
, the H-flag is set when carry occurs from bit 3 to bit 4.
I decided to test this on real hardware, so I wrote a simple test ROM in GB-Z80 assembly that tests the following scenarios:
[SP = $000F]
ADD SP,$01
[SP = $00F0]
ADD SP,$10
[SP = $0FF0]
ADD SP,$10
For each case I store the value of register F
after the ADD
in memory, and I later display bit 5 (the H-flag) of each of those bytes on the screen.
I ran this on 3 different models (Gameboy Pocket, Gameboy Color, and Gameboy Advance SP), and got the following output on all 3 devices: 1 0 0
. So a carry from bit 3->4 caused H to be set, while a carry from 7->8 or 11->12 did not.
For ADD HL,rr
(where rr
is BC/DE/HL/SP
) it appears to be a different story. Based on my testing, H is set if carry occurs from bit 11 to bit 12.