pub struct Word { /* private fields */ }
Expand description
A memory location that can be read and written to.
§Reading
A word’s value can be read with:
Word::get
to directly access the value, ignoring any initialization stateWord::get_if_init
to directly access the value after verifying initialization state
See the respective functions for more details.
Both functions return the unsigned representation of the word.
If needed, this can be converted to a signed integer with typical as
casting (data as i16
).
§Writing
A word can be written into with a value or with another word:
Word::set
to read a value into this wordWord::set_if_init
to read a word into this word
Word::set_if_init
may be more useful in situations where initialization state needs to be preserved
or when it needs to be verified.
See the respective functions for more details.
Words can also be written to by applying assign operations (e.g., add, sub, and, etc.). All arithmetic operations that can be applied to words are assumed to be wrapping. See those implementations for more details.
§Initialization
Internally, each memory location keeps track of two fields:
- its data (i.e., the value stored at this location)
- which bits of its data are truly “initialized” (as in the program knows what values are present there)
This second field is not used except for when the simulator is set to strict mode. Then, this second field is leveraged to detect if uninitialized memory is being written to places it shouldn’t be (e.g., PC, addresses, registers and memory).
When a Word
is created for memory/register files (i.e., via Word::new_uninit
),
it is created with the initialization bits set to fully uninitialized.
The data associated with this Word
is decided by the creation strategy
(see super::MachineInitStrategy
for details).
Implementations§
Source§impl Word
impl Word
Sourcepub fn new_uninit<F: WordFiller + ?Sized>(fill: &mut F) -> Self
pub fn new_uninit<F: WordFiller + ?Sized>(fill: &mut F) -> Self
Creates a new word that is considered uninitialized.
Sourcepub fn new_init(data: u16) -> Self
pub fn new_init(data: u16) -> Self
Creates a new word that is initialized with a given data value.
Sourcepub fn get(&self) -> u16
pub fn get(&self) -> u16
Reads the word, returning its unsigned representation.
The data is returned without checking for initialization state.
If the initialization state should be checked before trying to query the data,
then Word::get_if_init
should be used instead.
Sourcepub fn get_if_init<E>(&self, strict: bool, err: E) -> Result<u16, E>
pub fn get_if_init<E>(&self, strict: bool, err: E) -> Result<u16, E>
Reads the word if it is properly initialized under strictness requirements, returning its unsigned representation.
This function is more cognizant of word initialization than Word::get
.
- In non-strict mode (
strict == false
), this function unconditionally allows access to the data regardless of initialization state. - In strict mode (
strict == true
), this function verifiesself
is fully initialized, raising the provided error if not.
Sourcepub fn set(&mut self, data: u16)
pub fn set(&mut self, data: u16)
Writes to the word.
This sets the word to the data
value assuming it is fully initialized
and correspondingly sets the initialization state to be fully initialized.
If the initialization state of the data
value should be checked before
trying to write to the word, then Word::set_if_init
should be used instead.
Sourcepub fn set_if_init<E>(
&mut self,
data: Word,
strict: bool,
err: E,
) -> Result<(), E>
pub fn set_if_init<E>( &mut self, data: Word, strict: bool, err: E, ) -> Result<(), E>
Writes to the word while verifying the data stored is properly initialized under strictness requirements.
This function is more cognizant of word initialization than Word::set
.
- In non-strict mode, this function preserves the initialization data of the
data
argument. - In strict mode, this function verifies
data
is fully initialized, raising the provided error if not.
Sourcepub fn clear_init(&mut self)
pub fn clear_init(&mut self)
Clears initialization of this word.
Trait Implementations§
Source§impl Add for Word
impl Add for Word
Source§impl AddAssign<i16> for Word
impl AddAssign<i16> for Word
Source§fn add_assign(&mut self, rhs: i16)
fn add_assign(&mut self, rhs: i16)
Increments the word by the provided value.
If the word was fully initialized, its updated value is also fully initialized. Otherwise, the resulting word is fully uninitialized.
Source§impl AddAssign<u16> for Word
impl AddAssign<u16> for Word
Source§fn add_assign(&mut self, rhs: u16)
fn add_assign(&mut self, rhs: u16)
Increments the word by the provided value.
If the word was fully initialized, its updated value is also fully initialized. Otherwise, the resulting word is fully uninitialized.
Source§impl AddAssign for Word
impl AddAssign for Word
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl BitAndAssign for Word
impl BitAndAssign for Word
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&=
operation. Read moreSource§impl Sub for Word
impl Sub for Word
Source§impl SubAssign<i16> for Word
impl SubAssign<i16> for Word
Source§fn sub_assign(&mut self, rhs: i16)
fn sub_assign(&mut self, rhs: i16)
Decrements the word by the provided value.
If the word was fully initialized, its updated value is also fully initialized. Otherwise, the resulting word is fully uninitialized.
Source§impl SubAssign<u16> for Word
impl SubAssign<u16> for Word
Source§fn sub_assign(&mut self, rhs: u16)
fn sub_assign(&mut self, rhs: u16)
Decrements the word by the provided value.
If the word was fully initialized, its updated value is also fully initialized. Otherwise, the resulting word is fully uninitialized.
Source§impl SubAssign for Word
impl SubAssign for Word
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more