liveBook · Manning

RTI

As explained in Classic Game Programming on the NES: Make your own retro video game

A 6502 assembly instruction that returns execution from an interrupt-handling routine and restores interrupt state.

RTI is the 6502 instruction that finishes an interrupt-handling routine. It returns execution to the interrupted program after the routine has restored the registers and interrupt state saved on the stack. section 5.13 section 4.4.3

Use RTI for a routine entered by an interrupt, such as the NES NMI routine; use RTS for an ordinary subroutine. The return instruction must be reached with the stack restored to its expected position. section 4.4.3

A typical NMI ending proceeds in this order:

  • Pull the saved value for Y into A, then transfer it to Y with TAY. section 5.13
  • Pull the saved value for X into A, then transfer it to X with TAX. section 5.13
  • Pull the saved A value and execute RTI to return from the interrupt. section 5.13

Returning from the NMI routine

pla tay pla tax pla rti section 5.13

Listing 5.23 Restoring registers and returning (example.s)
            #1
    pla
    tay
    pla
    tax
    pla
    rti
.endproc

Inspect the complete register-restoration sequence and its final RTI instruction. section 5.13

How Classic Game Programming on the NES: Make your own retro video game covers it

How deep it goes
Explains RTI through stack restoration and contrasts it with RTS, without detailing the processor's interrupt-entry sequence.
Examples
Uses an NES NMI epilogue that restores A, X, and Y with PLA, TAY, and TAX before executing RTI.
What it assumes
Assumes basic 6502 instructions and stack operations while distinguishing ordinary subroutines from interrupt handlers.
What you can do after
Evaluate/understand interrupt-return code by distinguishing RTI from RTS and checking that pushed values are popped first.
How it connects
Connects interrupt handlers, NMI, RTS, JSR, stack operations, and CPU registers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Game Programming on the NES: Make your own retro video game. The section links point to the parts of the book it draws on.