From 4525e8b6f37a4a798202d15fecb068a01ca4e28f Mon Sep 17 00:00:00 2001 From: zilmar Date: Mon, 17 Oct 2022 17:29:05 +1030 Subject: [PATCH] Core: Move IMEM/DMEM into SPRegistersHandler --- .../MemoryHandler/SPRegistersHandler.cpp | 34 +++++++++++-- .../MemoryHandler/SPRegistersHandler.h | 12 +++++ .../N64System/Mips/MemoryVirtualMem.cpp | 48 ++++--------------- .../N64System/Mips/MemoryVirtualMem.h | 10 ++-- Source/Project64-core/N64System/N64System.cpp | 2 +- 5 files changed, 59 insertions(+), 47 deletions(-) diff --git a/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.cpp b/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.cpp index b546fb867..5677c9940 100644 --- a/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.cpp +++ b/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.cpp @@ -38,6 +38,19 @@ SPRegistersHandler::SPRegistersHandler(CN64System & System, CMipsMemoryVM & MMU, bool SPRegistersHandler::Read32(uint32_t Address, uint32_t & Value) { + if (Address < 0x04040000) + { + if ((Address & 0x1000) == 0) + { + Value = *((uint32_t *)&m_DMEM[Address & 0xFFC]); + } + else + { + Value = *((uint32_t *)&m_IMEM[Address & 0xFFC]); + } + return true; + } + switch (Address & 0x1FFFFFFF) { case 0x04040000: Value = m_ExecutedDMARead ? m_SPMemAddrRegRead : SP_MEM_ADDR_REG; break; @@ -85,6 +98,19 @@ bool SPRegistersHandler::Read32(uint32_t Address, uint32_t & Value) bool SPRegistersHandler::Write32(uint32_t Address, uint32_t Value, uint32_t Mask) { + uint32_t MaskedValue = Value & Mask; + if (Address < 0x04040000) + { + if ((Address & 0x1000) == 0) + { + *((uint32_t *)&m_DMEM[Address & 0xFFC]) = MaskedValue; + } + else + { + *((uint32_t *)&m_IMEM[Address & 0xFFC]) = MaskedValue; + } + return true; + } if (GenerateLog() && LogSPRegisters()) { switch (Address & 0x1FFFFFFF) @@ -106,7 +132,6 @@ bool SPRegistersHandler::Write32(uint32_t Address, uint32_t Value, uint32_t Mask } } - uint32_t MaskedValue = Value & Mask; switch (Address & 0x1FFFFFFF) { case 0x04040000: SP_MEM_ADDR_REG = (SP_MEM_ADDR_REG & ~Mask) | (MaskedValue); break; @@ -245,7 +270,7 @@ bool SPRegistersHandler::Write32(uint32_t Address, uint32_t Value, uint32_t Mask void SPRegistersHandler::SP_DMA_READ() { - uint8_t * Dest = ((SP_MEM_ADDR_REG & 0x1000) != 0 ? m_MMU.Imem() : m_MMU.Dmem()); + uint8_t * Dest = ((SP_MEM_ADDR_REG & 0x1000) != 0 ? m_IMEM : m_DMEM); uint8_t * Source = m_MMU.Rdram(); uint32_t ReadPos = SP_DRAM_ADDR_REG & 0x00FFFFF8; int32_t Length = ((SP_RD_LEN_REG & 0xFFF) | 7) + 1; @@ -335,7 +360,7 @@ void SPRegistersHandler::SP_DMA_READ() void SPRegistersHandler::SP_DMA_WRITE() { - uint8_t * Source = ((SP_MEM_ADDR_REG & 0x1000) != 0 ? m_MMU.Imem() : m_MMU.Dmem()); + uint8_t * Source = ((SP_MEM_ADDR_REG & 0x1000) != 0 ? m_IMEM : m_DMEM); uint8_t * Dest = m_MMU.Rdram(); uint32_t WritePos = SP_DRAM_ADDR_REG & 0x00FFFFF8; int32_t Length = ((SP_WR_LEN_REG & 0xFFF) | 7) + 1; @@ -407,6 +432,9 @@ void SPRegistersHandler::SystemReset(void) { SP_RD_LEN_REG = 0x00000FF8; SP_WR_LEN_REG = 0x00000FF8; + + memset(m_IMEM, 0, sizeof(m_IMEM)); + memset(m_DMEM, 0, sizeof(m_DMEM)); } void SPRegistersHandler::LoadedGameState(void) diff --git a/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.h b/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.h index 57b36290a..e86cec453 100644 --- a/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.h +++ b/Source/Project64-core/N64System/MemoryHandler/SPRegistersHandler.h @@ -46,6 +46,16 @@ public: bool Read32(uint32_t Address, uint32_t & Value); bool Write32(uint32_t Address, uint32_t Value, uint32_t Mask); + uint8_t * Imem() + { + return &m_IMEM[0]; + } + + uint8_t * Dmem() + { + return &m_DMEM[0]; + } + private: SPRegistersHandler(); SPRegistersHandler(const SPRegistersHandler &); @@ -65,6 +75,8 @@ private: void SystemReset(void); void LoadedGameState(void); + uint8_t m_IMEM[0x1000]; + uint8_t m_DMEM[0x1000]; uint32_t m_SPMemAddrRegRead; uint32_t m_SPDramAddrRegRead; bool m_ExecutedDMARead; diff --git a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp index 84b23a31e..71ff9a94e 100755 --- a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp +++ b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp @@ -39,8 +39,6 @@ CMipsMemoryVM::CMipsMemoryVM(CN64System & System, bool SavesReadOnly) : m_TLB_ReadMap(nullptr), m_TLB_WriteMap(nullptr), m_RDRAM(nullptr), - m_DMEM(nullptr), - m_IMEM(nullptr), m_Rom(*g_Rom) { g_Settings->RegisterChangeCB(Game_RDRamSize, this, (CSettings::SettingChangedFunc)RdramChanged); @@ -68,13 +66,11 @@ void CMipsMemoryVM::Reset(bool /*EraseMemory*/) } for (size_t Address = BaseAddress + 0x04000000; Address < (BaseAddress + 0x04001000); Address += 0x1000) { - m_MemoryReadMap[Address >> 12] = (size_t)(m_DMEM - Address); - m_MemoryWriteMap[Address >> 12] = (size_t)(m_DMEM - Address); + m_MemoryReadMap[Address >> 12] = (size_t)(Dmem() - Address); } for (size_t Address = BaseAddress + 0x04001000; Address < (BaseAddress + 0x04002000); Address += 0x1000) { - m_MemoryReadMap[Address >> 12] = (size_t)(m_IMEM - Address); - m_MemoryWriteMap[Address >> 12] = (size_t)(m_IMEM - Address); + m_MemoryReadMap[Address >> 12] = (size_t)(Imem() - Address); } } } @@ -173,16 +169,6 @@ bool CMipsMemoryVM::Initialize(bool SyncSystem) return false; } - if (CommitMemory(m_RDRAM + 0x04000000, 0x2000, MEM_READWRITE) == nullptr) - { - WriteTrace(TraceN64System, TraceError, "Failed to allocate DMEM/IMEM (Size: 0x%X)", 0x2000); - FreeMemory(); - return false; - } - - m_DMEM = (uint8_t *)(m_RDRAM + 0x04000000); - m_IMEM = (uint8_t *)(m_RDRAM + 0x04001000); - m_MemoryReadMap = new size_t[0x100000]; if (m_MemoryReadMap == nullptr) { @@ -241,8 +227,6 @@ void CMipsMemoryVM::FreeMemory() FreeAddressSpace(m_RDRAM, 0x20000000); } m_RDRAM = nullptr; - m_IMEM = nullptr; - m_DMEM = nullptr; } if (m_TLB_ReadMap) { @@ -278,13 +262,13 @@ uint8_t * CMipsMemoryVM::MemoryPtr(uint32_t VAddr, uint32_t Size, bool Read) return (uint8_t *)(m_RDRAM + PAddr); } - if (PAddr >= 0x04000000 && (PAddr + Size) < 0x04001000) + if (PAddr >= 0x04000000 && (PAddr + Size) <= 0x04001000) { - return (uint8_t *)(m_DMEM + (PAddr - 0x04000000)); + return (uint8_t *)&m_SPRegistersHandler.Dmem()[PAddr - 0x04000000]; } - if (PAddr >= 0x04001000 && (PAddr + Size) < 0x04002000) + if (PAddr >= 0x04001000 && (PAddr + Size) <= 0x04002000) { - return (uint8_t *)(m_IMEM + (PAddr - 0x04001000)); + return (uint8_t *)&m_SPRegistersHandler.Imem()[PAddr - 0x04001000]; } if (Read && PAddr >= 0x10000000 && (PAddr + Size) < (0x10000000 + m_Rom.GetRomSize())) { @@ -595,11 +579,6 @@ bool CMipsMemoryVM::SD_Memory(uint64_t VAddr, uint64_t Value) *(uint32_t *)(MemoryPtr + VAddr32 + 4) = *((uint32_t *)(&Value)); return true; } - if (m_TLB_WriteMap[VAddr32 >> 12] == -1) - { - GenerateTLBWriteException(VAddr, __FUNCTION__); - return false; - } return SD_NonMemory(VAddr32, Value); } @@ -784,6 +763,7 @@ bool CMipsMemoryVM::SB_NonMemory(uint32_t VAddr, uint32_t Value) *(uint8_t *)(m_RDRAM + (PAddr ^ 3)) = (uint8_t)Value; } break; + case 0x04000000: m_SPRegistersHandler.Write32(PAddr & ~3, Value << ((3 - (PAddr & 3)) * 8), 0xFFFFFFFF); break; case 0x1FC00000: m_PifRamHandler.Write32(PAddr & ~3, Value << ((3 - (PAddr & 3)) * 8), 0xFFFFFFFF); break; default: if (PAddr >= 0x10000000 && PAddr < 0x20000000) @@ -834,6 +814,7 @@ bool CMipsMemoryVM::SH_NonMemory(uint32_t VAddr, uint32_t Value) } } break; + case 0x04000000: m_SPRegistersHandler.Write32(PAddr & ~3, Value << ((2 - (PAddr & 2)) * 8), 0xFFFFFFFF); break; case 0x1FC00000: m_PifRamHandler.Write32(PAddr & ~3, Value << ((2 - (PAddr & 2)) * 8), 0xFFFFFFFF); break; default: if (PAddr >= 0x10000000 && PAddr < 0x20000000) @@ -888,17 +869,7 @@ bool CMipsMemoryVM::SW_NonMemory(uint32_t VAddr, uint32_t Value) } break; case 0x03F00000: m_RDRAMRegistersHandler.Write32(PAddr, Value, 0xFFFFFFFF); break; - case 0x04000000: - if (PAddr < 0x04002000) - { - g_Recompiler->ClearRecompCode_Phys(PAddr & ~0xFFF, 0xFFF, CRecompiler::Remove_ProtectedMem); - *(uint32_t *)(m_RDRAM + PAddr) = Value; - } - else - { - m_SPRegistersHandler.Write32(PAddr, Value, 0xFFFFFFFF); - } - break; + case 0x04000000: m_SPRegistersHandler.Write32(PAddr, Value, 0xFFFFFFFF); break; case 0x04100000: m_DPCommandRegistersHandler.Write32(PAddr, Value, 0xFFFFFFFF); break; case 0x04300000: m_MIPSInterfaceHandler.Write32(PAddr, Value, 0xFFFFFFFF); break; case 0x04400000: m_VideoInterfaceHandler.Write32(PAddr, Value, 0xFFFFFFFF); break; @@ -955,6 +926,7 @@ bool CMipsMemoryVM::SD_NonMemory(uint32_t VAddr, uint64_t Value) *(uint64_t *)(m_RDRAM + PAddr) = Value; } break; + case 0x04000000: m_SPRegistersHandler.Write32(PAddr, (int32_t)(Value >> 32), 0xFFFFFFFF); break; default: if (PAddr >= 0x10000000 && PAddr < 0x20000000) { diff --git a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h index e65d913ee..c38086903 100644 --- a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h +++ b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h @@ -69,13 +69,13 @@ public: { return m_AllocatedRdramSize; } - uint8_t * Dmem() const + uint8_t * Dmem() { - return m_DMEM; + return m_SPRegistersHandler.Dmem(); } - uint8_t * Imem() const + uint8_t * Imem() { - return m_IMEM; + return m_SPRegistersHandler.Imem(); } CSram & GetSram() @@ -217,7 +217,7 @@ private: SerialInterfaceHandler m_SerialInterfaceHandler; SPRegistersHandler m_SPRegistersHandler; VideoInterfaceHandler m_VideoInterfaceHandler; - uint8_t *m_RDRAM, *m_DMEM, *m_IMEM; + uint8_t * m_RDRAM; uint32_t m_AllocatedRdramSize; CN64Rom & m_Rom; diff --git a/Source/Project64-core/N64System/N64System.cpp b/Source/Project64-core/N64System/N64System.cpp index 7c1b75af5..3cb3b61ab 100644 --- a/Source/Project64-core/N64System/N64System.cpp +++ b/Source/Project64-core/N64System/N64System.cpp @@ -906,6 +906,7 @@ void CN64System::Reset(bool bInitReg, bool ClearMenory) { WriteTrace(TraceN64System, TraceDebug, "Start (bInitReg: %s, ClearMenory: %s)", bInitReg ? "true" : "false", ClearMenory ? "true" : "false"); g_Settings->SaveBool(GameRunning_InReset, true); + NotifyCallback(CN64SystemCB_Reset); RefreshGameSettings(); m_MMU_VM.Reset(ClearMenory); @@ -952,7 +953,6 @@ void CN64System::Reset(bool bInitReg, bool ClearMenory) { m_SyncCPU->Reset(bInitReg, ClearMenory); } - NotifyCallback(CN64SystemCB_Reset); g_Settings->SaveBool(GameRunning_InReset, false); WriteTrace(TraceN64System, TraceDebug, "Done");