It is currently Mon May 21, 2018 7:45 pm

 All times are UTC - 7 hours

 Page 1 of 1 [ 8 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: When does DEC set the half carry flag?Posted: Sat Aug 12, 2017 3:38 pm

Joined: Wed Jun 15, 2016 11:49 am
Posts: 69
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:

Quote:
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!

Top

 Post subject: Re: When does DEC set the half carry flag?Posted: Sat Aug 12, 2017 4:21 pm

Joined: Sat Aug 28, 2010 9:01 am
Posts: 219
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.

_________________
Gameboy Genius (Blog) - Gameboy development forum (+wiki and file area)

Top

 Post subject: Re: When does DEC set the half carry flag?Posted: Sat Aug 12, 2017 4:35 pm

Joined: Wed Jun 15, 2016 11:49 am
Posts: 69
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:

Quote:
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.

Top

 Post subject: Re: When does DEC set the half carry flag?Posted: Sat Aug 12, 2017 9:27 pm

Joined: Sat Aug 28, 2010 9:01 am
Posts: 219
Again, you're misunderstanding what carry means because you are rewriting a subtraction as an addition.

_________________
Gameboy Genius (Blog) - Gameboy development forum (+wiki and file area)

Top

 Post subject: Re: When does DEC set the half carry flag?Posted: Sun Aug 13, 2017 5:49 pm

Joined: Mon Nov 10, 2008 3:09 pm
Posts: 431
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.

Top

 Post subject: Re: When does DEC set the half carry flag?Posted: Mon Aug 14, 2017 1:29 pm

Joined: Wed Jun 15, 2016 11:49 am
Posts: 69
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.

Top

 Post subject: Re: When does DEC set the half carry flag?Posted: Tue Aug 15, 2017 12:35 pm

Joined: Wed Jun 15, 2016 11:49 am
Posts: 69
One more question. At the end of the BIOS right before it turns control over to the game, it does this:

Code:
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.

Top

 Post subject: Re: When does DEC set the half carry flag?Posted: Sun Sep 10, 2017 5:25 am

Joined: Tue May 17, 2016 10:15 pm
Posts: 68
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.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 8 posts ]

 All times are UTC - 7 hours