097. Lsr 累加器

添加 LSR Accumulator 操作码。

97 / 356 · tests/chapter_01_cpu/test_097_LSR_accumulator.py

在这一步中,通过操作码分派暴露测试 096 中的累加器行为。

文件与符号

emulator/cpu/opcodes.py: imported lsr_a, OPCODE_TABLE[0x4A]

为什么需要这一步

测试 096 已经建立了累加器行为。这次改动让 CPU 分派直接到达该函数,因为累加器模式没有操作数或地址解码。

本步骤的建议实现

# emulator/cpu/opcodes.py
from emulator.cpu.instructions import lsr_a

OPCODE_TABLE = {
    # existing entries unchanged
    0x4A: lsr_a,
}

重要不变量

  • 该表直接映射到 instructions.lsr_a;不需要包装函数
  • 没有寻址辅助函数会消耗字节
  • CPU.step 已经消耗了操作码,因此 PC 恰好前进一字节
  • 只有 A 和 C/Z/N 可能发生变化

常见误解

累加器模式不是带隐含地址的零页模式;0x4A 没有操作数。

本步骤不涉及

  • 测试 098-101 中的内存 LSR 操作码
  • 对 CPU 分派或 lsr_a 的改动
  • 周期时序

运行本课

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