From 7becee22842e0210e9b5c47c7a4d38327e0370c1 Mon Sep 17 00:00:00 2001 From: pjgat09 Date: Thu, 9 May 2013 00:36:01 +0000 Subject: [PATCH] N64: Rescue save ram from the clutches of race conditions --- .../Consoles/Nintendo/N64/N64.cs | 4 +-- .../Consoles/Nintendo/N64/mupen64plusApi.cs | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Nintendo/N64/N64.cs b/BizHawk.Emulation/Consoles/Nintendo/N64/N64.cs index 79994f975c..694079050d 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/N64/N64.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/N64/N64.cs @@ -119,9 +119,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo.N64 public byte[] ReadSaveRam() { - byte[] ret = new byte[0x800 + 4 * 0x8000]; - api.SaveSaveram(ret); - return ret; + return api.SaveSaveram(); } public void StoreSaveRam(byte[] data) diff --git a/BizHawk.Emulation/Consoles/Nintendo/N64/mupen64plusApi.cs b/BizHawk.Emulation/Consoles/Nintendo/N64/mupen64plusApi.cs index f41176e2c2..487417497a 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/N64/mupen64plusApi.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/N64/mupen64plusApi.cs @@ -673,14 +673,33 @@ namespace BizHawk.Emulation.Consoles.Nintendo.N64 m64pCoreLoadState(buffer); } + byte[] saveram_backup; + public void InitSaveram() { m64pinit_saveram(); } - public void SaveSaveram(byte[] dest) + public byte[] SaveSaveram() { - m64psave_saveram(dest); + if (disposed) + { + if (saveram_backup != null) + { + return saveram_backup; + } + else + { + // This shouldn't happen!! + return new byte[0x800 + 4 * 0x8000]; + } + } + else + { + byte[] dest = new byte[0x800 + 4 * 0x8000]; + m64psave_saveram(dest); + return dest; + } } public void LoadSaveram(byte[] src) @@ -697,6 +716,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo.N64 //m64pEmulator.Join(); while (emulator_running) { } + // Backup the saveram in case bizhawk wants to get at is after we've freed the libraries + saveram_backup = SaveSaveram(); + bizhawkCore.resampler.Dispose(); bizhawkCore.resampler = null;