freezer: Add documentation for methods

This commit is contained in:
Zach Hilman 2019-05-30 16:57:23 -04:00
parent 1b7d619914
commit c9983ad9a7
2 changed files with 49 additions and 30 deletions

View file

@ -3,18 +3,20 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "common/assert.h" #include "common/assert.h"
#include "common/logging/log.h"
#include "core/core.h" #include "core/core.h"
#include "core/core_timing.h"
#include "core/core_timing_util.h" #include "core/core_timing_util.h"
#include "core/memory.h" #include "core/memory.h"
#include "core/memory/freezer.h" #include "core/memory/freezer.h"
namespace Memory { namespace Memory {
constexpr s64 MEMORY_FREEZER_TICKS = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 60);
namespace { namespace {
u64 MemoryReadWidth(u8 width, VAddr addr) { constexpr s64 MEMORY_FREEZER_TICKS = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 60);
u64 MemoryReadWidth(u32 width, VAddr addr) {
switch (width) { switch (width) {
case 1: case 1:
return Read8(addr); return Read8(addr);
@ -30,7 +32,7 @@ u64 MemoryReadWidth(u8 width, VAddr addr) {
} }
} }
void MemoryWriteWidth(u8 width, VAddr addr, u64 value) { void MemoryWriteWidth(u32 width, VAddr addr, u64 value) {
switch (width) { switch (width) {
case 1: case 1:
Write8(addr, static_cast<u8>(value)); Write8(addr, static_cast<u8>(value));
@ -73,19 +75,19 @@ void Freezer::SetActive(bool active) {
} }
bool Freezer::IsActive() const { bool Freezer::IsActive() const {
return active.load(); return active.load(std::memory_order_relaxed);
} }
void Freezer::Clear() { void Freezer::Clear() {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
LOG_DEBUG(Common_Memory, "Clearing all frozen memory values."); LOG_DEBUG(Common_Memory, "Clearing all frozen memory values.");
entries.clear(); entries.clear();
} }
u64 Freezer::Freeze(VAddr address, u8 width) { u64 Freezer::Freeze(VAddr address, u32 width) {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
const auto current_value = MemoryReadWidth(width, address); const auto current_value = MemoryReadWidth(width, address);
entries.push_back({address, width, current_value}); entries.push_back({address, width, current_value});
@ -98,7 +100,7 @@ u64 Freezer::Freeze(VAddr address, u8 width) {
} }
void Freezer::Unfreeze(VAddr address) { void Freezer::Unfreeze(VAddr address) {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address); LOG_DEBUG(Common_Memory, "Unfreezing memory for address={:016X}", address);
@ -108,8 +110,8 @@ void Freezer::Unfreeze(VAddr address) {
entries.end()); entries.end());
} }
bool Freezer::IsFrozen(VAddr address) { bool Freezer::IsFrozen(VAddr address) const {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { return std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) {
return entry.address == address; return entry.address == address;
@ -117,7 +119,7 @@ bool Freezer::IsFrozen(VAddr address) {
} }
void Freezer::SetFrozenValue(VAddr address, u64 value) { void Freezer::SetFrozenValue(VAddr address, u64 value) {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) {
return entry.address == address; return entry.address == address;
@ -135,8 +137,8 @@ void Freezer::SetFrozenValue(VAddr address, u64 value) {
iter->value = value; iter->value = value;
} }
std::optional<Freezer::Entry> Freezer::GetEntry(VAddr address) { std::optional<Freezer::Entry> Freezer::GetEntry(VAddr address) const {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) { const auto iter = std::find_if(entries.begin(), entries.end(), [&address](const Entry& entry) {
return entry.address == address; return entry.address == address;
@ -149,19 +151,19 @@ std::optional<Freezer::Entry> Freezer::GetEntry(VAddr address) {
return *iter; return *iter;
} }
std::vector<Freezer::Entry> Freezer::GetEntries() { std::vector<Freezer::Entry> Freezer::GetEntries() const {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
return entries; return entries;
} }
void Freezer::FrameCallback(u64 userdata, s64 cycles_late) { void Freezer::FrameCallback(u64 userdata, s64 cycles_late) {
if (!active.load()) { if (!IsActive()) {
LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events."); LOG_DEBUG(Common_Memory, "Memory freezer has been deactivated, ending callback events.");
return; return;
} }
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
for (const auto& entry : entries) { for (const auto& entry : entries) {
LOG_DEBUG(Common_Memory, LOG_DEBUG(Common_Memory,
@ -174,7 +176,7 @@ void Freezer::FrameCallback(u64 userdata, s64 cycles_late) {
} }
void Freezer::FillEntryReads() { void Freezer::FillEntryReads() {
std::lock_guard<std::recursive_mutex> lock(entries_mutex); std::lock_guard lock{entries_mutex};
LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values."); LOG_DEBUG(Common_Memory, "Updating memory freeze entries to current values.");

View file

@ -4,14 +4,16 @@
#pragma once #pragma once
#include <atomic>
#include <mutex>
#include <optional> #include <optional>
#include <vector> #include <vector>
#include "common/common_types.h" #include "common/common_types.h"
#include "core/core_timing.h"
namespace Core { namespace Core::Timing {
class System; class CoreTiming;
} // namespace Core struct EventType;
} // namespace Core::Timing
namespace Memory { namespace Memory {
@ -20,27 +22,42 @@ class Freezer {
public: public:
struct Entry { struct Entry {
VAddr address; VAddr address;
u8 width; u32 width;
u64 value; u64 value;
}; };
Freezer(Core::Timing::CoreTiming& core_timing); explicit Freezer(Core::Timing::CoreTiming& core_timing);
~Freezer(); ~Freezer();
// Enables or disables the entire memory freezer.
void SetActive(bool active); void SetActive(bool active);
// Returns whether or not the freezer is active.
bool IsActive() const; bool IsActive() const;
// Removes all entries from the freezer.
void Clear(); void Clear();
u64 Freeze(VAddr address, u8 width); // Freezes a value to its current memory address. The value the memory is kept at will be the
// value that is read during this function. Width can be 1, 2, 4, or 8 (in bytes).
u64 Freeze(VAddr address, u32 width);
// Unfreezes the memory value at address. If the address isn't frozen, this is a no-op.
void Unfreeze(VAddr address); void Unfreeze(VAddr address);
bool IsFrozen(VAddr address); // Returns whether or not the address is frozen.
bool IsFrozen(VAddr address) const;
// Sets the value that address should be frozen to. This doesn't change the width set by using
// Freeze(). If the value isn't frozen, this will not freeze it and is thus a no-op.
void SetFrozenValue(VAddr address, u64 value); void SetFrozenValue(VAddr address, u64 value);
std::optional<Entry> GetEntry(VAddr address); // Returns the entry corresponding to the address if the address is frozen, otherwise
// std::nullopt.
std::optional<Entry> GetEntry(VAddr address) const;
std::vector<Entry> GetEntries(); // Returns all the entries in the freezer, an empty vector means nothing is frozen.
std::vector<Entry> GetEntries() const;
private: private:
void FrameCallback(u64 userdata, s64 cycles_late); void FrameCallback(u64 userdata, s64 cycles_late);
@ -48,7 +65,7 @@ private:
std::atomic_bool active{false}; std::atomic_bool active{false};
std::recursive_mutex entries_mutex; mutable std::mutex entries_mutex;
std::vector<Entry> entries; std::vector<Entry> entries;
Core::Timing::EventType* event; Core::Timing::EventType* event;