diff --git a/Source/Project64-core/Logging.cpp b/Source/Project64-core/Logging.cpp index 25aec82df..739035e48 100644 --- a/Source/Project64-core/Logging.cpp +++ b/Source/Project64-core/Logging.cpp @@ -32,7 +32,6 @@ void CLogging::Log_LW(uint32_t PC, uint32_t VAddr) VAddr = PAddr + 0xA0000000; } - uint32_t Value; if ((VAddr >= 0xA0000000 && VAddr < (0xA0000000 + g_MMU->RdramSize())) || (VAddr >= 0xA3F00000 && VAddr <= 0xA3F00024) || (VAddr >= 0xA4000000 && VAddr <= 0xA4001FFC) || @@ -42,21 +41,11 @@ void CLogging::Log_LW(uint32_t PC, uint32_t VAddr) (VAddr >= 0xA4400000 && VAddr <= 0xA4400034) || (VAddr >= 0xA4500000 && VAddr <= 0xA4500014) || (VAddr == 0xA4800000 && VAddr <= 0xA4800018) || - (VAddr >= 0xBFC00000 && VAddr <= 0xBFC007C0) || - (VAddr >= 0xB0000000 && ((VAddr - 0xB0000000) < g_Rom->GetRomSize()))) + (VAddr >= 0xB0000000 && ((VAddr - 0xB0000000) < g_Rom->GetRomSize())) || + (VAddr >= 0xBFC00000 && VAddr <= 0xBFC007FC)) { return; } - if (VAddr >= 0xBFC007C0 && VAddr <= 0xBFC007FC) - { - if (!LogPRDirectMemLoads()) - { - return; - } - g_MMU->LW_VAddr(VAddr, Value); - LogMessage("%08X: read word from PIF RAM at 0x%X (%08X)", PC, VAddr - 0xBFC007C0, Value); - return; - } if (!LogUnknown()) { return; @@ -85,17 +74,6 @@ void CLogging::Log_SW(uint32_t PC, uint32_t VAddr, uint32_t Value) VAddr = PAddr + 0xA0000000; } - if ((VAddr >= 0xA0000000 && VAddr < (0xA0000000 + g_MMU->RdramSize())) || - (VAddr >= 0xA3F00000 && VAddr <= 0xA3F00024) || - (VAddr >= 0xA4000000 && VAddr <= 0xA4001FFC) || - (VAddr >= 0xA4040000 && VAddr <= 0xA404001C) || - (VAddr == 0xA4080000) || - (VAddr >= 0xA4100000 && VAddr <= 0xA410001C)) - { - return; - } - - if (VAddr >= 0xA4200000 && VAddr <= 0xA420000C) { if (!LogDPSRegisters()) @@ -111,23 +89,21 @@ void CLogging::Log_SW(uint32_t PC, uint32_t VAddr, uint32_t Value) } } - if ((VAddr >= 0xA4300000 && VAddr <= 0xA430000C) || + if ((VAddr >= 0xA0000000 && VAddr < (0xA0000000 + g_MMU->RdramSize())) || + (VAddr >= 0xA3F00000 && VAddr <= 0xA3F00024) || + (VAddr >= 0xA4000000 && VAddr <= 0xA4001FFC) || + (VAddr >= 0xA4040000 && VAddr <= 0xA404001C) || + (VAddr == 0xA4080000) || + (VAddr >= 0xA4100000 && VAddr <= 0xA410001C) || + (VAddr >= 0xA4300000 && VAddr <= 0xA430000C) || (VAddr >= 0xA4400000 && VAddr <= 0xA4400034) || (VAddr >= 0xA4500000 && VAddr <= 0xA4500014) || - (VAddr >= 0xA4800000 && VAddr <= 0xA4800018)) + (VAddr >= 0xA4800000 && VAddr <= 0xA4800018) || + (VAddr >= 0xBFC007C0 && VAddr <= 0xBFC007FC)) { return; } - if (VAddr >= 0xBFC007C0 && VAddr <= 0xBFC007FC) - { - if (!LogPRDirectMemStores()) - { - return; - } - LogMessage("%08X: Writing 0x%08X to PIF RAM at 0x%X", PC, Value, VAddr - 0xBFC007C0); - return; - } if (!LogUnknown()) { return; diff --git a/Source/Project64-core/N64System/MemoryHandler/MIPSInterfaceHandler.cpp b/Source/Project64-core/N64System/MemoryHandler/MIPSInterfaceHandler.cpp index c6bdfa03d..6c4586695 100644 --- a/Source/Project64-core/N64System/MemoryHandler/MIPSInterfaceHandler.cpp +++ b/Source/Project64-core/N64System/MemoryHandler/MIPSInterfaceHandler.cpp @@ -36,7 +36,7 @@ bool MIPSInterfaceHandler::Read32(uint32_t Address, uint32_t & Value) } } - if (LogMIPSInterface()) + if (GenerateLog() && LogMIPSInterface()) { switch (Address & 0x1FFFFFFF) { @@ -56,7 +56,7 @@ bool MIPSInterfaceHandler::Read32(uint32_t Address, uint32_t & Value) bool MIPSInterfaceHandler::Write32(uint32_t Address, uint32_t Value, uint32_t Mask) { - if (LogMIPSInterface()) + if (GenerateLog() && LogMIPSInterface()) { switch (Address & 0x1FFFFFFF) { diff --git a/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.cpp b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.cpp new file mode 100644 index 000000000..682fb7971 --- /dev/null +++ b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.cpp @@ -0,0 +1,89 @@ +#include "stdafx.h" +#include "PifRamHandler.h" +#include +#include + +PifRamHandler::PifRamHandler(CMipsMemoryVM & MMU, CRegisters & Reg) : + m_MMU(MMU), + m_PifRam(MMU.PifRam()), + m_PC(Reg.m_PROGRAM_COUNTER) +{ +} + +bool PifRamHandler::Read32(uint32_t Address, uint32_t & Value) +{ + Address &= 0x1FFFFFFF; + if (Address < 0x1FC007C0) + { + //Value = swap32by8(*(uint32_t *)(&PifRom[PAddr - 0x1FC00000])); + Value = 0; + if (HaveDebugger()) + { + g_Notify->BreakPoint(__FILE__, __LINE__); + } + } + else if (Address < 0x1FC00800) + { + Value = *(uint32_t *)(&m_PifRam[Address - 0x1FC007C0]); + Value = swap32by8(Value); + } + else + { + Value = 0; + if (HaveDebugger()) + { + g_Notify->BreakPoint(__FILE__, __LINE__); + } + } + + if (GenerateLog() && LogPRDirectMemLoads() && Address >= 0x1FC007C0 && Address <= 0x1FC007FC) + { + LogMessage("%08X: read word from PIF RAM at 0x%X (%08X)", m_PC, Address - 0x1FC007C0, Value); + } + return true; +} + +bool PifRamHandler::Write32(uint32_t Address, uint32_t Value, uint32_t Mask) +{ + Address &= 0x1FFFFFFF; + if (GenerateLog() && LogPRDirectMemStores() && Address >= 0x1FC007C0 && Address <= 0x1FC007FC) + { + LogMessage("%08X: Writing 0x%08X to PIF RAM at 0x%X", m_PC, Value, Address - 0x1FC007C0); + } + + if (Address < 0x1FC007C0) + { + if (HaveDebugger()) + { + g_Notify->BreakPoint(__FILE__, __LINE__); + } + } + else if (Address < 0x1FC00800) + { + uint32_t SwappedMask = swap32by8(Mask); + Value = ((*(uint32_t *)(&m_PifRam[Address - 0x1FC007C0])) & ~SwappedMask) | (Value & SwappedMask); + *(uint32_t *)(&m_PifRam[Address - 0x1FC007C0]) = Value; + if (Address == 0x1FC007FC) + { + m_MMU.PifRamWrite(); + } + } + return true; +} + +uint32_t PifRamHandler::swap32by8(uint32_t word) +{ + const uint32_t swapped = +#if defined(_MSC_VER) + _byteswap_ulong(word) +#elif defined(__GNUC__) + __builtin_bswap32(word) +#else + (word & 0x000000FFul) << 24 + | (word & 0x0000FF00ul) << 8 + | (word & 0x00FF0000ul) >> 8 + | (word & 0xFF000000ul) >> 24 +#endif + ; + return (swapped & 0xFFFFFFFFul); +} \ No newline at end of file diff --git a/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.h b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.h new file mode 100644 index 000000000..bafb805a0 --- /dev/null +++ b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.h @@ -0,0 +1,30 @@ +#pragma once +#include "MemoryHandler.h" +#include +#include +#include + +class CMipsMemoryVM; +class CRegisters; + +class PifRamHandler : + public MemoryHandler, + private CDebugSettings, + private CLogging +{ +public: + PifRamHandler(CMipsMemoryVM & MMU, CRegisters & Reg); + bool Read32(uint32_t Address, uint32_t & Value); + bool Write32(uint32_t Address, uint32_t Value, uint32_t Mask); + +private: + PifRamHandler(); + PifRamHandler(const PifRamHandler &); + PifRamHandler & operator=(const PifRamHandler &); + + static uint32_t swap32by8(uint32_t word); + + CMipsMemoryVM & m_MMU; + uint8_t * m_PifRam; + uint32_t & m_PC; +}; \ No newline at end of file diff --git a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp index 481c974ff..e2edc91c7 100755 --- a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp +++ b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp @@ -8,9 +8,8 @@ #include #include #include - +#include #include -#include uint8_t * CMipsMemoryVM::m_Reserve1 = nullptr; uint8_t * CMipsMemoryVM::m_Reserve2 = nullptr; @@ -33,6 +32,7 @@ CMipsMemoryVM::CMipsMemoryVM(CN64System & System, bool SavesReadOnly) : m_DPCommandRegistersHandler(System, System.GetPlugins(), System.m_Reg), m_MIPSInterfaceHandler(System.m_Reg), m_PeripheralInterfaceHandler(*this, System.m_Reg), + m_PifRamHandler(*this, System.m_Reg), m_RDRAMInterfaceHandler(System.m_Reg), m_RomMemoryHandler(System, System.m_Reg, *g_Rom), m_SerialInterfaceHandler(*this, System.m_Reg), @@ -52,23 +52,6 @@ CMipsMemoryVM::CMipsMemoryVM(CN64System & System, bool SavesReadOnly) : g_Settings->RegisterChangeCB(Game_RDRamSize, this, (CSettings::SettingChangedFunc)RdramChanged); } -uint32_t swap32by8(uint32_t word) -{ - const uint32_t swapped = -#if defined(_MSC_VER) - _byteswap_ulong(word) -#elif defined(__GNUC__) - __builtin_bswap32(word) -#else - (word & 0x000000FFul) << 24 - | (word & 0x0000FF00ul) << 8 - | (word & 0x00FF0000ul) >> 8 - | (word & 0xFF000000ul) >> 24 -#endif - ; - return (swapped & 0xFFFFFFFFul); -} - CMipsMemoryVM::~CMipsMemoryVM() { g_Settings->UnregisterChangeCB(Game_RDRamSize, this, (CSettings::SettingChangedFunc)RdramChanged); @@ -561,7 +544,7 @@ bool CMipsMemoryVM::LW_NonMemory(uint32_t PAddr, uint32_t* Value) case 0x05000000: m_CartridgeDomain2Address1Handler.Read32(PAddr, m_MemLookupValue.UW[0]); break; case 0x06000000: Load32CartridgeDomain1Address1(); break; case 0x08000000: Load32CartridgeDomain2Address2(); break; - case 0x1FC00000: Load32PifRam(); break; + case 0x1FC00000: m_PifRamHandler.Read32(PAddr, m_MemLookupValue.UW[0]); break; case 0x1FF00000: Load32CartridgeDomain1Address3(); break; default: if (PAddr >= 0x10000000 && PAddr < 0x16000000) @@ -672,7 +655,7 @@ bool CMipsMemoryVM::SW_NonMemory(uint32_t PAddr, uint32_t Value) case 0x04800000: m_SerialInterfaceHandler.Write32(PAddr, Value, 0xFFFFFFFF); break; case 0x05000000: m_CartridgeDomain2Address1Handler.Write32(PAddr, Value, 0xFFFFFFFF); break; case 0x08000000: Write32CartridgeDomain2Address2(); break; - case 0x1FC00000: Write32PifRam(); break; + case 0x1FC00000: m_PifRamHandler.Write32(PAddr, Value, 0xFFFFFFFF); break; default: if (PAddr >= 0x10000000 && PAddr < 0x16000000) { @@ -1045,33 +1028,6 @@ void CMipsMemoryVM::Load32CartridgeDomain2Address2(void) } } -void CMipsMemoryVM::Load32PifRam(void) -{ - if ((m_MemLookupAddress & 0x1FFFFFFF) < 0x1FC007C0) - { - //m_MemLookupValue.UW[0] = swap32by8(*(uint32_t *)(&PifRom[PAddr - 0x1FC00000])); - m_MemLookupValue.UW[0] = 0; - if (HaveDebugger()) - { - g_Notify->BreakPoint(__FILE__, __LINE__); - } - } - else if ((m_MemLookupAddress & 0x1FFFFFFF) < 0x1FC00800) - { - uint8_t * PIF_Ram = g_MMU->PifRam(); - m_MemLookupValue.UW[0] = *(uint32_t *)(&PIF_Ram[(m_MemLookupAddress & 0x1FFFFFFF) - 0x1FC007C0]); - m_MemLookupValue.UW[0] = swap32by8(m_MemLookupValue.UW[0]); - } - else - { - m_MemLookupValue.UW[0] = 0; - if (HaveDebugger()) - { - g_Notify->BreakPoint(__FILE__, __LINE__); - } - } -} - void CMipsMemoryVM::Write32CartridgeDomain2Address2(void) { uint32_t offset = (m_MemLookupAddress & 0x1FFFFFFF) - 0x08000000; @@ -1105,24 +1061,4 @@ void CMipsMemoryVM::Write32CartridgeDomain2Address2(void) { g_MMU->WriteToFlashCommand(m_MemLookupValue.UW[0]); } -} - -void CMipsMemoryVM::Write32PifRam(void) -{ - if ((m_MemLookupAddress & 0x1FFFFFFF) < 0x1FC007C0) - { - if (HaveDebugger()) - { - g_Notify->BreakPoint(__FILE__, __LINE__); - } - } - else if ((m_MemLookupAddress & 0x1FFFFFFF) < 0x1FC00800) - { - uint32_t Value = swap32by8(m_MemLookupValue.UW[0]); - *(uint32_t *)(&g_MMU->m_PifRam[(m_MemLookupAddress & 0x1FFFFFFF) - 0x1FC007C0]) = Value; - if ((m_MemLookupAddress & 0x1FFFFFFF) == 0x1FC007FC) - { - g_MMU->PifRamWrite(); - } - } } \ No newline at end of file diff --git a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h index eb88eddfe..08ff7970a 100644 --- a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h +++ b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h @@ -12,9 +12,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -136,10 +138,8 @@ private: static void Load32CartridgeDomain1Address1(void); static void Load32CartridgeDomain1Address3(void); static void Load32CartridgeDomain2Address2(void); - static void Load32PifRam(void); static void Write32CartridgeDomain2Address2(void); - static void Write32PifRam(void); #if defined(__i386__) || defined(_M_IX86) @@ -171,6 +171,7 @@ private: DisplayControlRegHandler m_DPCommandRegistersHandler; MIPSInterfaceHandler m_MIPSInterfaceHandler; PeripheralInterfaceHandler m_PeripheralInterfaceHandler; + PifRamHandler m_PifRamHandler; RomMemoryHandler m_RomMemoryHandler; RDRAMInterfaceHandler m_RDRAMInterfaceHandler; RDRAMRegistersHandler m_RDRAMRegistersHandler; diff --git a/Source/Project64-core/Project64-core.vcxproj b/Source/Project64-core/Project64-core.vcxproj index aacbbd567..612c1cc82 100644 --- a/Source/Project64-core/Project64-core.vcxproj +++ b/Source/Project64-core/Project64-core.vcxproj @@ -58,6 +58,7 @@ + @@ -164,6 +165,7 @@ + diff --git a/Source/Project64-core/Project64-core.vcxproj.filters b/Source/Project64-core/Project64-core.vcxproj.filters index 693bb2d48..6d0e39462 100644 --- a/Source/Project64-core/Project64-core.vcxproj.filters +++ b/Source/Project64-core/Project64-core.vcxproj.filters @@ -387,6 +387,9 @@ Source Files\N64 System\MemoryHandler + + Source Files\N64 System\MemoryHandler + @@ -740,6 +743,9 @@ Header Files\N64 System\MemoryHandler + + Header Files\N64 System\MemoryHandler +