1use crate::PortError;
2
3pub trait PortBus {
4 fn input(&mut self, port: u8) -> Result<u8, PortError>;
5 fn output(&mut self, port: u8, value: u8) -> Result<(), PortError>;
6}
7
8#[derive(Clone, Debug)]
9pub struct NullBus {
10 ports: [u8; 256],
11 writes: Vec<(u8, u8)>,
12}
13
14impl Default for NullBus {
15 fn default() -> Self {
16 Self {
17 ports: [0; 256],
18 writes: Vec::new(),
19 }
20 }
21}
22
23impl NullBus {
24 pub fn set_input(&mut self, port: u8, value: u8) {
25 self.ports[port as usize] = value;
26 }
27
28 pub fn writes(&self) -> &[(u8, u8)] {
29 &self.writes
30 }
31}
32
33impl PortBus for NullBus {
34 fn input(&mut self, port: u8) -> Result<u8, PortError> {
35 Ok(self.ports[port as usize])
36 }
37
38 fn output(&mut self, port: u8, value: u8) -> Result<(), PortError> {
39 self.ports[port as usize] = value;
40 self.writes.push((port, value));
41 Ok(())
42 }
43}