From aa7b13f35344d077b43b3be8a368ce3e998d6fb9 Mon Sep 17 00:00:00 2001 From: Martino Fontana Date: Thu, 19 Jun 2025 15:41:02 +0200 Subject: [PATCH] PPCDebugInterface: Small refactor to ApplyMemoryPatch Invalidate icache only if target address has a different value. Take separate arguements, instead of a struct, to allow easier usage elsewhere. Overload with u8, u16 and u32 values for the same reason. --- .../Core/Core/Debugger/PPCDebugInterface.cpp | 37 ++++++++++--------- Source/Core/Core/Debugger/PPCDebugInterface.h | 14 ++++++- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Source/Core/Core/Debugger/PPCDebugInterface.cpp b/Source/Core/Core/Debugger/PPCDebugInterface.cpp index 2c840d0ae5..92a0a26ea8 100644 --- a/Source/Core/Core/Debugger/PPCDebugInterface.cpp +++ b/Source/Core/Core/Debugger/PPCDebugInterface.cpp @@ -29,55 +29,58 @@ #include "Core/PowerPC/PowerPC.h" #include "Core/System.h" -void ApplyMemoryPatch(const Core::CPUThreadGuard& guard, Common::Debug::MemoryPatch& patch, +void ApplyMemoryPatch(const Core::CPUThreadGuard& guard, std::span value, const u32 address, bool store_existing_value) { if (AchievementManager::GetInstance().IsHardcoreModeActive()) return; - if (patch.value.empty()) + if (value.empty()) return; - const u32 address = patch.address; - const std::size_t size = patch.value.size(); + const std::size_t size = value.size(); if (!PowerPC::MMU::HostIsRAMAddress(guard, address)) return; auto& power_pc = guard.GetSystem().GetPowerPC(); + + bool should_invalidate_cache = false; for (u32 offset = 0; offset < size; ++offset) { - if (store_existing_value) + u8 old_value = PowerPC::MMU::HostRead_U8(guard, address + offset); + if (old_value != value[offset]) { - const u8 value = PowerPC::MMU::HostRead_U8(guard, address + offset); - PowerPC::MMU::HostWrite_U8(guard, patch.value[offset], address + offset); - patch.value[offset] = value; - } - else - { - PowerPC::MMU::HostWrite_U8(guard, patch.value[offset], address + offset); + PowerPC::MMU::HostWrite_U8(guard, value[offset], address + offset); + should_invalidate_cache = true; + if (store_existing_value) + value[offset] = old_value; } if (((address + offset) % 4) == 3) - power_pc.ScheduleInvalidateCacheThreadSafe(Common::AlignDown(address + offset, 4)); + { + if (should_invalidate_cache) + power_pc.ScheduleInvalidateCacheThreadSafe(Common::AlignDown(address + offset, 4)); + should_invalidate_cache = false; + } } - if (((address + size) % 4) != 0) + if (should_invalidate_cache) { power_pc.ScheduleInvalidateCacheThreadSafe( - Common::AlignDown(address + static_cast(size), 4)); + Common::AlignDown(address + static_cast(size) - 1, 4)); } } void PPCPatches::ApplyExistingPatch(const Core::CPUThreadGuard& guard, std::size_t index) { auto& patch = m_patches[index]; - ApplyMemoryPatch(guard, patch, false); + ApplyMemoryPatch(guard, patch.value, patch.address, false); } void PPCPatches::Patch(const Core::CPUThreadGuard& guard, std::size_t index) { auto& patch = m_patches[index]; if (patch.type == Common::Debug::MemoryPatch::ApplyType::Once) - ApplyMemoryPatch(guard, patch); + ApplyMemoryPatch(guard, patch.value, patch.address); else PatchEngine::AddMemoryPatch(index); } diff --git a/Source/Core/Core/Debugger/PPCDebugInterface.h b/Source/Core/Core/Debugger/PPCDebugInterface.h index 797f2d5254..c68864583d 100644 --- a/Source/Core/Core/Debugger/PPCDebugInterface.h +++ b/Source/Core/Core/Debugger/PPCDebugInterface.h @@ -3,12 +3,15 @@ #pragma once +#include #include #include +#include #include #include "Common/Debug/MemoryPatches.h" #include "Common/Debug/Watches.h" +#include "Common/Swap.h" #include "Core/Debugger/DebugInterface.h" #include "Core/NetworkCaptureLogger.h" @@ -19,9 +22,18 @@ class System; } // namespace Core class PPCSymbolDB; -void ApplyMemoryPatch(const Core::CPUThreadGuard&, Common::Debug::MemoryPatch& patch, +void ApplyMemoryPatch(const Core::CPUThreadGuard& guard, std::span value, const u32 address, bool store_existing_value = true); +template +void ApplyMemoryPatch(const Core::CPUThreadGuard& guard, T value, const u32 address) +{ + Common::BigEndianValue big_endian{value}; + auto data = + std::span{reinterpret_cast(std::addressof(big_endian)), sizeof(T)}; + ApplyMemoryPatch(guard, data, address); +} + class PPCPatches final : public Common::Debug::MemoryPatches { public: