123. 间接,X AND
添加 AND (indirect,X) 操作码 ``0x21``。
第 123 / 356 · tests/chapter_01_cpu/test_123_AND_indirect_x.py
此步骤存在的原因
AND 需要对零页中的指针表进行预索引间接访问,而指针解析仍属于寻址方面的问题,而非逻辑行为本身。
在此步骤中,在此前的 AND 寻址模式之后,请在 `emulator/cpu/opcodes.py` 中恰好添加以下内容:
def and_indirect_x(cpu: CPU):
addr = indirect_x(cpu)
value = cpu.bus.read(addr)
and_a(cpu, value)
OPCODE_TABLE = {
...
0x21: and_indirect_x,
}`emulator/cpu/addressing_modes.py::indirect_x 获取操作数,计算 ptr = (base + cpu.x) & 0xFF,并从 ptr 与 (ptr + 1) & 0xFF 处读取小端序指针。处理程序读取该最终地址并调用 instructions.and_a`。A 与 Z/N 会改变;X、Carry/Overflow、指针字节及目标内存保持不变;PC 前进两个字节。
常见误解:(indirect,X) 是在解引用之前应用 X,而不是应用于最终的 16 位地址。不在本步骤范围内:AND (indirect),Y(第 124 课)以及之后所有的 ORA/EOR/BIT 相关工作。
运行本课
uv run pytest tests/chapter_01_cpu/test_123_AND_indirect_x.py -v