Skip to content

Commit e55c55c

Browse files
ihonenavivaceISSOtm
authored
Clarifications to the explanation of interrupts (#417)
* Highlight instructions more consistently * Clarify the explanation of interrupts Co-authored-by: Antonio Vivace <[email protected]> Co-authored-by: Eldred Habert <[email protected]>
1 parent ee590ca commit e55c55c

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/Interrupts.md

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ the following instructions/events only:
1616
EI ; Enables interrupts (that is, IME=1)
1717
DI ; Disables interrupts (that is, IME=0)
1818
RETI ; Enables interrupts and returns (same as the instruction sequence EI, RET)
19-
<INT> ; Disables interrupts and calls interrupt vector
19+
<ISR> ; Disables interrupts and calls the interrupt handler
2020
```
2121

22-
Where \<INT\> means the operation which is automatically executed by the
23-
CPU when it executes an interrupt.
22+
Where \<ISR\> is the Interrupt Service Routine that is automatically executed
23+
by the CPU when it services an interrupt request.
2424

2525
The effect of `ei` is delayed by one instruction. This means that `ei`
26-
followed immediately by DI does not allow any interrupts between them.
26+
followed immediately by `di` does not allow any interrupts between them.
2727
This interacts with the [`halt` bug](<#halt bug>) in an interesting way.
2828

2929
## FFFF - IE - Interrupt Enable (R/W)
@@ -46,61 +46,59 @@ Bit 3: Serial Interrupt Request (INT $58) (1=Request)
4646
Bit 4: Joypad Interrupt Request (INT $60) (1=Request)
4747
```
4848

49-
When an interrupt signal changes from low to high, the
50-
corresponding bit in the IF register becomes set. For example, Bit 0
49+
When an interrupt request signal changes from low to high, the
50+
corresponding bit in the IF register is set. For example, Bit 0
5151
becomes set when the LCD controller enters the VBlank period.
5252

53-
Any set bits in the IF register are only **requesting** an interrupt to be
54-
executed. The actual **execution** happens only if both the IME flag and
55-
the corresponding bit in the IE register are set, otherwise the
56-
interrupt "waits" until both IME and IE allow its execution.
53+
Any set bits in the IF register are only **requesting** an interrupt.
54+
The actual **execution** of the interrupt handler happens only if both the IME flag and
55+
the corresponding bit in the IE register are set; otherwise the
56+
interrupt "waits" until both IME and IE allow it to be serviced.
5757

5858
Since the CPU automatically sets and clears the bits in the IF register, it
59-
is usually not required to write to the IF register. However, the user
59+
is usually not necessary to write to the IF register. However, the user
6060
may still do that in order to manually request (or discard) interrupts.
61-
Like with real interrupts, a manually requested interrupt isn't executed
62-
unless/until IME and IE allow its execution.
61+
Just like real interrupts, a manually requested interrupt isn't serviced
62+
unless/until IME and IE allow it.
6363

6464
## Interrupt Handling
6565

6666
1. The IF bit corresponding to this interrupt and the IME flag are reset by the CPU.
6767
The former "acknowledges" the interrupt, while the latter prevents any further interrupts
6868
from being handled until the program re-enables them, typically by using the `reti` instruction.
6969
2. The corresponding interrupt handler (see the IE and IF register descriptions [above](<#FFFF - IE - Interrupt Enable (R/W)>)) is
70-
called by the CPU. This is a regular call, exactly like what would be performed by a `call <vector>` instruction (the current PC is pushed on the stack
71-
and then set to the address of the interrupt vector).
70+
called by the CPU. This is a regular call, exactly like what would be performed by a `call <address>` instruction (the current PC is pushed onto the stack
71+
and then set to the address of the interrupt handler).
7272

73-
The following occurs when control is being transferred to an interrupt handler:
73+
The following interrupt service routine is executed when control is being transferred to an interrupt handler:
7474

75-
1. Two wait states are executed (2 M-cycles pass while nothing
76-
occurs, presumably the CPU is executing `nop`s during this time).
77-
2. The current PC is pushed to the stack, consuming 2 more M-cycles.
78-
3. The PC register is set to the address of the handler ($40, $48, $50, $58, $60).
75+
1. Two wait states are executed (2 M-cycles pass while nothing happens; presumably the CPU is executing `nop`s during this time).
76+
2. The current value of the PC register is pushed onto the stack, consuming 2 more M-cycles.
77+
3. The PC register is set to the address of the handler (one of: $40, $48, $50, $58, $60).
7978
This consumes one last M-cycle.
8079

81-
The entire ISR **should** last a total of 5 M-cycles.
80+
The entire routine **should** last a total of 5 M-cycles.
8281
This has yet to be tested, but is what the Z80 datasheet implies.
8382

8483
## Interrupt Priorities
8584

86-
In the following three situations it might happen that more than one bit in the IF register is set, requesting more than one interrupt at once:
85+
In the following circumstances it is possible that more than one bit in the IF register is set, requesting more than one interrupt at once:
8786

88-
1. More than one interrupt signal changed from Low to High at the same time.
89-
2. Several interrupts have been requested while IME/IE didn't allow them to be handled directly.
87+
1. More than one interrupt request signal changed from low to high at the same time.
88+
2. Several interrupts have been requested while IME/IE didn't allow them to be serviced.
9089
3. The user has written a value with several bits set (for example binary 00011111) to the IF register.
9190

92-
If IME and IE allow the execution of more than one of the
91+
If IME and IE allow the servicing of more than one of the
9392
requested interrupts, the interrupt with the highest priority
94-
is executed first. The priorities follow the same order as the bits in the IE
93+
is serviced first. The priorities follow the order of the bits in the IE
9594
and IF registers: Bit 0 (VBlank) has the highest priority, and Bit 4
9695
(Joypad) has the lowest priority.
9796

9897
## Nested Interrupts
9998

10099
The CPU automatically disables all the other interrupts by setting IME=0
101-
when it executes an interrupt. Usually IME remains zero until the
102-
interrupt handler returns (and sets IME=1 by means of the RETI instruction).
103-
However, if you want any other interrupts (of any priority)
104-
to be allowed to be executed from inside the interrupt
105-
handler, then you can use the EI instruction in the interrupt
106-
handler.
100+
when it services an interrupt. Usually IME remains zero until the
101+
interrupt handler returns (and sets IME=1 by means of the `reti` instruction).
102+
However, if you want to allow the servicing of other interrupts (of any priority)
103+
during the execution of an interrupt handler, you may do so by using the
104+
`ei` instruction in the handler.

src/Memory_Map.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ The following addresses are supposed to be used as jump vectors:
2525
- Interrupts: 0040, 0048, 0050, 0058, 0060
2626

2727
However, this memory area (0000-00FF) may be used for any other purpose in case that your
28-
program doesn't use any (or only some) RST instructions or interrupts. RST
29-
is a 1-byte instruction that works similarly to the 3-byte CALL instruction, except
28+
program doesn't use any (or only some) [`rst`](https://rgbds.gbdev.io/docs/v0.5.2/gbz80.7/#RST_vec) instructions or interrupts. `rst`
29+
is a 1-byte instruction that works similarly to the 3-byte `call` instruction, except
3030
that the destination address is restricted. Since it is 1-byte sized,
3131
it is also slightly faster.
3232

0 commit comments

Comments
 (0)