-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.cc
66 lines (52 loc) · 1.43 KB
/
memory.cc
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
#include "memory.h"
Memory::Memory(mem_t size)
: memory(size), mem_size(size), aliases_count(0), ZF(false), SF(false) {};
void Memory::set_val(const word_t *adr, word_t val) {
word_t ind = adr - (&memory[0]);
memory[ind] = val;
}
const word_t *Memory::get_val(const word_t *adr) const {
if (!valid_address(adr))
throw IndexOutOfBound();
return &memory[*adr];
}
void Memory::add_variable(const char *id, word_t val) {
if (aliases_count == mem_size)
throw NotEnoughSpaceForVariables();
memory[aliases_count] = val;
if (aliases.find(id) == aliases.end()) {
word_ptr el = std::make_shared<word_t>(word_t(aliases_count));
aliases.insert(std::make_pair(id, el));
}
aliases_count++;
}
const word_t *Memory::find_variable(const char *id) const {
if (aliases.find(id) == aliases.end())
throw InvalidIdentifier();
return aliases.find(id)->second.get();
}
void Memory::set_ZF(bool val) {
ZF = val;
}
bool Memory::get_ZF() const {
return ZF;
}
void Memory::set_SF(bool val) {
SF = val;
}
bool Memory::get_SF() const {
return SF;
}
void Memory::memory_dump(std::ostream &os) const {
for (auto x: memory) {
os << x << " ";
}
}
void Memory::memory_clear() {
std::fill(memory.begin(), memory.end(), 0);
aliases.clear();
aliases_count = 0;
}
bool Memory::valid_address(const word_t *adr) const {
return (*adr < (word_t)mem_size && *adr >= 0);
}