023. Sta 零页X寻址

添加零页,X的STA($95)。

23 / 356 · tests/chapter_01_cpu/test_023_STA_zero_page_x.py

需要更新的文件

emulator/cpu/opcodes.py

位置

opcodes.sta_zero_page_x
opcodes.OPCODE_TABLE[$95]

为什么需要这一步

本课复用了测试022中的 sta 指令,以及测试017中带回绕的 zero_page_x 寻址方式,说明一个存储操作码处理函数只需组合已有的地址计算与已有的写入操作即可。

完整示例实现

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


OPCODE_TABLE = {
    # Preserve existing entries.
    0x95: sta_zero_page_x,
}

重要不变量

  • $95 消耗一个操作数字节
  • 基址加X会在 $00 页内回绕
  • 写入的值是cpu.a
  • STA不修改Zero或Negative标志位

常见误解

零页索引不会进位到 $01 页;当X=$01时,$FF,X 指向的是 $0000,而不是 $0100。

不在本课范围内

  • 对zero_page_x或sta的修改
  • 绝对寻址和间接寻址的STA操作码
  • 周期时序

运行本课

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