044. Ldy 零页,X

添加 LDY 零页,X 寻址($B4)。

44 / 356 · tests/chapter_01_cpu/test_044_LDY_zero_page_x.py

要更新的文件

emulator/cpu/opcodes.py

位置

opcodes imports of zero_page_x and ldy
opcodes.ldy_zero_page_x
opcodes.OPCODE_TABLE[$B4]

为什么需要这一步

这种编码复用了已有的零页,X 地址计算方式,包括其 8 位环绕行为,然后再通过核心指令 ldy 加载解析出的字节。

完整示例实现

# emulator/cpu/opcodes.py
from emulator.cpu.addressing_modes import zero_page_x
from emulator.cpu.instructions import ldy


def ldy_zero_page_x(cpu: CPU):
    addr = zero_page_x(cpu)
    value = cpu.bus.read(addr)
    ldy(cpu, value)


OPCODE_TABLE = {
    # Preserve existing entries.
    0xB4: ldy_zero_page_x,
}

重要不变量

  • $B4 映射到 ldy_zero_page_x 并消耗一个操作数字节
  • 对零页操作数进行变址的是 X,而不是 Y
  • 有效地址在页 $00 内环绕
  • 解析出的内存值会传递给 ldy,由它更新 Zero 和 Negative

常见误区

LDY 命名的是目的寄存器,而不是变址寄存器。$B4 编码使用 X 来计算地址。

范围之外

  • 绝对寻址和绝对,X 寻址的 LDY 编码
  • 修改 zero_page_x
  • 周期时序

运行本课

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