The NES CPU is a 6502 and supports two kinds of interrupt: the ordinary request (IRQ)
and the non-maskable interrupt (NMI). This page covers the details of when they are
noticed and what the CPU does. It assumes you know the basic idea. The vector addresses
are on CPU memory map: NMI $FFFA,
reset $FFFC, IRQ and BRK $FFFE.
Edge and level detectors
- Both inputs are active low. The NMI input is edge-sensitive (a high to low change) and the IRQ input is level-sensitive (a low level).
- The NMI input feeds an edge detector. It looks at the NMI line during φ2 (the second half) of every CPU cycle. If the line was high in one cycle and is low in the next, it raises an internal signal in φ1 of the following cycle, and keeps it high until the NMI has been handled. In other words, a flip-flop in the CPU is set.
- The IRQ input feeds a level detector. If it sees a low level during φ2 of a cycle, it raises an internal signal during φ1 of the next cycle, for that cycle only (so the signal stays high as long as the line was low in the previous φ2).
- “Non-maskable” means no state inside the CPU can stop an NMI. Most boards let software switch off the generation of the signal. On the NES the PPU generates it for vblank; see below.
Polling
- The outputs of the two detectors are polled at set points. For most instructions the poll is in the last cycle of the instruction, before the opcode fetch of the next. If an interrupt is found, the next “instruction” is the interrupt sequence.
- The statement “interrupts are polled in the last cycle” is true only of the detector outputs. Because of the detectors’ delay, what really matters is the state of the interrupt lines at the end of the second-to-last cycle.
- If an NMI and an IRQ are both pending at the end of an instruction, the NMI is handled and the IRQ’s pending state is forgotten (it will probably be detected again later).
- The interrupt sequences do not poll, so at least one instruction of the handler runs before another interrupt can be taken.
Delayed response after CLI, SEI and PLP
- RTI changes the I flag at once: it restores I from the stack before polling. If an IRQ is pending and RTI clears I, the IRQ handler is entered right after the RTI.
- CLI, SEI and PLP change I after polling (like every two-cycle instruction they poll at the end of the first cycle). So they can delay an interrupt until after the next instruction. For example, with an IRQ pending and I set, CLI lets the next instruction run before the IRQ handler is entered.
Branches
Interrupts are always polled before the second cycle of a branch (the operand fetch), but not before the third cycle of a taken branch. For a taken branch that crosses a page, they are polled before the PCH fix-up cycle. An interrupt detected at either polling point, even only the first, triggers the interrupt sequence.
These behaviours are checked by the cpu_interrupts_v2 test ROM.
The interrupt sequences, tick by tick
BRK is the model for all of them:
# address R/W description
--- ------- --- -----------------------------------------------
1 PC R fetch opcode, increment PC
2 PC R read next instruction byte (and throw it away), increment PC
3 $0100,S W push PCH on stack, decrement S
4 $0100,S W push PCL on stack, decrement S
*** the signal status at this point decides which vector is used ***
5 $0100,S W push P on stack (with B flag set), decrement S
6 $FFFE R fetch PCL, set I flag
7 $FFFF R fetch PCH
IRQ and NMI are the same, with these differences (from Visual 6502):
# address R/W description
--- ------- --- -----------------------------------------------
1 PC R fetch opcode (discarded: $00, BRK, is forced into the opcode register)
2 PC R read next instruction byte (the same byte, since the PC increment is suppressed; discarded)
3 $0100,S W push PCH, decrement S
4 $0100,S W push PCL, decrement S
*** the signal status at this point decides which vector is used ***
5 $0100,S W push P (with B flag CLEAR), decrement S
6 A R fetch PCL (A = $FFFE for IRQ, $FFFA for NMI), set I flag
7 A R fetch PCH (A = $FFFF for IRQ, $FFFB for NMI)
Reset goes through the same sequence but suppresses the writes: the stack pointer is decremented three times without changing memory. That is why the I flag is always set on reset (the vector is $FFFC/$FFFD). The B flag does not physically exist in the CPU: it appears only as different values pushed for bit 4 by PHP, BRK and the NMI/IRQ sequences.
| Source | PC pushed | B in pushed P | Vector |
|---|---|---|---|
| BRK | address of BRK + 2 | 1 | $FFFE/$FFFF |
| IRQ | address of the interrupted instruction | 0 | $FFFE/$FFFF |
| NMI | address of the interrupted instruction | 0 | $FFFA/$FFFB |
| Reset | nothing is written | nothing is written | $FFFC/$FFFD |
The table follows from the tick sequences above: the BRK sequence increments PC twice, the interrupt sequences suppress the increment.
Interrupt hijacking
Normally an IRQ pushes PC and P and then loads the new PC from $FFFE/$FFFF, as if by JMP ($FFFE). The 6502, and so the 2A03 and 2A07, has a quirk: if a second interrupt arrives very close to the first, the wrong vector can be used. The vector is chosen by the signals at the point marked between ticks 4 and 5 above.
- If NMI is asserted during the first four ticks of a BRK, the BRK runs normally at first (PC increments happen and P is pushed with B set) but execution goes to the NMI vector, not the IRQ/BRK vector.
- In the same way an NMI can hijack an IRQ, and an IRQ can hijack a BRK (hardly visible, since they share a vector).
- An IRQ hijacked by an NMI is normally no problem: the IRQ is usually still asserted when the NMI returns and gives a new interrupt. A BRK, however, can be cancelled by an NMI (or IRQ), so code that uses BRK must avoid coinciding with an NMI. The NMI handler can check the B bit of the pushed P to find and dispatch the lost BRK.
How the PPU generates the NMI
Two 1-bit registers in the PPU control the signal, whether or not rendering is on:
- Start of vblank (line 241, dot 1):
vblank_flagis set. - End of vblank (line 261, dot 1):
vblank_flagis cleared. - A read of PPUSTATUS returns the old
vblank_flagin bit 7, then clears it. - A write to PPUCTRL sets
NMI_outputto bit 7 of the value.
The PPU pulls /NMI low if and only if both are true. A program can toggle NMI_output (PPUCTRL bit 7) during vblank without reading PPUSTATUS to produce several NMIs. When the CPU finds the flip-flop set it: pushes the return address high byte, then the low byte; pushes the status register with B clear; reads the handler address from $FFFA/$FFFB; clears the flip-flop; and jumps. A handler normally ends with RTI.
Cautions
- Race: if steps 1 and 3 happen at the same moment, bit 7 reads as 0 and the flag is cleared anyway: no NMI, and the program never learns that vblank began. A loop that spins on PPUSTATUS bit 7 (
bit $2002 / bpl) can therefore miss a frame now and then. It is fine while waiting for the PPU to warm up. Once the game runs, the dependable way is to enable NMI output and wait for the handler to change a counter. - A handler that only increments a counter fails if game code takes much more than about 24,000 cycles (more than a frame): the NMI during other code is missed, which can make a sprite 0 scroll split flicker. A step further is to do VRAM updates and sprite 0 waiting in a separate “NMI thread”. Gradius counts the time of each object handler and delays the rest when it might otherwise miss the sprite 0 hit; Micronics’ games reduce the frame rate well below 60 to match worst-case lag.
- Old emulators (the wiki names NESticle), like some other platforms, keep the flag on through the whole vblank, and did not clear it on a read. Some defective homebrew of that era waits for bit 7 to become false and expects that at the end of vblank. The right way to wait for the end of vblank is to trigger a sprite 0 hit and wait for that flag to clear.
See timing and regions for the vblank flag timing, and PPU registers.
Sources
The NESdev Wiki text is available under CC BY-SA 3.0. This page is written in our own words from the raw wikitext kept in samples/nes/sources/hardware. The PC-pushed column of the table is derived from the wiki’s tick tables, not stated there in a table. The wiki points to Visual 6502’s write-up of BRK and the B bit for the cause of hijacking; it is not summarised.