190. Brk implied

wire implied BRK opcode $00.

Lesson 190 of 356 · tests/chapter_01_cpu/test_190_BRK_implied.py

Prerequisite: step 189 completed `brk with the required status-bit behavior. In this step, add this import and direct mapping in emulator/cpu/opcodes.py`:

from emulator.cpu.instructions import brk

OPCODE_TABLE = {
    # existing entries...
    0x00: brk,
}

Opcode

0x00 -> BRK

Goal

add opcode 0x00 to OPCODE_TABLE.

Student guidance

BRK is usually listed as an implied instruction, but it has a special length:

opcode byte:       $00
padding byte:      one ignored byte after $00
total length:      2 bytes

This does NOT mean there are two opcode entries for BRK.

There is only one official opcode

0x00

The byte after BRK can be any value. The CPU skips it and pushes the return address after that byte.

Example

$8000: 00    BRK opcode
$8001: AB    padding/signature byte, ignored by BRK
$8002: EA    next real instruction

After CPU.step() executes BRK, the pushed return address should be $8002.

Why this step exists

`CPU.step fetches $00 and advances PC once; direct dispatch to brk` accounts for the implicit padding byte and performs all stack/vector work. Invariants: there is one table entry and no addressing-mode fetch; padding contents do not affect A or the destination; the step-189 BRK state and stack rules remain unchanged. Misconception: BRK's two-byte architectural length does not imply a two-byte opcode or an operand-decoding wrapper.

Out of scope: RTI/$40 are steps 191-192. Hardware interrupt dispatch, NMI, shared stack helpers, and later opcode APIs are not part of this transition.

Run this lesson

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