118. 零页 AND

添加 AND 零页操作码 ``0x25``。

118 / 356 · tests/chapter_01_cpu/test_118_AND_zero_page.py

在这一步中,在第 117 课已导入 `and_a 的基础上,只在 emulator/cpu/opcodes.py` 中添加零页处理函数和表条目。

为什么需要这一步

AND 也必须能从内存中取值;零页模式验证了操作数字节被当作地址而不是立即数来处理。

建议实现

def and_zero_page(cpu: CPU):
    addr = zero_page(cpu)
    value = cpu.bus.read(addr)
    and_a(cpu, value)

OPCODE_TABLE = {
    ...
    0x25: and_zero_page,
}

`addressing_modes.zero_page 取出这个单字节地址;与立即数模式不同,处理函数必须先读取该地址处的值,再调用 instructions.and_a25 10 使用 cpu.bus[$0010]`,改变 A 和 Z/N,保持 Carry/Overflow 和内存不变,并使 PC 前进两个字节。

误解澄清:既不能把地址 `0x10 直接传给 and_a,也不能把结果写回 $0010`;AND 不是一条读/改/写指令。本课不涉及的内容:零页,X 及更宽泛的 AND 模式(第 119-124 课)。

运行本课

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