Save and load SRAM at all the right times
This commit is contained in:
+31
-8
@@ -186,6 +186,8 @@ impl Emulator {
|
||||
}
|
||||
|
||||
fn reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> Result<()> {
|
||||
self.save_sram(sim_id)?;
|
||||
|
||||
let index = sim_id.to_index();
|
||||
while self.sims.len() <= index {
|
||||
self.sims.push(Sim::new());
|
||||
@@ -226,6 +228,10 @@ impl Emulator {
|
||||
|
||||
pub fn pause_sim(&mut self, sim_id: SimId) -> Result<()> {
|
||||
self.running[sim_id.to_index()].store(false, Ordering::Release);
|
||||
self.save_sram(sim_id)
|
||||
}
|
||||
|
||||
fn save_sram(&mut self, sim_id: SimId) -> Result<()> {
|
||||
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) {
|
||||
@@ -236,13 +242,15 @@ impl Emulator {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn stop_second_sim(&mut self) {
|
||||
pub fn stop_second_sim(&mut self) -> Result<()> {
|
||||
self.save_sram(SimId::Player2)?;
|
||||
self.renderers.remove(&SimId::Player2);
|
||||
self.sims.truncate(1);
|
||||
self.sim_count.store(self.sims.len(), Ordering::Relaxed);
|
||||
self.running[SimId::Player2.to_index()].store(false, Ordering::Release);
|
||||
self.has_game[SimId::Player2.to_index()].store(false, Ordering::Release);
|
||||
self.linked.store(false, Ordering::Release);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
@@ -336,7 +344,9 @@ impl Emulator {
|
||||
}
|
||||
}
|
||||
EmulatorCommand::StopSecondSim => {
|
||||
self.stop_second_sim();
|
||||
if let Err(error) = self.stop_second_sim() {
|
||||
eprintln!("error stopping second sim: {}", error);
|
||||
}
|
||||
}
|
||||
EmulatorCommand::Pause => {
|
||||
for sim_id in SimId::values() {
|
||||
@@ -373,6 +383,14 @@ impl Emulator {
|
||||
sim.set_keys(keys);
|
||||
}
|
||||
}
|
||||
EmulatorCommand::Exit(done) => {
|
||||
for sim_id in SimId::values() {
|
||||
if let Err(error) = self.save_sram(sim_id) {
|
||||
eprintln!("error saving sram on exit: {}", error);
|
||||
}
|
||||
}
|
||||
let _ = done.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -390,6 +408,7 @@ pub enum EmulatorCommand {
|
||||
Unlink,
|
||||
Reset(SimId),
|
||||
SetKeys(SimId, VBKey),
|
||||
Exit(oneshot::Sender<()>),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -418,12 +437,16 @@ impl EmulatorClient {
|
||||
pub fn is_audio_enabled(&self, sim_id: SimId) -> bool {
|
||||
self.audio_on[sim_id.to_index()].load(Ordering::Acquire)
|
||||
}
|
||||
pub fn send_command(&self, command: EmulatorCommand) {
|
||||
if let Err(err) = self.queue.send(command) {
|
||||
eprintln!(
|
||||
"could not send command {:?} as emulator is shut down",
|
||||
err.0
|
||||
);
|
||||
pub fn send_command(&self, command: EmulatorCommand) -> bool {
|
||||
match self.queue.send(command) {
|
||||
Ok(()) => true,
|
||||
Err(err) => {
|
||||
eprintln!(
|
||||
"could not send command {:?} as emulator is shut down",
|
||||
err.0
|
||||
);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user