[???] Lots of work
Cleanup un-needed elements, simplify memory management
This commit is contained in:
@@ -3,14 +3,15 @@ use std::cell::RefCell;
|
||||
use crate::emu::mmu::Memory;
|
||||
use crate::misc::emulator_error::DeviceType::RAM;
|
||||
use crate::misc::emulator_error::EmulatorError;
|
||||
use crate::misc::endian::read_big_endian_u24;
|
||||
use crate::misc::result::EmulatorResult;
|
||||
|
||||
const MAPPED_MEMORY_NOT_REQUIRED: u8 = 8;
|
||||
const MEM_LENGTH: usize = (2 << 24) - (MAPPED_MEMORY_NOT_REQUIRED as usize);
|
||||
|
||||
pub const MEM_LENGTH: usize = 2 << 23;
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RamMemory {
|
||||
data: RefCell<Box<[u8; MEM_LENGTH]>>,
|
||||
pub data: RefCell<Box<[u8; MEM_LENGTH]>>,
|
||||
}
|
||||
|
||||
impl RamMemory {
|
||||
@@ -24,6 +25,34 @@ impl RamMemory {
|
||||
data: data_refcell
|
||||
})
|
||||
}
|
||||
pub fn try_from(existing_data:&[u8]) -> EmulatorResult<RamMemory>{
|
||||
let alloc_result = vec![0u8; MEM_LENGTH].into_boxed_slice();
|
||||
// get box of fixed size
|
||||
let mut fixed_size_alloc_box:Box<[u8;MEM_LENGTH]> = alloc_result.try_into().unwrap();
|
||||
fixed_size_alloc_box.copy_from_slice(&existing_data);
|
||||
|
||||
Ok(RamMemory{
|
||||
data: RefCell::new(fixed_size_alloc_box)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn try_get_u24(&self,index:u32)->EmulatorResult<u32>{
|
||||
const U24_LEN:usize = 3;
|
||||
let index_usize = index as usize;
|
||||
let data = self.data.borrow();
|
||||
let a = data.get(index_usize..(index_usize +U24_LEN)).ok_or(EmulatorError::UnreachableMemory(RAM, index))?;
|
||||
Ok(read_big_endian_u24(a.try_into()?))
|
||||
}
|
||||
|
||||
pub fn get_block(&self,address:u32,output:&mut [u8])->EmulatorResult<()>{
|
||||
let address = address as usize;
|
||||
let data = self.data.borrow();
|
||||
let data_requested_slice = data.get(address..address+output.len()).ok_or(EmulatorError::UnreachableMemory(RAM,address as u32))?;
|
||||
output.copy_from_slice(data_requested_slice);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user