| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Debug/MemoryPatches.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <utility> |
| 8 | |
| 9 | namespace Common::Debug |
| 10 | { |
| 11 | MemoryPatch::MemoryPatch(u32 address_, std::vector<u8> value_) |
| 12 | : address(address_), value(std::move(value_)) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | MemoryPatch::MemoryPatch(u32 address_, u32 value_) |
| 17 | : MemoryPatch(address_, {static_cast<u8>(value_ >> 24), static_cast<u8>(value_ >> 16), |
| 18 | static_cast<u8>(value_ >> 8), static_cast<u8>(value_)}) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | MemoryPatches::MemoryPatches() = default; |
| 23 | MemoryPatches::~MemoryPatches() = default; |
| 24 | |
| 25 | void MemoryPatches::SetPatch(const Core::CPUThreadGuard& guard, u32 address, u32 value) |
| 26 | { |
| 27 | const std::size_t index = m_patches.size(); |
| 28 | m_patches.emplace_back(address, value); |
| 29 | Patch(guard, index); |
| 30 | } |
| 31 | |
| 32 | void MemoryPatches::SetPatch(const Core::CPUThreadGuard& guard, u32 address, std::vector<u8> value) |
| 33 | { |
| 34 | UnsetPatch(guard, address); |
| 35 | const std::size_t index = m_patches.size(); |
| 36 | m_patches.emplace_back(address, std::move(value)); |
| 37 | Patch(guard, index); |
| 38 | } |
| 39 | |
| 40 | void MemoryPatches::SetFramePatch(const Core::CPUThreadGuard& guard, u32 address, u32 value) |
| 41 | { |
| 42 | const std::size_t index = m_patches.size(); |
| 43 | m_patches.emplace_back(address, value); |
| 44 | m_patches.back().type = MemoryPatch::ApplyType::EachFrame; |
| 45 | Patch(guard, index); |
| 46 | } |
| 47 | |
| 48 | void MemoryPatches::SetFramePatch(const Core::CPUThreadGuard& guard, u32 address, |
| 49 | std::vector<u8> value) |
| 50 | { |
| 51 | UnsetPatch(guard, address); |
| 52 | const std::size_t index = m_patches.size(); |
| 53 | m_patches.emplace_back(address, std::move(value)); |
| 54 | m_patches.back().type = MemoryPatch::ApplyType::EachFrame; |
| 55 | Patch(guard, index); |
| 56 | } |
| 57 | |
| 58 | const std::vector<MemoryPatch>& MemoryPatches::GetPatches() const |
| 59 | { |
| 60 | return m_patches; |
| 61 | } |
| 62 | |
| 63 | void MemoryPatches::UnsetPatch(const Core::CPUThreadGuard& guard, u32 address) |
| 64 | { |
| 65 | const auto it = std::ranges::find(m_patches, address, &MemoryPatch::address); |
| 66 | |
| 67 | if (it == m_patches.end()) |
| 68 | return; |
| 69 | |
| 70 | const std::size_t index = std::distance(m_patches.begin(), it); |
| 71 | RemovePatch(guard, index); |
| 72 | } |
| 73 | |
| 74 | void MemoryPatches::EnablePatch(const Core::CPUThreadGuard& guard, std::size_t index) |
| 75 | { |
| 76 | if (m_patches[index].is_enabled == MemoryPatch::State::Enabled) |
| 77 | return; |
| 78 | m_patches[index].is_enabled = MemoryPatch::State::Enabled; |
| 79 | Patch(guard, index); |
| 80 | } |
| 81 | |
| 82 | void MemoryPatches::DisablePatch(const Core::CPUThreadGuard& guard, std::size_t index) |
| 83 | { |
| 84 | if (m_patches[index].is_enabled == MemoryPatch::State::Disabled) |
| 85 | return; |
| 86 | m_patches[index].is_enabled = MemoryPatch::State::Disabled; |
| 87 | Patch(guard, index); |
| 88 | } |
| 89 | |
| 90 | bool MemoryPatches::HasEnabledPatch(u32 address) const |
| 91 | { |
| 92 | return std::ranges::any_of(m_patches, [address](const MemoryPatch& patch) { |
| 93 | return patch.address == address && patch.is_enabled == MemoryPatch::State::Enabled; |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | void MemoryPatches::RemovePatch(const Core::CPUThreadGuard& guard, std::size_t index) |
| 98 | { |
| 99 | DisablePatch(guard, index); |
| 100 | UnPatch(index); |
| 101 | m_patches.erase(m_patches.begin() + index); |
| 102 | } |
| 103 | |
| 104 | void MemoryPatches::ClearPatches(const Core::CPUThreadGuard& guard) |
| 105 | { |
| 106 | const std::size_t size = m_patches.size(); |
| 107 | for (std::size_t index = 0; index < size; ++index) |
| 108 | { |
| 109 | DisablePatch(guard, index); |
| 110 | UnPatch(index); |
| 111 | } |
| 112 | m_patches.clear(); |
| 113 | } |
| 114 | } // namespace Common::Debug |