emu: MOV generic. Drop Reg16 register wrapper type

This commit is contained in:
2021-03-06 04:18:48 -08:00
parent be7e926842
commit b2aa3fd0e0

View File

@@ -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<T>(dst: &mut impl LValue<T>, src: &impl RValue<T>) {
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<T> {
fn write(&mut self, val: T);
}
pub trait RValue<T> {
fn read(&self) -> T;
}
impl LValue<u16> for u16 {
fn write(&mut self, val: u16) {
*self = val;
}
}
impl RValue<u16> 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 } }
}