diff options
| author | 2019-06-07 11:11:11 -0400 | |
|---|---|---|
| committer | 2019-06-20 19:22:53 -0400 | |
| commit | ed82fa3a91fc84f7f906b898d8f71e15fb42c16e (patch) | |
| tree | 4cacf3974405b80f64b5108481ea198fd08f4aee /src/core/tools/freezer.cpp | |
| parent | freezer: Add documentation for methods (diff) | |
| download | yuzu-ed82fa3a91fc84f7f906b898d8f71e15fb42c16e.tar.gz yuzu-ed82fa3a91fc84f7f906b898d8f71e15fb42c16e.tar.xz yuzu-ed82fa3a91fc84f7f906b898d8f71e15fb42c16e.zip | |
core: Move Freezer class to tools namespace
Diffstat (limited to 'src/core/tools/freezer.cpp')
| -rw-r--r-- | src/core/tools/freezer.cpp | 188 |
1 files changed, 188 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 | |||
| 13 | namespace Tools { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | constexpr s64 MEMORY_FREEZER_TICKS = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 60); | ||
| 18 | |||
| 19 | u64 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 | |||
| 35 | void 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 | |||
| 56 | Freezer::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 | |||
| 63 | Freezer::~Freezer() { | ||
| 64 | core_timing.UnscheduleEvent(event, 0); | ||
| 65 | } | ||
| 66 | |||
| 67 | void 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 | |||
| 77 | bool Freezer::IsActive() const { | ||
| 78 | return active.load(std::memory_order_relaxed); | ||
| 79 | } | ||
| 80 | |||
| 81 | void 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 | |||
| 89 | u64 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 | |||
| 102 | void 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 | |||
| 113 | bool 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 | |||
| 121 | void 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 | |||
| 140 | std::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 | |||
| 154 | std::vector<Freezer::Entry> Freezer::GetEntries() const { | ||
| 155 | std::lock_guard lock{entries_mutex}; | ||
| 156 | |||
| 157 | return entries; | ||
| 158 | } | ||
| 159 | |||
| 160 | void 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 | |||
| 178 | void 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 | ||