203. 指令 tsx
实现 TSX 操作。
第 203 / 356 · tests/chapter_01_cpu/test_203_instruction_tsx.py
本步骤存在的原因
在本步骤中,添加 `emulator/cpu/instructions.py::tsx`。TSX 与步骤 201 互补,它将栈指针字节复制到 X,并根据复制得到的值推导出两个结果标志。
建议实现
def tsx(cpu: CPU):
cpu.x = cpu.s
cpu.flags.set_zero_flag(cpu.x == 0)
cpu.flags.set_negative_flag((cpu.x & 0b1000_0000) != 0)不变式:S、A、Y、PC、内存以及除 Zero 和 Negative 之外的状态位均保持不变。Zero 精确地在值为 $00 时置位;Negative 跟随第 7 位,而不是第 6 位。常见的误解是像对待 TXS 那样对待 TSX,保留所有标志,或者在复制 S 之前就根据 X 计算标志。
范围之外:在 `emulator/cpu/opcodes.py` 中导入 TSX 并映射隐含寻址操作码 $BA 属于步骤 204;不添加其他栈相关 API。
运行本课
uv run pytest tests/chapter_01_cpu/test_203_instruction_tsx.py -v