tiny8.memory module¶
Memory model for tiny8 - simple RAM and ROM with change tracking.
- class tiny8.memory.Memory(ram_size=2048, rom_size=2048)[source]¶
Bases:
objectMemory class for a simple byte-addressable RAM/ROM model.
This class provides a minimal memory subsystem with separate RAM and ROM regions, change-logging for writes/loads, and convenience snapshot methods. All stored values are maintained as 8-bit unsigned bytes (0-255).
- Parameters:
- ram¶
Mutable list representing RAM contents (each element 0-255).
- rom¶
Mutable list representing ROM contents (each element 0-255).
- ram_changes¶
Change log for RAM writes. Each entry is a tuple (addr, old_value, new_value, step) appended only when a write changes the stored byte.
- rom_changes¶
Change log for ROM loads. Each entry is a tuple (addr, old_value, new_value, step) appended when load_rom changes bytes.
Note
All write/load operations mask values with 0xFF so stored values are always in the range 0..255.
ram_changes and rom_changes record only actual changes (old != new).
read_* methods perform bounds checking and raise IndexError for invalid addresses.
load_rom accepts an iterable of integers and raises ValueError if the input is larger than the configured ROM size.
- read_ram(addr)[source]¶
Read and return the value stored in RAM at the specified address.
- Parameters:
addr (int) – The RAM address to read. Must be within the valid range 0 <= addr < self.ram_size.
- Returns:
The integer value stored at the given RAM address.
- Raises:
IndexError – If the provided addr is negative or not less than self.ram_size.
- Return type:
- write_ram(addr, value, step=0)[source]¶
Write a byte to RAM at the specified address.
- Parameters:
- Raises:
IndexError – If addr is out of the valid RAM range.
- Return type:
None
Note
The provided value is masked to a single byte before storing. If the stored byte changes, a record (addr, old_value, new_value, step) is appended to self.ram_changes to track the modification.
- load_rom(data)[source]¶
Load a ROM image into the emulator’s ROM buffer.
- Parameters:
data (list[int]) – Sequence of integer byte values (expected 0-255) comprising the ROM image. Values outside 0-255 will be truncated to 8 bits.
- Raises:
ValueError – If len(data) is greater than self.rom_size.
- Return type:
None
Note
Overwrites self.rom[i] for i in range(len(data)) with (data[i] & 0xFF). Appends (index, old_value, new_value, 0) to self.rom_changes for each address where the value actually changed.
- read_rom(addr)[source]¶
Read a value from the ROM at the specified address.
- Parameters:
addr (int) – Zero-based address within ROM to read.
- Returns:
The value stored at the given ROM address.
- Raises:
IndexError – If
addris negative or greater than or equal toself.rom_size.- Return type: