028. Sta 间接Y寻址

添加间接索引的STA($91,写作 `(d),Y`)。

28 / 356 · tests/chapter_01_cpu/test_028_STA_indirect_y.py

需要更新的文件

emulator/cpu/opcodes.py

位置

opcodes.sta_indirect_y
opcodes.OPCODE_TABLE[$91]

为什么需要这一步

本课复用测试021的 indirect_y 地址解析方式,完成这一系列中的STA寻址方式。处理函数得到最终的Y索引目标地址,并把写入操作委托给 sta

完整示例实现

# emulator/cpu/opcodes.py
def sta_indirect_y(cpu) -> None:
    address = indirect_y(cpu)
    sta(cpu, address)


OPCODE_TABLE = {
    # Preserve existing entries.
    0x91: sta_indirect_y,
}

重要不变量

  • 先读取零页指针,再加上Y
  • 指针高字节的读取会从 $00FF 回绕到 $0000
  • Y对组装出的16位目标地址进行索引
  • $91 存储A而不改变标志位

常见误解

(d),Y 不会对零页指针位置本身进行索引。应先在 d 处读取指针,再把Y加到得到的地址上。

不在本课范围内

  • 对indirect_y或sta的修改
  • 跨页周期行为
  • 把STA的各处理函数重构为通用辅助函数

运行本课

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