051. 转移操作码

将四条转移指令注册为隐含寻址操作码。

51 / 356 · tests/chapter_01_cpu/test_051_transfer_opcodes.py

要更新的文件

emulator/cpu/opcodes.py

要更新的符号

the instruction import and OPCODE_TABLE

为什么需要这一步

测试 050 中的转移操作已经具备 CPU.step 期望从操作码表条目得到的同一种单参数形态。隐含寻址指令没有需要解码的操作数,因此增设中间的操作码处理函数不会带来任何行为。

完整示例实现

# emulator/cpu/opcodes.py
from emulator.cpu.instructions import (
    lda, sta, ldx, stx, ldy, sty, tax, txa, tay, tya,
)

OPCODE_TABLE = {
    # ...the existing load/store entries...
    0xAA: tax,
    0x8A: txa,
    0xA8: tay,
    0x98: tya,
}

重要不变量

  • 每个操作码直接映射到其指令函数
  • CPU.step 只消耗操作码,使 PC 前进一个字节
  • 转移与 Zero/Negative 行为仍保留在指令层

常见误区

不要创建 tax_implied 式的包装函数,也不要取操作数;这四条指令使用隐含寻址,已经符合分发契约。

范围之外

  • 测试 052 引入的 FlagsHandler 重构
  • ADC 及其操作码处理函数
  • 周期计数

运行本课

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