r/w slices

This commit is contained in:
Vivian Lim 2019-12-24 16:24:10 -08:00
parent 6b330d64ce
commit ff18f2d382
2 changed files with 23 additions and 17 deletions

View File

@ -8,17 +8,13 @@ pub struct ReadWriteMemoryMap {
}
impl ReadWriteMemoryMap {
pub fn peek_region(&self, offset: usize, length: usize, bank_switch: usize) -> Option<&'static [u8]> {
pub fn get_slice_from_region(&self, offset: usize, length: usize, bank_switch: usize) -> Option<&'static mut [u8]> {
match self.find_bank(offset, length, bank_switch) {
Some(ptr) => Some(unsafe { std::slice::from_raw_parts(ptr as *const u8, length) }),
Some(ptr) => Some(unsafe { std::slice::from_raw_parts_mut(ptr as *mut u8, length) }),
None => None
}
}
pub fn poke_region<T>(&mut self, offset: usize, data: T, bank_switch: usize) {
}
fn find_bank(&self, offset: usize, length: usize, bank_switch: usize) -> Option<*const std::ffi::c_void> {
if self.regions.len() == 0 {
return None;

View File

@ -3,6 +3,7 @@ use std::fmt::Display;
use crate::sync::memory_rw::ReadWriteMemoryMap;
use serde::{Serialize, Deserialize};
use ferretro::retro::wrapper::LibretroWrapper; // want LibretroWrapper.api : LibretroApi.get_memory()
use crate::num::ToPrimitive;
#[derive(Default)]
pub struct SyncedPokemonRedBlue {
@ -25,16 +26,16 @@ struct EmulatorMemory {
offset: usize,
length: usize,
bank_switch: usize,
data: Option<&'static [u8]>,
slice: Option<&'static mut [u8]>,
}
impl EmulatorMemory {
pub fn get_data(&mut self, mem: &ReadWriteMemoryMap) -> Option<&'static [u8]> {
match self.data {
Some(_) => self.data,
pub fn get_slice(&mut self, mem: &ReadWriteMemoryMap) -> &mut Option<&'static mut [u8]> {
match self.slice {
Some(_) => &mut self.slice,
None => {
self.data = mem.peek_region(self.offset, self.length, self.bank_switch);
self.data
self.slice = mem.get_slice_from_region(self.offset, self.length, self.bank_switch);
&mut self.slice
}
}
}
@ -48,12 +49,21 @@ struct BattleContext {
impl SyncedPokemonRedBlue {
pub fn update_from_mem(&mut self, mem: &ReadWriteMemoryMap) {
let battle_context = BattleContext {
active_pokemon: match self.raw.active_pokemon_raw.get_data(mem) {
active_pokemon: match self.raw.active_pokemon_raw.get_slice(mem) {
Some(d) => num::FromPrimitive::from_u8(d[0xB]).unwrap_or(Pokemon::Unknown),
None => Pokemon::Unknown
}
};
// test to see that we can indeed write back to the slice
// match self.raw.active_pokemon_raw.get_slice(mem){
// Some(s) => {
// s[0xD] = 69
// },
// _ => ()
// }
self.battle_context = battle_context;
}
@ -73,19 +83,19 @@ impl Default for Raw {
offset: 0xd009,
length: 0x27,
bank_switch: 0,
data: None,
slice: None,
},
player_and_party: EmulatorMemory {
offset: 0xd158,
length: 0x19e,
bank_switch: 0,
data: None,
slice: None,
},
pokemon_out: EmulatorMemory {
offset: 0xcc2f,
length: 0x1,
bank_switch: 0,
data: None,
slice: None,
},
}
}
@ -104,7 +114,7 @@ impl Display for SyncedPokemonRedBlue {
write!(f, "active: pk {:?}", &self.battle_context.active_pokemon)
}
}
#[derive(FromPrimitive, Debug)]
#[derive(FromPrimitive, ToPrimitive, Debug)]
enum Pokemon {
Unknown = 0x0,
Abra = 0x94,