043. Ldy 零页

添加 LDY 零页寻址($A4)。

43 / 356 · tests/chapter_01_cpu/test_043_LDY_zero_page.py

要更新的文件

emulator/cpu/opcodes.py

位置

opcodes imports of zero_page and ldy
opcodes.ldy_zero_page
opcodes.OPCODE_TABLE[$A4]

为什么需要这一步

与立即数寻址的 LDY 不同,零页寻址的 LDY 会将操作数解析为地址,读取该地址处的字节,然后将寄存器和标志位行为委托给 ldy

完整示例实现

# emulator/cpu/opcodes.py
from emulator.cpu.addressing_modes import zero_page
from emulator.cpu.instructions import ldy


def ldy_zero_page(cpu: CPU):
    addr = zero_page(cpu)
    value = cpu.bus.read(addr)
    ldy(cpu, value)


OPCODE_TABLE = {
    # Preserve existing entries.
    0xA4: ldy_zero_page,
}

重要不变量

  • $A4 映射到 ldy_zero_page 并消耗一个操作数字节
  • zero_page 返回的地址位于 $0000-$00FF 之间
  • 处理函数执行一次数据读取,并将该数值(而非其地址)传递给 ldy
  • ldy 负责更新 Zero 和 Negative

常见误区

如果直接把 addr 传给 ldy,加载到的会是零页位置编号本身,而不是存储在那里的字节。

范围之外

  • 零页,X 和绝对寻址的 LDY 编码
  • 新的寻址模式辅助函数
  • 周期时序

运行本课

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