From 5d71ac268f0cada886a101b74af410fd05c3eab3 Mon Sep 17 00:00:00 2001 From: Martino Fontana Date: Thu, 19 Jun 2025 15:47:09 +0200 Subject: [PATCH] ActionReplay/PatchEngine: Replace HostWrite with ApplyMemoryPatch Compared to the former, the latter invalidates the icache, which is something that is likely desired for patches (especially if they are applied while the game is running). --- Source/Core/Core/ActionReplay.cpp | 39 ++++++++++++++----------------- Source/Core/Core/PatchEngine.cpp | 6 ++--- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Source/Core/Core/ActionReplay.cpp b/Source/Core/Core/ActionReplay.cpp index 4b309fd5a6..7642de40ac 100644 --- a/Source/Core/Core/ActionReplay.cpp +++ b/Source/Core/Core/ActionReplay.cpp @@ -42,6 +42,7 @@ #include "Core/AchievementManager.h" #include "Core/CheatCodes.h" #include "Core/Config/MainSettings.h" +#include "Core/Debugger/PPCDebugInterface.h" #include "Core/PowerPC/MMU.h" namespace ActionReplay @@ -379,7 +380,7 @@ static bool Subtype_RamWriteAndFill(const Core::CPUThreadGuard& guard, const ARA const u32 repeat = data >> 8; for (u32 i = 0; i <= repeat; ++i) { - PowerPC::MMU::HostWrite_U8(guard, data & 0xFF, new_addr + i); + ApplyMemoryPatch(guard, static_cast(data), new_addr + i); LogInfo("Wrote {:08x} to address {:08x}", data & 0xFF, new_addr + i); } LogInfo("--------"); @@ -393,7 +394,7 @@ static bool Subtype_RamWriteAndFill(const Core::CPUThreadGuard& guard, const ARA const u32 repeat = data >> 16; for (u32 i = 0; i <= repeat; ++i) { - PowerPC::MMU::HostWrite_U16(guard, data & 0xFFFF, new_addr + i * 2); + ApplyMemoryPatch(guard, static_cast(data), new_addr + i * 2); LogInfo("Wrote {:08x} to address {:08x}", data & 0xFFFF, new_addr + i * 2); } LogInfo("--------"); @@ -404,7 +405,7 @@ static bool Subtype_RamWriteAndFill(const Core::CPUThreadGuard& guard, const ARA case DATATYPE_32BIT: // Dword write LogInfo("32-bit Write"); LogInfo("--------"); - PowerPC::MMU::HostWrite_U32(guard, data, new_addr); + ApplyMemoryPatch(guard, data, new_addr); LogInfo("Wrote {:08x} to address {:08x}", data, new_addr); LogInfo("--------"); break; @@ -435,12 +436,12 @@ static bool Subtype_WriteToPointer(const Core::CPUThreadGuard& guard, const ARAd { LogInfo("Write 8-bit to pointer"); LogInfo("--------"); - const u8 thebyte = data & 0xFF; + const u8 thebyte = static_cast(data); const u32 offset = data >> 8; LogInfo("Pointer: {:08x}", ptr); LogInfo("Byte: {:08x}", thebyte); LogInfo("Offset: {:08x}", offset); - PowerPC::MMU::HostWrite_U8(guard, thebyte, ptr + offset); + ApplyMemoryPatch(guard, thebyte, ptr + offset); LogInfo("Wrote {:08x} to address {:08x}", thebyte, ptr + offset); LogInfo("--------"); break; @@ -450,12 +451,12 @@ static bool Subtype_WriteToPointer(const Core::CPUThreadGuard& guard, const ARAd { LogInfo("Write 16-bit to pointer"); LogInfo("--------"); - const u16 theshort = data & 0xFFFF; + const u16 theshort = static_cast(data); const u32 offset = (data >> 16) << 1; LogInfo("Pointer: {:08x}", ptr); LogInfo("Byte: {:08x}", theshort); LogInfo("Offset: {:08x}", offset); - PowerPC::MMU::HostWrite_U16(guard, theshort, ptr + offset); + ApplyMemoryPatch(guard, theshort, ptr + offset); LogInfo("Wrote {:08x} to address {:08x}", theshort, ptr + offset); LogInfo("--------"); break; @@ -465,7 +466,7 @@ static bool Subtype_WriteToPointer(const Core::CPUThreadGuard& guard, const ARAd case DATATYPE_32BIT: LogInfo("Write 32-bit to pointer"); LogInfo("--------"); - PowerPC::MMU::HostWrite_U32(guard, data, ptr); + ApplyMemoryPatch(guard, data, ptr); LogInfo("Wrote {:08x} to address {:08x}", data, ptr); LogInfo("--------"); break; @@ -493,7 +494,7 @@ static bool Subtype_AddCode(const Core::CPUThreadGuard& guard, const ARAddr& add case DATATYPE_8BIT: LogInfo("8-bit Add"); LogInfo("--------"); - PowerPC::MMU::HostWrite_U8(guard, PowerPC::MMU::HostRead_U8(guard, new_addr) + data, new_addr); + ApplyMemoryPatch(guard, PowerPC::MMU::HostRead_U8(guard, new_addr) + data, new_addr); LogInfo("Wrote {:02x} to address {:08x}", PowerPC::MMU::HostRead_U8(guard, new_addr), new_addr); LogInfo("--------"); break; @@ -501,8 +502,7 @@ static bool Subtype_AddCode(const Core::CPUThreadGuard& guard, const ARAddr& add case DATATYPE_16BIT: LogInfo("16-bit Add"); LogInfo("--------"); - PowerPC::MMU::HostWrite_U16(guard, PowerPC::MMU::HostRead_U16(guard, new_addr) + data, - new_addr); + ApplyMemoryPatch(guard, PowerPC::MMU::HostRead_U16(guard, new_addr) + data, new_addr); LogInfo("Wrote {:04x} to address {:08x}", PowerPC::MMU::HostRead_U16(guard, new_addr), new_addr); LogInfo("--------"); @@ -511,8 +511,7 @@ static bool Subtype_AddCode(const Core::CPUThreadGuard& guard, const ARAddr& add case DATATYPE_32BIT: LogInfo("32-bit Add"); LogInfo("--------"); - PowerPC::MMU::HostWrite_U32(guard, PowerPC::MMU::HostRead_U32(guard, new_addr) + data, - new_addr); + ApplyMemoryPatch(guard, PowerPC::MMU::HostRead_U32(guard, new_addr) + data, new_addr); LogInfo("Wrote {:08x} to address {:08x}", PowerPC::MMU::HostRead_U32(guard, new_addr), new_addr); LogInfo("--------"); @@ -528,7 +527,7 @@ static bool Subtype_AddCode(const Core::CPUThreadGuard& guard, const ARAddr& add // data contains an (unsigned?) integer value const float fread = read_float + static_cast(data); const u32 newval = std::bit_cast(fread); - PowerPC::MMU::HostWrite_U32(guard, newval, new_addr); + ApplyMemoryPatch(guard, newval, new_addr); LogInfo("Old Value {:08x}", read); LogInfo("Increment {:08x}", data); LogInfo("New value {:08x}", newval); @@ -586,7 +585,7 @@ static bool ZeroCode_FillAndSlide(const Core::CPUThreadGuard& guard, const u32 v LogInfo("--------"); for (int i = 0; i < write_num; ++i) { - PowerPC::MMU::HostWrite_U8(guard, val & 0xFF, curr_addr); + ApplyMemoryPatch(guard, static_cast(val), curr_addr); curr_addr += addr_incr; val += val_incr; LogInfo("Write {:08x} to address {:08x}", val & 0xFF, curr_addr); @@ -602,7 +601,7 @@ static bool ZeroCode_FillAndSlide(const Core::CPUThreadGuard& guard, const u32 v LogInfo("--------"); for (int i = 0; i < write_num; ++i) { - PowerPC::MMU::HostWrite_U16(guard, val & 0xFFFF, curr_addr); + ApplyMemoryPatch(guard, static_cast(val), curr_addr); LogInfo("Write {:08x} to address {:08x}", val & 0xFFFF, curr_addr); curr_addr += addr_incr * 2; val += val_incr; @@ -617,7 +616,7 @@ static bool ZeroCode_FillAndSlide(const Core::CPUThreadGuard& guard, const u32 v LogInfo("--------"); for (int i = 0; i < write_num; ++i) { - PowerPC::MMU::HostWrite_U32(guard, val, curr_addr); + ApplyMemoryPatch(guard, val, curr_addr); LogInfo("Write {:08x} to address {:08x}", val, curr_addr); curr_addr += addr_incr * 4; val += val_incr; @@ -664,8 +663,7 @@ static bool ZeroCode_MemoryCopy(const Core::CPUThreadGuard& guard, const u32 val LogInfo("Resolved Src Address to: {:08x}", ptr_src); for (int i = 0; i < num_bytes; ++i) { - PowerPC::MMU::HostWrite_U8(guard, PowerPC::MMU::HostRead_U8(guard, ptr_src + i), - ptr_dest + i); + ApplyMemoryPatch(guard, PowerPC::MMU::HostRead_U8(guard, ptr_src + i), ptr_dest + i); LogInfo("Wrote {:08x} to address {:08x}", PowerPC::MMU::HostRead_U8(guard, ptr_src + i), ptr_dest + i); } @@ -677,8 +675,7 @@ static bool ZeroCode_MemoryCopy(const Core::CPUThreadGuard& guard, const u32 val LogInfo("--------"); for (int i = 0; i < num_bytes; ++i) { - PowerPC::MMU::HostWrite_U8(guard, PowerPC::MMU::HostRead_U8(guard, addr_src + i), - addr_dest + i); + ApplyMemoryPatch(guard, PowerPC::MMU::HostRead_U8(guard, addr_src + i), addr_dest + i); LogInfo("Wrote {:08x} to address {:08x}", PowerPC::MMU::HostRead_U8(guard, addr_src + i), addr_dest + i); } diff --git a/Source/Core/Core/PatchEngine.cpp b/Source/Core/Core/PatchEngine.cpp index 65765c44e0..44e5ecedc8 100644 --- a/Source/Core/Core/PatchEngine.cpp +++ b/Source/Core/Core/PatchEngine.cpp @@ -219,19 +219,19 @@ static void ApplyPatches(const Core::CPUThreadGuard& guard, const std::vector(comparand)) { - PowerPC::MMU::HostWrite_U8(guard, static_cast(value), addr); + ApplyMemoryPatch(guard, static_cast(value), addr); } break; case PatchType::Patch16Bit: if (!entry.conditional || PowerPC::MMU::HostRead_U16(guard, addr) == static_cast(comparand)) { - PowerPC::MMU::HostWrite_U16(guard, static_cast(value), addr); + ApplyMemoryPatch(guard, static_cast(value), addr); } break; case PatchType::Patch32Bit: if (!entry.conditional || PowerPC::MMU::HostRead_U32(guard, addr) == comparand) - PowerPC::MMU::HostWrite_U32(guard, value, addr); + ApplyMemoryPatch(guard, value, addr); break; default: // unknown patchtype