summaryrefslogtreecommitdiffstats
path: root/src/core/memory/freezer.h
blob: b0c610039df1cb1d0aadc1f7a65cbe7f62b02b81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <atomic>
#include <mutex>
#include <optional>
#include <vector>
#include "common/common_types.h"

namespace Core::Timing {
class CoreTiming;
struct EventType;
} // namespace Core::Timing

namespace Memory {

// A class that will effectively freeze memory values.
class Freezer {
public:
    struct Entry {
        VAddr address;
        u32 width;
        u64 value;
    };

    explicit Freezer(Core::Timing::CoreTiming& core_timing);
    ~Freezer();

    // Enables or disables the entire memory freezer.
    void SetActive(bool 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:
    void FrameCallback(u64 userdata, s64 cycles_late);
    void FillEntryReads();

    std::atomic_bool active{false};

    mutable std::mutex entries_mutex;
    std::vector<Entry> entries;

    Core::Timing::EventType* event;
    Core::Timing::CoreTiming& core_timing;
};

} // namespace Memory