summaryrefslogtreecommitdiff
path: root/src/core/tools
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/tools')
-rw-r--r--src/core/tools/freezer.cpp188
-rw-r--r--src/core/tools/freezer.h75
2 files changed, 263 insertions, 0 deletions
diff --git a/src/core/tools/freezer.cpp b/src/core/tools/freezer.cpp
new file mode 100644
index 000000000..17f050068
--- /dev/null
+++ b/src/core/tools/freezer.cpp
@@ -0,0 +1,188 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/assert.h"
6#include "common/logging/log.h"
7#include "core/core.h"
8#include "core/core_timing.h"
9#include "core/core_timing_util.h"
10#include "core/memory.h"
11#include "core/tools/freezer.h"
12
13namespace Tools {
14
15namespace {
16
17constexpr s64 MEMORY_FREEZER_TICKS = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 60);
18
19u64 MemoryReadWidth(u32 width, VAddr addr) {
20 switch (width) {
21 case 1:
22 return Memory::Read8(addr);
23 case 2:
24 return Memory::Read16(addr);
25 case 4:
26 return Memory::Read32(addr);
27 case 8:
28 return Memory::Read64(addr);
29 default:
30 UNREACHABLE();
31 return 0;
32 }
33}
34
35void MemoryWriteWidth(u32 width, VAddr addr, u64 value) {
36 switch (width) {
37 case 1:
38 Memory::Write8(addr, static_cast<u8>(value));
39 break;
40 case 2:
41 Memory::Write16(addr, static_cast<u16>(value));
42 break;
43 case 4:
44 Memory::Write32(addr, static_cast<u32>(value));
45 break;
46 case 8:
47 Memory::Write64(addr, value);
48 break;
49 default:
50 UNREACHABLE();
51 }
52}
53
54} // Anonymous namespace
55
56Freezer::Freezer(Core::Timing::CoreTiming& core_timing) : core_timing(core_timing) {
57 event = core_timing.RegisterEvent(
58 "MemoryFreezer::FrameCallback",
59 [this](u64 userdata, s64 cycles_late) { FrameCallback(userdata, cycles_late); });
60 core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event);
61}
62
63Freezer::~Freezer() {
64 core_timing.UnscheduleEvent(event, 0);
65}
66
67void Freezer::SetActive(bool active) {
68 if (!this->active.exchange(active)) {
69 FillEntryReads();
70 core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS, event);
71 LOG_DEBUG(Common_Memory, "Memory freezer activated!");
72 } else {
73 LOG_DEBUG(Common_Memory, "Memory freezer deactivated!");
74 }
75}
76
77bool Freezer::IsActive() const {
78 return active.load(std::memory_order_relaxed);
79}
80
81void Freezer::Clear() {
82 std::lock_guard lock{entries_mutex};
83
84 LOG_DEBUG(Common_Memory, "Clearing all frozen memory values.");
85
86 entries.clear();
87}
88
89u64 Freezer::Freeze(VAddr address, u32 width) {
90 std::lock_guard lock{entries_mutex};
91
92 const auto current_value = MemoryReadWidth(width, address);
93 entries.push_back({address, width, current_value});
94
95 LOG_DEBUG(Common_Memory,
96 "Freezing memory for address={:016X}, width={:02X}, current_value={:016X}", address,
97 width, current_value);
98
99 return current_value;
100}
101
102void Freezer::Unfreeze(VAddr address) {
103 std::lock_guard lock{entries_mutex};
104
105 LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address);
106
107 entries.erase(
108 std::remove_if(entries.begin(), entries.end(),
109 [&address](const Entry& entry) { return entry.address == address; }),
110 entries.end());
111}
112
113bool Freezer::IsFrozen(VAddr address) const {
114 std::lock_guard lock{entries_mutex};
115
116 return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) {
117 return entry.address == address;
118 }) != entries.end();
119}
120
121void Freezer::SetFrozenValue(VAddr address, u64 value) {
122 std::lock_guard lock{entries_mutex};
123
124 const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) {
125 return entry.address == address;
126 });
127
128 if (iter == entries.end()) {
129 LOG_ERROR(Common_Memory,
130 "Tried to set freeze value for address={:016X} that is not frozen!", address);
131 return;
132 }
133
134 LOG_DEBUG(Common_Memory,
135 "Manually overridden freeze value for address={:016X}, width={:02X} to value={:016X}",
136 iter->address, iter->width, value);
137 iter->value = value;
138}
139
140std::optional<Freezer::Entry> Freezer::GetEntry(VAddr address) const {
141 std::lock_guard lock{entries_mutex};
142
143 const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) {
144 return entry.address == address;
145 });
146
147 if (iter == entries.end()) {
148 return std::nullopt;
149 }
150
151 return *iter;
152}
153
154std::vector<Freezer::Entry> Freezer::GetEntries() const {
155 std::lock_guard lock{entries_mutex};
156
157 return entries;
158}
159
160void Freezer::FrameCallback(u64 userdata, s64 cycles_late) {
161 if (!IsActive()) {
162 LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events.");
163 return;
164 }
165
166 std::lock_guard lock{entries_mutex};
167
168 for (const auto& entry : entries) {
169 LOG_DEBUG(Common_Memory,
170 "Enforcing memory freeze at address={:016X}, value={:016X}, width={:02X}",
171 entry.address, entry.value, entry.width);
172 MemoryWriteWidth(entry.width, entry.address, entry.value);
173 }
174
175 core_timing.ScheduleEvent(MEMORY_FREEZER_TICKS - cycles_late, event);
176}
177
178void Freezer::FillEntryReads() {
179 std::lock_guard lock{entries_mutex};
180
181 LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values.");
182
183 for (auto& entry : entries) {
184 entry.value = MemoryReadWidth(entry.width, entry.address);
185 }
186}
187
188} // namespace Tools
diff --git a/src/core/tools/freezer.h b/src/core/tools/freezer.h
new file mode 100644
index 000000000..5edd049c1
--- /dev/null
+++ b/src/core/tools/freezer.h
@@ -0,0 +1,75 @@
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// A class that will effectively freeze memory values.
21class Freezer {
22public:
23 struct Entry {
24 VAddr address;
25 u32 width;
26 u64 value;
27 };
28
29 explicit Freezer(Core::Timing::CoreTiming& core_timing);
30 ~Freezer();
31
32 // Enables or disables the entire memory freezer.
33 void SetActive(bool active);
34
35 // Returns whether or not the freezer is active.
36 bool IsActive() const;
37
38 // Removes all entries from the freezer.
39 void Clear();
40
41 // Freezes a value to its current memory address. The value the memory is kept at will be the
42 // value that is read during this function. Width can be 1, 2, 4, or 8 (in bytes).
43 u64 Freeze(VAddr address, u32 width);
44
45 // Unfreezes the memory value at address. If the address isn't frozen, this is a no-op.
46 void Unfreeze(VAddr address);
47
48 // Returns whether or not the address is frozen.
49 bool IsFrozen(VAddr address) const;
50
51 // Sets the value that address should be frozen to. This doesn't change the width set by using
52 // Freeze(). If the value isn't frozen, this will not freeze it and is thus a no-op.
53 void SetFrozenValue(VAddr address, u64 value);
54
55 // Returns the entry corresponding to the address if the address is frozen, otherwise
56 // std::nullopt.
57 std::optional<Entry> GetEntry(VAddr address) const;
58
59 // Returns all the entries in the freezer, an empty vector means nothing is frozen.
60 std::vector<Entry> GetEntries() const;
61
62private:
63 void FrameCallback(u64 userdata, s64 cycles_late);
64 void FillEntryReads();
65
66 std::atomic_bool active{false};
67
68 mutable std::mutex entries_mutex;
69 std::vector<Entry> entries;
70
71 Core::Timing::EventType* event;
72 Core::Timing::CoreTiming& core_timing;
73};
74
75} // namespace Tools