summaryrefslogtreecommitdiff
path: root/src/core/tools/freezer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/tools/freezer.h')
-rw-r--r--src/core/tools/freezer.h82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/core/tools/freezer.h b/src/core/tools/freezer.h
new file mode 100644
index 000000000..b58de5472
--- /dev/null
+++ b/src/core/tools/freezer.h
@@ -0,0 +1,82 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <atomic>
8#include <mutex>
9#include <optional>
10#include <vector>
11#include "common/common_types.h"
12
13namespace Core::Timing {
14class CoreTiming;
15struct EventType;
16} // namespace Core::Timing
17
18namespace Tools {
19
20/**
21 * This class allows the user to prevent an application from writing new values to certain memory
22 * locations. This has a variety of uses when attempting to reverse a game.
23 *
24 * One example could be a cheat to prevent Mario from taking damage in SMO. One could freeze the
25 * memory address that the game uses to store Mario's health so when he takes damage (and the game
26 * tries to write the new health value to memory), the value won't change.
27 */
28class Freezer {
29public:
30 struct Entry {
31 VAddr address;
32 u32 width;
33 u64 value;
34 };
35
36 explicit Freezer(Core::Timing::CoreTiming& core_timing);
37 ~Freezer();
38
39 // Enables or disables the entire memory freezer.
40 void SetActive(bool active);
41
42 // Returns whether or not the freezer is active.
43 bool IsActive() const;
44
45 // Removes all entries from the freezer.
46 void Clear();
47
48 // Freezes a value to its current memory address. The value the memory is kept at will be the
49 // value that is read during this function. Width can be 1, 2, 4, or 8 (in bytes).
50 u64 Freeze(VAddr address, u32 width);
51
52 // Unfreezes the memory value at address. If the address isn't frozen, this is a no-op.
53 void Unfreeze(VAddr address);
54
55 // Returns whether or not the address is frozen.
56 bool IsFrozen(VAddr address) const;
57
58 // Sets the value that address should be frozen to. This doesn't change the width set by using
59 // Freeze(). If the value isn't frozen, this will not freeze it and is thus a no-op.
60 void SetFrozenValue(VAddr address, u64 value);
61
62 // Returns the entry corresponding to the address if the address is frozen, otherwise
63 // std::nullopt.
64 std::optional<Entry> GetEntry(VAddr address) const;
65
66 // Returns all the entries in the freezer, an empty vector means nothing is frozen.
67 std::vector<Entry> GetEntries() const;
68
69private:
70 void FrameCallback(u64 userdata, s64 cycles_late);
71 void FillEntryReads();
72
73 std::atomic_bool active{false};
74
75 mutable std::mutex entries_mutex;
76 std::vector<Entry> entries;
77
78 Core::Timing::EventType* event;
79 Core::Timing::CoreTiming& core_timing;
80};
81
82} // namespace Tools