-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMemory.cpp
More file actions
executable file
·51 lines (42 loc) · 1.08 KB
/
Memory.cpp
File metadata and controls
executable file
·51 lines (42 loc) · 1.08 KB
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
#include <cassert>
#include "Memory.hpp"
using namespace std;
Memory::Memory(unsigned size)
{
_mem.resize(size);
}
byte Memory::Read8(unsigned offset) const
{
return _mem.at(offset);
}
uint16_t Memory::Read16(unsigned offset) const
{
return (0xFFFF & ((uint16_t)(_mem.at(offset)) << 8)) | _mem.at(offset+1);
}
void Memory::Write8(byte val, unsigned offset)
{
_delta.push_back(PrevVal(offset, _mem.at(offset)));
_mem.at(offset) = val;
}
void Memory::Write16(uint16_t val, unsigned offset)
{
_delta.push_back(PrevVal(offset, _mem.at(offset)));
_delta.push_back(PrevVal(offset+1, _mem.at(offset+1)));
_mem.at(offset) = (byte)(val >> 8);
_mem.at(offset+1) = (byte)(val);
}
void Memory::TakeSnapshot()
{
_history.push(_delta);
_delta.clear();
}
void Memory::Rewind()
{
assert(_delta.empty()); // Shouldn't be in the middle of a state
Delta& changes = _history.top();
for (Delta::const_iterator it = changes.begin(); it != changes.end(); ++it)
{
_mem.at(it->Offset) = it->Value;
}
_history.pop(); // Pop off current state
}