From b2aa3fd0e0f0cfbc7ed4387ffaea0f1ed663c59f Mon Sep 17 00:00:00 2001 From: Jared Burce Date: Sat, 6 Mar 2021 04:18:48 -0800 Subject: [PATCH] emu: MOV generic. Drop Reg16 register wrapper type --- src/emu/i8088.rs | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/emu/i8088.rs b/src/emu/i8088.rs index d5d2646..3e42c8d 100644 --- a/src/emu/i8088.rs +++ b/src/emu/i8088.rs @@ -8,10 +8,10 @@ use emu::pc::Bus; #[derive(Clone, Copy, Debug, Default)] pub struct i8088 { // Data Registers - pub a: Reg16, - pub b: Reg16, - pub c: Reg16, - pub d: Reg16, + pub a: u16, + pub b: u16, + pub c: u16, + pub d: u16, // Index Registers pub si: u16, // Source Index @@ -50,18 +50,17 @@ pub struct Flags { } mod ops { - use emu::i8088::segoff_to_addr; + use emu::i8088::{LValue, RValue, segoff_to_addr}; use emu::byteorder::{ByteOrder, LittleEndian}; pub fn call(ip: &mut u16, ss: u16, sp: &mut u16, mem: &mut [u8], addr: i16) { let target = ip.wrapping_add(addr as u16); - trace!("CALL 0x{:04X}", target); push16(ss, sp, mem, *ip); *ip = target; } - pub fn mov(reg: &mut super::Reg16, val: u16) { - reg.x = val; + pub fn mov(dst: &mut impl LValue, src: &impl RValue) { + dst.write(src.read()); } pub fn pop16(ss: u16, sp: &mut u16, mem: &[u8]) -> u16 { @@ -77,10 +76,28 @@ mod ops { } pub fn ret(ip: &mut u16, ss: u16, sp: &mut u16, mem: &[u8]) { - trace!("RET"); *ip = pop16(ss, sp, mem); } +} +pub trait LValue { + fn write(&mut self, val: T); +} + +pub trait RValue { + fn read(&self) -> T; +} + +impl LValue for u16 { + fn write(&mut self, val: u16) { + *self = val; + } +} + +impl RValue for u16 { + fn read(&self) -> u16 { + *self + } } macro_rules! step { @@ -110,7 +127,7 @@ macro_rules! step { let mut buf = [0; 2]; buf[0] = $cpu.next_ip($bus); buf[1] = $cpu.next_ip($bus); - step!(@arg $cookie, LittleEndian::read_u16(&buf)) + step!(@arg $cookie, &LittleEndian::read_u16(&buf)) } }; (@mem $cookie:tt, $cpu:ident, $bus:ident) => { @@ -222,20 +239,3 @@ impl Debug for Flags { Ok(()) } } - -#[repr(C)] -#[derive(Clone, Copy)] -pub union Reg16 { - x: u16, - lh: [u8; 2], -} - -impl Debug for Reg16 { - fn fmt(&self, fmt: &mut Formatter) -> Result<(), std::fmt::Error> { - unsafe { self.x.fmt(fmt) } - } -} - -impl Default for Reg16 { - fn default() -> Self { Self { x: 0u16 } } -}