From e47af664cc54e2d20aae66b07d8e92d0074a70ca Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Thu, 13 Oct 2022 04:22:36 +0200 Subject: [PATCH] HW: Move MemoryInterface variables to Core::System. --- Source/Core/Core/HW/MemoryInterface.cpp | 55 +++++++++++++++---------- Source/Core/Core/HW/MemoryInterface.h | 19 +++++++++ Source/Core/Core/System.cpp | 7 ++++ Source/Core/Core/System.h | 5 +++ 4 files changed, 65 insertions(+), 21 deletions(-) diff --git a/Source/Core/Core/HW/MemoryInterface.cpp b/Source/Core/Core/HW/MemoryInterface.cpp index 1d1de23184..d27c6ed24c 100644 --- a/Source/Core/Core/HW/MemoryInterface.cpp +++ b/Source/Core/Core/HW/MemoryInterface.cpp @@ -11,6 +11,7 @@ #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" #include "Core/HW/MMIO.h" +#include "Core/System.h" namespace MemoryInterface { @@ -134,13 +135,22 @@ struct MIMemStruct u16 unknown2 = 0; }; -// STATE_TO_SAVE -static MIMemStruct g_mi_mem; +struct MemoryInterfaceState::Data +{ + MIMemStruct mi_mem; +}; + +MemoryInterfaceState::MemoryInterfaceState() : m_data(std::make_unique()) +{ +} + +MemoryInterfaceState::~MemoryInterfaceState() = default; void Init() { + auto& state = Core::System::GetInstance().GetMemoryInterfaceState().GetData(); static_assert(std::is_trivially_copyable_v); - std::memset(&g_mi_mem, 0, sizeof(MIMemStruct)); + std::memset(&state.mi_mem, 0, sizeof(MIMemStruct)); } void Shutdown() @@ -150,51 +160,54 @@ void Shutdown() void DoState(PointerWrap& p) { - p.Do(g_mi_mem); + auto& state = Core::System::GetInstance().GetMemoryInterfaceState().GetData(); + p.Do(state.mi_mem); } void RegisterMMIO(MMIO::Mapping* mmio, u32 base) { + auto& state = Core::System::GetInstance().GetMemoryInterfaceState().GetData(); + for (u32 i = MI_REGION0_FIRST; i <= MI_REGION3_LAST; i += 4) { - auto& region = g_mi_mem.regions[i / 4]; + auto& region = state.mi_mem.regions[i / 4]; mmio->Register(base | i, MMIO::DirectRead(®ion.first_page), MMIO::DirectWrite(®ion.first_page)); mmio->Register(base | (i + 2), MMIO::DirectRead(®ion.last_page), MMIO::DirectWrite(®ion.last_page)); } - mmio->Register(base | MI_PROT_TYPE, MMIO::DirectRead(&g_mi_mem.prot_type.hex), - MMIO::DirectWrite(&g_mi_mem.prot_type.hex)); + mmio->Register(base | MI_PROT_TYPE, MMIO::DirectRead(&state.mi_mem.prot_type.hex), + MMIO::DirectWrite(&state.mi_mem.prot_type.hex)); - mmio->Register(base | MI_IRQMASK, MMIO::DirectRead(&g_mi_mem.irq_mask.hex), - MMIO::DirectWrite(&g_mi_mem.irq_mask.hex)); + mmio->Register(base | MI_IRQMASK, MMIO::DirectRead(&state.mi_mem.irq_mask.hex), + MMIO::DirectWrite(&state.mi_mem.irq_mask.hex)); - mmio->Register(base | MI_IRQFLAG, MMIO::DirectRead(&g_mi_mem.irq_flag.hex), - MMIO::DirectWrite(&g_mi_mem.irq_flag.hex)); + mmio->Register(base | MI_IRQFLAG, MMIO::DirectRead(&state.mi_mem.irq_flag.hex), + MMIO::DirectWrite(&state.mi_mem.irq_flag.hex)); - mmio->Register(base | MI_UNKNOWN1, MMIO::DirectRead(&g_mi_mem.unknown1), - MMIO::DirectWrite(&g_mi_mem.unknown1)); + mmio->Register(base | MI_UNKNOWN1, MMIO::DirectRead(&state.mi_mem.unknown1), + MMIO::DirectWrite(&state.mi_mem.unknown1)); // The naming is confusing here: the register contains the lower part of // the address (hence MI_..._LO but this is still the high part of the // overall register. - mmio->Register(base | MI_PROT_ADDR_LO, MMIO::DirectRead(&g_mi_mem.prot_addr.hi), - MMIO::DirectWrite(&g_mi_mem.prot_addr.hi)); - mmio->Register(base | MI_PROT_ADDR_HI, MMIO::DirectRead(&g_mi_mem.prot_addr.lo), - MMIO::DirectWrite(&g_mi_mem.prot_addr.lo)); + mmio->Register(base | MI_PROT_ADDR_LO, MMIO::DirectRead(&state.mi_mem.prot_addr.hi), + MMIO::DirectWrite(&state.mi_mem.prot_addr.hi)); + mmio->Register(base | MI_PROT_ADDR_HI, MMIO::DirectRead(&state.mi_mem.prot_addr.lo), + MMIO::DirectWrite(&state.mi_mem.prot_addr.lo)); - for (u32 i = 0; i < g_mi_mem.timers.size(); ++i) + for (u32 i = 0; i < state.mi_mem.timers.size(); ++i) { - auto& timer = g_mi_mem.timers[i]; + auto& timer = state.mi_mem.timers[i]; mmio->Register(base | (MI_TIMER0_HI + 4 * i), MMIO::DirectRead(&timer.hi), MMIO::DirectWrite(&timer.hi)); mmio->Register(base | (MI_TIMER0_LO + 4 * i), MMIO::DirectRead(&timer.lo), MMIO::DirectWrite(&timer.lo)); } - mmio->Register(base | MI_UNKNOWN2, MMIO::DirectRead(&g_mi_mem.unknown2), - MMIO::DirectWrite(&g_mi_mem.unknown2)); + mmio->Register(base | MI_UNKNOWN2, MMIO::DirectRead(&state.mi_mem.unknown2), + MMIO::DirectWrite(&state.mi_mem.unknown2)); for (u32 i = 0; i < 0x1000; i += 4) { diff --git a/Source/Core/Core/HW/MemoryInterface.h b/Source/Core/Core/HW/MemoryInterface.h index 3b1129a1b8..e66b8a3f00 100644 --- a/Source/Core/Core/HW/MemoryInterface.h +++ b/Source/Core/Core/HW/MemoryInterface.h @@ -3,6 +3,8 @@ #pragma once +#include + #include "Common/CommonTypes.h" namespace MMIO @@ -13,6 +15,23 @@ class PointerWrap; namespace MemoryInterface { +class MemoryInterfaceState +{ +public: + MemoryInterfaceState(); + MemoryInterfaceState(const MemoryInterfaceState&) = delete; + MemoryInterfaceState(MemoryInterfaceState&&) = delete; + MemoryInterfaceState& operator=(const MemoryInterfaceState&) = delete; + MemoryInterfaceState& operator=(MemoryInterfaceState&&) = delete; + ~MemoryInterfaceState(); + + struct Data; + Data& GetData() { return *m_data; } + +private: + std::unique_ptr m_data; +}; + void Init(); void Shutdown(); diff --git a/Source/Core/Core/System.cpp b/Source/Core/Core/System.cpp index 700a1dd15c..62d119f071 100644 --- a/Source/Core/Core/System.cpp +++ b/Source/Core/Core/System.cpp @@ -12,6 +12,7 @@ #include "Core/HW/DVD/DVDInterface.h" #include "Core/HW/DVD/DVDThread.h" #include "Core/HW/EXI/EXI.h" +#include "Core/HW/MemoryInterface.h" #include "Core/HW/SI/SI.h" #include "Core/HW/Sram.h" #include "Core/HW/VideoInterface.h" @@ -29,6 +30,7 @@ struct System::Impl DVDInterface::DVDInterfaceState m_dvd_interface_state; DVDThread::DVDThreadState m_dvd_thread_state; ExpansionInterface::ExpansionInterfaceState m_expansion_interface_state; + MemoryInterface::MemoryInterfaceState m_memory_interface_state; SerialInterface::SerialInterfaceState m_serial_interface_state; Sram m_sram; VideoInterface::VideoInterfaceState m_video_interface_state; @@ -102,6 +104,11 @@ ExpansionInterface::ExpansionInterfaceState& System::GetExpansionInterfaceState( return m_impl->m_expansion_interface_state; } +MemoryInterface::MemoryInterfaceState& System::GetMemoryInterfaceState() const +{ + return m_impl->m_memory_interface_state; +} + SerialInterface::SerialInterfaceState& System::GetSerialInterfaceState() const { return m_impl->m_serial_interface_state; diff --git a/Source/Core/Core/System.h b/Source/Core/Core/System.h index 75a28ead97..3e70f215e4 100644 --- a/Source/Core/Core/System.h +++ b/Source/Core/Core/System.h @@ -28,6 +28,10 @@ namespace ExpansionInterface { class ExpansionInterfaceState; }; +namespace MemoryInterface +{ +class MemoryInterfaceState; +}; namespace SerialInterface { class SerialInterfaceState; @@ -76,6 +80,7 @@ public: DVDInterface::DVDInterfaceState& GetDVDInterfaceState() const; DVDThread::DVDThreadState& GetDVDThreadState() const; ExpansionInterface::ExpansionInterfaceState& GetExpansionInterfaceState() const; + MemoryInterface::MemoryInterfaceState& GetMemoryInterfaceState() const; SerialInterface::SerialInterfaceState& GetSerialInterfaceState() const; Sram& GetSRAM() const; VideoInterface::VideoInterfaceState& GetVideoInterfaceState() const;