When does DEC set the half carry flag?

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
Alyosha_TAS
Posts: 173
Joined: Wed Jun 15, 2016 11:49 am

When does DEC set the half carry flag?

Post by Alyosha_TAS »

Hi,

I am working on a GB emulator, but I am getting stuck trying to understand the half carry flag.

In the CPU manual here: http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf for the DEC instruction on page 89 it says that:
H - Set if no borrow from bit 4.
I understand this to mean that the half carry flag is set whenever (register & 0xF) > 0.
This makes sense to me since for example:

(4 - 1) = 0b0100 + 0b1111 = 0b(1)0011 -> (Half carry = 1)
(0 - 1) = 0b0000 + 0b1111 = 0b(0)1111 -> (Half carry = 0)

But, every other emulator I look at says the opposite, that in the first example the half carry flag is not set, and in the second example it is.

Where am I going wrong here? I don't see how it can be the other way around. If anyone can provide an explanation or example I would really appreciate it.

Thanks!
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: When does DEC set the half carry flag?

Post by nitro2k01 »

The problem with your example is that you're using addition, which inverts the relationship. Try to think about it as regular math instead.
4-1... Do you need to borrow to get 3? No. So H=0.
0-1... Do you need to borrow to get F? Yes. So H=1.

Or put differently, H=1 if and only if the upper nibble had to change as a result of the operation on the lower nibble. This general rule holds true for all arithmetic operations: inc, dec, add, sub.
Alyosha_TAS
Posts: 173
Joined: Wed Jun 15, 2016 11:49 am

Re: When does DEC set the half carry flag?

Post by Alyosha_TAS »

So it's just wrong documentation then? Are there any other pitfalls I should watch out for with that guide?

EDIT:
Given that the gameboy cpu is based off of the z80, I looked at the z80 insrtuction and indeed:
H is set if borrow from bit 4, otherwise, it is reset.
I guess it would be strange if this somehow changed for the gameboy cpu, but all the gameboy documents say the opposite 0_0. Strange stuff. :|
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: When does DEC set the half carry flag?

Post by nitro2k01 »

Again, you're misunderstanding what carry means because you are rewriting a subtraction as an addition.
AWJ
Posts: 433
Joined: Mon Nov 10, 2008 3:09 pm

Re: When does DEC set the half carry flag?

Post by AWJ »

In some architectures, like 6502, PowerPC and ARM, subtraction clears the carry flag if a borrow occurs and sets it if no borrow occurs. In other architectures like Intel, 6800 and 68000, the carry flag is set if a borrow occurs and cleared if no borrow occurs. The Game Boy CPU is Intel-derived and works the latter way.
Alyosha_TAS
Posts: 173
Joined: Wed Jun 15, 2016 11:49 am

Re: When does DEC set the half carry flag?

Post by Alyosha_TAS »

Thanks for the replies. Maybe GB CPU documentation could use a revision, since both sources I found that indicated what happens with the half carry flag say the opposite of what should actually happen.
Alyosha_TAS
Posts: 173
Joined: Wed Jun 15, 2016 11:49 am

Re: When does DEC set the half carry flag?

Post by Alyosha_TAS »

One more question. At the end of the BIOS right before it turns control over to the game, it does this:

Code: Select all

00F9:  86        ADD  A,(HL)         A:68 B:00 C:13 D:00 E:d8 F:c0 H:01 L:4d LY:91 SP:fffe  Cy:23440288
00FA:  20 FE     JR   NZ,00FAh       A:00 B:00 C:13 D:00 E:d8 F:90 H:01 L:4d LY:91 SP:fffe  Cy:23440296
That is what Gambatte does.
But shouldn't that ADD A,(HL) produce a half carry flag? F should then equal $B0 and not $90 right?

The value in $014D is $98, so it definitely seems like that 8 + 8 should produce a half carry.

EDIT: Hmmm, looking at the source code, it looks like maybe gambatte only calculates the half carry flag when it needs it, need to look into that.
zerowalker
Posts: 68
Joined: Tue May 17, 2016 10:15 pm

Re: When does DEC set the half carry flag?

Post by zerowalker »

Isn't the Half Carry flag set only if the value to subtract is higher than the source?

so, 3-4, 5-6 etc.
Cause it causes an overflow which makes it use the higher bits, thus bit 4 is used.
Post Reply