050. 转移指令

添加核心 TAX、TXA、TAY 和 TYA 指令。

50 / 356 · tests/chapter_01_cpu/test_050_transfer_instructions.py

要更新的文件

emulator/cpu/instructions.py

位置

instructions.tax
instructions.txa
instructions.tay
instructions.tya

为什么需要这一步

这些隐含寻址的操作在 A、X 和 Y 之间进行复制,且不需要取操作数。每个目的寄存器随后都会使用加载指令已经用过的相同 Zero/Negative 更新逻辑。

完整示例实现

# emulator/cpu/instructions.py
def tax(cpu: CPU):
    cpu.x = cpu.a
    cpu._update_zero_and_negative_flags(cpu.x)


def txa(cpu: CPU):
    cpu.a = cpu.x
    cpu._update_zero_and_negative_flags(cpu.a)


def tay(cpu: CPU):
    cpu.y = cpu.a
    cpu._update_zero_and_negative_flags(cpu.y)


def tya(cpu: CPU):
    cpu.a = cpu.y
    cpu._update_zero_and_negative_flags(cpu.a)

重要不变量

  • TAX 把 A 复制到 X,TXA 把 X 复制到 A,TAY 把 A 复制到 Y,TYA 把 Y 复制到 A
  • 源寄存器保持不变
  • 当结果恰为 $00 时设置 Zero,Negative 则镜像目的寄存器的第 7 位
  • 当复制得到的值不再满足这两个标志位的条件时,它们也会被清除
  • 这些函数不取任何字节,也不进行总线访问

常见误区

只更新会被置位的标志位会遗留过期状态。每次复制之后都应对目的寄存器复用 cpu._update_zero_and_negative_flags

范围之外

  • 转移指令操作码的导入及 OPCODE_TABLE 条目,将在 Test 051 中引入
  • 栈指针相关的转移操作
  • 周期时序

运行本课

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