HALT Bug

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
DParrott
Posts: 25
Joined: Thu Jul 29, 2010 4:39 am

HALT Bug

Post by DParrott »

Hi all,

I'm hoping someone can give me a steer on implementing the HALT bug?

I have had a few attempts in my emulator at implementing this but based on the results of a halt_bug.gb test rom I found at https://github.com/retrio/gb-test-roms I cannot get it passing.

The closest I have got has the results reversed from those that appear to be expected in the test rom when I run it with BGB, I don't understand why I would have them the other way round and believe I must be missing a condition that triggers the bug.

I have the simplest implementation based on the IME flag, is this all that should trigger the bug or is it also based on interrupts being enabled in IE?

My current attempt just has a haltBug flag that is set to !IME when HALT is called, and in my step proc if haltBug is set then it reads and executes the opcode without incrementing PC.

The test output also raised another question, when reading the value of IE/IF are the unused bits returned as 1 or 0? The test in BGB appears to suggest that they return as 1 where as I have been storing IF ANDed with 0x1F and returning that so zero in the upper 3 bits. Should I be ORing with 0xE0 before reading?

Thanks
DParrott
Posts: 25
Joined: Thu Jul 29, 2010 4:39 am

Re: HALT Bug

Post by DParrott »

Forgive the self post but I thought I'd follow up.

I wonder is this another of those GameBoy issues where a few people have worked it out but it's not documented anywhere?

I have consulted some other emulators and the behaviour seems to be that if interrupts are disabled and there is a pending interrupt when the HALT instruction executes then the bug is triggered. From what I could find the docs only mention interrupts being disabled.

Implementing the above behaviour passes the halt_bug.gb and still passes the individual instructions tests from Blargg.

Hopefully this helps if others are trying to implement the same issue.

Cheers
binji
Posts: 11
Joined: Thu Jun 16, 2016 11:53 am

Re: HALT Bug

Post by binji »

The correct behavior is actually documented in AntonioND's cycle accurate gameboy docs. It's funny, I didn't even notice that there was a test for this (strangely without source). Tried it on my emulator, and I realized I had it implemented completely wrong... but now it's fixed! Thanks for mentioning this :)
DParrott
Posts: 25
Joined: Thu Jul 29, 2010 4:39 am

Re: HALT Bug

Post by DParrott »

Oooh, new documentation.

Thanks for pointing that out, a quick look shows I have come across it before but forgot about it. Excellent resource.
Post Reply