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
49
50
51
52
53
54
55
use crate::memory::{ActiveInterrupt, Memory};

/// Memory that does nothing when read or written to.
#[derive(Default)]
pub struct NullMemory {
  warn: Option<&'static str>,
}

impl NullMemory {
  /// Create a new NullMemory that will not warn when read or written to.
  pub fn new() -> Self {
    Self { warn: None }
  }

  /// Create a new NullMemory that will warn when read or written to.
  pub fn with_warnings(message: &'static str) -> Self {
    Self {
      warn: Some(message),
    }
  }
}

impl Memory for NullMemory {
  fn read(&mut self, address: u16) -> u8 {
    if let Some(message) = self.warn {
      println!("attempted to read from {message} at address {address:04x}",);
    }
    0
  }

  fn write(&mut self, address: u16, _value: u8) {
    if let Some(message) = self.warn {
      println!("attempted to write to {message} at address {address:04x}",);
    }
  }

  fn reset(&mut self) {}

  fn poll(&mut self, _cycles_since_poll: u64, _total_cycle_count: u64) -> ActiveInterrupt {
    ActiveInterrupt::None
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_null() {
    let mut memory = NullMemory::new();
    assert_eq!(memory.read(0x0000), 0);
    memory.write(0x0000, 0x12);
    assert_eq!(memory.read(0x0000), 0);
  }
}