From a3ffe295cc7e39e7e47b9393a9af72f433cb07ca Mon Sep 17 00:00:00 2001 From: Ziemas Date: Tue, 9 Dec 2025 14:04:32 +0100 Subject: [PATCH] Enable extension of IOP RAM to devkit spec (8MB) --- pcsx2-qt/Settings/AdvancedSettingsWidget.cpp | 4 +- pcsx2-qt/Settings/AdvancedSettingsWidget.ui | 2 +- pcsx2/DebugTools/DebugInterface.cpp | 2 +- pcsx2/IopMem.cpp | 5 +- pcsx2/IopMem.h | 2 +- pcsx2/Memory.cpp | 2 + pcsx2/MemoryTypes.h | 10 ++-- pcsx2/SaveState.cpp | 2 +- pcsx2/VMManager.cpp | 2 +- pcsx2/x86/iR3000A.cpp | 50 +++++++++++++------- pcsx2/x86/iR3000Atables.cpp | 2 +- 11 files changed, 53 insertions(+), 30 deletions(-) diff --git a/pcsx2-qt/Settings/AdvancedSettingsWidget.cpp b/pcsx2-qt/Settings/AdvancedSettingsWidget.cpp index 0aeb64ff30..b1811c76de 100644 --- a/pcsx2-qt/Settings/AdvancedSettingsWidget.cpp +++ b/pcsx2-qt/Settings/AdvancedSettingsWidget.cpp @@ -100,8 +100,8 @@ AdvancedSettingsWidget::AdvancedSettingsWidget(SettingsWindow* settings_dialog, "end of the block, not on the instruction which caused the exception. Refer to the console to see the address where the invalid " "access occurred.")); - dialog()->registerWidgetHelp(m_ui.extraMemory, tr("Enable 128MB RAM (Dev Console)"), tr("Unchecked"), - tr("Exposes an additional 96MB of memory to the virtual machine.")); + dialog()->registerWidgetHelp(m_ui.extraMemory, tr("Enable Extended RAM (Dev Console)"), tr("Unchecked"), + tr("Exposes additional memory to the virtual machine, expanding the EE and IOP memory to 128MB and 8MB respectively.")); dialog()->registerWidgetHelp(m_ui.vu0RoundingMode, tr("VU0 Rounding Mode"), tr("Chop/Zero (Default)"), tr("Changes how PCSX2 handles rounding while emulating the Emotion Engine's Vector Unit 0 (EE VU0). " "The default value handles the vast majority of games; modifying this setting when a game is not having a visible problem will cause stability issues and/or crashes.")); diff --git a/pcsx2-qt/Settings/AdvancedSettingsWidget.ui b/pcsx2-qt/Settings/AdvancedSettingsWidget.ui index e5cd7b2b13..6992e77364 100644 --- a/pcsx2-qt/Settings/AdvancedSettingsWidget.ui +++ b/pcsx2-qt/Settings/AdvancedSettingsWidget.ui @@ -176,7 +176,7 @@ - Enable 128MB RAM (Dev Console) + Enable Extended RAM (Dev Console) diff --git a/pcsx2/DebugTools/DebugInterface.cpp b/pcsx2/DebugTools/DebugInterface.cpp index 0c6fffd3ab..76ff126ac5 100644 --- a/pcsx2/DebugTools/DebugInterface.cpp +++ b/pcsx2/DebugTools/DebugInterface.cpp @@ -1039,7 +1039,7 @@ bool R3000DebugInterface::isValidAddress(u32 addr) return true; } - if (addr < 0x200000) + if (addr < Ps2MemSize::ExposedIopRam) { return true; } diff --git a/pcsx2/IopMem.cpp b/pcsx2/IopMem.cpp index c633e8bfab..2145d9f74d 100644 --- a/pcsx2/IopMem.cpp +++ b/pcsx2/IopMem.cpp @@ -52,10 +52,11 @@ void iopMemReset() // at 0x0, 0x8000, and 0xa000: for (int i = 0; i < 0x0080; i++) { - psxMemWLUT[i + 0x0000] = (uptr)&iopMem->Main[(i & 0x1f) << 16]; + u32 mask = (Ps2MemSize::ExposedIopRam / _64kb) - 1; + psxMemWLUT[i + 0x0000] = (uptr)&iopMem->Main[(i & mask) << 16]; // RLUTs, accessed through WLUT. - psxMemWLUT[i + 0x2000] = (uptr)&iopMem->Main[(i & 0x1f) << 16]; + psxMemWLUT[i + 0x2000] = (uptr)&iopMem->Main[(i & mask) << 16]; } // A few single-page allocations for things we store in special locations. diff --git a/pcsx2/IopMem.h b/pcsx2/IopMem.h index f4c148214b..ba2a6cbf96 100644 --- a/pcsx2/IopMem.h +++ b/pcsx2/IopMem.h @@ -37,7 +37,7 @@ static __fi const T* iopVirtMemR( u32 mem ) // Obtains a pointer to the IOP's physical mapping (bypasses the TLB) static __fi u8* iopPhysMem( u32 addr ) { - return &iopMem->Main[addr & 0x1fffff]; + return &iopMem->Main[addr & (Ps2MemSize::ExposedIopRam - 1)]; } #define psxSs8(mem) iopMem->Sif[(mem) & 0x00ff] diff --git a/pcsx2/Memory.cpp b/pcsx2/Memory.cpp index ec6ad55bab..0c54a12c30 100644 --- a/pcsx2/Memory.cpp +++ b/pcsx2/Memory.cpp @@ -45,6 +45,7 @@ BIOS namespace Ps2MemSize { u32 ExposedRam = MainRam; + u32 ExposedIopRam = IopRam; } // namespace Ps2MemSize namespace SysMemory @@ -305,6 +306,7 @@ void memSetExtraMemMode(bool mode) // update the amount of RAM exposed to the VM Ps2MemSize::ExposedRam = mode ? Ps2MemSize::TotalRam : Ps2MemSize::MainRam; + Ps2MemSize::ExposedIopRam = mode ? Ps2MemSize::TotalIopRam: Ps2MemSize::IopRam; } void memSetKernelMode() { diff --git a/pcsx2/MemoryTypes.h b/pcsx2/MemoryTypes.h index cb04c60953..f02933bf9e 100644 --- a/pcsx2/MemoryTypes.h +++ b/pcsx2/MemoryTypes.h @@ -16,11 +16,14 @@ namespace Ps2MemSize static constexpr u32 Scratch = _16kb; static constexpr u32 IopRam = _1mb * 2; // 2MB main ram on the IOP. + static constexpr u32 ExtraIopRam = _1mb * 6; // 2MB main ram on the IOP. + static constexpr u32 TotalIopRam = _8mb; // 2MB main ram on the IOP. static constexpr u32 IopHardware = _64kb; static constexpr u32 GSregs = 0x00002000; // 8k for the GS registers and stuff. extern u32 ExposedRam; + extern u32 ExposedIopRam; } // namespace Ps2MemSize typedef u8 mem8_t; @@ -51,9 +54,10 @@ struct EEVM_MemoryAllocMess // Needs to fit within IOPmemSize of Memory.h struct IopVM_MemoryAllocMess { - u8 Main[Ps2MemSize::IopRam]; // Main memory (hard-wired to 2MB) - u8 P[_64kb]; // I really have no idea what this is... --air - u8 Sif[0x100]; // a few special SIF/SBUS registers (likely not needed) + u8 Main[Ps2MemSize::IopRam]; // Main memory (hard-wired to 2MB) + u8 ExtraMemory[Ps2MemSize::ExtraIopRam]; // Extended memory to 16mb + u8 P[_64kb]; // I really have no idea what this is... --air + u8 Sif[0x100]; // a few special SIF/SBUS registers (likely not needed) }; diff --git a/pcsx2/SaveState.cpp b/pcsx2/SaveState.cpp index 5744d68719..7c058a0f76 100644 --- a/pcsx2/SaveState.cpp +++ b/pcsx2/SaveState.cpp @@ -536,7 +536,7 @@ public: const char* GetFilename() const override { return "iopMemory.bin"; } u8* GetDataPtr() const override { return iopMem->Main; } - uint GetDataSize() const override { return sizeof(iopMem->Main); } + uint GetDataSize() const override { return Ps2MemSize::ExposedIopRam; } }; class SavestateEntry_HwRegs final : public MemorySavestateEntry diff --git a/pcsx2/VMManager.cpp b/pcsx2/VMManager.cpp index fc1c4b712b..b4e7eeb142 100644 --- a/pcsx2/VMManager.cpp +++ b/pcsx2/VMManager.cpp @@ -3276,7 +3276,7 @@ void VMManager::WarnAboutUnsafeSettings() if (EmuConfig.Cpu.ExtraMemory) { append(ICON_PF_MICROCHIP, - TRANSLATE_SV("VMManager", "128MB RAM is enabled. Compatibility with some games may be affected.")); + TRANSLATE_SV("VMManager", "Extended RAM is enabled. Compatibility with some games may be affected.")); } if (!EmuConfig.EnableGameFixes) { diff --git a/pcsx2/x86/iR3000A.cpp b/pcsx2/x86/iR3000A.cpp index afc5b87e4a..29f451a6b1 100644 --- a/pcsx2/x86/iR3000A.cpp +++ b/pcsx2/x86/iR3000A.cpp @@ -841,36 +841,44 @@ void psxRecompileCodeConst3(R3000AFNPTR constcode, R3000AFNPTR_INFO constscode, } static u8* m_recBlockAlloc = NULL; +static bool extraRam = false; -static const uint m_recBlockAllocSize = - (((Ps2MemSize::IopRam + Ps2MemSize::Rom + Ps2MemSize::Rom1 + Ps2MemSize::Rom2) / 4) * sizeof(BASEBLOCK)); - -static void recReserve() +static void recReserveRAM() { - recPtr = SysMemory::GetIOPRec(); - recPtrEnd = SysMemory::GetIOPRecEnd() - _64kb; + uint m_recBlockAllocSize = + (((Ps2MemSize::ExposedIopRam + Ps2MemSize::Rom + Ps2MemSize::Rom1 + Ps2MemSize::Rom2) / 4) * sizeof(BASEBLOCK)); // Goal: Allocate BASEBLOCKs for every possible branch target in IOP memory. // Any 4-byte aligned address makes a valid branch target as per MIPS design (all instructions are // always 4 bytes long). - if (!m_recBlockAlloc) + if (m_recBlockAlloc) { - // We're on 64-bit, if these memory allocations fail, we're in real trouble. - m_recBlockAlloc = (u8*)_aligned_malloc(m_recBlockAllocSize, 4096); - if (!m_recBlockAlloc) - pxFailRel("Failed to allocate R3000A BASEBLOCK lookup tables"); + _aligned_free(m_recBlockAlloc); } + // We're on 64-bit, if these memory allocations fail, we're in real trouble. + m_recBlockAlloc = (u8*)_aligned_malloc(m_recBlockAllocSize, 4096); + if (!m_recBlockAlloc) + pxFailRel("Failed to allocate R3000A BASEBLOCK lookup tables"); + u8* curpos = m_recBlockAlloc; recRAM = (BASEBLOCK*)curpos; - curpos += (Ps2MemSize::IopRam / 4) * sizeof(BASEBLOCK); + curpos += (Ps2MemSize::ExposedIopRam / 4) * sizeof(BASEBLOCK); recROM = (BASEBLOCK*)curpos; curpos += (Ps2MemSize::Rom / 4) * sizeof(BASEBLOCK); recROM1 = (BASEBLOCK*)curpos; curpos += (Ps2MemSize::Rom1 / 4) * sizeof(BASEBLOCK); recROM2 = (BASEBLOCK*)curpos; curpos += (Ps2MemSize::Rom2 / 4) * sizeof(BASEBLOCK); +} + +static void recReserve() +{ + recPtr = SysMemory::GetIOPRec(); + recPtrEnd = SysMemory::GetIOPRecEnd() - _64kb; + + recReserveRAM(); pxAssertRel(!s_pInstCache, "InstCache not allocated"); s_nInstCacheSize = 128; @@ -883,12 +891,18 @@ void recResetIOP() { DevCon.WriteLn("iR3000A Recompiler reset."); + if (CHECK_EXTRAMEM != extraRam) + { + recReserveRAM(); + extraRam = !extraRam; + } + xSetPtr(SysMemory::GetIOPRec()); _DynGen_Dispatchers(); recPtr = xGetPtr(); iopClearRecLUT((BASEBLOCK*)m_recBlockAlloc, - (((Ps2MemSize::IopRam + Ps2MemSize::Rom + Ps2MemSize::Rom1 + Ps2MemSize::Rom2) / 4))); + (((Ps2MemSize::ExposedIopRam + Ps2MemSize::Rom + Ps2MemSize::Rom1 + Ps2MemSize::Rom2) / 4))); for (int i = 0; i < 0x10000; i++) recLUT_SetPage(psxRecLUT, 0, 0, 0, i, 0); @@ -899,13 +913,15 @@ void recResetIOP() // the pc indexer into it's lower common denominator. // We're only mapping 20 pages here in 4 places. - // 0x80 comes from : (Ps2MemSize::IopRam / 0x10000) * 4 + // 0x80 comes from : (Ps2MemSize::IopRam / _64kb) * 4 for (int i = 0; i < 0x80; i++) { - recLUT_SetPage(psxRecLUT, psxhwLUT, recRAM, 0x0000, i, i & 0x1f); - recLUT_SetPage(psxRecLUT, psxhwLUT, recRAM, 0x8000, i, i & 0x1f); - recLUT_SetPage(psxRecLUT, psxhwLUT, recRAM, 0xa000, i, i & 0x1f); + u32 mask = (Ps2MemSize::ExposedIopRam / _64kb) - 1; + + recLUT_SetPage(psxRecLUT, psxhwLUT, recRAM, 0x0000, i, i & mask); + recLUT_SetPage(psxRecLUT, psxhwLUT, recRAM, 0x8000, i, i & mask); + recLUT_SetPage(psxRecLUT, psxhwLUT, recRAM, 0xa000, i, i & mask); } for (int i = 0x1fc0; i < 0x2000; i++) diff --git a/pcsx2/x86/iR3000Atables.cpp b/pcsx2/x86/iR3000Atables.cpp index 6ad9bae50b..902a3c2af9 100644 --- a/pcsx2/x86/iR3000Atables.cpp +++ b/pcsx2/x86/iR3000Atables.cpp @@ -1115,7 +1115,7 @@ static void rpsxLoad(int size, bool sign) is_ram_read.SetTarget(); // read from psM directly - xAND(arg1regd, 0x1fffff); + xAND(arg1regd, Ps2MemSize::ExposedIopRam - 1); auto addr = xComplexAddress(rax, iopMem->Main, arg1reg); switch (size)