pub trait Port {
fn read(&mut self) -> u8;
fn write(&mut self, value: u8);
fn poll(&mut self, cycles_since_poll: u64, total_cycle_count: u64) -> bool;
fn reset(&mut self);
}
#[derive(Default)]
pub struct NullPort {
warn: Option<&'static str>,
}
impl NullPort {
pub fn new() -> Self {
Self { warn: None }
}
pub fn with_warnings(message: &'static str) -> Self {
Self {
warn: Some(message),
}
}
}
impl Port for NullPort {
fn read(&mut self) -> u8 {
if let Some(message) = self.warn {
println!("attempted to read from {} at address {:04x}", message, 0);
}
0
}
fn write(&mut self, _value: u8) {
if let Some(message) = self.warn {
println!("attempted to write to {} at address {:04x}", message, 0);
}
}
fn poll(&mut self, _cycles_since_poll: u64, _total_cycle_count: u64) -> bool {
false
}
fn reset(&mut self) {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_null() {
let mut port = NullPort::new();
assert_eq!(port.read(), 0);
port.write(0x12);
assert_eq!(port.read(), 0);
}
}