040. ldy 指令

添加核心 LDY 指令。

40 / 356 · tests/chapter_01_cpu/test_040_instruction_ldy.py

要更新的文件

emulator/cpu/instructions.py

位置

instructions.ldy, beside stx

为什么需要这一步

LDY 在任何 LDY 操作码接入之前先确立其值层面行为,由此开启 Y 寄存器加载指令家族。它沿用 LDA 和 LDX 的模式:把已解析好的值赋给目标寄存器,然后更新 Zero 和 Negative。

完整示例实现

# emulator/cpu/instructions.py
def ldy(cpu: CPU, value):
    cpu.y = value
    cpu._update_zero_and_negative_flags(cpu.y)

重要不变式

  • ldy 接收的是值,不是地址
  • Y 接收该值,而 A 和 X 保持不变
  • Zero 恰好在值为 $00 时置位,在其他情况下清零
  • Negative 反映第 7 位,当第 7 位为 0 时清零

常见误区

不要在 ldy 内部获取操作数或读取内存;未来的操作码处理函数会先解析出值,再调用这条指令。

超出范围

  • 所有 LDY 操作码处理函数和操作码表项
  • STY 指令及其编码
  • 周期时序

运行本课

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