Download crates/subleq-vm/src/memory.rs from Snapkitty/rowm-polymorphic-notebook: direct link, hf CLI and curl.
- Browser
- Download file 1.84 kB
-
https://huggingface.co/Snapkitty/rowm-polymorphic-notebook/resolve/main/crates/subleq-vm/src/memory.rs
- Command line
-
hf download hf://Snapkitty/rowm-polymorphic-notebook/crates/subleq-vm/src/memory.rs
-
curl -L -o memory.rs https://huggingface.co/Snapkitty/rowm-polymorphic-notebook/resolve/main/crates/subleq-vm/src/memory.rs
1.84 kB
| //! Memory Model — Von Neumann unified address space | |
| use anyhow::{Result, anyhow}; | |
| use std::fmt; | |
| pub type Address = usize; | |
| pub type Word = i64; | |
| /// Flat 64-bit memory model with code and data unified | |
| pub struct Memory { | |
| cells: Vec<Word>, | |
| max_addr: Address, | |
| } | |
| impl Memory { | |
| pub fn new(size: Address) -> Self { | |
| Self { | |
| cells: vec![0; size], | |
| max_addr: size - 1, | |
| } | |
| } | |
| pub fn read(&self, addr: Address) -> Option<Word> { | |
| if addr <= self.max_addr { | |
| Some(self.cells[addr]) | |
| } else { | |
| None | |
| } | |
| } | |
| pub fn write(&mut self, addr: Address, value: Word) -> Result<()> { | |
| if addr > self.max_addr { | |
| return Err(anyhow!("Address out of bounds: {} (max {})", addr, self.max_addr)); | |
| } | |
| self.cells[addr] = value; | |
| Ok(()) | |
| } | |
| pub fn size(&self) -> Address { | |
| self.cells.len() | |
| } | |
| pub fn as_slice(&self) -> &[Word] { | |
| &self.cells | |
| } | |
| pub fn as_mut_slice(&mut self) -> &mut [Word] { | |
| &mut self.cells | |
| } | |
| } | |
| impl fmt::Debug for Memory { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.debug_struct("Memory") | |
| .field("size", &self.cells.len()) | |
| .field("sample", &self.cells.iter().take(10).collect::<Vec<_>>()) | |
| .finish() | |
| } | |
| } | |
| mod tests { | |
| use super::*; | |
| fn test_memory_basic() { | |
| let mut mem = Memory::new(1024); | |
| mem.write(0, 42).unwrap(); | |
| assert_eq!(mem.read(0), Some(42)); | |
| } | |
| fn test_memory_bounds() { | |
| let mem = Memory::new(10); | |
| assert_eq!(mem.read(5), Some(0)); | |
| assert_eq!(mem.read(15), None); | |
| } | |
| } | |