rustboy_core/internal/memory/
linear_memory.rs

1use serde::{Deserialize, Serialize};
2use serde_with::serde_as;
3
4use super::memory::Memory;
5
6#[serde_as]
7#[derive(Serialize, Deserialize)]
8pub struct LinearMemory<const SIZE: usize, const START_ADDRESS: u16> {
9  #[serde_as(as = "[_;SIZE]")]
10  bytes: [u8; SIZE],
11}
12
13impl<const SIZE: usize, const START_ADDRESS: u16> Memory for LinearMemory<SIZE, START_ADDRESS> {
14  fn read(&self, address: u16) -> u8 {
15    self.bytes[address as usize - START_ADDRESS as usize]
16  }
17
18  fn write(&mut self, address: u16, value: u8) {
19    self.bytes[address as usize - START_ADDRESS as usize] = value
20  }
21}
22
23impl<const SIZE: usize, const START_ADDRESS: u16> LinearMemory<SIZE, START_ADDRESS> {
24  pub fn new() -> LinearMemory<SIZE, START_ADDRESS> {
25    LinearMemory {
26      bytes: [0; SIZE],
27    }
28  }
29}