029. Zero page y addressing

Add zero-page,Y addressing.

Lesson 29 of 356 · tests/chapter_01_cpu/test_029_zero_page_y_addressing.py

File to update

emulator/cpu/addressing_modes.py

Location

addressing_modes.zero_page_y

Why this step exists

Some later instruction encodings index a zero-page operand with Y rather than X. This lesson introduces only that reusable address calculation, parallel to Test 017's zero_page_x, before connecting it to any opcode.

Complete example implementation

# emulator/cpu/addressing_modes.py
def zero_page_y(cpu) -> int:
    base = cpu.fetch_byte()
    address = (base + cpu.y) & 0xFF
    return address

Important invariants

  • exactly one operand byte is fetched
  • Y, not X, is added to the operand
  • the result wraps to eight bits and therefore remains in page $00
  • the helper returns an address and performs no memory read at that address

Common misconception

Mask the sum, not merely the operand: $FF + $01 must become $0000 rather than $0100.

Out of scope

  • adding or changing opcode handlers
  • the LDX instruction itself
  • cycle timing

Run this lesson

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