You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/halt.md
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,14 +21,15 @@ interrupt is pending as the `halt` instruction is first executed.
21
21
22
22
## `halt` bug
23
23
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).
25
25
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:
28
28
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
- 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