034. Ldx 绝对寻址

添加 LDX 绝对寻址($AE)。

34 / 356 · tests/chapter_01_cpu/test_034_LDX_absolute.py

要更新的文件

emulator/cpu/opcodes.py

位置

opcodes.ldx_absolute
opcodes.OPCODE_TABLE[$AE]

为什么需要这一步

本课将 LDX 从单字节地址扩展到完整的 16 位内存地址,同时保持既定的边界:absolute 解码操作数,处理函数读取内存,ldx 更新 X 及其标志。

完整示例实现

# emulator/cpu/opcodes.py
def ldx_absolute(cpu: CPU):
    addr = absolute(cpu)
    value = cpu.bus.read(addr)
    ldx(cpu, value)


OPCODE_TABLE = {
    # Preserve existing entries.
    0xAE: ldx_absolute,
}

重要不变式

  • $AE 映射到 ldx_absolute
  • absolute 先消耗操作数的低位字节,再消耗高位字节
  • 从得到的 16 位地址读取一个字节并传给 ldx
  • 整条指令使 PC 前进 3 字节

常见误区

AE 00 02 寻址的是 $0200,因为 6502 的操作数采用小端序;它既不会寻址 $0002,也不会直接加载任何一个操作数字节。

超出范围

  • 绝对Y变址 LDX
  • 对 absolute 或 ldx 的改动
  • 周期时序

运行本课

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