reading emulator memory as u16s (crudely)

This commit is contained in:
Vivian Lim 2020-03-11 01:35:07 -07:00
parent f91c9c91f5
commit 1de59414da
6 changed files with 90 additions and 4 deletions

7
Cargo.lock generated
View File

@ -69,6 +69,11 @@ name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byte-slice-cast"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "1.3.2"
@ -210,6 +215,7 @@ name = "ferretro-synced"
version = "0.1.0"
dependencies = [
"ascii 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"byte-slice-cast 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"cc 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-channel 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1367,6 +1373,7 @@ dependencies = [
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
"checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643"
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
"checksum byte-slice-cast 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b0a5e3906bcbf133e33c1d4d95afc664ad37fbdb9f6568d8043e7ea8c27d93d3"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
"checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c"
"checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb"

View File

@ -22,5 +22,6 @@ serde_bytes = "0.11"
serde_json = "1.0.44"
websocket = "0.24.0"
ascii = "1.0.0"
byte-slice-cast = "0.3.5"
[dev-dependencies]

View File

@ -5,6 +5,7 @@ extern crate sdl2;
extern crate serde;
extern crate serde_bytes;
extern crate ascii;
extern crate byte_slice_cast;
use structopt::StructOpt;
use std::path::{Path, PathBuf};

View File

@ -1,4 +1,40 @@
use std::convert::{TryFrom, TryInto};
use crate::emulator::libretro_memory_map::LibRetroMemoryMap;
use failure::format_err;
use byte_slice_cast::*;
use std::result::Result;
pub struct MemoryBackedNumbers<'a, T: 'a + FromByteSlice> {
handle: MemorySliceHandle<'a>,
value_type: std::marker::PhantomData<T>,
}
impl<'a, T: 'a + FromByteSlice> MemoryBackedNumbers<'a, T> {
pub fn create(offset: usize, length: usize, bank_switch: usize, memory_map: &LibRetroMemoryMap) -> Self {
MemoryBackedNumbers::<'a, T> {
handle: MemorySliceHandle::create(offset, length, bank_switch, memory_map),
value_type: std::marker::PhantomData,
}
}
pub fn get(&mut self) -> Result<&mut [T], failure::Error> {
let byte_slices = self.handle.slice.as_mut().unwrap();
let slices = byte_slices.as_mut_slice_of::<T>()?;
Ok(slices)
}
/*
pub fn set(&self, value: T) -> Result<(), failure::Error> {
let new_bytes = value.try_into::<&[u8]>()?;
if new_bytes.len() != self.handle.slice.len() {
Err(format_err!("Size of bytes from converted value ({}) differs from handle size {}", new_bytes.len(), self.handle.slice.len()))
}
else {
Ok(())
}
}
*/
}
pub struct MemorySliceHandle<'a> {
pub offset: usize,
@ -21,6 +57,12 @@ impl MemorySliceHandle<'_> {
}
}
pub fn get_at_offset/*<T: AsMutSliceOf + FromByteSlice>*/(&mut self, offset: usize) -> &u16 {
let byte_slices = self.slice.as_mut().unwrap();
let slices = byte_slices.as_mut_slice_of::<u16>().unwrap();
&slices[0]
}
pub fn write_to_slice(&mut self, data: Vec<u8>) -> Result<(), failure::Error> {
match &mut self.slice {
Some(slice) => {
@ -57,7 +99,7 @@ impl MemorySliceHandle<'_> {
}
}
pub fn get_copy_if_changed_since_last_read(&mut self) -> Option<Vec<u8>> {
pub fn get_copy_if_changed(&mut self) -> Option<Vec<u8>> {
match &mut self.slice {
None => None,
Some(data) => match &mut self.last_read_value {

View File

@ -23,7 +23,7 @@ pub struct PokemonRedBlueMessage {
impl From<&mut PokemonRedBlueMemoryHandles<'_>> for PokemonRedBlueMessage {
fn from(src: &mut PokemonRedBlueMemoryHandles) -> Self {
PokemonRedBlueMessage {
active_pkmn: src.active_pokemon.get_copy_if_changed_since_last_read()
active_pkmn: src.active_pokemon.get_copy_if_changed()
}
}
}

View File

@ -6,8 +6,9 @@ use ferretro::retro::wrapper::LibretroWrapper; // want LibretroWrapper.api : Lib
use crate::num::ToPrimitive;
use crate::sync::comms::Communication;
use crate::sync::comms::CommunicationSettings;
use crate::sync::memory_slice_handle::MemorySliceHandle;
use crate::sync::memory_slice_handle::{MemoryBackedNumbers, MemorySliceHandle};
use crate::sync::game::SyncableGame;
use std::result::Result;
pub struct SyncedSonic2 {
memory_handles: Sonic2MemoryHandles<'static>,
@ -22,7 +23,7 @@ pub struct Sonic2Message {
impl From<&mut Sonic2MemoryHandles<'_>> for Sonic2Message {
fn from(src: &mut Sonic2MemoryHandles) -> Self {
Sonic2Message {
sonic_pos: src.sonic_pos.get_copy_if_changed_since_last_read()
sonic_pos: src.sonic_pos.get_copy_if_changed()
}
}
}
@ -30,10 +31,41 @@ impl From<&mut Sonic2MemoryHandles<'_>> for Sonic2Message {
struct Sonic2MemoryHandles<'a> {
sonic_pos: MemorySliceHandle<'a>,
tails_pos: MemorySliceHandle<'a>,
sonic_pos2: MemoryBackedNumbers<'a, u16>,
}
struct Object<'a> {
handle: MemorySliceHandle<'a>,
//x_pos: u16,
x_pos: MemoryBackedNumbers<'a, u16>,
//y_pos: MemoryBackedNumbers<'a, u16>,
}
impl<'a> Object<'_> {
pub fn create(start_offset: usize, memory_map: &LibRetroMemoryMap) -> Self {
/* Object {
x_pos: MemoryBackedNumbers::create(start_offset + 0x08, 2, 0, memory_map),
y_pos: MemoryBackedNumbers::create(start_offset + 0x0a, 2, 0, memory_map),
}
*/
let handle = MemorySliceHandle::create(start_offset, 10, 0, memory_map);
let x_pos = MemoryBackedNumbers::create(start_offset + 0x08, 2, 0, memory_map);
//let x_pos = handle.get_at_offset(0x08);
Object {
handle,
x_pos,
}
}
}
impl SyncableGame for SyncedSonic2 {
fn update_state(&mut self) {
let mut pos = self.memory_handles.sonic_pos2.get().unwrap();
println!("sonic pos: {:?}", pos);
/*
if let Some(d) = &self.memory_handles.sonic_pos.slice {
writeln!("sonic pos: {:?}", d)
@ -60,9 +92,12 @@ impl SyncableGame for SyncedSonic2 {
impl Sonic2MemoryHandles<'_> {
pub fn create(memory_map: &LibRetroMemoryMap) -> Self {
let sonic_pos2 = MemoryBackedNumbers::create(0xb000, 0x10, 0, memory_map);
Sonic2MemoryHandles {
sonic_pos: MemorySliceHandle::create(0xb008, 0x10, 0, memory_map),
tails_pos: MemorySliceHandle::create(0xb048, 0x10, 0, memory_map),
sonic_pos2,
}
}
}