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

放肆的年华 提交于 2019-11-30 05:34:07

问题


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

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


回答1:


I realise this is an old question but I had a similar problem a while ago and would just like to add my solution as there is absolutely no documentation or open-source emulator that does it correctly to my knowledge. Took me some actual debugging on a real gameboy to find the solution.

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

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

the half carry flag is set if there's an overflow from the 3rd into the 4th bit.

I found it easier to do the behaviour separately for both a 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



回答2:


As seen here: http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html The sum of SP+e affects the Half Carry and the Carry flag, so you should check if there's carry from bit 3 to 4 and from 7 to 8 (Starting by 0!)



来源:https://stackoverflow.com/questions/5159603/gbz80-how-does-ld-hl-spe-affect-h-and-c-flags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!