024. Sta 绝对寻址

添加绝对寻址的STA($8D)。

24 / 356 · tests/chapter_01_cpu/test_024_STA_absolute.py

需要更新的文件

emulator/cpu/opcodes.py

位置

opcodes.sta_absolute
opcodes.OPCODE_TABLE[$8D]

为什么需要这一步

STA现在需要一个完整的16位目标地址。本课复用 absolutesta,说明操作数宽度的变化应属于寻址逻辑,而不是存储指令本身。

完整示例实现

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


OPCODE_TABLE = {
    # Preserve existing entries.
    0x8D: sta_absolute,
}

重要不变量

  • absolute先消耗低字节,再消耗高字节
  • 得到的16位地址会直接传给sta
  • $8D 使PC前进三个字节,包括操作码本身
  • 存储A不改变Zero和Negative标志位

常见误解

这两个操作数字节编码的是一个小端目标地址,而不是两个要写入的值,不能按大端方式读取。

不在本课范围内

  • 带索引的绝对寻址STA操作码
  • 对absolute或sta的修改
  • 周期时序

运行本课

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