From cac0f533db63e03b4cb02905e2a1d88e9f3cc6f2 Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 22 Aug 2016 15:07:46 -0400 Subject: [PATCH] Fix SMS memory domains --- .../Consoles/Sega/SMS/SMS.IMemoryDomains.cs | 57 ++++++++++++------- .../Consoles/Sega/SMS/SMS.IStatable.cs | 5 ++ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IMemoryDomains.cs b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IMemoryDomains.cs index 264e1f7c85..a19dadae35 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IMemoryDomains.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IMemoryDomains.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using BizHawk.Emulation.Common; @@ -8,16 +9,14 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem public sealed partial class SMS { private MemoryDomainList MemoryDomains; + private readonly Dictionary _byteArrayDomains = new Dictionary(); + private bool _memoryDomainsInit = false; void SetupMemoryDomains() { - var domains = new List(3); - var MainMemoryDomain = new MemoryDomainByteArray("Main RAM", MemoryDomain.Endian.Little, SystemRam, true, 1); - var VRamDomain = new MemoryDomainByteArray("Video RAM", MemoryDomain.Endian.Little, Vdp.VRAM, true, 1); - - var ROMDomain = new MemoryDomainByteArray("ROM", MemoryDomain.Endian.Little, RomData, true, 1); - - var SystemBusDomain = new MemoryDomainDelegate("System Bus", 0x10000, MemoryDomain.Endian.Little, + var domains = new List + { + new MemoryDomainDelegate("System Bus", 0x10000, MemoryDomain.Endian.Little, (addr) => { if (addr < 0 || addr >= 65536) @@ -35,29 +34,49 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem } Cpu.WriteMemory((ushort)addr, value); - }, 1); - - domains.Add(MainMemoryDomain); - domains.Add(VRamDomain); - domains.Add(ROMDomain); - domains.Add(SystemBusDomain); + }, 1) + }; if (SaveRAM != null) { - var SaveRamDomain = new MemoryDomainDelegate("Save RAM", SaveRAM.Length, MemoryDomain.Endian.Little, + var saveRamDomain = new MemoryDomainDelegate("Save RAM", SaveRAM.Length, MemoryDomain.Endian.Little, addr => SaveRAM[addr], (addr, value) => { SaveRAM[addr] = value; SaveRamModified = true; }, 1); - domains.Add(SaveRamDomain); + domains.Add(saveRamDomain); } + SyncAllByteArrayDomains(); + + MemoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList()); + (ServiceProvider as BasicServiceProvider).Register(MemoryDomains); + + _memoryDomainsInit = true; + } + + private void SyncAllByteArrayDomains() + { + SyncByteArrayDomain("Main RAM", SystemRam); + SyncByteArrayDomain("Video RAM", Vdp.VRAM); + SyncByteArrayDomain("ROM", RomData); + if (ExtRam != null) { - var ExtRamDomain = new MemoryDomainByteArray("Cart (Volatile) RAM", MemoryDomain.Endian.Little, ExtRam, true, 1); - domains.Add(ExtRamDomain); + SyncByteArrayDomain("Cart (Volatile) RAM", Vdp.VRAM); } + } - MemoryDomains = new MemoryDomainList(domains); - (ServiceProvider as BasicServiceProvider).Register(MemoryDomains); + private void SyncByteArrayDomain(string name, byte[] data) + { + if (_memoryDomainsInit) + { + var m = _byteArrayDomains[name]; + m.Data = data; + } + else + { + var m = new MemoryDomainByteArray(name, MemoryDomain.Endian.Little, data, true, 1); + _byteArrayDomains.Add(name, m); + } } } } diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs index f1d564ef08..155386c244 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.IStatable.cs @@ -93,6 +93,11 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem ser.Sync("IsLag", ref _isLag); ser.EndSection(); + + if (ser.IsReader) + { + SyncAllByteArrayDomains(); + } } } }