ft: initial work in interpreter
This commit is contained in:
75
src/interpreter/register.rs
Normal file
75
src/interpreter/register.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use crate::operands::{Byte, Word};
|
||||
use core::fmt;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Register {
|
||||
pub ax: AX,
|
||||
pub bx: BX,
|
||||
pub cx: CX,
|
||||
pub dx: DX,
|
||||
pub sp: Word,
|
||||
pub bp: Word,
|
||||
pub si: Word,
|
||||
pub di: Word,
|
||||
}
|
||||
|
||||
impl Register {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
ax: AX::new(),
|
||||
bx: BX::new(),
|
||||
cx: CX::new(),
|
||||
dx: DX::new(),
|
||||
sp: 0,
|
||||
bp: 0,
|
||||
si: 0,
|
||||
di: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Register {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"AX({}) BX({}) CX({}) DX({}) SP({:04x}) BP({:04x}) SI({:04x}) DI({:04x})",
|
||||
self.ax, self.bx, self.cx, self.dx, self.sp, self.bp, self.si, self.di
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! gen_regs {
|
||||
($ident:ident) => {
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct $ident {
|
||||
upper: Byte,
|
||||
lower: Byte,
|
||||
}
|
||||
|
||||
impl $ident {
|
||||
pub fn new() -> Self {
|
||||
Self { upper: 0, lower: 0 }
|
||||
}
|
||||
|
||||
pub fn read(self) -> Word {
|
||||
Word::from_le_bytes([self.lower, self.upper])
|
||||
}
|
||||
|
||||
pub fn write(&mut self, word: Word) {
|
||||
let [low, high]: [u8; 2] = word.to_le_bytes();
|
||||
self.lower = low;
|
||||
self.upper = high;
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for $ident {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:04x}", Word::from_le_bytes([self.lower, self.upper]))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
gen_regs!(AX);
|
||||
gen_regs!(BX);
|
||||
gen_regs!(CX);
|
||||
gen_regs!(DX);
|
||||
Reference in New Issue
Block a user