From 84542d915d32852fa85c0e341c5dcf268f993e8a Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Fri, 10 Mar 2023 21:27:35 +0100 Subject: [PATCH] HW/MemoryInterface: Refactor to class. --- Source/Core/Core/HW/HW.cpp | 6 +- Source/Core/Core/HW/Memmap.cpp | 2 +- Source/Core/Core/HW/MemoryInterface.cpp | 146 +++++------------------- Source/Core/Core/HW/MemoryInterface.h | 115 ++++++++++++++++--- Source/Core/Core/System.cpp | 6 +- Source/Core/Core/System.h | 4 +- 6 files changed, 135 insertions(+), 144 deletions(-) diff --git a/Source/Core/Core/HW/HW.cpp b/Source/Core/Core/HW/HW.cpp index 2de2300e96..471d48e9f4 100644 --- a/Source/Core/Core/HW/HW.cpp +++ b/Source/Core/Core/HW/HW.cpp @@ -48,7 +48,7 @@ void Init(const Sram* override_sram) system.GetHSP().Init(); system.GetMemory().Init(); // Needs to be initialized before AddressSpace AddressSpace::Init(); - MemoryInterface::Init(); + system.GetMemoryInterface().Init(); system.GetDSP().Init(Config::Get(Config::MAIN_DSP_HLE)); system.GetDVDInterface().Init(); system.GetGPFifo().Init(); @@ -74,7 +74,7 @@ void Shutdown() system.GetCPU().Shutdown(); system.GetDVDInterface().Shutdown(); system.GetDSP().Shutdown(); - MemoryInterface::Shutdown(); + system.GetMemoryInterface().Shutdown(); AddressSpace::Shutdown(); system.GetMemory().Shutdown(); system.GetHSP().Shutdown(); @@ -91,7 +91,7 @@ void DoState(PointerWrap& p) auto& system = Core::System::GetInstance(); system.GetMemory().DoState(p); p.DoMarker("Memory"); - MemoryInterface::DoState(p); + system.GetMemoryInterface().DoState(p); p.DoMarker("MemoryInterface"); VideoInterface::DoState(p); p.DoMarker("VideoInterface"); diff --git a/Source/Core/Core/HW/Memmap.cpp b/Source/Core/Core/HW/Memmap.cpp index 3f65683227..051822afab 100644 --- a/Source/Core/Core/HW/Memmap.cpp +++ b/Source/Core/Core/HW/Memmap.cpp @@ -53,7 +53,7 @@ void MemoryManager::InitMMIO(bool is_wii) system.GetPixelEngine().RegisterMMIO(m_mmio_mapping.get(), 0x0C001000); VideoInterface::RegisterMMIO(m_mmio_mapping.get(), 0x0C002000); system.GetProcessorInterface().RegisterMMIO(m_mmio_mapping.get(), 0x0C003000); - MemoryInterface::RegisterMMIO(m_mmio_mapping.get(), 0x0C004000); + system.GetMemoryInterface().RegisterMMIO(m_mmio_mapping.get(), 0x0C004000); system.GetDSP().RegisterMMIO(m_mmio_mapping.get(), 0x0C005000); system.GetDVDInterface().RegisterMMIO(m_mmio_mapping.get(), 0x0C006000, false); SerialInterface::RegisterMMIO(m_mmio_mapping.get(), 0x0C006400); diff --git a/Source/Core/Core/HW/MemoryInterface.cpp b/Source/Core/Core/HW/MemoryInterface.cpp index 62452bcde9..7bba5450fe 100644 --- a/Source/Core/Core/HW/MemoryInterface.cpp +++ b/Source/Core/Core/HW/MemoryInterface.cpp @@ -11,7 +11,6 @@ #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" #include "Core/HW/MMIO.h" -#include "Core/System.h" namespace MemoryInterface { @@ -55,166 +54,75 @@ enum MI_UNKNOWN2 = 0x05A, }; -union MIRegion +MemoryInterfaceManager::MemoryInterfaceManager() = default; + +MemoryInterfaceManager::~MemoryInterfaceManager() = default; + +void MemoryInterfaceManager::Init() { - u32 hex = 0; - struct - { - u16 first_page; - u16 last_page; - }; -}; - -union MIProtType -{ - u16 hex = 0; - - BitField<0, 2, u16> reg0; - BitField<2, 2, u16> reg1; - BitField<4, 2, u16> reg2; - BitField<6, 2, u16> reg3; - BitField<8, 8, u16> reserved; -}; - -union MIIRQMask -{ - u16 hex = 0; - - BitField<0, 1, u16> reg0; - BitField<1, 1, u16> reg1; - BitField<2, 1, u16> reg2; - BitField<3, 1, u16> reg3; - BitField<4, 1, u16> all_regs; - BitField<5, 11, u16> reserved; -}; - -union MIIRQFlag -{ - u16 hex = 0; - - BitField<0, 1, u16> reg0; - BitField<1, 1, u16> reg1; - BitField<2, 1, u16> reg2; - BitField<3, 1, u16> reg3; - BitField<4, 1, u16> all_regs; - BitField<5, 11, u16> reserved; -}; - -union MIProtAddr -{ - u32 hex = 0; - struct - { - u16 lo; - u16 hi; - }; - BitField<0, 5, u32> reserved_1; - BitField<5, 25, u32> addr; - BitField<30, 2, u32> reserved_2; -}; - -union MITimer -{ - u32 hex = 0; - struct - { - u16 lo; - u16 hi; - }; -}; - -struct MIMemStruct -{ - std::array regions; - MIProtType prot_type; - MIIRQMask irq_mask; - MIIRQFlag irq_flag; - u16 unknown1 = 0; - MIProtAddr prot_addr; - std::array timers; - u16 unknown2 = 0; -}; - -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); #ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wclass-memaccess" #endif - std::memset(&state.mi_mem, 0, sizeof(MIMemStruct)); + std::memset(&m_mi_mem, 0, sizeof(MIMemStruct)); #ifdef __GNUC__ #pragma GCC diagnostic pop #endif } -void Shutdown() +void MemoryInterfaceManager::Shutdown() { Init(); } -void DoState(PointerWrap& p) +void MemoryInterfaceManager::DoState(PointerWrap& p) { - auto& state = Core::System::GetInstance().GetMemoryInterfaceState().GetData(); - p.Do(state.mi_mem); + p.Do(m_mi_mem); } -void RegisterMMIO(MMIO::Mapping* mmio, u32 base) +void MemoryInterfaceManager::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 = state.mi_mem.regions[i / 4]; + auto& region = m_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(&state.mi_mem.prot_type.hex), - MMIO::DirectWrite(&state.mi_mem.prot_type.hex)); + mmio->Register(base | MI_PROT_TYPE, MMIO::DirectRead(&m_mi_mem.prot_type.hex), + MMIO::DirectWrite(&m_mi_mem.prot_type.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_IRQMASK, MMIO::DirectRead(&m_mi_mem.irq_mask.hex), + MMIO::DirectWrite(&m_mi_mem.irq_mask.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_IRQFLAG, MMIO::DirectRead(&m_mi_mem.irq_flag.hex), + MMIO::DirectWrite(&m_mi_mem.irq_flag.hex)); - mmio->Register(base | MI_UNKNOWN1, MMIO::DirectRead(&state.mi_mem.unknown1), - MMIO::DirectWrite(&state.mi_mem.unknown1)); + mmio->Register(base | MI_UNKNOWN1, MMIO::DirectRead(&m_mi_mem.unknown1), + MMIO::DirectWrite(&m_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(&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)); + mmio->Register(base | MI_PROT_ADDR_LO, MMIO::DirectRead(&m_mi_mem.prot_addr.hi), + MMIO::DirectWrite(&m_mi_mem.prot_addr.hi)); + mmio->Register(base | MI_PROT_ADDR_HI, MMIO::DirectRead(&m_mi_mem.prot_addr.lo), + MMIO::DirectWrite(&m_mi_mem.prot_addr.lo)); - for (u32 i = 0; i < state.mi_mem.timers.size(); ++i) + for (u32 i = 0; i < m_mi_mem.timers.size(); ++i) { - auto& timer = state.mi_mem.timers[i]; + auto& timer = m_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(&state.mi_mem.unknown2), - MMIO::DirectWrite(&state.mi_mem.unknown2)); + mmio->Register(base | MI_UNKNOWN2, MMIO::DirectRead(&m_mi_mem.unknown2), + MMIO::DirectWrite(&m_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 e66b8a3f00..b96bc5120c 100644 --- a/Source/Core/Core/HW/MemoryInterface.h +++ b/Source/Core/Core/HW/MemoryInterface.h @@ -3,10 +3,16 @@ #pragma once +#include #include +#include "Common/BitField.h" #include "Common/CommonTypes.h" +namespace Core +{ +class System; +} namespace MMIO { class Mapping; @@ -15,26 +21,103 @@ class PointerWrap; namespace MemoryInterface { -class MemoryInterfaceState +class MemoryInterfaceManager { public: - MemoryInterfaceState(); - MemoryInterfaceState(const MemoryInterfaceState&) = delete; - MemoryInterfaceState(MemoryInterfaceState&&) = delete; - MemoryInterfaceState& operator=(const MemoryInterfaceState&) = delete; - MemoryInterfaceState& operator=(MemoryInterfaceState&&) = delete; - ~MemoryInterfaceState(); + MemoryInterfaceManager(); + MemoryInterfaceManager(const MemoryInterfaceManager&) = delete; + MemoryInterfaceManager(MemoryInterfaceManager&&) = delete; + MemoryInterfaceManager& operator=(const MemoryInterfaceManager&) = delete; + MemoryInterfaceManager& operator=(MemoryInterfaceManager&&) = delete; + ~MemoryInterfaceManager(); - struct Data; - Data& GetData() { return *m_data; } + void Init(); + void Shutdown(); + + void DoState(PointerWrap& p); + void RegisterMMIO(MMIO::Mapping* mmio, u32 base); private: - std::unique_ptr m_data; + union MIRegion + { + u32 hex = 0; + struct + { + u16 first_page; + u16 last_page; + }; + }; + + union MIProtType + { + u16 hex = 0; + + BitField<0, 2, u16> reg0; + BitField<2, 2, u16> reg1; + BitField<4, 2, u16> reg2; + BitField<6, 2, u16> reg3; + BitField<8, 8, u16> reserved; + }; + + union MIIRQMask + { + u16 hex = 0; + + BitField<0, 1, u16> reg0; + BitField<1, 1, u16> reg1; + BitField<2, 1, u16> reg2; + BitField<3, 1, u16> reg3; + BitField<4, 1, u16> all_regs; + BitField<5, 11, u16> reserved; + }; + + union MIIRQFlag + { + u16 hex = 0; + + BitField<0, 1, u16> reg0; + BitField<1, 1, u16> reg1; + BitField<2, 1, u16> reg2; + BitField<3, 1, u16> reg3; + BitField<4, 1, u16> all_regs; + BitField<5, 11, u16> reserved; + }; + + union MIProtAddr + { + u32 hex = 0; + struct + { + u16 lo; + u16 hi; + }; + BitField<0, 5, u32> reserved_1; + BitField<5, 25, u32> addr; + BitField<30, 2, u32> reserved_2; + }; + + union MITimer + { + u32 hex = 0; + struct + { + u16 lo; + u16 hi; + }; + }; + + struct MIMemStruct + { + std::array regions; + MIProtType prot_type; + MIIRQMask irq_mask; + MIIRQFlag irq_flag; + u16 unknown1 = 0; + MIProtAddr prot_addr; + std::array timers; + u16 unknown2 = 0; + }; + + MIMemStruct m_mi_mem; }; - -void Init(); -void Shutdown(); - -void DoState(PointerWrap& p); -void RegisterMMIO(MMIO::Mapping* mmio, u32 base); } // namespace MemoryInterface diff --git a/Source/Core/Core/System.cpp b/Source/Core/Core/System.cpp index 58cb0628a1..3abe0fb1ea 100644 --- a/Source/Core/Core/System.cpp +++ b/Source/Core/Core/System.cpp @@ -60,7 +60,7 @@ struct System::Impl HSP::HSPManager m_hsp; IOS::HLE::USB::SkylanderPortal m_skylander_portal; Memory::MemoryManager m_memory; - MemoryInterface::MemoryInterfaceState m_memory_interface_state; + MemoryInterface::MemoryInterfaceManager m_memory_interface; PixelEngine::PixelEngineManager m_pixel_engine; PixelShaderManager m_pixel_shader_manager; PowerPC::PowerPCState& m_ppc_state; @@ -184,9 +184,9 @@ Memory::MemoryManager& System::GetMemory() const return m_impl->m_memory; } -MemoryInterface::MemoryInterfaceState& System::GetMemoryInterfaceState() const +MemoryInterface::MemoryInterfaceManager& System::GetMemoryInterface() const { - return m_impl->m_memory_interface_state; + return m_impl->m_memory_interface; } PixelEngine::PixelEngineManager& System::GetPixelEngine() const diff --git a/Source/Core/Core/System.h b/Source/Core/Core/System.h index 558c2ac7c2..ae1ab518b8 100644 --- a/Source/Core/Core/System.h +++ b/Source/Core/Core/System.h @@ -62,7 +62,7 @@ class MemoryManager; }; namespace MemoryInterface { -class MemoryInterfaceState; +class MemoryInterfaceManager; }; namespace PixelEngine { @@ -133,7 +133,7 @@ public: HSP::HSPManager& GetHSP() const; IOS::HLE::USB::SkylanderPortal& GetSkylanderPortal() const; Memory::MemoryManager& GetMemory() const; - MemoryInterface::MemoryInterfaceState& GetMemoryInterfaceState() const; + MemoryInterface::MemoryInterfaceManager& GetMemoryInterface() const; PixelEngine::PixelEngineManager& GetPixelEngine() const; PixelShaderManager& GetPixelShaderManager() const; PowerPC::PowerPCState& GetPPCState() const;