Add basic memory for load instruction backing
parent
af95676b15
commit
4c6eec264a
|
@ -1,4 +1,5 @@
|
|||
#![warn(clippy::all, clippy::pedantic)]
|
||||
|
||||
mod memory;
|
||||
mod register;
|
||||
mod run;
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
const MAX_MEMORY_ADDRESS: usize = 0xFFFF;
|
||||
|
||||
/// `Memory` is an abstraction over the Gameboy's memory space.
|
||||
///
|
||||
/// In its current state, this implementation ignores literally all properties.
|
||||
/// of the Gameboy's memory, and should not be relied upon for anything other than development
|
||||
#[derive(Debug, Clone)]
|
||||
struct Memory {
|
||||
data: [u8; MAX_MEMORY_ADDRESS],
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn get(&self, index: usize) -> Option<u8> {
|
||||
self.data.get(index).copied()
|
||||
}
|
||||
|
||||
pub fn set(&mut self, index: usize, value: u8) -> Option<u8> {
|
||||
let val_ref = self.data.get_mut(index)?;
|
||||
*val_ref = value;
|
||||
|
||||
Some(*val_ref)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
data: [0_u8; MAX_MEMORY_ADDRESS],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_set_valid_address() {
|
||||
let mut memory = Memory::new();
|
||||
let set_val = memory.set(0xCCCC, 100);
|
||||
assert_eq!(Some(100), set_val);
|
||||
|
||||
let get_val = memory.get(0xCCCC);
|
||||
assert_eq!(Some(100), get_val);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_invalid_address() {
|
||||
let memory = Memory::new();
|
||||
let get_val = memory.get(0xCC_CC_CC_CC_CC);
|
||||
assert_eq!(None, get_val);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_invalid_address() {
|
||||
let mut memory = Memory::new();
|
||||
let set_val = memory.set(0xCC_CC_CC_CC_CC, 100);
|
||||
assert_eq!(None, set_val);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue