002. Ram read write
Implement raw 2 KiB RAM storage.
Lesson 2 of 356 · tests/chapter_01_cpu/test_002_ram_read_write.py
File to update
emulator/memory/ram.pyLocation
class RAMWhy this step exists
The NES contains 2 KiB of internal CPU RAM. This class owns only physical byte storage; the CPU bus will introduce address mirroring in the next lesson.
Complete example implementation
from dataclasses import dataclass, field
@dataclass
class RAM:
_data: bytearray = field(
default_factory=lambda: bytearray(0x800),
init=False,
)
def write(self, addr: int, value: int) -> None:
self._data[addr] = value
def read(self, addr: int) -> int:
return self._data[addr]Important invariants
- storage contains exactly 0x800 bytes
- a write changes the byte read from the same physical address
- RAM does not translate or mirror CPU addresses
Common misconception
RAM does not need to understand addresses $0800, $1000, or $1800. Those are CPU bus aliases of physical RAM and belong to the mapping mechanism introduced in Test 003.
Out of scope
- a shared memory-device interface
- ROM storage
- CPU bus routing
Run this lesson
uv run pytest tests/chapter_01_cpu/test_002_ram_read_write.py -v