nesdev.comhttp://forums.nesdev.com/ When does DEC set the half carry flag?http://forums.nesdev.com/viewtopic.php?f=20&t=16349 Page 1 of 1

 Author: Alyosha_TAS [ Sat Aug 12, 2017 3:38 pm ] Post subject: When does DEC set the half carry flag? 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!

 Author: nitro2k01 [ Sat Aug 12, 2017 4:21 pm ] Post subject: Re: When does DEC set the half carry flag? 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.

 Author: Alyosha_TAS [ Sat Aug 12, 2017 4:35 pm ] Post subject: Re: When does DEC set the half carry flag? 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.

 Author: nitro2k01 [ Sat Aug 12, 2017 9:27 pm ] Post subject: Re: When does DEC set the half carry flag? Again, you're misunderstanding what carry means because you are rewriting a subtraction as an addition.

 Author: AWJ [ Sun Aug 13, 2017 5:49 pm ] Post subject: Re: When does DEC set the half carry flag? 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.

 Author: Alyosha_TAS [ Mon Aug 14, 2017 1:29 pm ] Post subject: Re: When does DEC set the half carry flag? 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.

 Author: Alyosha_TAS [ Tue Aug 15, 2017 12:35 pm ] Post subject: Re: When does DEC set the half carry flag? 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:2344028800FA:  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:23440296That 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.

 Author: zerowalker [ Sun Sep 10, 2017 5:25 am ] Post subject: Re: When does DEC set the half carry flag? 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.

 Page 1 of 1 All times are UTC - 7 hours Powered by phpBB® Forum Software © phpBB Grouphttp://www.phpbb.com/