009. Cpu step
Execute the first opcode: immediate LDA ($A9).
Lesson 9 of 356 · tests/chapter_01_cpu/test_009_cpu_step.py
File to update
emulator/cpu/cpu.pyLocation
CPU.stepReference
https://www.nesdev.org/wiki/Instruction_reference#LDAWhy this step exists
CPU.step introduces the minimum fetch-decode-execute loop. Immediate LDA reads the byte directly following opcode $A9 and stores it in accumulator A.
Complete example implementation
class CPU:
# Keep the state, fetch, and reset behavior from earlier tests.
def step(self) -> None:
opcode = self.fetch_byte()
if opcode == 0xA9:
self.a = self.fetch_byte()
return
raise NotImplementedError(
f"Opcode {opcode:02X} not implemented"
)Execution timeline
PC=$8000 -> fetch $A9 -> PC=$8001
PC=$8001 -> fetch operand $42 -> PC=$8002 -> A=$42Important invariants
- one step executes exactly one instruction
- an unknown opcode fails visibly
- immediate LDA consumes two bytes in total
Common misconception
The operand $42 is a value, not an address. Immediate mode does not perform another bus lookup using $0042.
Out of scope
- Zero and Negative flag updates, introduced in Test 010
- other LDA addressing modes
- opcode tables and cycle counts
Run this lesson
uv run pytest tests/chapter_01_cpu/test_009_cpu_step.py -v