From cffeceef707f0291420b07706c2ee625c34cbf19 Mon Sep 17 00:00:00 2001 From: zilmar Date: Mon, 1 Aug 2022 10:15:56 +0930 Subject: [PATCH] Core: Handle rom written to better --- .../CartridgeDomain1Address1Handler.cpp | 5 ++- .../CartridgeDomain1Address1Handler.h | 4 ++- .../MemoryHandler/RomMemoryHandler.cpp | 36 +++++++++---------- .../N64System/Mips/MemoryVirtualMem.cpp | 6 ++-- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.cpp b/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.cpp index eb4234ed4..fe290d29f 100644 --- a/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.cpp +++ b/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.cpp @@ -1,8 +1,10 @@ #include "stdafx.h" #include "CartridgeDomain1Address1Handler.h" +#include #include -CartridgeDomain1Address1Handler::CartridgeDomain1Address1Handler(CN64Rom * DDRom) : +CartridgeDomain1Address1Handler::CartridgeDomain1Address1Handler(CRegisters & Reg, CN64Rom * DDRom) : + m_Reg(Reg), m_DDRom(DDRom != nullptr ? DDRom->GetRomAddress() : nullptr), m_DDRomSize(DDRom != nullptr ? DDRom->GetRomSize() : 0) { @@ -16,6 +18,7 @@ bool CartridgeDomain1Address1Handler::Read32(uint32_t Address, uint32_t & Value) } else { + m_Reg.PI_CART_ADDR_REG = (Address + 4) & ~1; Value = ((Address & 0xFFFF) << 16) | (Address & 0xFFFF); } return true; diff --git a/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.h b/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.h index 9d4396886..5de9abc41 100644 --- a/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.h +++ b/Source/Project64-core/N64System/MemoryHandler/CartridgeDomain1Address1Handler.h @@ -1,13 +1,14 @@ #pragma once #include "MemoryHandler.h" +class CRegisters; class CN64Rom; class CartridgeDomain1Address1Handler : public MemoryHandler { public: - CartridgeDomain1Address1Handler(CN64Rom * DDRom); + CartridgeDomain1Address1Handler(CRegisters & Reg, CN64Rom * DDRom); bool Read32(uint32_t Address, uint32_t & Value); bool Write32(uint32_t Address, uint32_t Value, uint32_t Mask); @@ -17,6 +18,7 @@ private: CartridgeDomain1Address1Handler(const CartridgeDomain1Address1Handler &); CartridgeDomain1Address1Handler & operator=(const CartridgeDomain1Address1Handler &); + CRegisters & m_Reg; uint32_t m_DDRomSize; uint8_t * m_DDRom; }; diff --git a/Source/Project64-core/N64System/MemoryHandler/RomMemoryHandler.cpp b/Source/Project64-core/N64System/MemoryHandler/RomMemoryHandler.cpp index b7f5fe6e8..6d90dc95c 100644 --- a/Source/Project64-core/N64System/MemoryHandler/RomMemoryHandler.cpp +++ b/Source/Project64-core/N64System/MemoryHandler/RomMemoryHandler.cpp @@ -18,43 +18,43 @@ RomMemoryHandler::RomMemoryHandler(CN64System & System, CRegisters & Reg, CN64Ro bool RomMemoryHandler::Read32(uint32_t Address, uint32_t & Value) { + uint32_t ReadAddr = (Address + 2) & ~0x3; + if (g_DDRom == nullptr) { - m_Reg.PI_CART_ADDR_REG = Address + 4; + m_Reg.PI_CART_ADDR_REG = (Address + 4) & ~1; } if (m_RomWrittenTo) { Value = m_RomWroteValue; - //LogMessage("%X: Read crap from ROM %08X from %08X",PROGRAM_COUNTER,*Value,PAddr); m_RomWrittenTo = false; } else if ((Address & 0xFFFFFFF) < m_Rom.GetRomSize()) { - Value = *(uint32_t *)&m_Rom.GetRomAddress()[(Address & 0xFFFFFFF)]; + Value = *(uint32_t *)&m_Rom.GetRomAddress()[(ReadAddr & 0xFFFFFFF)]; + if (LogRomHeader() && (ReadAddr & 0x1FFFFFFF) >= 0x10000000 && (ReadAddr & 0x1FFFFFFF) < 0x10000040) + { + switch (ReadAddr & 0x1FFFFFFF) + { + case 0x10000004: LogMessage("%08X: read from ROM clock rate (%08X)", m_PC, Value); break; + case 0x10000008: LogMessage("%08X: read from ROM boot address offset (%08X)", m_PC, Value); break; + case 0x1000000C: LogMessage("%08X: read from ROM release offset (%08X)", m_PC, Value); break; + case 0x10000010: LogMessage("%08X: read from ROM CRC1 (%08X)", m_PC, Value); break; + case 0x10000014: LogMessage("%08X: read from ROM CRC2 (%08X)", m_PC, Value); break; + default: LogMessage("%08X: read from ROM header 0x%X (%08X)", m_PC, ReadAddr & 0xFF, Value); break; + } + } } else { Value = (Address << 16) | (Address & 0xFFFF); } - - if (LogRomHeader() && (Address & 0x1FFFFFFF) >= 0x10000000 && (Address & 0x1FFFFFFF) < 0x10000040) - { - switch (Address & 0x1FFFFFFF) - { - case 0x10000004: LogMessage("%08X: read from ROM clock rate (%08X)", m_PC, Value); break; - case 0x10000008: LogMessage("%08X: read from ROM boot address offset (%08X)", m_PC, Value); break; - case 0x1000000C: LogMessage("%08X: read from ROM release offset (%08X)", m_PC, Value); break; - case 0x10000010: LogMessage("%08X: read from ROM CRC1 (%08X)", m_PC, Value); break; - case 0x10000014: LogMessage("%08X: read from ROM CRC2 (%08X)", m_PC, Value); break; - default: LogMessage("%08X: read from ROM header 0x%X (%08X)", m_PC, Address & 0xFF, Value); break; - } - } return true; } -bool RomMemoryHandler::Write32(uint32_t Address, uint32_t Value, uint32_t Mask) +bool RomMemoryHandler::Write32(uint32_t /*Address*/, uint32_t Value, uint32_t Mask) { - if (((Address & 0x1FFFFFFF) - 0x10000000) < m_Rom.GetRomSize()) + if (!m_RomWrittenTo) { m_RomWrittenTo = true; m_RomWroteValue = (Value & Mask); diff --git a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp index ede2c3173..f6455ba25 100755 --- a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp +++ b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp @@ -22,7 +22,7 @@ CMipsMemoryVM::CMipsMemoryVM(CN64System & System, bool SavesReadOnly) : m_System(System), m_Reg(System.m_Reg), m_AudioInterfaceHandler(System, System.m_Reg), - m_CartridgeDomain1Address1Handler(g_DDRom), + m_CartridgeDomain1Address1Handler(System.m_Reg, g_DDRom), m_CartridgeDomain2Address1Handler(System.m_Reg), m_CartridgeDomain2Address2Handler(System, System.m_Reg, *this, SavesReadOnly), m_RDRAMRegistersHandler(System.m_Reg), @@ -602,7 +602,7 @@ bool CMipsMemoryVM::LB_NonMemory(uint32_t VAddr, uint8_t & Value) else if (PAddr >= 0x10000000 && PAddr < 0x16000000) { uint32_t Value32; - if (!m_RomMemoryHandler.Read32((PAddr + 2) & ~0x3, Value32)) + if (!m_RomMemoryHandler.Read32(PAddr, Value32)) { return false; } @@ -635,7 +635,7 @@ bool CMipsMemoryVM::LH_NonMemory(uint32_t VAddr, uint16_t & Value) else if (PAddr >= 0x10000000 && PAddr < 0x16000000) { uint32_t Value32; - if (!m_RomMemoryHandler.Read32((PAddr + 2) & ~0x3, Value32)) + if (!m_RomMemoryHandler.Read32(PAddr, Value32)) { return false; }