55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
use crate::emulator::libretro_memory_map::LibRetroMemoryMap;
|
|
|
|
pub struct MemorySliceHandle<'a> {
|
|
pub offset: usize,
|
|
pub length: usize,
|
|
pub bank_switch: usize,
|
|
pub slice: Option<&'a mut [u8]>,
|
|
pub last_read_value: Option<Vec<u8>>,
|
|
}
|
|
|
|
impl MemorySliceHandle<'_> {
|
|
pub fn create(offset: usize, length: usize, bank_switch: usize, memory_map: &LibRetroMemoryMap) -> Self {
|
|
let slice = memory_map.get_slice_from_region(offset, length, bank_switch);
|
|
|
|
MemorySliceHandle {
|
|
offset: offset,
|
|
length: length,
|
|
bank_switch: bank_switch,
|
|
slice,
|
|
last_read_value: None
|
|
}
|
|
}
|
|
|
|
pub fn get_copy_if_changed_since_last_read(&mut self) -> Option<Vec<u8>> {
|
|
match &mut self.slice {
|
|
None => None,
|
|
Some(data) => match &mut self.last_read_value {
|
|
None => {
|
|
// last read value isn't allocated yet
|
|
let last = data.to_vec();
|
|
self.last_read_value = Some(last);
|
|
self.last_read_value.clone()
|
|
},
|
|
Some(last) => {
|
|
let mut index: usize = 0;
|
|
let mut any_changes = false;
|
|
|
|
for value in data.into_iter() {
|
|
if last[index] != *value {
|
|
last[index] = *value;
|
|
any_changes = true;
|
|
}
|
|
index += 1;
|
|
}
|
|
|
|
if any_changes {
|
|
return Some(last.clone());
|
|
}
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
} |