080. 指令 inx

添加 INX 指令原语。

80 / 356 · tests/chapter_01_cpu/test_080_instruction_inx.py

在本步骤中,只添加 emulator/cpu/instructions.py:inx。操作码 0xE8 是第 081 课,而 dex 和操作码 0xCA 是第 082-083 课。

本步骤存在的原因

INX 是隐含寄存器行为,因此不需要寻址辅助函数或总线访问。该原语执行 8 位运算,并直接根据新的 X 值推导标志位。

emulator/cpu/instructions.py 中的建议实现,位于 dec 之后:

def inx(cpu: CPU):
    result = cpu.x + 1
    result_8 = result & 0xFF

    # Set flags
    cpu.flags.set_negative_flag((result_8 & 0b1000_0000) != 0)
    cpu.flags.set_zero_flag(result_8 == 0)

    cpu.x = result_8

不变量:X 保持 8 位($FF + 1 == $00);Zero 和 Negative 反映掩码后的结果;A、Y、内存、Carry 和 Overflow 保持不变;该原语本身不获取操作数,也不使 PC 前进。

常见误解:INX 不是以 X 作为地址的内存 INC。它直接改变 X 寄存器,并且只接受 cpu 作为参数。

范围之外:导入并映射 inx、添加 dex,以及映射 0xCA,属于第 081-083 课。

运行本课

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