018. Lda 绝对,X 寻址

添加绝对,X 寻址的 LDA($BD)。

18 / 356 · tests/chapter_01_cpu/test_018_LDA_absolute_x.py

要更新的文件

emulator/cpu/addressing_modes.py
emulator/cpu/opcodes.py

位置

addressing_modes.absolute_x
opcodes.lda_absolute_x
opcodes.OPCODE_TABLE[$BD]

为什么需要这一步

绝对,X 寻址将 X 加到一个完整的 16 位基地址上。与零页,X 不同,其结果可以从一个 256 字节的页跨入下一页。

完整示例实现

# emulator/cpu/addressing_modes.py
def absolute_x(cpu) -> int:
    base = cpu.fetch_word()
    return base + cpu.x


# emulator/cpu/opcodes.py
from emulator.cpu.addressing_modes import absolute_x


def lda_absolute_x(cpu) -> None:
    address = absolute_x(cpu)
    lda(cpu, cpu.bus.read(address))


OPCODE_TABLE = {
    # Preserve existing entries.
    0xBD: lda_absolute_x,
}

重要不变量

$12FF + X=$01 变为 $1300;它不会在零页内回绕。

常见误解

跨页不会改变有效地址的计算。额外的周期行为是有意排除在本课之外的。

范围之外

  • 跨页周期惩罚
  • 绝对,Y
  • 间接寻址

运行本课

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