Core: add ability to add memory patches to the patch engine that will be executed each frame

This commit is contained in:
iwubcode 2022-12-22 17:38:08 -06:00
parent 1f87bcd202
commit 2f2f906bf5
5 changed files with 60 additions and 1 deletions

View File

@ -38,6 +38,23 @@ void MemoryPatches::SetPatch(u32 address, std::vector<u8> value)
Patch(index); Patch(index);
} }
void MemoryPatches::SetFramePatch(u32 address, u32 value)
{
const std::size_t index = m_patches.size();
m_patches.emplace_back(address, value);
m_patches.back().type = MemoryPatch::ApplyType::EachFrame;
Patch(index);
}
void MemoryPatches::SetFramePatch(u32 address, std::vector<u8> value)
{
UnsetPatch(address);
const std::size_t index = m_patches.size();
m_patches.emplace_back(address, std::move(value));
m_patches.back().type = MemoryPatch::ApplyType::EachFrame;
Patch(index);
}
const std::vector<MemoryPatch>& MemoryPatches::GetPatches() const const std::vector<MemoryPatch>& MemoryPatches::GetPatches() const
{ {
return m_patches; return m_patches;
@ -81,6 +98,7 @@ bool MemoryPatches::HasEnabledPatch(u32 address) const
void MemoryPatches::RemovePatch(std::size_t index) void MemoryPatches::RemovePatch(std::size_t index)
{ {
DisablePatch(index); DisablePatch(index);
UnPatch(index);
m_patches.erase(m_patches.begin() + index); m_patches.erase(m_patches.begin() + index);
} }
@ -88,7 +106,10 @@ void MemoryPatches::ClearPatches()
{ {
const std::size_t size = m_patches.size(); const std::size_t size = m_patches.size();
for (std::size_t index = 0; index < size; ++index) for (std::size_t index = 0; index < size; ++index)
{
DisablePatch(index); DisablePatch(index);
UnPatch(index);
}
m_patches.clear(); m_patches.clear();
} }
} // namespace Common::Debug } // namespace Common::Debug

View File

@ -19,12 +19,19 @@ struct MemoryPatch
Disabled Disabled
}; };
enum class ApplyType
{
Once,
EachFrame
};
MemoryPatch(u32 address_, std::vector<u8> value_); MemoryPatch(u32 address_, std::vector<u8> value_);
MemoryPatch(u32 address_, u32 value_); MemoryPatch(u32 address_, u32 value_);
u32 address; u32 address;
std::vector<u8> value; std::vector<u8> value;
State is_enabled = State::Enabled; State is_enabled = State::Enabled;
ApplyType type = ApplyType::Once;
}; };
class MemoryPatches class MemoryPatches
@ -35,6 +42,8 @@ public:
void SetPatch(u32 address, u32 value); void SetPatch(u32 address, u32 value);
void SetPatch(u32 address, std::vector<u8> value); void SetPatch(u32 address, std::vector<u8> value);
void SetFramePatch(u32 address, u32 value);
void SetFramePatch(u32 address, std::vector<u8> value);
const std::vector<MemoryPatch>& GetPatches() const; const std::vector<MemoryPatch>& GetPatches() const;
void UnsetPatch(u32 address); void UnsetPatch(u32 address);
void EnablePatch(std::size_t index); void EnablePatch(std::size_t index);
@ -46,6 +55,7 @@ public:
protected: protected:
virtual void Patch(std::size_t index) = 0; virtual void Patch(std::size_t index) = 0;
virtual void UnPatch(std::size_t index) = 0;
std::vector<MemoryPatch> m_patches; std::vector<MemoryPatch> m_patches;
}; };

View File

@ -43,6 +43,8 @@ public:
// Memory Patches // Memory Patches
virtual void SetPatch(u32 address, u32 value) = 0; virtual void SetPatch(u32 address, u32 value) = 0;
virtual void SetPatch(u32 address, std::vector<u8> value) = 0; virtual void SetPatch(u32 address, std::vector<u8> value) = 0;
virtual void SetFramePatch(u32 address, u32 value) = 0;
virtual void SetFramePatch(u32 address, std::vector<u8> value) = 0;
virtual const std::vector<Debug::MemoryPatch>& GetPatches() const = 0; virtual const std::vector<Debug::MemoryPatch>& GetPatches() const = 0;
virtual void UnsetPatch(u32 address) = 0; virtual void UnsetPatch(u32 address) = 0;
virtual void EnablePatch(std::size_t index) = 0; virtual void EnablePatch(std::size_t index) = 0;

View File

@ -19,6 +19,7 @@
#include "Core/Core.h" #include "Core/Core.h"
#include "Core/Debugger/OSThread.h" #include "Core/Debugger/OSThread.h"
#include "Core/HW/DSP.h" #include "Core/HW/DSP.h"
#include "Core/PatchEngine.h"
#include "Core/PowerPC/MMU.h" #include "Core/PowerPC/MMU.h"
#include "Core/PowerPC/PPCSymbolDB.h" #include "Core/PowerPC/PPCSymbolDB.h"
#include "Core/PowerPC/PowerPC.h" #include "Core/PowerPC/PowerPC.h"
@ -58,7 +59,19 @@ void PPCPatches::ApplyExistingPatch(std::size_t index)
void PPCPatches::Patch(std::size_t index) void PPCPatches::Patch(std::size_t index)
{ {
auto& patch = m_patches[index]; auto& patch = m_patches[index];
ApplyMemoryPatch(patch); if (patch.type == Common::Debug::MemoryPatch::ApplyType::Once)
ApplyMemoryPatch(patch);
else
PatchEngine::AddMemoryPatch(index);
}
void PPCPatches::UnPatch(std::size_t index)
{
auto& patch = m_patches[index];
if (patch.type == Common::Debug::MemoryPatch::ApplyType::Once)
return;
PatchEngine::RemoveMemoryPatch(index);
} }
PPCDebugInterface::PPCDebugInterface() = default; PPCDebugInterface::PPCDebugInterface() = default;
@ -144,6 +157,16 @@ void PPCDebugInterface::SetPatch(u32 address, std::vector<u8> value)
m_patches.SetPatch(address, std::move(value)); m_patches.SetPatch(address, std::move(value));
} }
void PPCDebugInterface::SetFramePatch(u32 address, u32 value)
{
m_patches.SetFramePatch(address, value);
}
void PPCDebugInterface::SetFramePatch(u32 address, std::vector<u8> value)
{
m_patches.SetFramePatch(address, std::move(value));
}
const std::vector<Common::Debug::MemoryPatch>& PPCDebugInterface::GetPatches() const const std::vector<Common::Debug::MemoryPatch>& PPCDebugInterface::GetPatches() const
{ {
return m_patches.GetPatches(); return m_patches.GetPatches();

View File

@ -21,6 +21,7 @@ public:
private: private:
void Patch(std::size_t index) override; void Patch(std::size_t index) override;
void UnPatch(std::size_t index) override;
}; };
// wrapper between disasm control and Dolphin debugger // wrapper between disasm control and Dolphin debugger
@ -50,6 +51,8 @@ public:
// Memory Patches // Memory Patches
void SetPatch(u32 address, u32 value) override; void SetPatch(u32 address, u32 value) override;
void SetPatch(u32 address, std::vector<u8> value) override; void SetPatch(u32 address, std::vector<u8> value) override;
void SetFramePatch(u32 address, u32 value) override;
void SetFramePatch(u32 address, std::vector<u8> value) override;
const std::vector<Common::Debug::MemoryPatch>& GetPatches() const override; const std::vector<Common::Debug::MemoryPatch>& GetPatches() const override;
void UnsetPatch(u32 address) override; void UnsetPatch(u32 address) override;
void EnablePatch(std::size_t index) override; void EnablePatch(std::size_t index) override;