openMSX
TrackedRam.hh
Go to the documentation of this file.
1 #ifndef TRACKED_RAM_HH
2 #define TRACKED_RAM_HH
3 
4 #include "Ram.hh"
5 
6 namespace openmsx {
7 
8 // Ram with dirty tracking
9 class TrackedRam
10 {
11 public:
12  // Most methods simply delegate to the internal 'ram' object.
13  TrackedRam(const DeviceConfig& config, const std::string& name,
14  const std::string& description, unsigned size)
15  : ram(config, name, description, size) {}
16 
17  TrackedRam(const XMLElement& xml, unsigned size)
18  : ram(xml, size) {}
19 
20  unsigned getSize() const {
21  return ram.getSize();
22  }
23 
24  const std::string& getName() const {
25  return ram.getName();
26  }
27 
28  // Allow read via an explicit read() method or via backdoor access.
29  byte read(unsigned addr) const {
30  return ram[addr];
31  }
32 
33  const byte& operator[](unsigned addr) const {
34  return ram[addr];
35  }
36 
37  // Only allow write/clear via an explicit method.
38  void write(unsigned addr, byte value) {
39  writeSinceLastReverseSnapshot = true;
40  ram[addr] = value;
41  }
42 
43  void clear(byte c = 0xff) {
44  writeSinceLastReverseSnapshot = true;
45  ram.clear(c);
46  }
47 
48  // Some write operations are more efficient in bulk. For those this
49  // method can be used. It will mark the ram as dirty on each
50  // invocation, so the resulting pointer (although the same each time)
51  // should not be reused for multiple (distinct) bulk write operations.
53  writeSinceLastReverseSnapshot = true;
54  return &ram[0];
55  }
56 
57  template<typename Archive>
58  void serialize(Archive& ar, unsigned version);
59 
60 private:
61  Ram ram;
62  bool writeSinceLastReverseSnapshot = true;
63 };
64 
65 } // namespace openmsx
66 
67 #endif
TrackedRam(const XMLElement &xml, unsigned size)
Definition: TrackedRam.hh:17
const std::string & getName() const
Definition: Ram.cc:95
byte read(unsigned addr) const
Definition: TrackedRam.hh:29
byte * getWriteBackdoor()
Definition: TrackedRam.hh:52
void clear(byte c=0xff)
Definition: TrackedRam.hh:43
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
unsigned getSize() const
Definition: TrackedRam.hh:20
TrackedRam(const DeviceConfig &config, const std::string &name, const std::string &description, unsigned size)
Definition: TrackedRam.hh:13
void clear(byte c=0xff)
Definition: Ram.cc:51
const std::string & getName() const
Definition: TrackedRam.hh:24
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void serialize(Archive &ar, unsigned version)
Definition: TrackedRam.cc:7
const byte & operator[](unsigned addr) const
Definition: TrackedRam.hh:33
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.hh:62
unsigned getSize() const
Definition: Ram.hh:33
void write(unsigned addr, byte value)
Definition: TrackedRam.hh:38