Game Boy: Half-carry flag and 16-bit instructions (especially opcode 0xE8)

前端 未结 2 1794
感情败类
感情败类 2021-02-19 08:47

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

2条回答
  •  攒了一身酷
    2021-02-19 09:25

    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.

提交回复
热议问题