fox32: Move Memory to a separate file

This commit is contained in:
ry755 2022-01-27 16:42:57 -08:00 committed by Ry
parent 994f6d7152
commit 99621394ec
3 changed files with 96 additions and 90 deletions

View File

@ -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<u8>,
rom: Vec<u8>,
pub shared_memory: Arc<Mutex<Vec<u8>>>,
pub overlays: Arc<Mutex<Vec<Overlay>>>,
}
impl Memory {
pub fn new(size: u32, shared_memory: Arc<Mutex<Vec<u8>>>, overlays: Arc<Mutex<Vec<Overlay>>>, read_only_memory: Vec<u8>) -> 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,

View File

@ -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;

92
src/memory.rs Normal file
View File

@ -0,0 +1,92 @@
// memory.rs
use crate::{Overlay};
use std::sync::{Arc, Mutex};
pub struct Memory {
ram: Vec<u8>,
rom: Vec<u8>,
pub shared_memory: Arc<Mutex<Vec<u8>>>,
pub overlays: Arc<Mutex<Vec<Overlay>>>,
}
impl Memory {
pub fn new(size: u32, shared_memory: Arc<Mutex<Vec<u8>>>, overlays: Arc<Mutex<Vec<Overlay>>>, read_only_memory: Vec<u8>) -> 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;
}
}
}