Save and load SRAM at all the right times
This commit is contained in:
		
							parent
							
								
									e9ae8bed1b
								
							
						
					
					
						commit
						0504e351ba
					
				| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
# This file is automatically @generated by Cargo.
 | 
					# This file is automatically @generated by Cargo.
 | 
				
			||||||
# It is not intended for manual editing.
 | 
					# It is not intended for manual editing.
 | 
				
			||||||
version = 3
 | 
					version = 4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "ab_glyph"
 | 
					name = "ab_glyph"
 | 
				
			||||||
| 
						 | 
					@ -2200,6 +2200,12 @@ version = "1.20.2"
 | 
				
			||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
 | 
					checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[package]]
 | 
				
			||||||
 | 
					name = "oneshot"
 | 
				
			||||||
 | 
					version = "0.1.8"
 | 
				
			||||||
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
 | 
					checksum = "e296cf87e61c9cfc1a61c3c63a0f7f286ed4554e0e22be84e8a38e1d264a2a29"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "orbclient"
 | 
					name = "orbclient"
 | 
				
			||||||
version = "0.3.48"
 | 
					version = "0.3.48"
 | 
				
			||||||
| 
						 | 
					@ -2684,6 +2690,7 @@ dependencies = [
 | 
				
			||||||
 "itertools",
 | 
					 "itertools",
 | 
				
			||||||
 "num-derive",
 | 
					 "num-derive",
 | 
				
			||||||
 "num-traits",
 | 
					 "num-traits",
 | 
				
			||||||
 | 
					 "oneshot",
 | 
				
			||||||
 "pollster 0.4.0",
 | 
					 "pollster 0.4.0",
 | 
				
			||||||
 "rfd",
 | 
					 "rfd",
 | 
				
			||||||
 "rtrb",
 | 
					 "rtrb",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,6 +17,7 @@ gilrs = "0.11"
 | 
				
			||||||
itertools = "0.13"
 | 
					itertools = "0.13"
 | 
				
			||||||
num-derive = "0.4"
 | 
					num-derive = "0.4"
 | 
				
			||||||
num-traits = "0.2"
 | 
					num-traits = "0.2"
 | 
				
			||||||
 | 
					oneshot = "0.1"
 | 
				
			||||||
pollster = "0.4"
 | 
					pollster = "0.4"
 | 
				
			||||||
rfd = "0.15"
 | 
					rfd = "0.15"
 | 
				
			||||||
rtrb = "0.3"
 | 
					rtrb = "0.3"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										13
									
								
								src/app.rs
								
								
								
								
							
							
						
						
									
										13
									
								
								src/app.rs
								
								
								
								
							| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
use std::{collections::HashSet, num::NonZero, sync::Arc, thread};
 | 
					use std::{collections::HashSet, num::NonZero, sync::Arc, thread, time::Duration};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use egui::{
 | 
					use egui::{
 | 
				
			||||||
    ahash::{HashMap, HashMapExt},
 | 
					    ahash::{HashMap, HashMapExt},
 | 
				
			||||||
| 
						 | 
					@ -15,7 +15,7 @@ use winit::{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    controller::ControllerManager,
 | 
					    controller::ControllerManager,
 | 
				
			||||||
    emulator::{EmulatorClient, SimId},
 | 
					    emulator::{EmulatorClient, EmulatorCommand, SimId},
 | 
				
			||||||
    input::MappingProvider,
 | 
					    input::MappingProvider,
 | 
				
			||||||
    window::{AppWindow, GameWindow, InputWindow},
 | 
					    window::{AppWindow, GameWindow, InputWindow},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					@ -170,6 +170,15 @@ impl ApplicationHandler<UserEvent> for Application {
 | 
				
			||||||
            viewport.window.request_redraw();
 | 
					            viewport.window.request_redraw();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn exiting(&mut self, _event_loop: &ActiveEventLoop) {
 | 
				
			||||||
 | 
					        let (sender, receiver) = oneshot::channel();
 | 
				
			||||||
 | 
					        if self.client.send_command(EmulatorCommand::Exit(sender)) {
 | 
				
			||||||
 | 
					            if let Err(err) = receiver.recv_timeout(Duration::from_secs(5)) {
 | 
				
			||||||
 | 
					                eprintln!("could not gracefully exit: {}", err);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Viewport {
 | 
					struct Viewport {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -186,6 +186,8 @@ impl Emulator {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> Result<()> {
 | 
					    fn reset_sim(&mut self, sim_id: SimId, new_cart: Option<Cart>) -> Result<()> {
 | 
				
			||||||
 | 
					        self.save_sram(sim_id)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let index = sim_id.to_index();
 | 
					        let index = sim_id.to_index();
 | 
				
			||||||
        while self.sims.len() <= index {
 | 
					        while self.sims.len() <= index {
 | 
				
			||||||
            self.sims.push(Sim::new());
 | 
					            self.sims.push(Sim::new());
 | 
				
			||||||
| 
						 | 
					@ -226,6 +228,10 @@ impl Emulator {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn pause_sim(&mut self, sim_id: SimId) -> Result<()> {
 | 
					    pub fn pause_sim(&mut self, sim_id: SimId) -> Result<()> {
 | 
				
			||||||
        self.running[sim_id.to_index()].store(false, Ordering::Release);
 | 
					        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 sim = self.sims.get_mut(sim_id.to_index());
 | 
				
			||||||
        let cart = self.carts[sim_id.to_index()].as_mut();
 | 
					        let cart = self.carts[sim_id.to_index()].as_mut();
 | 
				
			||||||
        if let (Some(sim), Some(cart)) = (sim, cart) {
 | 
					        if let (Some(sim), Some(cart)) = (sim, cart) {
 | 
				
			||||||
| 
						 | 
					@ -236,13 +242,15 @@ impl Emulator {
 | 
				
			||||||
        Ok(())
 | 
					        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.renderers.remove(&SimId::Player2);
 | 
				
			||||||
        self.sims.truncate(1);
 | 
					        self.sims.truncate(1);
 | 
				
			||||||
        self.sim_count.store(self.sims.len(), Ordering::Relaxed);
 | 
					        self.sim_count.store(self.sims.len(), Ordering::Relaxed);
 | 
				
			||||||
        self.running[SimId::Player2.to_index()].store(false, Ordering::Release);
 | 
					        self.running[SimId::Player2.to_index()].store(false, Ordering::Release);
 | 
				
			||||||
        self.has_game[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);
 | 
					        self.linked.store(false, Ordering::Release);
 | 
				
			||||||
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn run(&mut self) {
 | 
					    pub fn run(&mut self) {
 | 
				
			||||||
| 
						 | 
					@ -336,7 +344,9 @@ impl Emulator {
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            EmulatorCommand::StopSecondSim => {
 | 
					            EmulatorCommand::StopSecondSim => {
 | 
				
			||||||
                self.stop_second_sim();
 | 
					                if let Err(error) = self.stop_second_sim() {
 | 
				
			||||||
 | 
					                    eprintln!("error stopping second sim: {}", error);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            EmulatorCommand::Pause => {
 | 
					            EmulatorCommand::Pause => {
 | 
				
			||||||
                for sim_id in SimId::values() {
 | 
					                for sim_id in SimId::values() {
 | 
				
			||||||
| 
						 | 
					@ -373,6 +383,14 @@ impl Emulator {
 | 
				
			||||||
                    sim.set_keys(keys);
 | 
					                    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,
 | 
					    Unlink,
 | 
				
			||||||
    Reset(SimId),
 | 
					    Reset(SimId),
 | 
				
			||||||
    SetKeys(SimId, VBKey),
 | 
					    SetKeys(SimId, VBKey),
 | 
				
			||||||
 | 
					    Exit(oneshot::Sender<()>),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Clone)]
 | 
					#[derive(Clone)]
 | 
				
			||||||
| 
						 | 
					@ -418,12 +437,16 @@ impl EmulatorClient {
 | 
				
			||||||
    pub fn is_audio_enabled(&self, sim_id: SimId) -> bool {
 | 
					    pub fn is_audio_enabled(&self, sim_id: SimId) -> bool {
 | 
				
			||||||
        self.audio_on[sim_id.to_index()].load(Ordering::Acquire)
 | 
					        self.audio_on[sim_id.to_index()].load(Ordering::Acquire)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    pub fn send_command(&self, command: EmulatorCommand) {
 | 
					    pub fn send_command(&self, command: EmulatorCommand) -> bool {
 | 
				
			||||||
        if let Err(err) = self.queue.send(command) {
 | 
					        match self.queue.send(command) {
 | 
				
			||||||
            eprintln!(
 | 
					            Ok(()) => true,
 | 
				
			||||||
                "could not send command {:?} as emulator is shut down",
 | 
					            Err(err) => {
 | 
				
			||||||
                err.0
 | 
					                eprintln!(
 | 
				
			||||||
            );
 | 
					                    "could not send command {:?} as emulator is shut down",
 | 
				
			||||||
 | 
					                    err.0
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
 | 
					                false
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue