008. Cpu reset vector
Initialize CPU state from the reset vector.
Lesson 8 of 356 · tests/chapter_01_cpu/test_008_cpu_reset_vector.py
File to update
emulator/cpu/cpu.pyLocation
CPU.resetReference
https://www.nesdev.org/wiki/CPU_power_up_stateWhy this step exists
The CPU does not choose its program start address directly. On reset it reads a little-endian 16-bit vector from CPU addresses $FFFC-$FFFD through CpuBus.
Complete example implementation
class CPU:
# Keep the constructor and fetch helpers from Test 004.
def reset(self) -> None:
low = self.bus.read(0xFFFC)
high = self.bus.read(0xFFFD)
self.pc = low | (high << 8)
self.s = 0xFD
self.p = 0x04Important invariants
- vector low byte comes from $FFFC
- vector high byte comes from $FFFD
- reset reads through the bus rather than indexing FakeROM directly
Minimal example
FakeROM offsets $7FFC=$00 and $7FFD=$80 appear at CPU addresses $FFFC-$FFFD and set PC to $8000. The next fetch therefore reads FakeROM offset $0000.
Common misconception
Reset must not increment PC while reading the vector. It assigns PC from fixed bus addresses; instruction fetching begins afterward.
Out of scope
- opcode dispatch
- interrupt entry
- cycle timing
Run this lesson
uv run pytest tests/chapter_01_cpu/test_008_cpu_reset_vector.py -v