Skip to content

Commit

Permalink
Device Supertrait
Browse files Browse the repository at this point in the history
  • Loading branch information
sevonj committed Dec 29, 2023
1 parent 1197d58 commit a48ff28
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/emulator/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,15 +32,15 @@ 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<i32, ()>;
/// MMIO write. In implementation, address is **relative to device offset**, not global. So your first addr is always 0x0.
fn write(&mut self, addr: usize, value: i32) -> Result<(), ()>;
}

/// 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<i32, ()>;
/// PMIO write. In implementation, port index is **relative to device offset**, not global. So your first port is always 0x0.
Expand Down
8 changes: 7 additions & 1 deletion src/emulator/devices/dev_crt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<i32, ()> {
Expand Down
8 changes: 7 additions & 1 deletion src/emulator/devices/dev_kbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<i32, ()> {
if port != 0 {
Expand Down
8 changes: 7 additions & 1 deletion src/emulator/devices/dev_rtc.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<i32, ()> {
if port != 0 {
Expand Down

0 comments on commit a48ff28

Please sign in to comment.