From 7b19db2d6402b82ae72e7eb58f6b4b821f2b0283 Mon Sep 17 00:00:00 2001 From: nattthebear Date: Sat, 16 Jul 2016 16:13:50 -0400 Subject: [PATCH] Fix NES memory domains --- .../Base Implementations/MemoryDomainList.cs | 31 +++++++++++++++++++ .../Nintendo/NES/NES.IMemoryDomains.cs | 14 +++++++-- .../Consoles/Nintendo/NES/NES.IStatable.cs | 2 ++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs b/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs index b9dd915503..973adb76b7 100644 --- a/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs +++ b/BizHawk.Emulation.Common/Base Implementations/MemoryDomainList.cs @@ -84,5 +84,36 @@ namespace BizHawk.Emulation.Common _systemBus = value; } } + + /// + /// for core use only + /// + public void MergeList(MemoryDomainList other) + { + var domains = this.ToDictionary(m => m.Name); + foreach (var src in other) + { + MemoryDomain dst; + if (domains.TryGetValue(src.Name, out dst)) + { + TryMerge(dst, src, (d, s) => d.Data = s.Data); + TryMerge(dst, src, (d, s) => d.Data = s.Data); + TryMerge(dst, src, (d, s) => d.Data = s.Data); + TryMerge(dst, src, (d, s) => { d.Peek = s.Peek; d.Poke = s.Poke; }); + } + } + } + + /// + /// big hacks + /// + private static void TryMerge(MemoryDomain dest, MemoryDomain src, Action func) + where T : MemoryDomain + { + var d1 = dest as T; + var s1 = src as T; + if (d1 != null && s1 != null) + func(d1, s1); + } } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IMemoryDomains.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IMemoryDomains.cs index ca834d73b4..0997f6e269 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IMemoryDomains.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IMemoryDomains.cs @@ -9,6 +9,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public partial class NES { private MemoryDomainList _memoryDomains; + private bool _memoryDomainsSetup = false; private void SetupMemoryDomains() { @@ -67,8 +68,17 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES domains.Add((Board as ExROM).GetExRAM()); } - _memoryDomains = new MemoryDomainList(domains); - (ServiceProvider as BasicServiceProvider).Register(_memoryDomains); + if (!_memoryDomainsSetup) + { + _memoryDomains = new MemoryDomainList(domains); + (ServiceProvider as BasicServiceProvider).Register(_memoryDomains); + _memoryDomainsSetup = true; + } + else + { + var src = new MemoryDomainList(domains); + _memoryDomains.MergeList(src); + } } } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs index 2ac1a434a5..29bc7d39e3 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.IStatable.cs @@ -23,6 +23,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public void LoadStateText(TextReader reader) { SyncState(Serializer.CreateTextReader(reader)); + SetupMemoryDomains(); // resync the memory domains } public void SaveStateBinary(BinaryWriter bw) @@ -33,6 +34,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public void LoadStateBinary(BinaryReader br) { SyncState(Serializer.CreateBinaryReader(br)); + SetupMemoryDomains(); // resync the memory domains } public byte[] SaveStateBinary()