022. Sta 零页寻址
添加STA指令以及零页操作码($85)。
第 22 / 356 · tests/chapter_01_cpu/test_022_STA_zero_page.py
需要更新的文件
emulator/cpu/instructions.py
emulator/cpu/opcodes.py位置
instructions.sta
opcodes imports of sta and zero_page
opcodes.sta_zero_page
opcodes.OPCODE_TABLE[$85]为什么需要这一步
此前的加载课程都是把值传给 lda。STA建立了与之互补的写入边界:寻址方式提供目标地址,而指令则通过CPU总线写入A,且不改变处理器标志位。
完整示例实现
# emulator/cpu/instructions.py
def sta(cpu, address: int) -> None:
value = cpu.a
cpu.bus.write(address, value)
# emulator/cpu/opcodes.py
from emulator.cpu.addressing_modes import zero_page
from emulator.cpu.instructions import sta
def sta_zero_page(cpu) -> None:
address = zero_page(cpu)
sta(cpu, address)
OPCODE_TABLE = {
# Preserve existing entries.
0x85: sta_zero_page,
}重要不变量
- sta接收的是一个地址,而不是从该地址读取的值
- 写入操作通过cpu.bus.write完成
- $85 消耗一个操作数字节,并将A存储到 $00nn
- STA不改变Zero和Negative标志位,即使A为 $00 或第7位被置位也是如此
常见误解
不要照搬LDA处理函数中的 cpu.bus.read(address):STA是把A写入解析出的地址,而不是根据存入的字节推导标志位。
不在本课范围内
- STA的其他寻址方式
- 新增寻址辅助函数
- 周期时序以及写入侧的硬件效应
运行本课
uv run pytest tests/chapter_01_cpu/test_022_STA_zero_page.py -v