Skip to content

Commit f729867

Browse files
Alvaro Burnettavivace
andauthored
Improve HALT instruction explanations (#175)
Co-authored-by: Antonio Vivace <avivace4@gmail.com>
1 parent f6b8f1b commit f729867

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

content/Reducing_Power_Consumption.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ batteries.
55
# Using the HALT Instruction
66

77
The HALT instruction should be used whenever possible to reduce power
8-
consumption & extend the life of the batteries. This instruction stops the
9-
system clock, reducing the power consumption of both the CPU and ROM.
8+
consumption.
109

11-
The CPU will remain stopped until an interrupt *enabled by [the IE register ($FFFF)](#ffff-ie-interrupt-enable-r-w)* occurs at which point the
12-
interrupt is serviced and then the instruction immediately following the
13-
HALT is executed.
10+
The CPU will remain halted until an interrupt *enabled by [the IE register ($FFFF)](#ffff-ie-interrupt-enable-r-w)* is
11+
flagged in IF, at which point the interrupt is serviced if IME is enabled,
12+
and then execution continues at the instruction immediately following the
13+
HALT.
1414

1515
Depending on how much CPU time is required by a game, the HALT
16-
instruction can extend battery life anywhere from 5 to 50% or possibly
16+
instruction can extend battery life anywhere from 5% to 50% or possibly
1717
more.
1818

19-
When waiting for a vblank event, this would be a BAD example:
19+
When waiting for a VBlank event, this would be a BAD example:
2020

2121
```
2222
.wait:
@@ -26,30 +26,32 @@ When waiting for a vblank event, this would be a BAD example:
2626
```
2727

2828
A better example would be a procedure as shown below. In this case the
29-
vblank interrupt must be enabled, and your vblank interrupt procedure
30-
must set vblank_flag to a non-zero value.
29+
VBlank interrupt must be enabled, and your VBlank interrupt handler
30+
must set `vblank_flag` to a non-zero value.
3131

3232
```
3333
ld hl, vblank_flag ;hl=pointer to vblank_flag
3434
xor a ;a=0
35-
.wait: ;wait...
35+
.wait: ;wait...
3636
halt ;suspend CPU - wait for ANY enabled interrupt
37-
cp a, [hl] ;vblank flag still zero?
38-
jr z, .wait ;wait more if zero
39-
ld [hl], a ;set vblank_flag back to zero
37+
cp a, [hl] ;is the vblank_flag still zero?
38+
jr z, .wait ;keep waiting if zero
39+
ld [hl], a ;set the vblank_flag back to zero
4040
```
41-
The vblank_flag is used to determine whether the HALT period has been
42-
terminated by a vblank interrupt, or by another interrupt. In case your
43-
program has all other interrupts disabled, then it would be okay to
44-
replace the above procedure by a single HALT instruction.
41+
42+
The `vblank_flag` is used to determine whether the HALT period has been
43+
terminated by a VBlank interrupt or by another interrupt. Note though
44+
that a VBlank interrupt might happen after the cp instruction
45+
and before the jr, in which case the interrupt would go unnoticed by the
46+
procedure, which would jump again into a halt.
4547

4648
Another possibility is, if your game uses no other interrupt than VBlank
47-
(or uses no interrupt), to only enable VBlank interrupts and simply use
48-
a halt instruction, which will only resume main code execution when a
49+
(or uses no interrupts), to only enable VBlank interrupts and simply use
50+
a HALT instruction, which will only resume main code execution when a
4951
VBlank occurs.
5052

51-
Remember when using HALT to wait between VBlanks, your interrupt
52-
routines MUST enable interrupts (ie with ei during the execution, or
53+
Remember, when using HALT to wait between VBlanks, that your interrupt
54+
handlers MUST enable interrupts (using EI before returning, or
5355
better, using the RETI instruction)
5456

5557
# Using the STOP Instruction

0 commit comments

Comments
 (0)