use crate::*; pub trait DeviceBus { /// Attempt to wake the system. fn wake(&mut self) -> bool; /// Get a device by slot number. fn get_device(&mut self, slot: u8) -> Option<&mut dyn Device>; // Read from a device port. fn read(&mut self, port: u8) -> u8 { match self.get_device(port >> 4) { Some(device) => device.read(port & 0xF), None => 0, } } // Write to a device port. fn write(&mut self, port: u8, value: u8) -> Option { match self.get_device(port >> 4) { Some(device) => device.write(port & 0xF, value), None => None, } } /// Reset all connected devices. fn reset(&mut self) { for slot in 0..16 { if let Some(device) = self.get_device(slot) { device.reset(); } } } } pub trait Device { fn read(&mut self, port: u8) -> u8; fn write(&mut self, port: u8, value: u8) -> Option; /// Return and clear the wake flag for this device. fn wake(&mut self) -> bool; /// Reset device to the initial state. fn reset(&mut self); } impl Device for () { fn read(&mut self, _: u8) -> u8 { 0 } fn write(&mut self, _: u8, _: u8) -> Option { None } fn wake(&mut self) -> bool { false } fn reset(&mut self) { } }