192. Rti implied

register implied RTI.

Lesson 192 of 356 · tests/chapter_01_cpu/test_192_RTI_implied.py

Prerequisite: step 191 added `rti. In this step, change only emulator/cpu/opcodes.py by importing that symbol and registering opcode $40 in OPCODE_TABLE`.

Why this step exists

RTI is a one-byte implied instruction. `CPU.step()` has already fetched $40, and all remaining inputs come from the stack, so dispatch must be wired directly to the operation without consuming an operand.

Suggested implementation

from emulator.cpu.instructions import rti

OPCODE_TABLE = {
    # existing entries
    0x40: rti,
}

Invariants: preserve every existing opcode entry; map exactly integer $40 to the same `rti function object; add no operand fetch or wrapper; let rti(cpu)` determine PC from the stack rather than sequential execution.

Misconception: implied does not mean that the byte after $40 is an operand. Fetching one would consume the next instruction before RTI restores PC.

Out of scope: RTI's stack algorithm is step 191. PHA and the remaining stack instructions begin at step 193. NMI entry is later behavior and must not be anticipated here.

Run this lesson

uv run pytest tests/chapter_01_cpu/test_192_RTI_implied.py -v