From 79d340f4fe99dc12c8c3ff0a0f6e87784337c24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 19:46:59 +0200 Subject: [PATCH 01/16] SaveState: Keep JIT blocks in module code a load doesn't change Loading a state started the JIT over, so every block the game runs had to be compiled again, which stutters. Now the loaded modules' code is compared with the state's as RAM is loaded (a chunk that already matches isn't copied either), and blocks entirely inside unchanged code stay. Everything else is invalidated as before, and without a JIT, or with no unchanged code, the JIT still starts over. The JIT throws its blocks away if the loaded state needs code they weren't compiled for (rounding mode switches, or no default prefixes at block start). The other way round it keeps them, along with the flags they were compiled under: before, a loaded hasSetRounding of false, or the reset of lastSetRounding, had the next compile rebuild everything anyway. In the three seconds after loading a state, Ridge Racer compiles 28 blocks instead of 4823, Force Unleashed 31 instead of 7727. Frames rendered after the load are identical either way. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/HLE/sceKernelModule.cpp | 13 +++++++ Core/HLE/sceKernelModule.h | 2 + Core/MIPS/IR/IRFrontend.cpp | 24 +++++++++++- Core/MIPS/IR/IRFrontend.h | 3 +- Core/MIPS/IR/IRJit.cpp | 57 ++++++++++++++++++++++++----- Core/MIPS/IR/IRJit.h | 2 + Core/MIPS/JitCommon/JitCommon.h | 2 + Core/MIPS/MIPS.cpp | 35 ++++++++++++------ Core/MIPS/MIPS.h | 9 +++++ Core/MemMap.cpp | 65 +++++++++++++++++++++++++++++++-- Core/MemMap.h | 13 ++++++- Core/SaveState.cpp | 11 +++++- 12 files changed, 206 insertions(+), 30 deletions(-) diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index dce0c2120b..2943ed4443 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -534,6 +534,19 @@ static void __KernelModuleInit() { eventModuleLoadDone = CoreTiming::RegisterEvent("LoadModuleDone", &__KernelLoadModuleDone); } +std::vector> __KernelModuleCodeRanges() { + std::vector> ranges; + u32 error; + for (SceUID moduleId : loadedModules) { + const PSPModule *module = kernelObjects.Get(moduleId, error); + // textEnd is the last instruction. + if (module && !module->isFake && module->textStart != 0 && module->textEnd >= module->textStart) { + ranges.emplace_back(module->textStart, module->textEnd + 4 - module->textStart); + } + } + return ranges; +} + void __KernelModuleDoState(PointerWrap &p) { auto s = p.Section("sceKernelModule", 1, 5); if (!s) diff --git a/Core/HLE/sceKernelModule.h b/Core/HLE/sceKernelModule.h index fe1933d0fb..fd1224b4c9 100644 --- a/Core/HLE/sceKernelModule.h +++ b/Core/HLE/sceKernelModule.h @@ -229,6 +229,8 @@ public: KernelObject *__KernelModuleObject(); void __KernelModuleDoState(PointerWrap &p); +// The code of the loaded modules, as (address, size). +std::vector> __KernelModuleCodeRanges(); void __KernelModuleShutdown(); class MIPSState; diff --git a/Core/MIPS/IR/IRFrontend.cpp b/Core/MIPS/IR/IRFrontend.cpp index 9373319494..29cf74e8cf 100644 --- a/Core/MIPS/IR/IRFrontend.cpp +++ b/Core/MIPS/IR/IRFrontend.cpp @@ -44,11 +44,14 @@ IRFrontend::IRFrontend(bool startDefaultPrefix) { g_breakpoints.ClearSkipFirst(); } -void IRFrontend::DoState(PointerWrap &p) { +bool IRFrontend::DoState(PointerWrap &p) { auto s = p.Section("Jit", 1, 2); if (!s) - return; + return false; + const bool oldStartDefaultPrefix = js.startDefaultPrefix; + const bool oldHasSetRounding = js.hasSetRounding; + const u8 oldLastSetRounding = js.lastSetRounding; Do(p, js.startDefaultPrefix); if (s >= 2) { Do(p, js.hasSetRounding); @@ -62,6 +65,23 @@ void IRFrontend::DoState(PointerWrap &p) { // The debugger sets this so that "go" on a breakpoint will actually... go. // But if they reset, we can end up hitting it by mistake, since it's based on PC and ticks. g_breakpoints.ClearSkipFirst(); + + if (p.mode != PointerWrap::MODE_READ) { + return false; + } + // Blocks only switch rounding modes if hasSetRounding was set when they were compiled, and + // assume default prefixes at their start if startDefaultPrefix was. + if ((js.hasSetRounding && !oldHasSetRounding) || (!js.startDefaultPrefix && oldStartDefaultPrefix)) { + return true; + } + // The other way round, the blocks already compiled just do more than they need to. Keep the + // flags that way, or a loaded hasSetRounding of false would have UpdateFCR31() throw them all + // away for a rounding mode they handle fine. lastSetRounding says they were compiled with the + // rounding checks, so the next compile doesn't throw them away to add them either. + js.hasSetRounding = js.hasSetRounding || oldHasSetRounding; + js.startDefaultPrefix = js.startDefaultPrefix && oldStartDefaultPrefix; + js.lastSetRounding = oldLastSetRounding; + return false; } bool IRFrontend::UpdateFCR31(u32 fcr31) { diff --git a/Core/MIPS/IR/IRFrontend.h b/Core/MIPS/IR/IRFrontend.h index 95d5c6fcce..31e304329b 100644 --- a/Core/MIPS/IR/IRFrontend.h +++ b/Core/MIPS/IR/IRFrontend.h @@ -85,7 +85,8 @@ public: void Comp_Vbfy(MIPSOpcode op) override; int Replace_fabsf() override; - void DoState(PointerWrap &p); + // Returns true if a loaded state means blocks compiled before it can't run any more. + bool DoState(PointerWrap &p); bool UpdateFCR31(u32 fcr31); // returns true if the block cache needs clearing bool CheckRounding(u32 blockAddress); // returns true if we need a do-over diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp index aba70c8e79..7944f9e357 100644 --- a/Core/MIPS/IR/IRJit.cpp +++ b/Core/MIPS/IR/IRJit.cpp @@ -99,7 +99,9 @@ IRJit::~IRJit() { } void IRJit::DoState(PointerWrap &p) { - frontend_.DoState(p); + if (frontend_.DoState(p)) { + ClearCache(); + } } void IRJit::UpdateFCR31() { @@ -173,18 +175,55 @@ void IRJit::InvalidateCacheAt(u32 em_address, int length) { invalidatedBlocks_ += (int)numbers.size(); for (int block_num : numbers) { - auto block = blocks_.GetBlock(block_num); // TODO: We are invalidating a lot of blocks that are already invalid (yu gi oh). - // INFO_LOG(Log::JIT, "Block at %08x invalidated: valid: %d", block->GetOriginalStart(), block->IsValid()); - // If we're a native JIT (IR->JIT, not just IR interpreter), we write native offsets into the blocks. - int cookie = compileToNative_ ? block->GetNativeOffset() : block->GetIRArenaOffset(); - // Before Destroy, which forgets the block's address. Blocks linked to it jump straight in. - InvalidateNativeBlock(&blocks_, block_num); - blocks_.RemoveBlockFromPageLookup(block_num); - block->Destroy(cookie); + InvalidateBlock(block_num); } } +void IRJit::InvalidateBlock(int block_num) { + IRBlock *block = blocks_.GetBlock(block_num); + // If we're a native JIT (IR->JIT, not just IR interpreter), we write native offsets into the blocks. + int cookie = compileToNative_ ? block->GetNativeOffset() : block->GetIRArenaOffset(); + // Before Destroy, which forgets the block's address. Blocks linked to it jump straight in. + InvalidateNativeBlock(&blocks_, block_num); + blocks_.RemoveBlockFromPageLookup(block_num); + block->Destroy(cookie); +} + +void IRJit::InvalidateCacheExcept(const std::vector> &keep) { + if (keep.empty()) { + ClearCache(); + return; + } + + int kept = 0; + int invalidated = 0; + for (int block_num = 0; block_num < blocks_.GetNumBlocks(); ++block_num) { + const IRBlock *block = blocks_.GetBlock(block_num); + if (!block->IsValid()) { + continue; + } + u32 start, size; + block->GetRange(&start, &size); + start &= 0x3FFFFFFF; + bool inside = false; + for (const auto &[keepStart, keepSize] : keep) { + const u32 maskedStart = keepStart & 0x3FFFFFFF; + if (start >= maskedStart && start + size <= maskedStart + keepSize) { + inside = true; + break; + } + } + if (inside) { + kept++; + } else { + InvalidateBlock(block_num); + invalidated++; + } + } + INFO_LOG(Log::JIT, "IRJit: Kept %d blocks, invalidated %d", kept, invalidated); +} + void IRJit::Compile(u32 em_address) { _dbg_assert_(compilerEnabled_); diff --git a/Core/MIPS/IR/IRJit.h b/Core/MIPS/IR/IRJit.h index 72252b9aab..fd7ff2a0b2 100644 --- a/Core/MIPS/IR/IRJit.h +++ b/Core/MIPS/IR/IRJit.h @@ -209,6 +209,7 @@ public: void ClearCache() override; void InvalidateCacheAt(u32 em_address, int length = 4) override; + void InvalidateCacheExcept(const std::vector> &keep) override; void UpdateFCR31() override; void GetStats(JitStats *stats) const override; @@ -238,6 +239,7 @@ protected: virtual bool CompileNativeBlock(IRBlockCache *irBlockCache, int block_num) { return true; } virtual void FinalizeNativeBlock(IRBlockCache *irBlockCache, int block_num) {} virtual void InvalidateNativeBlock(IRBlockCache *irBlockCache, int block_num) {} + void InvalidateBlock(int block_num); bool compileToNative_; diff --git a/Core/MIPS/JitCommon/JitCommon.h b/Core/MIPS/JitCommon/JitCommon.h index 6077991a3c..6ac328df69 100644 --- a/Core/MIPS/JitCommon/JitCommon.h +++ b/Core/MIPS/JitCommon/JitCommon.h @@ -148,6 +148,8 @@ namespace MIPSComp { virtual const u8 *GetCrashHandler() const = 0; virtual JitBlockCacheDebugInterface *GetBlockCacheDebugInterface() = 0; virtual void InvalidateCacheAt(u32 em_address, int length = 4) = 0; + // Invalidates every block not entirely inside one of the (address, size) ranges. + virtual void InvalidateCacheExcept(const std::vector> &keep) = 0; virtual void DoState(PointerWrap &p) = 0; virtual void RunLoopUntil(u64 globalticks) = 0; virtual void Compile(u32 em_address) = 0; diff --git a/Core/MIPS/MIPS.cpp b/Core/MIPS/MIPS.cpp index 29fbaabea9..609a0ab036 100644 --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -326,6 +326,18 @@ void MIPSState::Reset() { } void MIPSState::Init() { + InitRegisters(); + if (PSP_CoreParameter().cpuCore == CPUCore::JIT || PSP_CoreParameter().cpuCore == CPUCore::JIT_IR) { + MIPSComp::jit = MIPSComp::CreateNativeJit(this); + } else if (PSP_CoreParameter().cpuCore == CPUCore::IR_INTERPRETER) { + MIPSComp::jit = new MIPSComp::IRJit(this, false); + } else { + MIPSComp::jit = nullptr; + } + UpdateBlockShadowHook(); +} + +void MIPSState::InitRegisters() { memset(r, 0, sizeof(r)); memset(f, 0, sizeof(f)); memset(v, 0, sizeof(v)); @@ -359,15 +371,6 @@ void MIPSState::Init() { downcount = 0; memset(vcmpResult, 0, sizeof(vcmpResult)); - - if (PSP_CoreParameter().cpuCore == CPUCore::JIT || PSP_CoreParameter().cpuCore == CPUCore::JIT_IR) { - MIPSComp::jit = MIPSComp::CreateNativeJit(this); - } else if (PSP_CoreParameter().cpuCore == CPUCore::IR_INTERPRETER) { - MIPSComp::jit = new MIPSComp::IRJit(this, false); - } else { - MIPSComp::jit = nullptr; - } - UpdateBlockShadowHook(); } const float vfpuPrefixConstants[8] = { 0.f, 1.f, 2.f, 0.5f, 3.f, 1.f / 3.f, 0.25f, 1.f / 6.f }; @@ -427,9 +430,17 @@ void MIPSState::DoState(PointerWrap &p) { if (!s) return; - // Reset the jit if we're loading. - if (p.mode == p.MODE_READ) - Reset(); + if (p.mode == p.MODE_READ) { + if (MIPSComp::jit && !keepJitBlocks_.empty()) { + // Blocks in code the load didn't change can stay. Queued invalidations stay too. + InitRegisters(); + MIPSComp::jit->InvalidateCacheExcept(keepJitBlocks_); + } else { + // Start the JIT over. + Reset(); + } + keepJitBlocks_.clear(); + } // Assume we're not saving state during a CPU core reset, so no lock. if (MIPSComp::jit) MIPSComp::jit->DoState(p); diff --git a/Core/MIPS/MIPS.h b/Core/MIPS/MIPS.h index 693cd6da79..53f131b637 100644 --- a/Core/MIPS/MIPS.h +++ b/Core/MIPS/MIPS.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "Common/CommonTypes.h" @@ -180,11 +181,18 @@ public: ~MIPSState(); void Init(); + // Init without the JIT. + void InitRegisters(); void Shutdown(); void Reset(); void UpdateCore(CPUCore desired); void DoState(PointerWrap &p); + // For the next DoState load: keep the JIT blocks entirely inside these (address, size) ranges, + // code the load leaves as it is, instead of starting the JIT over. + void KeepJitBlocksOnNextLoad(std::vector> ranges) { + keepJitBlocks_ = std::move(ranges); + } // MUST start with r and be followed by f, v, and t! u32 r[32]; @@ -295,6 +303,7 @@ public: // Doesn't need save stating. std::vector pendingInvalidates_; bool invalidateAll_ = false; + std::vector> keepJitBlocks_; }; // Access control ('private') break standard layout. Let's not allow it, to avoid excessive diff --git a/Core/MemMap.cpp b/Core/MemMap.cpp index df059e8d4a..09c4681c92 100644 --- a/Core/MemMap.cpp +++ b/Core/MemMap.cpp @@ -449,7 +449,60 @@ bool Reinit() { return success; } -static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size) { +// Loads size bytes at start, comparing the parts of it in compare->ranges with what's there as it +// goes. A chunk that already matches doesn't need copying, and the matching parts are reported in +// compare->unchanged. The rest is a plain copy. +static void CopyComparing(uint8_t *dest, const uint8_t *src, u32 start, u32 size, RamCompare *compare) { + // In the size of the JIT's block lookup pages. One changed byte costs its chunk's blocks. + constexpr u32 CHUNK_SIZE = 1024; + + // As offsets into the copy, in order, merged. + std::vector> ranges; + for (const auto &[address, length] : compare->ranges) { + const u32 rangeStart = std::max(address & 0x3FFFFFFF, start); + const u32 rangeEnd = std::min((address & 0x3FFFFFFF) + length, start + size); + if (rangeStart < rangeEnd) { + ranges.emplace_back(rangeStart - start, rangeEnd - start); + } + } + std::sort(ranges.begin(), ranges.end()); + + u32 pos = 0; + for (const auto &[rangeStart, rangeEnd] : ranges) { + const u32 from = std::max(rangeStart, pos); + if (from >= rangeEnd) { + continue; + } + if (from > pos) { + ParallelMemcpy(&g_threadManager, dest + pos, src + pos, from - pos); + } + u32 runStart = 0; + bool inRun = false; + for (u32 chunk = from; chunk < rangeEnd; chunk += CHUNK_SIZE) { + const u32 chunkSize = std::min(CHUNK_SIZE, rangeEnd - chunk); + const bool same = memcmp(dest + chunk, src + chunk, chunkSize) == 0; + if (same && !inRun) { + runStart = chunk; + inRun = true; + } else if (!same) { + memcpy(dest + chunk, src + chunk, chunkSize); + if (inRun) { + compare->unchanged.emplace_back(start + runStart, chunk - runStart); + inRun = false; + } + } + } + if (inRun) { + compare->unchanged.emplace_back(start + runStart, rangeEnd - runStart); + } + pos = rangeEnd; + } + if (pos < size) { + ParallelMemcpy(&g_threadManager, dest + pos, src + pos, size - pos); + } +} + +static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size, RamCompare *compare = nullptr) { uint8_t *d = GetPointerWriteOrException(start); uint8_t *&storage = *p.ptr; @@ -463,7 +516,11 @@ static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size) { switch (p.mode) { case PointerWrap::MODE_READ: - ParallelMemcpy(&g_threadManager, d, storage, size); + if (compare && !compare->ranges.empty()) { + CopyComparing(d, storage, start, size, compare); + } else { + ParallelMemcpy(&g_threadManager, d, storage, size); + } break; case PointerWrap::MODE_WRITE: ParallelMemcpy(&g_threadManager, storage, d, size); @@ -483,7 +540,7 @@ static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size) { storage += size; } -void DoState(PointerWrap &p) { +void DoState(PointerWrap &p, RamCompare *compare) { auto s = p.Section("Memory", 1, 3); if (!s) return; @@ -529,7 +586,7 @@ void DoState(PointerWrap &p) { } } - DoMemoryVoid(p, PSP_GetKernelMemoryBase(), g_MemorySize); + DoMemoryVoid(p, PSP_GetKernelMemoryBase(), g_MemorySize, compare); p.DoMarker("RAM"); DoMemoryVoid(p, PSP_GetVidMemBase(), VRAM_SIZE); diff --git a/Core/MemMap.h b/Core/MemMap.h index 0ac3684f13..3040db0a47 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -21,6 +21,8 @@ #include #include +#include +#include #ifndef offsetof #include #endif @@ -169,10 +171,19 @@ enum class MemMapSetupFlags { }; ENUM_CLASS_BITOPS(MemMapSetupFlags); +// For a savestate load: parts of RAM to compare with what's loaded over them, as they're loaded. +// Addresses can be in any mirror. The JIT keeps its blocks in code the load doesn't change. +struct RamCompare { + // In: (address, size) ranges. + std::vector> ranges; + // Out: the parts of them the load left as they were. + std::vector> unchanged; +}; + // Init and Shutdown bool Init(MemMapSetupFlags flags); void Shutdown(); -void DoState(PointerWrap &p); +void DoState(PointerWrap &p, RamCompare *compare = nullptr); // False when shutdown has already been called. bool IsActive(); diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 9f042f2cea..1c9bf75b76 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -53,6 +53,7 @@ #include "Core/HLE/ReplaceTables.h" #include "Core/HLE/sceDisplay.h" #include "Core/HLE/sceKernel.h" +#include "Core/HLE/sceKernelModule.h" #include "Core/HLE/sceUtility.h" #include "Core/HLE/sceSas.h" #include "Core/HLE/sceIo.h" @@ -60,6 +61,7 @@ #include "GPU/GPU.h" #include "GPU/GPUCommon.h" #include "Core/MIPS/JitCommon/JitBlockCache.h" +#include "Core/MIPS/JitCommon/JitCommon.h" #include "Core/RetroAchievements.h" #include "HW/MemoryStick.h" @@ -233,13 +235,20 @@ int g_screenshotFailures; // The state may hold different code. KernelModuleDoState hooks the functions again. std::vector> hookedOps; + // The loaded modules' code is compared with the state's as it's loaded. Where it's the same, + // the JIT keeps its blocks, so they don't all have to be compiled again. + Memory::RamCompare codeCompare; if (p.mode == p.MODE_READ) { hookedOps = Replacement_GetHookedOps(); RestoreReplacedInstructions(0, 0xFFFFFFFF); + if (MIPSComp::jit) { + codeCompare.ranges = __KernelModuleCodeRanges(); + } } - Memory::DoState(p); + Memory::DoState(p, &codeCompare); if (p.mode == p.MODE_READ) { Replacement_RepairStaleHookOps(hookedOps); + currentMIPS->KeepJitBlocksOnNextLoad(std::move(codeCompare.unchanged)); } if (s >= 3) { From eec6afaecaf4269250a7ec61decbe795e4307f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:17:47 +0200 Subject: [PATCH 02/16] SaveState: Reject implausible event counts, ids and CPU speed from a state They came straight from the file: a CPU frequency of 0 divided by zero, and counts or ids sized tables of up to tens of gigabytes, or tripped an assert. A file like that now fails to load like any other broken state. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/CoreTiming.cpp | 17 +++++++++++++++++ Core/HLE/HLE.cpp | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Core/CoreTiming.cpp b/Core/CoreTiming.cpp index 8fb90106e1..73f0c87984 100644 --- a/Core/CoreTiming.cpp +++ b/Core/CoreTiming.cpp @@ -44,6 +44,8 @@ int CPU_HZ = 222000000; namespace CoreTiming { static std::vector event_types; +// Far more than ever registered. Counts and ids from a savestate are checked against it. +static constexpr int MAX_EVENT_TYPES = 4096; // Only used during restore. static std::set usedEventTypes; static std::set restoredEventTypes; @@ -189,6 +191,9 @@ void RestoreRegisterEvent(int &event_type, const char *name, TimedCallback callb // Some old states have a duplicate restore, do our best to fix... if (restoredEventTypes.count(event_type) != 0) event_type = -1; + // Straight from the file, so don't let a bogus id size the table. + if (event_type < -1 || event_type >= MAX_EVENT_TYPES) + event_type = -1; if (event_type == -1) event_type = nextEventTypeRestoreId++; if (event_type >= (int)event_types.size()) { @@ -542,6 +547,11 @@ void DoState(PointerWrap &p) { int n = (int)event_types.size(); int current = n; Do(p, n); + if (n < 0 || n > MAX_EVENT_TYPES) { + ERROR_LOG(Log::SaveState, "Savestate failure: implausible event type count %d", n); + p.SetError(p.ERROR_FAILURE); + return; + } if (n > current) { if (p.mode != PointerWrap::MODE_READ) { WARN_LOG(Log::SaveState, "Savestate failure: more events than current"); @@ -579,6 +589,13 @@ void DoState(PointerWrap &p) { } Do(p, CPU_HZ); + if (CPU_HZ <= 0) { + // Everything divides by it. + ERROR_LOG(Log::SaveState, "Savestate failure: bad CPU frequency %d", (int)CPU_HZ); + p.SetError(p.ERROR_FAILURE); + CPU_HZ = 222000000; + return; + } Do(p, slicelength); Do(p, globalTimer); Do(p, idledCycles); diff --git a/Core/HLE/HLE.cpp b/Core/HLE/HLE.cpp index a15ae8f701..f3e12f4d05 100644 --- a/Core/HLE/HLE.cpp +++ b/Core/HLE/HLE.cpp @@ -497,7 +497,8 @@ void HLEDoState(PointerWrap &p) { delete action; } mipsCallActions.clear(); - if (actions < 0) { + // Each action takes at least its type id. + if (actions < 0 || (size_t)actions > p.Remaining() / sizeof(int)) { p.SetError(p.ERROR_FAILURE); return; } From 198652d9b3115650656c838a708025e8d2a3bde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:18:46 +0200 Subject: [PATCH 03/16] SaveState: Don't keep JIT blocks across a memory remap or a failed load A load that changes the memory size remaps RAM, so there's nothing to compare with, and the JIT's dispatcher points at the old mapping: it has to start over. A load that fails partway may have replaced RAM without the JIT being told, so it keeps nothing either. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/MemMap.cpp | 7 +++++++ Core/SaveState.cpp | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/Core/MemMap.cpp b/Core/MemMap.cpp index 09c4681c92..d21f25e41b 100644 --- a/Core/MemMap.cpp +++ b/Core/MemMap.cpp @@ -545,6 +545,8 @@ void DoState(PointerWrap &p, RamCompare *compare) { if (!s) return; + const u32 memorySizeBefore = g_MemorySize; + if (s < 2) { if (!g_RemasterMode) g_MemorySize = RAM_NORMAL_SIZE; @@ -586,6 +588,11 @@ void DoState(PointerWrap &p, RamCompare *compare) { } } + // A new size means Reinit(): RAM is newly mapped, nothing in it to compare with, and the JIT has + // to start over (its dispatcher points at the old mapping.) + if (g_MemorySize != memorySizeBefore) { + compare = nullptr; + } DoMemoryVoid(p, PSP_GetKernelMemoryBase(), g_MemorySize, compare); p.DoMarker("RAM"); diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 1c9bf75b76..8db56ad1bd 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -262,6 +262,12 @@ int g_screenshotFailures; Achievements::DoState(p); // Kernel object destructors might close open files, so do the filesystem last. pspFileSystem.DoState(p); + + if (p.mode == p.MODE_READ && p.error == PointerWrap::ERROR_FAILURE) { + // RAM may be loaded and the JIT not told, or the other way round. Keep nothing. + currentMIPS->KeepJitBlocksOnNextLoad({}); + currentMIPS->ClearJitCacheDeferred(); + } } static void WorkerLoop() { From a62aa69986dccc353c91a2f16c410b8951108ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:18:46 +0200 Subject: [PATCH 04/16] libretro: Use the real SaveStart libretro declared its own, which no longer matched once SaveStart got a member, so serializing read an uninitialized byte. It's in SaveState.h now. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/SaveState.cpp | 6 ------ Core/SaveState.h | 7 +++++++ libretro/libretro.cpp | 8 -------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 8db56ad1bd..91bd706c32 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -108,12 +108,6 @@ static std::map> g_files; // the value is th // Set when a slot save finishes. That's on the emu thread, so the rescan is left to the next reader. static std::atomic g_filesStale; -struct SaveStart { - void DoState(PointerWrap &p); - // Rewind snapshots and verification don't count towards IsStale(). - bool countsAsSave = true; -}; - enum class OperationType { Save, Load, diff --git a/Core/SaveState.h b/Core/SaveState.h index 4984c6a84d..4373b7c573 100644 --- a/Core/SaveState.h +++ b/Core/SaveState.h @@ -36,6 +36,13 @@ namespace SaveState { }; typedef std::function Callback; + // The whole state, for CChunkFileReader. libretro uses it directly. + struct SaveStart { + void DoState(PointerWrap &p); + // Rewind snapshots and verification don't count towards IsStale(). + bool countsAsSave = true; + }; + static const char * const SCREENSHOT_EXTENSION = "jpg"; void Init(); diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index 1c88d78976..ea68e465c2 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -1808,14 +1808,6 @@ unsigned retro_get_region(void) { return RETRO_REGION_NTSC; } bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num) { return false; } -namespace SaveState -{ - struct SaveStart - { - void DoState(PointerWrap &p); - }; -} // namespace SaveState - size_t retro_serialize_size(void) { if (!gpu) // The HW renderer isn't ready on first pass. From f45f4d5c851bc583d3ce72a944251d7ccce1ba4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:18:46 +0200 Subject: [PATCH 05/16] SaveState: Only rehash functions in code a load changed Every load hashed all known functions again, for the replacement hooks: 2.4-6 ms, about half the load. Functions in module code the load left as it was keep their hash, so only the rest is hashed now (0.5-1 ms). The code is compared on every load for this, not only with a JIT. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/HLE/sceKernelModule.cpp | 11 ++++++++++- Core/HLE/sceKernelModule.h | 3 +++ Core/MIPS/MIPSAnalyst.cpp | 27 ++++++++++++++++++++++++--- Core/MIPS/MIPSAnalyst.h | 5 ++++- Core/SaveState.cpp | 5 ++--- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index 2943ed4443..a48c46ba6a 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -534,6 +534,13 @@ static void __KernelModuleInit() { eventModuleLoadDone = CoreTiming::RegisterEvent("LoadModuleDone", &__KernelLoadModuleDone); } +// Set for the next load by the savestate code: code the load left as it was. +static std::vector> g_unchangedCodeOnLoad; + +void __KernelModuleSetUnchangedCodeOnLoad(std::vector> unchanged) { + g_unchangedCodeOnLoad = std::move(unchanged); +} + std::vector> __KernelModuleCodeRanges() { std::vector> ranges; u32 error; @@ -615,7 +622,9 @@ void __KernelModuleDoState(PointerWrap &p) { } // The functions were found in memory from before the load, where other code may have been // (an overlay module, say.) Hash them from what's there now, so a hook only goes where it matches. - MIPSAnalyst::RehashFunctions(); + // Only what the load may have changed: hashing it all again takes milliseconds. + MIPSAnalyst::RehashFunctions(g_unchangedCodeOnLoad); + g_unchangedCodeOnLoad.clear(); if (g_Config.bFuncReplacements) { MIPSAnalyst::ReplaceFunctions(); } diff --git a/Core/HLE/sceKernelModule.h b/Core/HLE/sceKernelModule.h index fd1224b4c9..9df71ded49 100644 --- a/Core/HLE/sceKernelModule.h +++ b/Core/HLE/sceKernelModule.h @@ -231,6 +231,9 @@ KernelObject *__KernelModuleObject(); void __KernelModuleDoState(PointerWrap &p); // The code of the loaded modules, as (address, size). std::vector> __KernelModuleCodeRanges(); +// For the next savestate load: code it leaves as it was, as (address, size). Its functions aren't +// hashed again. +void __KernelModuleSetUnchangedCodeOnLoad(std::vector> unchanged); void __KernelModuleShutdown(); class MIPSState; diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index fe736e5dfd..362c061526 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -909,7 +909,22 @@ namespace MIPSAnalyst { return DetermineRegisterUsage(reg, addr, instrs) == USAGE_CLOBBERED; } - void HashFunctions() { + // Ranges are (address, size), sorted by address and not overlapping. + static bool InsideRanges(const std::vector> &ranges, u32 start, u32 end) { + start &= 0x3FFFFFFF; + end &= 0x3FFFFFFF; + auto iter = std::upper_bound(ranges.begin(), ranges.end(), start, [](u32 address, const std::pair &range) { + return address < (range.first & 0x3FFFFFFF); + }); + if (iter == ranges.begin()) { + return false; + } + --iter; + const u32 rangeStart = iter->first & 0x3FFFFFFF; + return start >= rangeStart && end <= rangeStart + iter->second; + } + + static void HashFunctions(const std::vector> &unchanged = {}) { std::lock_guard guard(functions_lock); std::vector buffer; @@ -918,6 +933,10 @@ namespace MIPSAnalyst { if (!Memory::IsValidRange(f.start, f.end - f.start + 4)) { continue; } + // end is the last instruction. + if (f.hasHash && InsideRanges(unchanged, f.start, f.end + 4)) { + continue; + } // Immediates are masked out, so relocations don't change the hash. buffer.resize((f.end - f.start + 4) / 4); @@ -1311,9 +1330,11 @@ namespace MIPSAnalyst { return false; } - void RehashFunctions() { + void RehashFunctions(const std::vector> &unchanged) { + std::vector> sorted = unchanged; + std::sort(sorted.begin(), sorted.end()); std::lock_guard guard(functions_lock); - HashFunctions(); + HashFunctions(sorted); UpdateHashToFunctionMap(); } diff --git a/Core/MIPS/MIPSAnalyst.h b/Core/MIPS/MIPSAnalyst.h index fda9908b56..bfd9b05071 100644 --- a/Core/MIPS/MIPSAnalyst.h +++ b/Core/MIPS/MIPSAnalyst.h @@ -18,6 +18,7 @@ #pragma once #include +#include #include #include "Common/CommonTypes.h" @@ -118,7 +119,9 @@ namespace MIPSAnalyst { const char *LookupHash(u64 hash, u32 funcSize); // Hashes the known functions again, from what memory holds now (after a savestate load.) - void RehashFunctions(); + // Functions that already have a hash and lie entirely in one of the (address, size) ranges in + // unchanged keep it: their code is what it was hashed from. + void RehashFunctions(const std::vector> &unchanged = {}); void ReplaceFunctions(); void UpdateHashMap(); diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 91bd706c32..7138d1d7e0 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -235,13 +235,12 @@ int g_screenshotFailures; if (p.mode == p.MODE_READ) { hookedOps = Replacement_GetHookedOps(); RestoreReplacedInstructions(0, 0xFFFFFFFF); - if (MIPSComp::jit) { - codeCompare.ranges = __KernelModuleCodeRanges(); - } + codeCompare.ranges = __KernelModuleCodeRanges(); } Memory::DoState(p, &codeCompare); if (p.mode == p.MODE_READ) { Replacement_RepairStaleHookOps(hookedOps); + __KernelModuleSetUnchangedCodeOnLoad(codeCompare.unchanged); currentMIPS->KeepJitBlocksOnNextLoad(std::move(codeCompare.unchanged)); } From 3cc61d3f8ee4a712d4c8d3bbde7f37934898e778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:18:46 +0200 Subject: [PATCH 06/16] SaveState: Report failed writes, and call back on network refusals - A failed final flush (a full disk) or a failed rename into place was reported as "Saved State". A failed load undo rename still pointed load undo at the current game, while the file was another game's state. - Refusing a save or load for being online didn't call the callback, unlike every other refusal. - A rewind dropped after its decode started stayed active, and stopped rewind snapshots until the next clear. Co-Authored-By: Claude Opus 5.5 (1M context) --- Common/Serialize/Serializer.cpp | 5 +++++ Core/SaveState.cpp | 16 ++++++++++++++-- Core/SaveState.h | 4 ++-- Core/SaveStateRewind.cpp | 10 ++++++++++ Core/SaveStateRewind.h | 2 ++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Common/Serialize/Serializer.cpp b/Common/Serialize/Serializer.cpp index d0ded74fd7..bcf53e5f92 100644 --- a/Common/Serialize/Serializer.cpp +++ b/Common/Serialize/Serializer.cpp @@ -601,6 +601,11 @@ CChunkFileReader::Error CChunkFileReader::SaveFile(const Path &filename, const s INFO_LOG(Log::SaveState, "Savestate: Compressed %i bytes into %i", (int)sz, (int)writeLen); } free(buffer); + // Buffered writes can still fail here (a full disk.) + if (!pFile.Close() && success) { + ERROR_LOG(Log::SaveState, "ChunkReader: Failed closing file"); + success = false; + } if (!success) { return ERROR_BAD_FILE; } diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 7138d1d7e0..e1e5db5272 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -414,6 +414,7 @@ int g_screenshotFailures; static bool QueueLoad(const Operation &op) { if (NetworkWarnUserIfOnlineAndCantSavestate()) { + CallbackRefused(op.callback); return false; } @@ -425,6 +426,7 @@ int g_screenshotFailures; static bool QueueSave(const Operation &op) { if (NetworkWarnUserIfOnlineAndCantSavestate()) { + CallbackRefused(op.callback); return false; } if (AttachToPendingSave(op.path, op.callback)) { @@ -646,6 +648,7 @@ int g_screenshotFailures; bool UndoLoad(std::string_view gamePrefix, Callback callback) { if (NetworkWarnUserIfOnlineAndCantSavestate()) { + CallbackRefused(callback); return false; } @@ -1159,7 +1162,10 @@ int g_screenshotFailures; } else { DeleteIfExists(job.path); } - File::Rename(job.writePath, job.path); + if (!File::Rename(job.writePath, job.path)) { + ERROR_LOG(Log::SaveState, "Failed to move '%s' into place", job.writePath.c_str()); + result = CChunkFileReader::ERROR_BAD_FILE; + } } PostCompletion([job, result](bool callCallbacks) { FinishSave(job, result, callCallbacks); @@ -1193,7 +1199,10 @@ int g_screenshotFailures; g_saveSnapshots--; if (result == CChunkFileReader::ERROR_NONE) { DeleteIfExists(backup); - File::Rename(temp, backup); + if (!File::Rename(temp, backup)) { + // The undo would load whatever was there before, maybe another game's state. + result = CChunkFileReader::ERROR_BAD_FILE; + } } // Not a callback, so it runs at shutdown too. PostCompletion([result, gamePrefix](bool) { @@ -1404,6 +1413,9 @@ int g_screenshotFailures; needsProcess = false; lock.unlock(); for (const Operation &op : operations) { + if (op.type == OperationType::Rewind && op.started) { + rewindStates.CancelRestore(); + } RefuseOperation(op); } return; diff --git a/Core/SaveState.h b/Core/SaveState.h index 4373b7c573..dfcdc30066 100644 --- a/Core/SaveState.h +++ b/Core/SaveState.h @@ -109,11 +109,11 @@ namespace SaveState { CChunkFileReader::Error LoadFromRam(std::vector &state, std::string *errorString); // For testing / automated tests. Runs a save state verification pass (async.) - // Warning: callback will be called on a different thread. + // The callback runs from Process(). bool Verify(Callback callback = Callback()); // To go back to a previous snapshot (only if enabled.) - // Warning: callback will be called on a different thread. + // The callback runs from Process(), once the snapshot has been applied. bool Rewind(Callback callback = Callback()); // Returns true if there are rewind snapshots available. diff --git a/Core/SaveStateRewind.cpp b/Core/SaveStateRewind.cpp index 47352e5f09..e54af38bf6 100644 --- a/Core/SaveStateRewind.cpp +++ b/Core/SaveStateRewind.cpp @@ -161,6 +161,16 @@ CChunkFileReader::Error StateRingbuffer::Restore(std::string *errorString, std:: return FinishRestore(errorString, metadata); } +void StateRingbuffer::CancelRestore() { + if (!restoreActive_) { + return; + } + // The decode may still be running into restoreBuffer_. + WaitForWorker(); + restoreActive_ = false; + std::vector().swap(restoreBuffer_); +} + // Only moves the ring back: the state's buffer may still be in use by a worker job, and is reused // by the next snapshot anyway. void StateRingbuffer::DropNewest() { diff --git a/Core/SaveStateRewind.h b/Core/SaveStateRewind.h index 190565af31..c7a7030d35 100644 --- a/Core/SaveStateRewind.h +++ b/Core/SaveStateRewind.h @@ -31,6 +31,8 @@ public: CChunkFileReader::Error FinishRestore(std::string *errorString, std::string *metadata); // Both halves, waiting for the worker in between. CChunkFileReader::Error Restore(std::string *errorString, std::string *metadata); + // For a rewind that was started and then dropped. + void CancelRestore(); void Clear(); bool Empty() const { From 26d9e540597904f7043022c00eda2a4fbaa4ffc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:18:47 +0200 Subject: [PATCH 07/16] SaveState: Don't fold a save into one from before a load A save to a path with a save still being written counted as a repeated key press, even with a load applied in between, so save, load, save didn't save the loaded state. Now a repeat only joins the earlier save if no load has been applied or queued since its snapshot. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/SaveState.cpp | 89 ++++++++++++++++++++++++++++------------ docs/async-savestates.md | 17 ++++---- 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index e1e5db5272..130e1e9912 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -127,6 +127,15 @@ struct PendingRead { std::string savedBy; }; +// A save from when it's asked for until its file is written. +struct SaveRecord { + Path path; + // How many loads had been applied when its snapshot was taken, or -1 before that. + int loadCount = -1; + // Callbacks of repeats of it (a save key pressed twice). They hear its result, with no message. + std::vector repeats; +}; + struct Operation { // The slot number is for visual purposes only. Set to -1 for operations where we don't display a message for example. Operation(OperationType t, const Path &f, int slot_, Callback cb) @@ -143,15 +152,18 @@ struct Operation { // worker part is done. bool started = false; std::shared_ptr read; + // For a save. Its fields are under mutex. + std::shared_ptr save; }; // Front to back, each operation's emu thread part runs in turn. A load or rewind that's still // waiting for the worker holds up the operations behind it. static std::deque g_pendingOperations; -// Saves asked for and not yet written, by path, with the callbacks of any repeats of them (a save -// key pressed again before the first save finished). Under mutex. -static std::map> g_pendingSaves; +// Saves asked for and not yet written, oldest first. Under mutex. +static std::vector> g_pendingSaves; +// Loads (and rewinds) applied, so a save can tell whether one came after its snapshot. +static std::atomic g_loadCount; // Save snapshots in memory, waiting to be compressed and written. Each is a whole state. static std::atomic g_saveSnapshots; constexpr int MAX_SAVE_SNAPSHOTS = 2; @@ -233,6 +245,7 @@ int g_screenshotFailures; // the JIT keeps its blocks, so they don't all have to be compiled again. Memory::RamCompare codeCompare; if (p.mode == p.MODE_READ) { + g_loadCount++; hookedOps = Replacement_GetHookedOps(); RestoreReplacedInstructions(0, 0xFFFFFFFF); codeCompare.ranges = __KernelModuleCodeRanges(); @@ -348,39 +361,52 @@ int g_screenshotFailures; static bool IsSavePending(const Path &path) { std::lock_guard guard(mutex); - return g_pendingSaves.find(path.ToString()) != g_pendingSaves.end(); + for (const auto &save : g_pendingSaves) { + if (save->path == path) { + return true; + } + } + return false; } - // If a save to path is already pending, this is a repeat of it (a key pressed twice): the callback - // hears that save's result, with no message so it's only shown once. Returns true if so. + // If a save to path hasn't finished yet, this is a repeat of it (a key pressed twice): the + // callback hears that save's result, with no message so it's only shown once. Returns true if so. + // Unless a load was applied after that save's snapshot: then this saves something else. static bool AttachToPendingSave(const Path &path, const Callback &callback) { std::lock_guard guard(mutex); - auto iter = g_pendingSaves.find(path.ToString()); - if (iter == g_pendingSaves.end()) { - return false; + // One queued and not yet applied comes before this save too. + for (const Operation &op : g_pendingOperations) { + if (op.type == OperationType::Load || op.type == OperationType::Rewind) { + return false; + } } - if (callback) { - iter->second.push_back(callback); + for (auto iter = g_pendingSaves.rbegin(); iter != g_pendingSaves.rend(); ++iter) { + SaveRecord &save = **iter; + if (!(save.path == path)) { + continue; + } + if (save.loadCount != -1 && save.loadCount != g_loadCount) { + return false; + } + if (callback) { + save.repeats.push_back(callback); + } + return true; } - return true; + return false; } - // Takes a save out of g_pendingSaves, returning the callbacks of its repeats. - static std::vector TakePendingSave(const Path &path) { + // A save is done (written, failed or refused). Returns the callbacks of its repeats. + static std::vector FinishPendingSave(const std::shared_ptr &save) { std::lock_guard guard(mutex); - std::vector repeats; - auto iter = g_pendingSaves.find(path.ToString()); - if (iter != g_pendingSaves.end()) { - repeats = std::move(iter->second); - g_pendingSaves.erase(iter); - } - return repeats; + g_pendingSaves.erase(std::remove(g_pendingSaves.begin(), g_pendingSaves.end(), save), g_pendingSaves.end()); + return std::move(save->repeats); } static void RefuseOperation(const Operation &op) { CallbackRefused(op.callback); - if (op.type == OperationType::Save) { - for (const Callback &repeat : TakePendingSave(op.path)) { + if (op.type == OperationType::Save && op.save) { + for (const Callback &repeat : FinishPendingSave(op.save)) { CallbackRefused(repeat); } } @@ -402,10 +428,13 @@ int g_screenshotFailures; } std::lock_guard guard(mutex); - if (op.type == OperationType::Save) { - g_pendingSaves.emplace(op.path.ToString(), std::vector()); - } g_pendingOperations.push_back(op); + if (op.type == OperationType::Save) { + Operation &queued = g_pendingOperations.back(); + queued.save = std::make_shared(); + queued.save->path = op.path; + g_pendingSaves.push_back(queued.save); + } // Don't actually run it until next frame. needsProcess = true; @@ -1076,6 +1105,7 @@ int g_screenshotFailures; bool keepUndo = false; Path undoPath; Callback callback; + std::shared_ptr save; }; // Emu thread, once the file is written (or failed to be). @@ -1103,7 +1133,7 @@ int g_screenshotFailures; status = Status::FAILURE; } - std::vector repeats = TakePendingSave(job.path); + std::vector repeats = FinishPendingSave(job.save); if (!callCallbacks) { return; } @@ -1128,6 +1158,7 @@ int g_screenshotFailures; job.slotGamePrefix = op.slotGamePrefix; job.slot = op.slot; job.callback = op.callback; + job.save = op.save; if (!op.slotGamePrefix.empty()) { job.keepUndo = g_Config.bEnableStateUndo; job.undoPath = GenerateSaveSlotPath(op.slotGamePrefix, op.slot, UNDO_STATE_EXTENSION); @@ -1426,6 +1457,10 @@ int g_screenshotFailures; // Enqueue() only appends, which leaves this reference alone. Unlocked, since callbacks can // queue more. Operation &op = g_pendingOperations.front(); + if (op.save) { + // Its snapshot is next: nothing can be loaded in between, it's first in the queue. + op.save->loadCount = g_loadCount; + } lock.unlock(); const bool finished = RunOperation(op); lock.lock(); diff --git a/docs/async-savestates.md b/docs/async-savestates.md index a4f616ba2f..70b742a14a 100644 --- a/docs/async-savestates.md +++ b/docs/async-savestates.md @@ -50,9 +50,11 @@ doesn't (worker). The thumbnail screenshot is already asynchronous and doesn't change. -**A save to a path that already has a save queued or in flight is a duplicate keypress.** It isn't -queued again. Its callback is attached to the save in flight and called with that save's status and -an empty message (so it shows nothing, and the in-flight save's own callback shows the result once). +**A save to a path that already has a save queued, and not yet snapshotted, is a duplicate +keypress.** It isn't queued again. Its callback is attached to the queued save and called with that +save's status and an empty message (so it shows nothing, and the queued save's own callback shows +the result once). Once the snapshot is taken, another save to the path is a new save: the game may +have moved on, or a load been applied, since. A save to a different path is queued, and takes its snapshot right away like any other. **At most two save snapshots exist at once**: a third save, while two are still being written, waits for the @@ -84,8 +86,7 @@ Operations still take effect in the order they were asked for: and snapshots the loaded state, as it does today when both run in the same `Process()`. A load waiting for its file holds up only the emu phases queued behind it. - **Worker jobs run in queue order**, so a save to a slot followed by a load of it writes before it - reads. As a shortcut, a load whose path has a write pending takes that save's uncompressed - snapshot directly and skips the file round trip. + reads. - **UI-thread file operations on slots wait for the worker** (`WaitForWorker()`, for all jobs, since it's rare and short): `UndoSaveSlot` and `DeleteSlot`. `UndoLoad` is a load, so it's queued behind the load undo write like anything else. `LoadSlot`'s existence check counts a slot with a save @@ -100,9 +101,11 @@ need the emu thread, so it can't deadlock. they are. So does the freeze-frame feature (`SaveToRam`/`LoadFromRam` every frame). - **Verify** stays synchronous. - **Shutdown and game switch** (`SaveState::Shutdown()`, from the kernel's shutdown, which app exit - goes through too): wait for the worker, so a save that was asked for is never lost. Then run the + goes through too): wait for the worker, so a save whose snapshot was taken is written. Then run the completions' bookkeeping (the undo slot in the config) without their callbacks, which can refer to - an EmuScreen that's going away, and drop whatever is still queued, as before. + an EmuScreen that's going away, and drop whatever is still queued, as before. A save still queued + at that point (asked for in the pause menu, which doesn't run `Process()`, then quitting) is lost + like it always was: by then most of the kernel has shut down, too late to take a snapshot. - Loads at boot (auto-load, `--state`, opening a `.ppst`) and the debugger's `game.state.*` with `wait` already work through callbacks, and need no special handling. The game already ran at least a frame before a boot-time load applied. From 6e16f46445b77e4d2257b4ba3f50b1101b22e29a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:30:02 +0200 Subject: [PATCH 08/16] GPU: Keep textures across a savestate load, rehashing them first A load cleared the texture cache, so every texture the game drew next was decoded and uploaded again. Now each one is rehashed against its memory the next time it's used, and only decoded again if it changed. In the three seconds after a load, Ridge Racer decodes no textures instead of 109, Force Unleashed none instead of 43. Rendered frames are the same. Framebuffers are still all destroyed on load, since loading a state is a known way out of some framebuffer problems. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/Common/TextureCacheCommon.cpp | 15 ++++++++++++++- GPU/Common/TextureCacheCommon.h | 4 ++++ GPU/GPUCommonHW.cpp | 3 +-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index 806b380a31..fa59ee3e44 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -2773,6 +2773,10 @@ void TextureCacheCommon::Clear(bool delete_them) { secondCache_.clear(); } + ReleaseDynamicClut(); +} + +void TextureCacheCommon::ReleaseDynamicClut() { if (dynamicClutFbo_) { dynamicClutFbo_->Release(); dynamicClutFbo_ = nullptr; @@ -2781,10 +2785,19 @@ void TextureCacheCommon::Clear(bool delete_them) { dynamicClutTemp_->Release(); dynamicClutTemp_ = nullptr; } - // The dynamic CLUT lived in the framebuffers released above. Fall back to the RAM copy until the next LoadClut. + // The dynamic CLUT lived in framebuffers that are going away. Fall back to the RAM copy until the next LoadClut. clutRenderAddress_ = 0xFFFFFFFF; } +void TextureCacheCommon::RecheckAllAfterLoad() { + // Decoding them all again stutters, while hashing finds the ones that are still the same. The + // secondary cache is keyed by hash, so it's fine as it is. + for (auto &iter : cache_) { + iter.second->status |= TexStatus::HASH_RECHECK; + } + ReleaseDynamicClut(); +} + // One type of texture update can happen without the involvement of the CPU: Block transfers. // This is done for example for the text rendering in Gran Turismo, where it only uses a single letter // worth of texture memory, and for each letter drawn, it just replaces it and then draws. diff --git a/GPU/Common/TextureCacheCommon.h b/GPU/Common/TextureCacheCommon.h index 2a823eb397..d626d1744b 100644 --- a/GPU/Common/TextureCacheCommon.h +++ b/GPU/Common/TextureCacheCommon.h @@ -367,6 +367,9 @@ public: virtual void ForgetLastTexture() = 0; virtual void Clear(bool delete_them); + // After a savestate load, which may have changed any texture's memory (and destroyed the + // framebuffers). Textures are kept, but hashed again before they're used. + void RecheckAllAfterLoad(); virtual void NotifyConfigChanged(); // FramebufferManager keeps TextureCache updated about what regions of memory are being rendered to, @@ -502,6 +505,7 @@ protected: CLUTProperties clutProperties_; // Facilities for GPU depal of static textures. + void ReleaseDynamicClut(); Draw::Framebuffer *dynamicClutTemp_ = nullptr; Draw::Framebuffer *dynamicClutFbo_ = nullptr; diff --git a/GPU/GPUCommonHW.cpp b/GPU/GPUCommonHW.cpp index f3cb8b83bd..9132ddcf33 100644 --- a/GPU/GPUCommonHW.cpp +++ b/GPU/GPUCommonHW.cpp @@ -524,10 +524,9 @@ bool GPUCommonHW::PresentedThisFrame() const { void GPUCommonHW::DoState(PointerWrap &p) { GPUCommon::DoState(p); - // TODO: Some of these things may not be necessary. // None of these are necessary when saving. if (p.mode == p.MODE_READ && !PSP_CoreParameter().frozen) { - textureCache_->Clear(true); + textureCache_->RecheckAllAfterLoad(); gstate_c.Dirty(DIRTY_TEXTURE_IMAGE); framebufferManager_->DestroyAllFBOs(); From 7e4127ae69670c148ba820a79e467a10a8d4713f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:42:14 +0200 Subject: [PATCH 09/16] Pause menu: Don't close on a state load until the state is in place Loading a state from the pause menu closed it right away, and the game ran a few frames from where it was (seen and heard) before the state was applied. Now savestate operations run while the game is paused under the menu, and the menu closes once the load (or rewind, or load undo) is done. Rewind snapshots aren't taken while paused. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/SaveState.cpp | 6 ++++-- Core/SaveState.h | 4 +++- UI/EmuScreen.cpp | 6 ++++++ UI/PauseScreen.cpp | 47 +++++++++++++++++++++++++++++++++++++--------- UI/PauseScreen.h | 6 ++++++ 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 130e1e9912..e73ab34cd3 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -1424,8 +1424,10 @@ int g_screenshotFailures; } // NOTE: This can cause ending of the current renderpass, due to the readback needed for the screenshot. - void Process() { - rewindStates.Process(); + void Process(bool takeRewindSnapshots) { + if (takeRewindSnapshots) { + rewindStates.Process(); + } RunCompletions(true); if (!needsProcess) diff --git a/Core/SaveState.h b/Core/SaveState.h index dfcdc30066..33840e41ef 100644 --- a/Core/SaveState.h +++ b/Core/SaveState.h @@ -133,7 +133,9 @@ namespace SaveState { bool IsOldVersion(); // Check if there's any save stating needing to be done. Normally called once per frame. - void Process(); + // While the game is paused (under the pause menu, say), pass false: there's nothing new to take + // rewind snapshots of. + void Process(bool takeRewindSnapshots = true); // Notify save state code that new save data has been written. void NotifySaveData(); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 93f6394d32..42b984b292 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -1622,6 +1622,12 @@ ScreenRenderFlags EmuScreen::PreRender(ScreenRenderMode mode) { // If a boot is in progress, update it. ProcessGameBoot(gamePath_); + if (PSP_IsInited() && !ShouldRunEmulation(mode) && !Achievements::IsBlockingExecution()) { + // Paused under a screen on top, like the pause menu. A state loaded from there should be in + // place before the game resumes, not a few frames into it, so savestate operations still run. + SaveState::Process(false); + } + using namespace Draw; skipBufferEffects_ = g_Config.bSkipBufferEffects; if (!skipBufferEffects_ && PSP_IsInited()) { diff --git a/UI/PauseScreen.cpp b/UI/PauseScreen.cpp index 6b717c1679..f5d64c3248 100644 --- a/UI/PauseScreen.cpp +++ b/UI/PauseScreen.cpp @@ -96,6 +96,10 @@ public: } const char *tag() const override { return "ScreenshotView"; } + // Closed with DR_OK to load the slot (the pause screen does it), rather than after saving it. + bool LoadRequested() const { + return loadRequested_; + } protected: bool FillVertical() const override { return false; } @@ -154,6 +158,7 @@ private: std::string title_; std::string customNameTemp_; int slot_; + bool loadRequested_ = false; }; void ScreenshotViewScreen::OnSaveState(UI::EventParams &e) { @@ -170,12 +175,12 @@ void ScreenshotViewScreen::OnLoadState(UI::EventParams &e) { if (g_Config.bConfirmLoadState) { screenManager()->push(new LoadStateConfirmScreen(saveStatePrefix_, slot_, [this](bool result) { if (result) { - SaveState::LoadSlot(saveStatePrefix_, slot_, &ShowMessageAfterSaveStateAction); + loadRequested_ = true; TriggerFinish(DR_OK); } })); } else { - SaveState::LoadSlot(saveStatePrefix_, slot_, &ShowMessageAfterSaveStateAction); + loadRequested_ = true; TriggerFinish(DR_OK); } } @@ -364,6 +369,11 @@ void GamePauseScreen::update() { UIBaseDialogScreen::update(); + if (stateLoadDone_ && *stateLoadDone_) { + stateLoadDone_.reset(); + TriggerFinish(DR_CANCEL); + } + { std::lock_guard lock(finishNextFrameMutex_); if (!firstFrame_ && g_controlMapper.PollPauseTrigger()) { @@ -440,8 +450,9 @@ void GamePauseScreen::CreateSavestateControls(UI::LinearLayout *leftColumnItems, SaveSlotView *v = static_cast(e.v); int slotNum = v->GetSlot(); auto doLoad = [this, slotNum]() { - SaveState::LoadSlot(saveStatePrefix_, slotNum, &ShowMessageAfterSaveStateAction); - FinishNextFrame(DR_CANCEL); + CloseAfterStateLoad([this, slotNum](SaveState::Callback callback) { + SaveState::LoadSlot(saveStatePrefix_, slotNum, callback); + }); }; if (g_Config.bConfirmLoadState) { screenManager()->push(new LoadStateConfirmScreen(saveStatePrefix_, slotNum, [doLoad](bool result) { @@ -483,8 +494,9 @@ void GamePauseScreen::CreateSavestateControls(UI::LinearLayout *leftColumnItems, UI::Choice *loadUndoButton = buttonRow->Add(new Choice(pa->T("Undo last load"), ImageID("I_NAVIGATE_BACK"), new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT))); loadUndoButton->SetEnabled(SaveState::HasUndoLoad(saveStatePrefix_)); loadUndoButton->OnClick.Add([this](UI::EventParams &e) { - SaveState::UndoLoad(saveStatePrefix_, &ShowMessageAfterSaveStateAction); - TriggerFinish(DR_CANCEL); + CloseAfterStateLoad([this](SaveState::Callback callback) { + SaveState::UndoLoad(saveStatePrefix_, callback); + }); }); } @@ -492,8 +504,9 @@ void GamePauseScreen::CreateSavestateControls(UI::LinearLayout *leftColumnItems, UI::Choice *rewindButton = buttonRow->Add(new Choice(pa->T("Rewind"), ImageID("I_REWIND"), new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT))); rewindButton->SetEnabled(SaveState::CanRewind()); rewindButton->OnClick.Add([this](UI::EventParams &e) { - SaveState::Rewind(&ShowMessageAfterSaveStateAction); - TriggerFinish(DR_CANCEL); + CloseAfterStateLoad([](SaveState::Callback callback) { + SaveState::Rewind(callback); + }); }); } @@ -861,10 +874,26 @@ void GamePauseScreen::OnState(UI::EventParams &e) { TriggerFinish(DR_CANCEL); } +void GamePauseScreen::CloseAfterStateLoad(const std::function &load) { + // Shared, so a callback arriving after the menu is gone (closed some other way) is harmless. + auto done = std::make_shared>(false); + stateLoadDone_ = done; + load([done](SaveState::Status status, std::string_view message, std::string_view metadata) { + ShowMessageAfterSaveStateAction(status, message, metadata); + *done = true; + }); +} + void GamePauseScreen::dialogFinished(const Screen *dialog, DialogResult dr) { std::string tag = dialog->tag(); if (tag == "ScreenshotView") { - if (dr == DR_OK) { + const ScreenshotViewScreen *view = static_cast(dialog); + if (dr == DR_OK && view->LoadRequested()) { + const int slot = view->GetSlot(); + CloseAfterStateLoad([this, slot](SaveState::Callback callback) { + SaveState::LoadSlot(saveStatePrefix_, slot, callback); + }); + } else if (dr == DR_OK) { FinishNextFrame(DR_BACK); } else if (dr != DR_CANCEL && dr != DR_BACK) { // Just go back to the pause menu, but refresh the savestate thumbnails in case something changed. diff --git a/UI/PauseScreen.h b/UI/PauseScreen.h index 2053ebd12b..91ec75d11f 100644 --- a/UI/PauseScreen.h +++ b/UI/PauseScreen.h @@ -17,6 +17,7 @@ #pragma once +#include #include #include @@ -67,6 +68,11 @@ private: void AddExtraOptions(UI::ViewGroup *parent); void FinishNextFrame(DialogResult finishNextFrameResult); + // Starts a savestate load (or rewind) with the callback it's given, and closes the menu once the + // state is in place. Closing right away would let the game run a few frames first. + void CloseAfterStateLoad(const std::function &load); + // Set by the load's callback. + std::shared_ptr> stateLoadDone_; // hack std::mutex finishNextFrameMutex_; From ef8961e17ac0de0353f98f4158f267bb3985b30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:54:29 +0200 Subject: [PATCH 10/16] Freeze frame: Keep away from GE stepping, and keep other loads Freeze-frame saves and loads its own snapshot outside SaveState::Process, so it missed the check for a display list stopped in the GE debugger: stepping a draw in a frozen frame reloaded the state under the list. And a slot load or rewind while frozen was undone by the next frame's freeze reload, while saying "Loaded State". Now it becomes the frozen state, and is a full load (the GPU only skips its resets for the freeze's own reloads). Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/SaveState.cpp | 24 +++++++++++++++++++++++- UI/EmuScreen.cpp | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index e73ab34cd3..b19b7ccd99 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -1094,6 +1094,22 @@ int g_screenshotFailures; #endif } + // Freeze-frame reloads its snapshot every frame, which would undo any other load right away. So a + // load while frozen becomes the new frozen state (freezeNext snapshots it), and is a full load: + // the GPU only skips its resets for freeze-frame's own reloads, which it tells by frozen. + struct FrozenLoadScope { + const bool frozen = PSP_CoreParameter().frozen; + FrozenLoadScope() { + PSP_CoreParameter().frozen = false; + } + ~FrozenLoadScope() { + if (frozen) { + PSP_CoreParameter().frozen = true; + PSP_CoreParameter().freezeNext = true; + } + } + }; + // What a save's worker job needs, and its completion after. struct SaveJob { Path path; @@ -1294,7 +1310,11 @@ int g_screenshotFailures; saveStateInitialGitVersion = read.gitVersion; SaveStart state; std::string errorString; - CChunkFileReader::Error result = CChunkFileReader::LoadPtr(read.buffer.get(), read.size, state, &errorString); + CChunkFileReader::Error result; + { + FrozenLoadScope frozenScope; + result = CChunkFileReader::LoadPtr(read.buffer.get(), read.size, state, &errorString); + } read.buffer.reset(); if (result == CChunkFileReader::ERROR_NONE) { @@ -1348,6 +1368,8 @@ int g_screenshotFailures; std::string message; std::string metadata; std::string errorString; + // Covers HandleLoadFailure()'s retries below too. + FrozenLoadScope frozenScope; CChunkFileReader::Error result = rewindStates.FinishRestore(&errorString, &metadata); if (result == CChunkFileReader::ERROR_NONE) { message = sc->T("Loaded State"); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 42b984b292..7651e3d8ed 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -1783,6 +1783,10 @@ ScreenRenderFlags EmuScreen::RunEmulation(bool skipBufferEffects) { if (Achievements::HardcoreModeActive()) { PSP_CoreParameter().freezeNext = false; PSP_CoreParameter().frozen = false; + } else if (coreState == CORE_STEPPING_GE || coreState == CORE_RUNNING_GE) { + // A display list stopped in the GE debugger still belongs to the sceGe call that started + // it. Loading over that, or saving it mid-list, leaves them out of step. SaveState::Process() + // waits this out too. } else if (PSP_CoreParameter().freezeNext) { PSP_CoreParameter().frozen = true; PSP_CoreParameter().freezeNext = false; From fa3eff96fbc5aa34cef848a5a6b897f3624be670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:54:29 +0200 Subject: [PATCH 11/16] Display: Don't read past memory presenting a framebuffer from RAM The PSP accepts display strides that run past the end of VRAM, and negative ones (setframebuf.expected has -64 and 2048), and presenting straight from memory read all 272 rows of whatever stride was set. Without a matching framebuffer, that's a read far outside PSP memory. Now the whole image has to be in memory, or the display shows black, on the hardware backends and the software renderer. A savestate's display framebuffer with an address outside RAM and VRAM, or an invalid format, turns the display off instead. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/HLE/sceDisplay.cpp | 15 +++++++++++++++ GPU/Common/FramebufferManagerCommon.cpp | 14 ++++++++++++-- GPU/Software/SoftGpu.cpp | 4 +++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Core/HLE/sceDisplay.cpp b/Core/HLE/sceDisplay.cpp index 95a18e154e..66abbb1c96 100644 --- a/Core/HLE/sceDisplay.cpp +++ b/Core/HLE/sceDisplay.cpp @@ -237,6 +237,17 @@ struct GPUStatistics_v0 { int moreInts[15]; }; +// A savestate's display framebuffer, straight from the file. The stride can be anything (the PSP +// takes negative ones, and GE dump playback sets others): presenting checks that. +static void SanitizeLoadedFramebuf(FrameBufferState &fb) { + const bool validAddr = fb.topaddr == 0 || Memory::IsRAMAddress(fb.topaddr) || Memory::IsVRAMAddress(fb.topaddr); + if (!validAddr || fb.fmt < GE_FORMAT_565 || fb.fmt > GE_FORMAT_8888) { + ERROR_LOG(Log::SaveState, "Savestate has an invalid display framebuffer (%08x, format %d), turning the display off", fb.topaddr, (int)fb.fmt); + fb.topaddr = 0; + fb.fmt = GE_FORMAT_8888; + } +} + void __DisplayDoState(PointerWrap &p) { auto s = p.Section("sceDisplay", 1, 8); if (!s) @@ -244,6 +255,10 @@ void __DisplayDoState(PointerWrap &p) { Do(p, framebuf); Do(p, latchedFramebuf); + if (p.mode == p.MODE_READ) { + SanitizeLoadedFramebuf(framebuf); + SanitizeLoadedFramebuf(latchedFramebuf); + } Do(p, framebufIsLatched); DisplayHWDoState(p, s <= 2); Do(p, hasSetMode); diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index 74d1e319ed..e26412365f 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -1479,6 +1479,16 @@ void FramebufferManagerCommon::CopyFramebufferForColorTexture(VirtualFramebuffer } } +// Whether the 480x272 the display shows from addr is all in memory. The PSP takes strides that +// aren't: negative ones (rows going up, which this doesn't handle), and larger than VRAM has room for. +static bool IsValidDisplayRange(u32 addr, int stride, GEBufferFormat format) { + if (stride <= 0) { + return false; + } + const u32 bpp = BufferFormatBytesPerPixel(format); + return Memory::IsValidRange(addr, ((272 - 1) * (u32)stride + 480) * bpp); +} + Draw::Texture *FramebufferManagerCommon::MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height) { Draw::DataFormat depthFormat = Draw::DataFormat::UNDEFINED; @@ -1787,7 +1797,7 @@ void FramebufferManagerCommon::PrepareCopyDisplayToOutput(const DisplayLayoutCon VirtualFramebuffer *vfb = FindDisplayVFB(fbaddr, &offsetX, &offsetY); if (!vfb) { - if (Memory::IsValidAddress(fbaddr)) { + if (IsValidDisplayRange(fbaddr, displayStride_, displayFormat_)) { // The game is displaying something directly from RAM. In GTA, it's decoded video. // If successful, this effectively calls presentation_->NotifyPresent(); if (!DrawFramebufferToOutput(config, Memory::GetPointerUnchecked(fbaddr), displayStride_, displayFormat_)) { @@ -3018,7 +3028,7 @@ void FramebufferManagerCommon::NotifyBlockTransferAfter(u32 dstBasePtr, int dstS if (!useBufferedRendering_ && dstStride >= 480 && width >= 480 && height == 272) { bool isPrevDisplayBuffer = PrevDisplayFramebufAddr() == dstBasePtr; bool isDisplayBuffer = CurrentDisplayFramebufAddr() == dstBasePtr; - if (isPrevDisplayBuffer || isDisplayBuffer) { + if ((isPrevDisplayBuffer || isDisplayBuffer) && IsValidDisplayRange(dstBasePtr, dstStride, displayFormat_)) { FlushBeforeCopy(); // HACK DrawFramebufferToOutput(displayLayoutConfigCopy_, Memory::GetPointerUnchecked(dstBasePtr), dstStride, displayFormat_); diff --git a/GPU/Software/SoftGpu.cpp b/GPU/Software/SoftGpu.cpp index 445a54ea5e..aa26ae31be 100644 --- a/GPU/Software/SoftGpu.cpp +++ b/GPU/Software/SoftGpu.cpp @@ -647,7 +647,9 @@ void SoftGPU::CopyToCurrentFboFromDisplayRam(const DisplayLayoutConfig &config, outputFlags |= OutputFlags::PILLARBOX; } } - } else if (!Memory::IsValidAddress(displayFramebuf_) || srcwidth == 0 || srcheight == 0) { + } else if (srcwidth == 0 || srcheight == 0 || displayStride_ < 0 || !Memory::IsValidRange(displayFramebuf_, (displayStride_ == 0 ? srcwidth : displayStride_) * srcheight * (displayFormat_ == GE_FORMAT_8888 ? 4 : 2))) { + // Nothing, or not all of it in memory: the PSP takes strides that run past the end of VRAM, or + // negative ones (rows going up.) hasImage = false; u1 = 1.0f; } else if (displayFormat_ == GE_FORMAT_8888) { From 814593ee4d94e3938f3f116f67a2ee7a2d2f4431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:54:29 +0200 Subject: [PATCH 12/16] Framebuffers: Forget the depth shift tracking with the framebuffers DestroyAllFBOs (on a savestate load) kept depthShiftedFor_ and lastRenderAddress_, so a new framebuffer at the same address as the old one skipped the depth shift for a frame. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/Common/FramebufferManagerCommon.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index e26412365f..add72494c6 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -3132,6 +3132,9 @@ void FramebufferManagerCommon::NotifyConfigChanged() { void FramebufferManagerCommon::DestroyAllFBOs() { DiscardFramebufferCopy(); currentRenderVfb_ = nullptr; + // Or a new framebuffer at the same address would skip its depth shift. + depthShiftedFor_ = 0; + lastRenderAddress_ = 0; displayFramebuf_ = nullptr; prevDisplayFramebuf_ = nullptr; prevPrevDisplayFramebuf_ = nullptr; From 130ea9ced9b4d8775e19ec3403a308debdbfb830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:54:29 +0200 Subject: [PATCH 13/16] GPU: Save the next display list id, and restart frame dumps on load - Which list slot and id the next sceGeListEnQueue gets wasn't saved, so after a load it depended on the session before it, and replays and rewinds diverged. Older states start it at 0, and also get the default EDRAM translation and visible matrices they don't carry, rather than the previous session's. - A frame dump recording during a load replayed the loaded state's commands on the registers from before it. It now starts over from the loaded state. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/Debugger/Record.cpp | 16 ++++++++++++++++ GPU/Debugger/Record.h | 3 +++ GPU/GPUCommon.cpp | 19 ++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/GPU/Debugger/Record.cpp b/GPU/Debugger/Record.cpp index b0ab2efd1e..a2c42e6bbc 100644 --- a/GPU/Debugger/Record.cpp +++ b/GPU/Debugger/Record.cpp @@ -695,6 +695,22 @@ bool Recorder::RecordNextFrame(const std::function callback) return true; } +void Recorder::RestartAfterLoad() { + if (!active) { + return; + } + NOTICE_LOG(Log::System, "Savestate loaded, restarting the recording"); + commands.clear(); + pushbuf.clear(); + lastVRAM.clear(); + lastEdramTrans = 0x400; + flipLastAction = gpuStats.totals.numFlips; + flipFinishAt = -1; + std::lock_guard guard(callbackLock_); + active = false; + nextFrame = true; +} + void Recorder::FinishRecording() { // We're done - this was just to write the result out. if (!active) { diff --git a/GPU/Debugger/Record.h b/GPU/Debugger/Record.h index 78964859b0..ea911be628 100644 --- a/GPU/Debugger/Record.h +++ b/GPU/Debugger/Record.h @@ -50,6 +50,9 @@ public: return nextFrame || active; } bool RecordNextFrame(const std::function callback); + // A savestate load replaced everything a recording in progress started from. It starts over + // from the loaded state, at the next frame. + void RestartAfterLoad(); void ClearCallback() { std::lock_guard guard(callbackLock_); writeCallback = nullptr; diff --git a/GPU/GPUCommon.cpp b/GPU/GPUCommon.cpp index 7372fc989d..913ada9910 100644 --- a/GPU/GPUCommon.cpp +++ b/GPU/GPUCommon.cpp @@ -1953,7 +1953,7 @@ struct DisplayList_v2 { }; void GPUCommon::DoState(PointerWrap &p) { - auto s = p.Section("GPUCommon", 1, 7); + auto s = p.Section("GPUCommon", 1, 8); if (!s) return; @@ -2056,9 +2056,26 @@ void GPUCommon::DoState(PointerWrap &p) { if (s >= 5) { Do(p, matrixVisible.all); + } else if (p.mode == PointerWrap::MODE_READ) { + ResetMatrices(); } if (s >= 6) { Do(p, edramTranslation_); + } else if (p.mode == PointerWrap::MODE_READ) { + edramTranslation_ = 0x400; + } + // Which slot (and so which id) the next list gets. Older states start over at 0, rather than + // carry on from the session before the load. + if (s >= 8) { + Do(p, nextListID); + } else { + nextListID = 0; + } + if (p.mode == PointerWrap::MODE_READ) { + if (nextListID < 0 || nextListID > DisplayListMaxCount) { + nextListID = 0; + } + recorder_.RestartAfterLoad(); } } From ca0e40bb0ab5e44fed16aa261944d44f089c0baf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:54:29 +0200 Subject: [PATCH 14/16] GPU: Don't load the host's window-minimized bit from a savestate skipDrawReason was saved whole, including the bit that says the host window is minimized. A state saved while minimized (a rewind snapshot) kept it set after loading in a normal window, and the other way round. Co-Authored-By: Claude Opus 5.5 (1M context) --- GPU/GPUState.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/GPU/GPUState.cpp b/GPU/GPUState.cpp index fa326349ce..3cc3b7a174 100644 --- a/GPU/GPUState.cpp +++ b/GPU/GPUState.cpp @@ -313,7 +313,12 @@ void GPUStateCache::DoState(PointerWrap &p) { bool framebufChanged = false; // legacy Do(p, framebufChanged); + // Minimized is about the host window now, whatever it was when the state was saved. + const u32 hostSkipDraw = skipDrawReason & SKIPDRAW_WINDOW_MINIMIZED; Do(p, skipDrawReason); + if (p.mode == p.MODE_READ) { + skipDrawReason = (skipDrawReason & ~SKIPDRAW_WINDOW_MINIMIZED) | hostSkipDraw; + } // Legacy, remove in the next bump. UVScale uv{}; From 28686cf428781dd411a2b7c9dee87708540881d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 20:54:29 +0200 Subject: [PATCH 15/16] sceGe: Check the pending interrupt before finishing it A broken savestate can have a GE interrupt running with nothing pending, or a pending entry for an invalid list. Finishing it read past the queue. Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/HLE/sceGe.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Core/HLE/sceGe.cpp b/Core/HLE/sceGe.cpp index ca41407e59..7793dea642 100644 --- a/Core/HLE/sceGe.cpp +++ b/Core/HLE/sceGe.cpp @@ -158,8 +158,17 @@ public: } void handleResult(PendingInterrupt& pend) override { + // Only from a broken savestate: the interrupt was running, but nothing is pending. + if (ge_pending_cb.empty()) { + ERROR_LOG(Log::sceGe, "GE interrupt finished with nothing pending"); + return; + } GeInterruptData intrdata = ge_pending_cb.front(); ge_pending_cb.pop_front(); + if (!gpu->IsValidListId(intrdata.listid)) { + ERROR_LOG(Log::sceGe, "GE interrupt finished for invalid list %d", intrdata.listid); + return; + } DisplayList* dl = gpu->getList(intrdata.listid); if (!dl->interruptsEnabled) { From 7202904f6092dfcfd5f07b12ec856137e2325353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 19 Oct 2026 21:32:43 +0200 Subject: [PATCH 16/16] libretro: Include SaveState.h for SaveStart, now declared there Co-Authored-By: Claude Opus 5.5 (1M context) --- libretro/libretro.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index ea68e465c2..e24ffd82f0 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -34,6 +34,7 @@ #include "Core/HLE/__sceAudio.h" #include "Core/HW/MemoryStick.h" #include "Core/MemMap.h" +#include "Core/SaveState.h" #include "Core/System.h" #include "Core/CoreTiming.h" #include "Core/HW/Display.h"