176. Bpl 相对寻址

将 BPL 接入其相对寻址操作码。

176 / 356 · tests/chapter_01_cpu/test_176_BPL_relative.py

前置条件:步骤 175 已接通 BNE。在这一步中,向 `emulator/cpu/opcodes.py` 添加以下内容:

from emulator.cpu.instructions import bpl

def bpl_relative(cpu: CPU):
    offset = relative(cpu)
    bpl(cpu, offset)

OPCODE_TABLE[0x10] = bpl_relative

将导入项和表项合并进已有的分组导入与表字面量中。

这一步存在的原因

`relative 消费操作数,而步骤 168 的 instructions.bpl` 在改变 PC 之前,将“plus”解释为 Negative 清零。

不变量:`0x10` 映射到该单参数处理函数;两种结果都会消费带符号操作数;Negative 清零时从操作数之后的 PC 出发分支,Negative 置位时不分支。标志位和内存保持不变。常见误解:BPL 并不检测偏移量或 PC 是否为正数,它只检测 Negative 标志。

不在本步范围内:BMI/BVC/BVS 的操作码接线由步骤 177-179 引入,间接寻址 JMP 是步骤 180。

运行本课

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