diff --git a/src/cpu.rs b/src/cpu.rs index d40ea12..606a955 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -3,98 +3,10 @@ // TODO: in the instruction match statement, all of the register ones have `let result` inside the if statement // move this up to match all of the other ones (or move all of the other ones down, which would probably be better anyways) -use crate::{Bus, Overlay}; -use std::sync::{Arc, Mutex}; +use crate::Bus; const DEBUG: bool = false; -pub struct Memory { - ram: Vec, - rom: Vec, - pub shared_memory: Arc>>, - pub overlays: Arc>>, -} - -impl Memory { - pub fn new(size: u32, shared_memory: Arc>>, overlays: Arc>>, read_only_memory: Vec) -> Self { - Memory { - ram: vec![0; size as usize], - rom: read_only_memory, - shared_memory, - overlays, - } - } - pub fn read_8(&self, address: u32) -> u8 { - let address = address as usize; - if address < 0x02000000 { - self.ram[address] - } else if address >= 0xF0000000 { - let address = address - 0xF0000000; - self.rom[address] - } else { - let address = address - 0x02000000; - let shared_memory_lock = self.shared_memory.lock().unwrap(); - shared_memory_lock[address] - } - } - pub fn read_16(&self, address: u32) -> u16 { - (self.read_8(address + 1) as u16) << 8 | - (self.read_8(address) as u16) - } - pub fn read_32(&self, address: u32) -> u32 { - let address = address as usize; - if address < 0x02000000 { - (self.ram[address + 3] as u32) << 24 | - (self.ram[address + 2] as u32) << 16 | - (self.ram[address + 1] as u32) << 8 | - (self.ram[address] as u32) - } else if address >= 0xF0000000 { - let address = address - 0xF0000000; - (self.rom[address + 3] as u32) << 24 | - (self.rom[address + 2] as u32) << 16 | - (self.rom[address + 1] as u32) << 8 | - (self.rom[address] as u32) - } else { - let address = address - 0x02000000; - let shared_memory_lock = self.shared_memory.lock().unwrap(); - (shared_memory_lock[address + 3] as u32) << 24 | - (shared_memory_lock[address + 2] as u32) << 16 | - (shared_memory_lock[address + 1] as u32) << 8 | - (shared_memory_lock[address] as u32) - } - } - pub fn write_8(&mut self, address: u32, byte: u8) { - let address = address as usize; - if address < 0x02000000 { - self.ram[address] = byte; - } else { - let address = address - 0x02000000; - let mut shared_memory_lock = self.shared_memory.lock().unwrap(); - shared_memory_lock[address] = byte; - } - } - pub fn write_16(&mut self, address: u32, half: u16) { - self.write_8(address, (half & 0x00FF) as u8); - self.write_8(address + 1, (half >> 8) as u8); - } - pub fn write_32(&mut self, address: u32, word: u32) { - let address = address as usize; - if address < 0x02000000 { - self.ram[address] = (word & 0x000000FF) as u8; - self.ram[address + 1] = ((word & 0x0000FF00) >> 8) as u8; - self.ram[address + 2] = ((word & 0x00FF0000) >> 16) as u8; - self.ram[address + 3] = ((word & 0xFF000000) >> 24) as u8; - } else { - let address = address - 0x02000000; - let mut shared_memory_lock = self.shared_memory.lock().unwrap(); - shared_memory_lock[address] = (word & 0x000000FF) as u8; - shared_memory_lock[address + 1] = ((word & 0x0000FF00) >> 8) as u8; - shared_memory_lock[address + 2] = ((word & 0x00FF0000) >> 16) as u8; - shared_memory_lock[address + 3] = ((word & 0xFF000000) >> 24) as u8; - } - } -} - #[derive(Copy, Clone)] pub struct Flag { pub carry: bool, diff --git a/src/main.rs b/src/main.rs index d00cc47..50ed330 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,11 @@ pub mod bus; pub mod cpu; +pub mod memory; pub mod mouse; use bus::Bus; -use cpu::{Cpu, Memory, Interrupt}; +use cpu::{Cpu, Interrupt}; +use memory::Memory; use mouse::Mouse; use std::env; diff --git a/src/memory.rs b/src/memory.rs new file mode 100644 index 0000000..84037ff --- /dev/null +++ b/src/memory.rs @@ -0,0 +1,92 @@ +// memory.rs + +use crate::{Overlay}; + +use std::sync::{Arc, Mutex}; + +pub struct Memory { + ram: Vec, + rom: Vec, + pub shared_memory: Arc>>, + pub overlays: Arc>>, +} + +impl Memory { + pub fn new(size: u32, shared_memory: Arc>>, overlays: Arc>>, read_only_memory: Vec) -> Self { + Memory { + ram: vec![0; size as usize], + rom: read_only_memory, + shared_memory, + overlays, + } + } + pub fn read_8(&self, address: u32) -> u8 { + let address = address as usize; + if address < 0x02000000 { + self.ram[address] + } else if address >= 0xF0000000 { + let address = address - 0xF0000000; + self.rom[address] + } else { + let address = address - 0x02000000; + let shared_memory_lock = self.shared_memory.lock().unwrap(); + shared_memory_lock[address] + } + } + pub fn read_16(&self, address: u32) -> u16 { + (self.read_8(address + 1) as u16) << 8 | + (self.read_8(address) as u16) + } + pub fn read_32(&self, address: u32) -> u32 { + let address = address as usize; + if address < 0x02000000 { + (self.ram[address + 3] as u32) << 24 | + (self.ram[address + 2] as u32) << 16 | + (self.ram[address + 1] as u32) << 8 | + (self.ram[address] as u32) + } else if address >= 0xF0000000 { + let address = address - 0xF0000000; + (self.rom[address + 3] as u32) << 24 | + (self.rom[address + 2] as u32) << 16 | + (self.rom[address + 1] as u32) << 8 | + (self.rom[address] as u32) + } else { + let address = address - 0x02000000; + let shared_memory_lock = self.shared_memory.lock().unwrap(); + (shared_memory_lock[address + 3] as u32) << 24 | + (shared_memory_lock[address + 2] as u32) << 16 | + (shared_memory_lock[address + 1] as u32) << 8 | + (shared_memory_lock[address] as u32) + } + } + pub fn write_8(&mut self, address: u32, byte: u8) { + let address = address as usize; + if address < 0x02000000 { + self.ram[address] = byte; + } else { + let address = address - 0x02000000; + let mut shared_memory_lock = self.shared_memory.lock().unwrap(); + shared_memory_lock[address] = byte; + } + } + pub fn write_16(&mut self, address: u32, half: u16) { + self.write_8(address, (half & 0x00FF) as u8); + self.write_8(address + 1, (half >> 8) as u8); + } + pub fn write_32(&mut self, address: u32, word: u32) { + let address = address as usize; + if address < 0x02000000 { + self.ram[address] = (word & 0x000000FF) as u8; + self.ram[address + 1] = ((word & 0x0000FF00) >> 8) as u8; + self.ram[address + 2] = ((word & 0x00FF0000) >> 16) as u8; + self.ram[address + 3] = ((word & 0xFF000000) >> 24) as u8; + } else { + let address = address - 0x02000000; + let mut shared_memory_lock = self.shared_memory.lock().unwrap(); + shared_memory_lock[address] = (word & 0x000000FF) as u8; + shared_memory_lock[address + 1] = ((word & 0x0000FF00) >> 8) as u8; + shared_memory_lock[address + 2] = ((word & 0x00FF0000) >> 16) as u8; + shared_memory_lock[address + 3] = ((word & 0xFF000000) >> 24) as u8; + } + } +} \ No newline at end of file