Game Boy: What constitutes a “half-carry”?

后端 未结 3 502
南笙
南笙 2020-12-13 13:21

The Game Boy Z80 CPU has a half-carry flag, and I can\'t seem to find much information about when to set/clear it.

What I understand so far is that any 8-bit add, su

3条回答
  •  时光说笑
    2020-12-13 13:40

    It's the carry from bit 3 to bit 4, just like the normal carry flag records carry from bit 7. So, e.g. to get the half carry bit in an add:

    ((a&0xf) + (value&0xf))&0x10
    

    Which gives 0x10 if half carry should be set, 0 otherwise. Getting half carry from the other relevant ops follows naturally - the questions is whether there was carry from the low nibble to the high.

    To put things in perspective, the z80 has a 4bit ALU and performs 8bit ops by doing two 4bit ops. So it gets half carry very naturally, as an intermediate result.

    DAA is interested in the flag because if half carry is set then two digits that add up to more than 16 were added in the low nibble; that will have correctly produced carry into the upper nibble but will have left the low nibble 6 lower than it should be, since there were six more values between 10, when it should have generated carry, and 16, when it did.

提交回复
热议问题