use crate::{Byte, DeviceHandler, Uxn, Value}; pub(crate) struct Devices<'a, D>(&'a mut D); impl<'a, D> Devices<'a, D> { pub fn new(handler: &'a mut D) -> Self { Self(handler) } } impl<'a, D> Devices<'a, D> where D: DeviceHandler, { pub fn get(&mut self, uxn: &mut Uxn, addr: u8) -> u8 { self.0.device_in(uxn, addr) } pub fn set(&mut self, uxn: &mut Uxn, addr: u8, val: u8) { self.0.device_out(uxn, addr, val) } } pub(crate) trait Dei { fn dei(dev: &mut Devices, uxn: &mut Uxn, addr: Byte) -> Self; } pub(crate) trait Deo { fn deo(self, dev: &mut Devices, uxn: &mut Uxn, addr: Byte); } impl<'a, D: DeviceHandler> Devices<'a, D> { pub fn dei(&mut self, uxn: &mut Uxn, addr: Byte) -> V { V::dei(self, uxn, addr) } pub fn deo(&mut self, uxn: &mut Uxn, addr: Byte, val: V) { val.deo(self, uxn, addr) } } // impl Default for Stack { // fn default() -> Self { // Self { // data: [Default::default(); 255], // ptr: Default::default(), // } // } // } // pub struct Devices { // buffer: [u8; 256], // dei: fn(&Self, u8) -> u8, // deo: fn(&mut Self, u8, u8), // } // impl Devices { // fn new(dei: fn(&Self, u8) -> u8, deo: fn(&mut Self, u8, u8)) -> Self { // Self { // buffer: [Default::default(); 256], // dei, // deo, // } // } // } // impl Devices { // fn devr(&self, addr: u8, is_short: bool) -> u16 { // if is_short { // let msb = (self.dei)(self, addr) as u16; // let lsb = (self.dei)(self, addr.wrapping_add(1)) as u16; // msb << 8 + lsb // } else { // (self.dei)(self, addr) as u16 // } // } // fn devw(&mut self, addr: u8, value: u16, is_short: bool) { // if is_short { // (self.deo)(self, addr, (value >> 8) as u8); // (self.deo)(self, addr.wrapping_add(1), value as u8); // } else { // (self.deo)(self, addr, value as u8); // } // } // } // impl Index for Devices { // type Output = u8; // fn index(&self, addr: u8) -> &Self::Output { // self.buffer.index(addr as usize) // } // } // impl Index for Devices { // type Output = u8; // fn index(&self, addr: usize) -> &Self::Output { // self.buffer.index(addr) // } // } // impl IndexMut for Devices { // fn index_mut(&mut self, addr: u8) -> &mut Self::Output { // self.buffer.index_mut(addr as usize) // } // } // impl IndexMut for Devices { // fn index_mut(&mut self, addr: usize) -> &mut Self::Output { // self.buffer.index_mut(addr) // } // }