From 701448be2c3c58e30960d46f090bf08adfc02832 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 3 Jul 2025 14:53:16 +1200 Subject: Initial commit --- src/components/device_bus.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/components/device_bus.rs (limited to 'src/components/device_bus.rs') diff --git a/src/components/device_bus.rs b/src/components/device_bus.rs new file mode 100644 index 0000000..a15d836 --- /dev/null +++ b/src/components/device_bus.rs @@ -0,0 +1,48 @@ +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) { } +} -- cgit v1.2.3-70-g09d2