1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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) { }
}
|