2019-05-30 13:52:20 +01:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
#include <atomic>
|
2020-07-16 00:14:21 +01:00
|
|
|
#include <chrono>
|
2019-11-27 02:48:56 +00:00
|
|
|
#include <memory>
|
2019-05-30 21:57:23 +01:00
|
|
|
#include <mutex>
|
2019-05-30 13:52:20 +01:00
|
|
|
#include <optional>
|
|
|
|
#include <vector>
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
namespace Core::Timing {
|
|
|
|
class CoreTiming;
|
|
|
|
struct EventType;
|
|
|
|
} // namespace Core::Timing
|
2019-05-30 13:52:20 +01:00
|
|
|
|
2020-03-31 20:10:44 +01:00
|
|
|
namespace Core::Memory {
|
2019-11-26 19:10:49 +00:00
|
|
|
class Memory;
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:11:11 +01:00
|
|
|
namespace Tools {
|
2019-05-30 13:52:20 +01:00
|
|
|
|
2019-06-10 19:11:06 +01:00
|
|
|
/**
|
|
|
|
* This class allows the user to prevent an application from writing new values to certain memory
|
|
|
|
* locations. This has a variety of uses when attempting to reverse a game.
|
|
|
|
*
|
|
|
|
* One example could be a cheat to prevent Mario from taking damage in SMO. One could freeze the
|
|
|
|
* memory address that the game uses to store Mario's health so when he takes damage (and the game
|
|
|
|
* tries to write the new health value to memory), the value won't change.
|
|
|
|
*/
|
2019-05-30 13:52:20 +01:00
|
|
|
class Freezer {
|
|
|
|
public:
|
|
|
|
struct Entry {
|
|
|
|
VAddr address;
|
2019-05-30 21:57:23 +01:00
|
|
|
u32 width;
|
2019-05-30 13:52:20 +01:00
|
|
|
u64 value;
|
|
|
|
};
|
|
|
|
|
2020-03-31 20:10:44 +01:00
|
|
|
explicit Freezer(Core::Timing::CoreTiming& core_timing_, Core::Memory::Memory& memory_);
|
2019-05-30 13:52:20 +01:00
|
|
|
~Freezer();
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
// Enables or disables the entire memory freezer.
|
2019-05-30 13:52:20 +01:00
|
|
|
void SetActive(bool active);
|
2019-05-30 21:57:23 +01:00
|
|
|
|
|
|
|
// Returns whether or not the freezer is active.
|
2019-05-30 13:52:20 +01:00
|
|
|
bool IsActive() const;
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
// Removes all entries from the freezer.
|
2019-05-30 13:52:20 +01:00
|
|
|
void Clear();
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
// 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.
|
2019-05-30 13:52:20 +01:00
|
|
|
void Unfreeze(VAddr address);
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
// 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.
|
2019-05-30 13:52:20 +01:00
|
|
|
void SetFrozenValue(VAddr address, u64 value);
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
// Returns the entry corresponding to the address if the address is frozen, otherwise
|
|
|
|
// std::nullopt.
|
|
|
|
std::optional<Entry> GetEntry(VAddr address) const;
|
2019-05-30 13:52:20 +01:00
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
// Returns all the entries in the freezer, an empty vector means nothing is frozen.
|
|
|
|
std::vector<Entry> GetEntries() const;
|
2019-05-30 13:52:20 +01:00
|
|
|
|
|
|
|
private:
|
2020-08-06 08:11:19 +01:00
|
|
|
using Entries = std::vector<Entry>;
|
|
|
|
|
|
|
|
Entries::iterator FindEntry(VAddr address);
|
|
|
|
Entries::const_iterator FindEntry(VAddr address) const;
|
|
|
|
|
2020-07-28 00:00:41 +01:00
|
|
|
void FrameCallback(std::uintptr_t user_data, std::chrono::nanoseconds ns_late);
|
2019-05-30 13:52:20 +01:00
|
|
|
void FillEntryReads();
|
|
|
|
|
|
|
|
std::atomic_bool active{false};
|
|
|
|
|
2019-05-30 21:57:23 +01:00
|
|
|
mutable std::mutex entries_mutex;
|
2020-08-06 08:11:19 +01:00
|
|
|
Entries entries;
|
2019-05-30 13:52:20 +01:00
|
|
|
|
2019-11-27 02:48:56 +00:00
|
|
|
std::shared_ptr<Core::Timing::EventType> event;
|
2019-05-30 13:52:20 +01:00
|
|
|
Core::Timing::CoreTiming& core_timing;
|
2020-03-31 20:10:44 +01:00
|
|
|
Core::Memory::Memory& memory;
|
2019-05-30 13:52:20 +01:00
|
|
|
};
|
|
|
|
|
2019-06-07 16:11:11 +01:00
|
|
|
} // namespace Tools
|