The SM83 CPU core used in Game Boy almost certainly has a 8-bit ALU, which means 16-bit ALU operations are actually composed of two 8-bit operations. Like a normal Z80 CPU, it also has a dedicated 16-bit increment/decrement/load unit, which can handle certain 16-bit operations quickly but can't update the flags. Basically:
- if flags are updated, a 16-bit operation definitely involves the ALU, so it actually uses two 8-bit ALU operations under the hood
- if flags are not updated, and the 16-bit operation is just +1 / -1 / load, it's done with the 16-bit incrementer unit
So, whenever you're dealing with flags, try to think in terms of 8-bit operations (low byte first, then the high byte) if you want to reason about the operation.
- How does the half-carry flag behave in opcode 0xE8?
As pointed out in the other answer, H is set when there's a carry from bit 3. (And C is set when there's a carry from bit 7).
Here's an interesting thought exercise: if SP=$FFFF
and you execute ADD SP, -1
, you get SP=$FFFE
and both H and C are set. Can you understand why?
Due to how signed numbers work, the low byte operation is in this case basically just a normal addition. -1
= $FF
, so it's calculating $FF
+ $FF
.
Hint above ↑
- How is the opcode 0xE8 implemented in the physical hardware?
We don't yet have a full understanding of it at the lowest possible level, but I know that there are two 8-bit operations. With my Game Boy testbench system I've confirmed that there's first an ALU operation that updates the flags (H, C) but not SP, then some other operation, and finally SP is updated atomically in one go. This suggests that ADD SP, e
might actually calculate the result into some temporary register (for example, a real Z80 has an invisible WZ temporary register for some ALU operations) in two separate 8-bit operations, and then load SP from it.
I think ADD HL, BC
is a bit more interesting example...with my testbench I've confirmed that it updates L first and then H, and flags are updated twice. This means that it literally executes something like
ADD L, C
ADC H, B
The latter 8-bit operation updates the flags, so we never see the resulting flags of ADD L, C
. But the half-carry flag might be temporarily set if there's a carry from L bit 3!
- Which is right, that half-carry occurs from bit 7 to bit 8 or that half-carry occurs from bit 11 to bit 12 (in the case of 16-bit instructions)?
It depends on the instruction, but the flags are always updated based on the same bit positions if you think in terms of 8-bit values...it just varies whether we're talking about the high or low byte of the 16-bit value. Bit 11 is just bit 3 of the high byte.
ADD SP, e
: H from bit 3, C from bit 7 (flags from low byte op)
LD HL, SP+e
: H from bit 3, C from bit 7 (flags from low byte op)
ADD HL, rr
: H from bit 11, C from bit 15 (flags from high byte op)
INC rr
: no flag updates (executed by the 16-bit inc/dec unit)
DEC rr
: no flag updates (executed by the 16-bit inc/dec unit)
ADD SP,n
. Makes the instruction smaller (saves you a byte), faster (saves you a fetch), while sufficient for its typical use case (push/pop a stack frame). Why are you interested in its effect on the half-carry flag? I would expect that flag to be relevant only forDAA
. – PropaedeuticDAA
instruction. However, I wish to make my emulator as accurate as possible (within reason, obviously), so I would very much like to implement this instruction the way it actually works in hardware. – Memorialistadd reg16, sign_extended_imm8
is obviously good for code density and not weird at all. Most ISAs with registers wider than 8-bit have immediates narrower than operand-size, and only a few (like x86) have encodings with full-width immediates (likeadd eax, imm32
as an option vs.add r/m32, imm8
). IMO the question would be better without that sidetrack. – Willowwillowy