From a48ff2897e0745d7a41831daea2a0136cf114998 Mon Sep 17 00:00:00 2001 From: sevonj <100710152+sevonj@users.noreply.github.com> Date: Fri, 29 Dec 2023 14:26:04 +0200 Subject: [PATCH] Device Supertrait --- src/emulator/devices.rs | 12 ++++++------ src/emulator/devices/dev_crt.rs | 8 +++++++- src/emulator/devices/dev_kbd.rs | 8 +++++++- src/emulator/devices/dev_rtc.rs | 8 +++++++- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/emulator/devices.rs b/src/emulator/devices.rs index e4e64c1..2b7ee12 100644 --- a/src/emulator/devices.rs +++ b/src/emulator/devices.rs @@ -7,17 +7,17 @@ use self::{ dev_crt::DevCRT, dev_display_classic::DevDisplayClassic, dev_kbd::DevKBD, dev_pic::DevPIC, - dev_ram::DevRAM, dev_rtc::DevRTC, dev_psg::DevPSG, + dev_psg::DevPSG, dev_ram::DevRAM, dev_rtc::DevRTC, }; mod dev_crt; mod dev_display_classic; mod dev_kbd; +mod dev_midi; +mod dev_pad; mod dev_pic; +mod dev_psg; mod dev_ram; mod dev_rtc; -mod dev_psg; -mod dev_midi; -mod dev_pad; #[cfg(test)] mod tests; @@ -32,7 +32,7 @@ pub(crate) trait Device { } /// Memory Mapped IO: Any device that occupies memory addresses shall implement this trait. -pub(crate) trait MMIO { +pub(crate) trait MMIO: Device { /// MMIO read. In implementation, address is **relative to device offset**, not global. So your first addr is always 0x0. fn read(&mut self, addr: usize) -> Result; /// MMIO write. In implementation, address is **relative to device offset**, not global. So your first addr is always 0x0. @@ -40,7 +40,7 @@ pub(crate) trait MMIO { } /// Port Mapped IO: Any device that occupies ports shall implement this trait. -pub(crate) trait PMIO { +pub(crate) trait PMIO: Device { /// PMIO read. In implementation, port index is **relative to device offset**, not global. So your first port is always 0x0. fn read_port(&mut self, port: u8) -> Result; /// PMIO write. In implementation, port index is **relative to device offset**, not global. So your first port is always 0x0. diff --git a/src/emulator/devices/dev_crt.rs b/src/emulator/devices/dev_crt.rs index d131729..d0bea40 100644 --- a/src/emulator/devices/dev_crt.rs +++ b/src/emulator/devices/dev_crt.rs @@ -3,7 +3,7 @@ //! Communication happens via an mpsc channel, which could be refactored away. //! //! -use super::PMIO; +use super::{Device, PMIO}; use std::sync::mpsc::Sender; /// Legacy output device =crt @@ -23,6 +23,12 @@ impl DevCRT { } } +impl Device for DevCRT { + fn reset(&mut self) {} + fn on(&mut self) {} + fn off(&mut self) {} +} + /// Port 0: crt output impl PMIO for DevCRT { fn read_port(&mut self, _port: u8) -> Result { diff --git a/src/emulator/devices/dev_kbd.rs b/src/emulator/devices/dev_kbd.rs index a94694e..3a6d67f 100644 --- a/src/emulator/devices/dev_kbd.rs +++ b/src/emulator/devices/dev_kbd.rs @@ -3,7 +3,7 @@ //! //! This will freeze the emulator thread until a reply is received. //! -use super::PMIO; +use super::{Device, PMIO}; use std::sync::mpsc::{Receiver, Sender}; /// Legacy input device =kbd @@ -28,6 +28,12 @@ impl DevKBD { } } +impl Device for DevKBD { + fn reset(&mut self) {} + fn on(&mut self) {} + fn off(&mut self) {} +} + impl PMIO for DevKBD { fn read_port(&mut self, port: u8) -> Result { if port != 0 { diff --git a/src/emulator/devices/dev_rtc.rs b/src/emulator/devices/dev_rtc.rs index ecc72c7..5589b45 100644 --- a/src/emulator/devices/dev_rtc.rs +++ b/src/emulator/devices/dev_rtc.rs @@ -1,7 +1,7 @@ //! //! Real-time clock in a port. Returns local 32-bit unix-time. //! -use super::PMIO; +use super::{Device, PMIO}; use chrono::Local; /// Real-time clock in a port. Returns local 32-bit unix-time. @@ -13,6 +13,12 @@ impl Default for DevRTC { } } +impl Device for DevRTC { + fn reset(&mut self) {} + fn on(&mut self) {} + fn off(&mut self) {} +} + impl PMIO for DevRTC { fn read_port(&mut self, port: u8) -> Result { if port != 0 {