Implement SRAM (only saves on pause)
This commit is contained in:
+74
-15
@@ -1,6 +1,7 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs,
|
||||
fs::{self, File},
|
||||
io::{Read, Seek, SeekFrom, Write},
|
||||
path::{Path, PathBuf},
|
||||
sync::{
|
||||
atomic::{AtomicBool, AtomicUsize, Ordering},
|
||||
@@ -44,6 +45,43 @@ impl SimId {
|
||||
}
|
||||
}
|
||||
|
||||
struct Cart {
|
||||
rom_path: PathBuf,
|
||||
rom: Vec<u8>,
|
||||
sram_file: File,
|
||||
sram: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Cart {
|
||||
fn load(rom_path: &Path, sim_id: SimId) -> Result<Self> {
|
||||
let rom = fs::read(rom_path)?;
|
||||
|
||||
let mut sram_file = File::options()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(false)
|
||||
.open(sram_path(rom_path, sim_id))?;
|
||||
sram_file.set_len(8 * 1024)?;
|
||||
|
||||
let mut sram = vec![];
|
||||
sram_file.read_to_end(&mut sram)?;
|
||||
Ok(Cart {
|
||||
rom_path: rom_path.to_path_buf(),
|
||||
rom,
|
||||
sram_file,
|
||||
sram,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn sram_path(rom_path: &Path, sim_id: SimId) -> PathBuf {
|
||||
match sim_id {
|
||||
SimId::Player1 => rom_path.with_extension(".p1.sram"),
|
||||
SimId::Player2 => rom_path.with_extension(".p2.sram"),
|
||||
}
|
||||
}
|
||||
|
||||
impl EmulatorBuilder {
|
||||
pub fn new() -> (Self, EmulatorClient) {
|
||||
let (queue, commands) = mpsc::channel();
|
||||
@@ -84,7 +122,7 @@ impl EmulatorBuilder {
|
||||
self.linked,
|
||||
)?;
|
||||
if let Some(path) = self.rom {
|
||||
emulator.load_rom(SimId::Player1, &path)?;
|
||||
emulator.load_cart(SimId::Player1, &path)?;
|
||||
}
|
||||
Ok(emulator)
|
||||
}
|
||||
@@ -92,6 +130,7 @@ impl EmulatorBuilder {
|
||||
|
||||
pub struct Emulator {
|
||||
sims: Vec<Sim>,
|
||||
carts: [Option<Cart>; 2],
|
||||
audio: Audio,
|
||||
commands: mpsc::Receiver<EmulatorCommand>,
|
||||
sim_count: Arc<AtomicUsize>,
|
||||
@@ -113,6 +152,7 @@ impl Emulator {
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
sims: vec![],
|
||||
carts: [None, None],
|
||||
audio: Audio::init()?,
|
||||
commands,
|
||||
sim_count,
|
||||
@@ -124,24 +164,28 @@ impl Emulator {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn load_rom(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
||||
let bytes = fs::read(path)?;
|
||||
self.reset_sim(sim_id, Some(bytes))?;
|
||||
pub fn load_cart(&mut self, sim_id: SimId, path: &Path) -> Result<()> {
|
||||
let cart = Cart::load(path, sim_id)?;
|
||||
self.reset_sim(sim_id, Some(cart))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn start_second_sim(&mut self, rom: Option<PathBuf>) -> Result<()> {
|
||||
let bytes = if let Some(path) = rom {
|
||||
Some(fs::read(path)?)
|
||||
let rom_path = if let Some(path) = rom {
|
||||
Some(path)
|
||||
} else {
|
||||
self.sims.first().and_then(|s| s.clone_rom())
|
||||
self.carts[0].as_ref().map(|c| c.rom_path.clone())
|
||||
};
|
||||
self.reset_sim(SimId::Player2, bytes)?;
|
||||
let cart = match rom_path {
|
||||
Some(rom_path) => Some(Cart::load(&rom_path, SimId::Player2)?),
|
||||
None => None,
|
||||
};
|
||||
self.reset_sim(SimId::Player2, cart)?;
|
||||
self.link_sims();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_sim(&mut self, sim_id: SimId, new_rom: Option<Vec<u8>>) -> Result<()> {
|
||||
fn reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> Result<()> {
|
||||
let index = sim_id.to_index();
|
||||
while self.sims.len() <= index {
|
||||
self.sims.push(Sim::new());
|
||||
@@ -149,8 +193,9 @@ impl Emulator {
|
||||
self.sim_count.store(self.sims.len(), Ordering::Relaxed);
|
||||
let sim = &mut self.sims[index];
|
||||
sim.reset();
|
||||
if let Some(bytes) = new_rom {
|
||||
sim.load_rom(bytes)?;
|
||||
if let Some(cart) = new_cart {
|
||||
sim.load_cart(cart.rom.clone(), cart.sram.clone())?;
|
||||
self.carts[index] = Some(cart);
|
||||
self.has_game[index].store(true, Ordering::Release);
|
||||
}
|
||||
if self.has_game[index].load(Ordering::Acquire) {
|
||||
@@ -179,6 +224,18 @@ impl Emulator {
|
||||
self.linked.store(false, Ordering::Release);
|
||||
}
|
||||
|
||||
pub fn pause_sim(&mut self, sim_id: SimId) -> Result<()> {
|
||||
self.running[sim_id.to_index()].store(false, Ordering::Release);
|
||||
let sim = self.sims.get_mut(sim_id.to_index());
|
||||
let cart = self.carts[sim_id.to_index()].as_mut();
|
||||
if let (Some(sim), Some(cart)) = (sim, cart) {
|
||||
sim.read_sram(&mut cart.sram);
|
||||
cart.sram_file.seek(SeekFrom::Start(0))?;
|
||||
cart.sram_file.write_all(&cart.sram)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn stop_second_sim(&mut self) {
|
||||
self.renderers.remove(&SimId::Player2);
|
||||
self.sims.truncate(1);
|
||||
@@ -269,7 +326,7 @@ impl Emulator {
|
||||
self.renderers.insert(sim_id, renderer);
|
||||
}
|
||||
EmulatorCommand::LoadGame(sim_id, path) => {
|
||||
if let Err(error) = self.load_rom(sim_id, &path) {
|
||||
if let Err(error) = self.load_cart(sim_id, &path) {
|
||||
eprintln!("error loading rom: {}", error);
|
||||
}
|
||||
}
|
||||
@@ -282,8 +339,10 @@ impl Emulator {
|
||||
self.stop_second_sim();
|
||||
}
|
||||
EmulatorCommand::Pause => {
|
||||
for sim in SimId::values() {
|
||||
self.running[sim.to_index()].store(false, Ordering::Release);
|
||||
for sim_id in SimId::values() {
|
||||
if let Err(error) = self.pause_sim(sim_id) {
|
||||
eprintln!("error pausing: {}", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
EmulatorCommand::Resume => {
|
||||
|
||||
Reference in New Issue
Block a user