Public accessors

This commit is contained in:
Erin 2023-11-02 16:53:44 +01:00
parent daca2a9847
commit 50722d4ee8
2 changed files with 26 additions and 28 deletions

View file

@ -13,7 +13,10 @@
#![no_std] #![no_std]
#![cfg_attr(feature = "nightly", feature(fn_align))] #![cfg_attr(feature = "nightly", feature(fn_align))]
use mem::{Address, Memory}; use {
mem::{Address, Memory},
value::ValueVariant,
};
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
extern crate alloc; extern crate alloc;
@ -66,6 +69,27 @@ where
copier: None, copier: None,
} }
} }
/// Read register
#[inline(always)]
pub fn read_reg(&self, n: u8) -> Value {
unsafe { *self.registers.get_unchecked(n as usize) }
}
/// Write a register.
/// Writing to register 0 is no-op.
#[inline(always)]
pub fn write_reg<T: ValueVariant>(&mut self, n: u8, value: T) {
if n != 0 {
unsafe {
core::ptr::copy_nonoverlapping(
(&value as *const T).cast::<u8>(),
self.registers.as_mut_ptr().add(n.into()).cast::<u8>(),
core::mem::size_of::<T>(),
);
};
}
}
} }
/// Virtual machine halt error /// Virtual machine halt error

View file

@ -3,12 +3,7 @@
//! Have fun //! Have fun
use { use {
super::{ super::{bmc::BlockCopier, mem::Memory, value::ValueVariant, Vm, VmRunError, VmRunOk},
bmc::BlockCopier,
mem::Memory,
value::{Value, ValueVariant},
Vm, VmRunError, VmRunOk,
},
crate::{ crate::{
mem::{addr::AddressOp, Address}, mem::{addr::AddressOp, Address},
value::CheckedDivRem, value::CheckedDivRem,
@ -534,27 +529,6 @@ where
} }
} }
/// Read register
#[inline(always)]
fn read_reg(&self, n: u8) -> Value {
unsafe { *self.registers.get_unchecked(n as usize) }
}
/// Write a register.
/// Writing to register 0 is no-op.
#[inline(always)]
fn write_reg<T: ValueVariant>(&mut self, n: u8, value: T) {
if n != 0 {
unsafe {
core::ptr::copy_nonoverlapping(
(&value as *const T).cast::<u8>(),
self.registers.as_mut_ptr().add(n.into()).cast::<u8>(),
core::mem::size_of::<T>(),
);
};
}
}
/// Load / Store Address check-computation überfunction /// Load / Store Address check-computation überfunction
#[inline(always)] #[inline(always)]
unsafe fn ldst_addr_uber( unsafe fn ldst_addr_uber(