Skip to content

Commit 1d9c973

Browse files
authored
Rework section about halt bug, and add citation (#481)
1 parent cad6bd9 commit 1d9c973

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/halt.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ interrupt is pending as the `halt` instruction is first executed.
2121

2222
## `halt` bug
2323

24-
Under some circumstances, `pc` fails to be normally incremented.
24+
When a `halt` instruction is executed with `IME = 0` and `[IE] & [IF] != 0`, the `halt` instruction ends immediately, but [`pc` fails to be normally incremented](https://github.com/nitro2k01/little-things-gb/tree/main/double-halt-cancel).
2525

26-
The most typical trigger, `halt` with `IME`=0 and `[IE] & [IF] != 0`, causes
27-
the byte after the `halt` to be read twice.
26+
Under most circumstances, this causes the byte after the `halt` to be read a second time (and this behaviour can repeat if said byte executes another `halt` instruction).
27+
But, if the `halt` is immediately followed by a jump to elsewhere, then the behaviour will be slightly different; this is possible in only one of two ways:
2828

29-
The behavior is different when `ei` (whose effect is typically delayed by one
30-
instruction) is followed immediately by a `halt`, and an interrupt is pending
31-
as the `halt` is executed. The interrupt is serviced and the handler called,
32-
but the interrupt returns to the `halt`, which is executed again, and thus
29+
- The `halt` comes immediately after a `ei` instruction (whose effect is typically delayed by one instruction, hence `IME` still being zero for the `halt`): the interrupt is serviced and the handler called, but the interrupt returns to the `halt`, which is executed again, and thus
3330
waits for another interrupt.
3431
([Source](https://github.com/LIJI32/SameSuite/blob/master/interrupt/ei_delay_halt.asm))
32+
- The `halt` is immediately followed by a `rst` instruction: the `rst` instruction's return address will point at the `rst` itself, instead of the byte after it.
33+
Notably, a `ret` would return to the `rst` an execute it again.
34+
35+
If the bugged `halt` is preceded by a `ei` and followed by a `rst`, the former "wins".

0 commit comments

Comments
 (0)