092. Asl 零页,X 寻址

添加 ASL Zero Page,X。

92 / 356 · tests/chapter_01_cpu/test_092_ASL_zero_page_x.py

在这一步中,为测试 091 中的零页形式扩展 X 变址。

文件与符号

emulator/cpu/opcodes.py: asl_zero_page_x, OPCODE_TABLE[0x16]

为什么需要这一步

在测试 091 接入普通零页 ASL 之后,这次改动复用了已有的 zero_page_x 解析器,使变址与零页环绕仍然只属于寻址层面的问题。

本步骤的建议实现

# emulator/cpu/opcodes.py
def asl_zero_page_x(cpu: CPU):
    addr = zero_page_x(cpu)
    asl(cpu, addr)

OPCODE_TABLE = {
    # existing entries unchanged
    0x16: asl_zero_page_x,
}

重要不变量

  • 有效地址为 (operand + cpu.x) & 0xFF
  • 消耗一个操作数字节,因此包含操作码在内 PC 前进两字节
  • asl 负责内存写入以及 Carry、Zero、Negative 的更新
  • 变址改变的是目标地址,而不是传给 asl 的值

常见误解

Zero Page,X 不会溢出到第一页:基址 0xFE 加上 X=0x03 指向的是 $0001。

本步骤不涉及

  • 测试 093-094 中的 ASL Absolute 与 Absolute,X
  • 修改 zero_page_xasl
  • 周期时序

运行本课

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