OXYGENHUB
◆ main openmsx / src / memory / TrackedRam.hh
1.8 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef TRACKED_RAM_HH
2
#define TRACKED_RAM_HH
3
4
#include "Ram.hh"
5
6
#include <cstdint>
7
8
namespace openmsx {
9
10
// Ram with dirty tracking
11
class TrackedRam
12
{
13
public:
14
	// Most methods simply delegate to the internal 'ram' object.
15
	TrackedRam(const DeviceConfig& config, const std::string& name,
16
	           static_string_view description, size_t size)
17
		: ram(config, name, description, size, &writeSinceLastReverseSnapshot) {}
18
19
	TrackedRam(const XMLElement& xml, size_t size)
20
		: ram(xml, size) {}
21
22
	[[nodiscard]] size_t size() const {
23
		return ram.size();
24
	}
25
26
	[[nodiscard]] const std::string& getName() const {
27
		return ram.getName();
28
	}
29
30
	// Allow read via an explicit read() method or via backdoor access.
31
	[[nodiscard]] uint8_t read(size_t addr) const {
32
		return ram[addr];
33
	}
34
35
	[[nodiscard]] const uint8_t& operator[](size_t addr) const {
36
		return ram[addr];
37
	}
38
39
	[[nodiscard]] auto begin() const { return ram.begin(); }
40
	[[nodiscard]] auto end()   const { return ram.end(); }
41
42
	// Only allow write/clear via an explicit method.
43
	void write(size_t addr, uint8_t value) {
44
		writeSinceLastReverseSnapshot = true;
45
		ram[addr] = value;
46
	}
47
48
	void clear(uint8_t c = 0xff) {
49
		writeSinceLastReverseSnapshot = true;
50
		ram.clear(c);
51
	}
52
53
	// Some write operations are more efficient in bulk. For those this
54
	// method can be used. It will mark the ram as dirty on each
55
	// invocation, so the resulting pointer (although the same each time)
56
	// should not be reused for multiple (distinct) bulk write operations.
57
	[[nodiscard]] std::span<uint8_t> getWriteBackdoor() {
58
		writeSinceLastReverseSnapshot = true;
59
		return {ram.data(), size()};
60
	}
61
62
	template<typename Archive>
63
	void serialize(Archive& ar, unsigned version);
64
65
private:
66
	Ram ram;
67
	bool writeSinceLastReverseSnapshot = true;
68
};
69
70
} // namespace openmsx
71
72
#endif