069. Sbc indirect x

Add SBC (Indirect,X).

Lesson 69 of 356 · tests/chapter_01_cpu/test_069_SBC_indirect_x.py

File to update

emulator/cpu/opcodes.py

Symbols to add/update

opcodes.sbc_indirect_x and OPCODE_TABLE[0xE1]

Why this step exists

SBC gains pre-indexed indirect access by composing the existing indirect_x resolver with the standard memory-read and instruction-delegation wrapper.

Complete example implementation

# emulator/cpu/opcodes.py
def sbc_indirect_x(cpu: CPU):
    addr = indirect_x(cpu)
    value = cpu.bus.read(addr)
    sbc(cpu, value)

OPCODE_TABLE = {
    # ... existing entries ...
    0xE1: sbc_indirect_x,
}

Important invariants

  • indirect_x adds X to the zero-page operand before reading the pointer
  • the pointer bytes and their zero-page wrap are handled by the resolver
  • the wrapper reads the final address and passes that byte to sbc
  • executing the two-byte instruction advances PC by two bytes

Common misconception

Do not read the zero-page pointer location as the SBC operand; indirect_x returns the final 16-bit target address whose contents must be read.

Out of scope

  • the (Indirect),Y SBC wrapper
  • changes to indirect addressing or SBC arithmetic
  • cycle timing

Run this lesson

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