summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Debug/MemoryPatches.h
blob: 187934beca7d8053bd7850a6ea2e5d376f6ecee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <cstddef>
#include <vector>

#include "Common/CommonTypes.h"

namespace Core
{
class CPUThreadGuard;
}

namespace Common::Debug
{
struct MemoryPatch
{
  enum class State
  {
    Enabled,
    Disabled
  };

  enum class ApplyType
  {
    Once,
    EachFrame
  };

  MemoryPatch(u32 address_, std::vector<u8> value_);
  MemoryPatch(u32 address_, u32 value_);

  u32 address;
  std::vector<u8> value;
  State is_enabled = State::Enabled;
  ApplyType type = ApplyType::Once;
};

class MemoryPatches
{
public:
  MemoryPatches();
  virtual ~MemoryPatches();

  void SetPatch(const Core::CPUThreadGuard& guard, u32 address, u32 value);
  void SetPatch(const Core::CPUThreadGuard& guard, u32 address, std::vector<u8> value);
  void SetFramePatch(const Core::CPUThreadGuard& guard, u32 address, u32 value);
  void SetFramePatch(const Core::CPUThreadGuard& guard, u32 address, std::vector<u8> value);
  const std::vector<MemoryPatch>& GetPatches() const;
  void UnsetPatch(const Core::CPUThreadGuard& guard, u32 address);
  void EnablePatch(const Core::CPUThreadGuard& guard, std::size_t index);
  void DisablePatch(const Core::CPUThreadGuard& guard, std::size_t index);
  bool HasEnabledPatch(u32 address) const;
  void RemovePatch(const Core::CPUThreadGuard& guard, std::size_t index);
  void ClearPatches(const Core::CPUThreadGuard& guard);
  virtual void ApplyExistingPatch(const Core::CPUThreadGuard& guard, std::size_t index) = 0;

protected:
  virtual void Patch(const Core::CPUThreadGuard& guard, std::size_t index) = 0;
  virtual void UnPatch(std::size_t index) = 0;

  std::vector<MemoryPatch> m_patches;
};
}  // namespace Common::Debug