summaryrefslogtreecommitdiff
path: root/src/components/device_bus.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2025-07-03 14:53:16 +1200
committerBen Bridle <bridle.benjamin@gmail.com>2025-07-03 14:53:16 +1200
commit701448be2c3c58e30960d46f090bf08adfc02832 (patch)
tree409be74cdbb4bf311cb98fbee14f67fcd9c680d0 /src/components/device_bus.rs
downloadbedrock-core-701448be2c3c58e30960d46f090bf08adfc02832.zip
Initial commit
Diffstat (limited to 'src/components/device_bus.rs')
-rw-r--r--src/components/device_bus.rs48
1 files changed, 48 insertions, 0 deletions
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<Signal> {
+ 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<Signal>;
+ /// 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<Signal> { None }
+ fn wake(&mut self) -> bool { false }
+ fn reset(&mut self) { }
+}