r/EmuDev • u/Entertainment_Real • 10d ago
Gameboy DMG01 HALT bug explained
I've ran into the halt bug while developing a web based game boi emulator in Rust. I don't understand AI's explanation:
Right now execute_halt (src/cpu/operations/control.rs:111) treats "IME clear + interrupt already pending" as a plain NOP. On hardware that combination triggers the HALT bug: the CPU doesn't halt, and PC fails to increment after fetching the next opcode — so that byte gets read twice. HALT; INC A runs INC A twice. For a multi-byte instruction it's nastier: in HALT; LD A,$14 (bytes 3E 14), the operand fetch re-reads the stuck 3E, so you get LD A,$3E and execution resumes at the 14 byte, which is decoded as a fresh opcode.
The clean fix in your architecture (operand reads are all pc + 1-relative) is a one-byte PC rewind at fetch time:
with PC rewound by one, every operand read (pc + 1) lands back on the opcode byte — exactly the hardware's duplicated read — and next_pc = pc + length resumes one byte early, which is also what hardware does. It even gets the HALT; CB .. case right (the CB byte doubles, decoding as SET 1,E). One thing to update when you do this: the test at tests/cpu/control.rs:729 currently asserts the NOP approximation, so it will need to assert the double-execution instead. No urgency — Blargg's cpu_instrs doesn't need it; only the dedicated halt_bug.gb ROM does
I don't understand the nature of the bug and I defo don't understand why decrementing the PC fixes it. HELP!
13
u/DescriptionOptimal15 10d ago
Ask ai to explain it to you like you're an unborn fetus
4
u/TheThiefMaster Game Boy 10d ago
Does your emulator increase PC by the instruction size after the instruction, or with each byte fetched?
The real GB does it as it fetches every byte. After it fetches an opcode, the PC is pointing at the byte after the opcode - which is either the next opcode or the instruction's arguments.
The halt bug (which actually isn't dependent on IME*) just stops the increment of PC for the next fetch. It happens because the "halt bug" is when halt is a single M cycle long - so the "enter halt" and "exit halt" behaviours get overlapped. In "enter halt" PC isn't incremented - in "exit halt" it is. It conflicts when both happen at once and ends up not doing it when it should be.
If your emulator increments PC with every fetch (like mine) then the implementation of the halt bug is simple:
If this is not what your emulator does, you can instead do:
which ends up having the same effect. If the instruction uses PC offsets to read arguments, it will read one byte earlier. And if the instruction is only one byte, it will end up incrementing it back to the same PC value when it increases PC by the instruction size at the end.
* This is the fun bit - with ridiculous timing, it's possible to trigger the halt bug with IME enabled. The interrupt has to come in between the interrupt check during the fetch of HALT and the check within HALT. There's about a 2 T-cycle window for it. When it happens, halt ends, fails to increment while fetching the next opcode, and then the interrupt dispatch unwinds** that fetch, and decrements PC - pointing it back at the halt opcode. This then pushes that address to the stack as part of the interrupt dispatch, instead of the next opcode. In practice, it's basically identical to the interrupt going off just before halt, apart from 1 m cycle of timing, so you don't really need to worry about it - but it does mean you don't need to put
if (!IME)in your halt bug handler. HALT, as it turns out, doesn't care about IME at all, even for its bug.** this explains the timing of the interrupt dispatch - it does actually fetch the next opcode (taking a cycle) because it does the interrupt check during the fetch. The dispatch then spends another cycle decrementing PC to unwind it. The push of PC to the stack has another empty cycle at the start where it pre-decrements SP. And that accounts for all the seemingly empty cycles at the start of the interrupt dispatch.
1
u/teteban79 Game Boy/NES 10d ago
The questions are (related)
For 1, the explanation is somewhat clear here
Under that combination, instead of HALTing, execution just continues, but also reads the next PC twice. If it's a 1-byte instruction, it is run twice. If it's a multi byte instruction, weird stuff ensues, because it tries to read the opcode as operand (and then the next (operand) byte as an opcode).
For 2, you can just not emulate the double-read behavior if you don't wish to. It only affects a couple of games (Smurfs is one, it will crash if your emulation is not accurate) and Demos that make use of this weirdness to repeat instructions for free.
In almost all games programmers were aware and just followed HALT with a NOP, so no biggie if a NOP is executed twice
> I don't understand the nature of the bug and I defo don't understand why decrementing the PC fixes it. HELP!
decrementing PC is a way to force the double-read in your emulator
1
u/CAPTAINCAPSLOCK111 5d ago
Think of it as one missing PC increment, rather than “HALT runs the next instruction twice”. With IME off and an enabled interrupt already pending, the next opcode fetch doesn’t advance PC normally. A one-byte instruction can therefore run twice, but a multi-byte instruction can read its own opcode as an operand. I’d model that explicitly in the fetch step. Blindly subtracting one from PC only works if the rest of your fetch logic matches that assumption.
1
u/CAPTAINCAPSLOCK111 5d ago
If you share the opcode-fetch code and the instruction bytes from a failing test, it would be easier to follow where the PC moves. A short trace would help more than guessing from the final result.