chore(interpreter): move memory access functions to computer

This is mainly to ease the usage of memory access functions, meaning
leaner access of memory with general resulution of memory accesses via
the MemoryIndex struct.
This commit is contained in:
2025-06-18 20:29:54 +09:00
parent 79dc560689
commit 4f3d864179
6 changed files with 299 additions and 274 deletions

View File

@@ -148,6 +148,16 @@ impl From<usize> for ImmediateOperand {
}
}
impl Into<MemoryIndex> for ImmediateOperand {
fn into(self) -> MemoryIndex {
MemoryIndex {
base: None,
index: None,
displacement: Some(self),
}
}
}
impl Into<Word> for ImmediateOperand {
fn into(self) -> u16 {
match self {
@@ -529,26 +539,6 @@ pub struct MemoryIndex {
pub displacement: Option<ImmediateOperand>,
}
impl MemoryIndex {
/// Creates a [`MemoryIndex`] with just a single [`Register`]
pub fn reg(reg: Register) -> Self {
Self {
base: Some(reg),
index: None,
displacement: None,
}
}
/// Creates a [`MemoryIndex`] with an [`ImmediateOperand`]
pub fn disp(disp: ImmediateOperand) -> Self {
Self {
base: None,
index: None,
displacement: Some(disp),
}
}
}
impl fmt::Display for MemoryIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.base {
@@ -607,6 +597,26 @@ pub struct Pointer16 {
pub word: Word,
}
impl Into<MemoryIndex> for Pointer16 {
fn into(self) -> MemoryIndex {
MemoryIndex {
base: None,
index: None,
displacement: Some(self.word.into()),
}
}
}
impl Add<u16> for Pointer16 {
type Output = Self;
fn add(self, rhs: u16) -> Self::Output {
Self {
word: self.word + rhs,
}
}
}
impl std::fmt::Display for Pointer16 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "ptr word [{:#04x}]", self.word)