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).
This commit is contained in:
Martino Fontana 2025-06-19 15:47:09 +02:00
commit 5d71ac268f
2 changed files with 21 additions and 24 deletions

View file

@ -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<u8>(guard, static_cast<u8>(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<u16>(guard, static_cast<u16>(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<u32>(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<u8>(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<u8>(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<u16>(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<u16>(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<u32>(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<u8>(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<u16>(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<u32>(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<float>(data);
const u32 newval = std::bit_cast<u32>(fread);
PowerPC::MMU::HostWrite_U32(guard, newval, new_addr);
ApplyMemoryPatch<u32>(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<u32>(guard, static_cast<u8>(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<u16>(guard, static_cast<u16>(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<u32>(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<u8>(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<u8>(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);
}

View file

@ -219,19 +219,19 @@ static void ApplyPatches(const Core::CPUThreadGuard& guard, const std::vector<Pa
if (!entry.conditional ||
PowerPC::MMU::HostRead_U8(guard, addr) == static_cast<u8>(comparand))
{
PowerPC::MMU::HostWrite_U8(guard, static_cast<u8>(value), addr);
ApplyMemoryPatch<u8>(guard, static_cast<u8>(value), addr);
}
break;
case PatchType::Patch16Bit:
if (!entry.conditional ||
PowerPC::MMU::HostRead_U16(guard, addr) == static_cast<u16>(comparand))
{
PowerPC::MMU::HostWrite_U16(guard, static_cast<u16>(value), addr);
ApplyMemoryPatch<u16>(guard, static_cast<u16>(value), addr);
}
break;
case PatchType::Patch32Bit:
if (!entry.conditional || PowerPC::MMU::HostRead_U32(guard, addr) == comparand)
PowerPC::MMU::HostWrite_U32(guard, value, addr);
ApplyMemoryPatch<u32>(guard, value, addr);
break;
default:
// unknown patchtype