-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move memory into its own module (#37)
- Loading branch information
1 parent
7a52940
commit aed3aec
Showing
3 changed files
with
60 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use super::Result; | ||
use crate::{primitive::Primitive, value::Value, Address}; | ||
|
||
/// KCEP's program memory. A flat, linear list of values. | ||
#[derive(Debug)] | ||
#[cfg_attr(test, derive(PartialEq))] | ||
pub struct Memory { | ||
addresses: Vec<Option<Primitive>>, | ||
} | ||
|
||
impl Default for Memory { | ||
fn default() -> Self { | ||
Self { | ||
addresses: vec![None; 1024], | ||
} | ||
} | ||
} | ||
|
||
impl Memory { | ||
/// Get a value from KCEP's program memory. | ||
pub fn get(&self, Address(addr): &Address) -> Option<&Primitive> { | ||
self.addresses[*addr].as_ref() | ||
} | ||
|
||
/// Store a value in KCEP's program memory. | ||
pub fn set(&mut self, Address(addr): Address, value: Primitive) { | ||
// If isn't big enough for this value, double the size of memory until it is. | ||
while addr > self.addresses.len() { | ||
self.addresses.extend(vec![None; self.addresses.len()]); | ||
} | ||
self.addresses[addr] = Some(value); | ||
} | ||
|
||
/// Store a value value (i.e. a value which takes up multiple addresses in memory). | ||
/// Store its parts in consecutive memory addresses starting at `start`. | ||
/// Returns how many memory addresses the data took up. | ||
pub fn set_composite<T: Value>(&mut self, start: Address, composite_value: T) -> usize { | ||
let parts = composite_value.into_parts().into_iter(); | ||
let mut total_addrs = 0; | ||
for (value, addr) in parts.zip(start.0..) { | ||
self.addresses[addr] = Some(value); | ||
total_addrs += 1; | ||
} | ||
total_addrs | ||
} | ||
|
||
/// Get a value value (i.e. a value which takes up multiple addresses in memory). | ||
/// Its parts are stored in consecutive memory addresses starting at `start`. | ||
pub fn get_composite<T: Value>(&self, start: Address) -> Result<T> { | ||
let mut values = self.addresses.iter().skip(start.0).cloned(); | ||
T::from_parts(&mut values) | ||
} | ||
|
||
pub fn iter(&self) -> impl Iterator<Item = (usize, &Option<Primitive>)> { | ||
self.addresses.iter().enumerate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters