diff --git a/src/emulator.rs b/src/emulator.rs index 32dc1e0..4843846 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -265,7 +265,8 @@ impl Emulator { } else if let Some(cart) = self.carts[index].as_mut() && cart.is_watching() { - cart.restart_watching(); + cart.reload()?; + sim.load_cart(cart.rom.clone(), cart.sram.clone())?; } let mut profiling = false; if let Some(profiler) = self.profilers[sim_id.to_index()].as_ref() diff --git a/src/emulator/cart.rs b/src/emulator/cart.rs index 8292719..6ad8df2 100644 --- a/src/emulator/cart.rs +++ b/src/emulator/cart.rs @@ -22,10 +22,7 @@ pub struct Cart { impl Cart { pub fn load(file_path: &Path, sim_id: SimId, watch: bool) -> Result { - let rom = fs::read(file_path)?; - let (rom, info) = try_parse_isx(file_path, &rom) - .or_else(|| try_parse_elf(file_path, &rom)) - .unwrap_or_else(|| (rom, GameInfo::empty(file_path))); + let (rom, info) = Self::read_rom(file_path)?; let mut sram_file = File::options() .read(true) @@ -66,6 +63,16 @@ impl Cart { }) } + pub fn reload(&mut self) -> Result<()> { + let (rom, info) = Self::read_rom(&self.file_path)?; + self.rom = rom; + self.info = Arc::new(info); + if self.is_watching() { + self.restart_watching(); + } + Ok(()) + } + pub fn save_sram(&mut self) -> Result<()> { self.sram_file.seek(SeekFrom::Start(0))?; self.sram_file.write_all(&self.sram)?; @@ -92,6 +99,14 @@ impl Cart { self.changed = Arc::new(AtomicBool::new(false)); self.watcher = None; } + + fn read_rom(file_path: &Path) -> Result<(Vec, GameInfo)> { + let rom = fs::read(file_path)?; + let (rom, info) = try_parse_isx(file_path, &rom) + .or_else(|| try_parse_elf(file_path, &rom)) + .unwrap_or_else(|| (rom, GameInfo::empty(file_path))); + Ok((rom, info)) + } } fn build_watcher(file_path: &Path, changed: Arc) -> Option {