048. Sty 零页,X

添加 STY 零页,X 寻址($94)。

48 / 356 · tests/chapter_01_cpu/test_048_STY_zero_page_x.py

要更新的文件

emulator/cpu/opcodes.py

位置

opcodes imports of zero_page_x and sty
opcodes.sty_zero_page_x
opcodes.OPCODE_TABLE[$94]

为什么需要这一步

这种编码将 STY 面向地址的指令边界与已有的零页,X 辅助函数结合在一起,包括页 $00 内的环绕行为。

完整示例实现

# emulator/cpu/opcodes.py
from emulator.cpu.addressing_modes import zero_page_x
from emulator.cpu.instructions import sty


def sty_zero_page_x(cpu: CPU):
    addr = zero_page_x(cpu)
    sty(cpu, addr)


OPCODE_TABLE = {
    # Preserve existing entries.
    0x94: sty_zero_page_x,
}

重要不变量

  • $94 映射到 sty_zero_page_x 并消耗一个操作数字节
  • X 对操作数进行变址,有效地址在 8 位范围内环绕
  • 传给 sty 的是有效地址,而不是其当前内容
  • 本次存储不会改变 Y 和处理器标志位

常见误区

当 X 等于 $01 时,$FF,X 存储到的地址是 $0000,而不是 $0100;零页寻址不能超出页 $00 的范围。

范围之外

  • 绝对寻址的 STY
  • 修改 zero_page_x
  • 周期时序

运行本课

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