086. Dey 指令

添加 DEY 指令行为。

86 / 356 · tests/chapter_01_cpu/test_086_instruction_dey.py

在这一步中,仅添加 dey;测试 087 会在该行为存在之后接入操作码。

生产代码位置与符号

emulator/cpu/instructions.py: `dey(cpu: CPU)`

本步骤存在的原因

DEY 独立于解码负责 Y 寄存器的递减及状态影响。为了在 8 位寄存器中模拟下溢,需要进行掩码处理。

实现建议

def dey(cpu: CPU):
    result = cpu.y - 1
    result_8 = result & 0xFF

    # Set flags
    cpu.flags.set_negative_flag((result_8 & 0b1000_0000) != 0)
    cpu.flags.set_zero_flag(result_8 == 0)

    cpu.y = result_8

重要不变量

  • Y 从 0x00 回绕到 0xFF
  • Zero 和 Negative 反映掩码后的 8 位结果
  • Carry 和 Overflow 保持不变;不访问内存

常见误解

DEY 不是对 Y 应用 SBC,因此不得将 Carry 用作借位输入。

不在本步骤范围内

  • 导入 dey 并映射操作码 0x88(测试 087)
  • 此前的 INY 行为与分发
  • 后续的 ASL 工作及周期计时

运行本课

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