045. Ldy 绝对寻址
添加 LDY 绝对寻址($AC)。
第 45 / 356 · tests/chapter_01_cpu/test_045_LDY_absolute.py
要更新的文件
emulator/cpu/opcodes.py位置
opcodes imports of absolute and ldy
opcodes.ldy_absolute
opcodes.OPCODE_TABLE[$AC]为什么需要这一步
绝对寻址把 LDY 的能力扩展到零页之外。处理函数解码已有的小端 16 位操作数,读取该地址,并将加载到的值委托给 ldy。
完整示例实现
# emulator/cpu/opcodes.py
from emulator.cpu.addressing_modes import absolute
from emulator.cpu.instructions import ldy
def ldy_absolute(cpu: CPU):
addr = absolute(cpu)
value = cpu.bus.read(addr)
ldy(cpu, value)
OPCODE_TABLE = {
# Preserve existing entries.
0xAC: ldy_absolute,
}重要不变量
- $AC 映射到 ldy_absolute 并消耗两个操作数字节
- absolute 先合并低字节,再合并高字节
- 处理函数读取有效地址,并将结果值传递给 ldy
- 执行总共前进三个字节,包括操作码在内
常见误区
不要颠倒 AC 00 02 的顺序;已有的绝对寻址辅助函数会把这些操作数字节解析为 $0200,而不是 $0002。
范围之外
- 绝对,X 寻址的 LDY
- 修改绝对寻址辅助函数
- 周期时序
运行本课
uv run pytest tests/chapter_01_cpu/test_045_LDY_absolute.py -v