GBZ80: How does LD HL (SP + e) ​​affect flags H and C?

In the Gameboy Z80, how exactly does the LD HL,(SP+e) operation LD HL,(SP+e) affect the flags H and C? (Half-carry + carry)

Link: http://www.devrs.com/gb/files/opcodes.html

+4
source share
2 answers

I understand that this is an old question, but I had a similar problem some time ago, and I would just like to add my solution, because there is absolutely no documentation or an open source emulator that does this correctly, as far as I know. Took me some actual debugging on a real gameboy to find a solution.

For both 16-bit SP + s8 (signed immediate) operations:

the carry flag is set if there is an overflow from the 7th to the 8th bit.

the half carry flag is set if the overflow is from third to fourth bit.

It was easier for me to execute the behavior separately for both positive and negative signed immediate (Lua):

 local D8 = self:Read(self.PC+1) local S8 = ((D8&127)-(D8&128)) local SP = self.SP + S8 if S8 >= 0 then self.Cf = ( (self.SP & 0xFF) + ( S8 ) ) > 0xFF self.Hf = ( (self.SP & 0xF) + ( S8 & 0xF ) ) > 0xF else self.Cf = (SP & 0xFF) <= (self.SP & 0xFF) self.Hf = (SP & 0xF) <= (self.SP & 0xF) end 
+10
source

As shown here: http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html The sum of SP + e affects the Half Carry and Carry flag, so you should check to see if it carries bits 3 to 4 and 7 to 8 (Starting at 0!)

0
source

Source: https://habr.com/ru/post/1341874/


All Articles