This repository has been archived on 2025-12-14. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
github.ong19th.Citron/src/core/tools/freezer.h
Zephyron 9ae0eeeb87 Revert incorrect copyright attribution for non-contributed files
- In commit b3facaa6bb, the copyright header was
  updated to include "Citron Homebrew Project" across multiple files, regardless
  of whether any contributions were made.

- This commit removes the incorrect attribution and reverts the copyright header
  to its previous state.

- Copyright attribution should only be added when meaningful contributions have
  been made to the file.

- This commit ensures proper compliance with copyright standards and maintains
  correct attribution to the respective contributors.

- Special thanks to Tachi for pointing out the need for these corrections and
  ensuring that proper attribution practices are followed.
2025-01-14 15:33:24 +10:00

93 lines
2.8 KiB
C++

// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <atomic>
#include <chrono>
#include <memory>
#include <mutex>
#include <optional>
#include <vector>
#include "common/common_types.h"
namespace Core::Timing {
class CoreTiming;
struct EventType;
} // namespace Core::Timing
namespace Core::Memory {
class Memory;
}
namespace Tools {
/**
* 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.
*/
class Freezer {
public:
struct Entry {
VAddr address;
u32 width;
u64 value;
};
explicit Freezer(Core::Timing::CoreTiming& core_timing_, Core::Memory::Memory& memory_);
~Freezer();
// Enables or disables the entire memory freezer.
void SetActive(bool is_active);
// Returns whether or not the freezer is active.
bool IsActive() const;
// Removes all entries from the freezer.
void Clear();
// 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);
// 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);
// Returns the entry corresponding to the address if the address is frozen, otherwise
// std::nullopt.
std::optional<Entry> GetEntry(VAddr address) const;
// Returns all the entries in the freezer, an empty vector means nothing is frozen.
std::vector<Entry> GetEntries() const;
private:
using Entries = std::vector<Entry>;
Entries::iterator FindEntry(VAddr address);
Entries::const_iterator FindEntry(VAddr address) const;
void FrameCallback(std::chrono::nanoseconds ns_late);
void FillEntryReads();
std::atomic_bool active{false};
mutable std::mutex entries_mutex;
Entries entries;
std::shared_ptr<Core::Timing::EventType> event;
Core::Timing::CoreTiming& core_timing;
Core::Memory::Memory& memory;
};
} // namespace Tools