-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer_components.h
117 lines (94 loc) · 3.04 KB
/
computer_components.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef JNP1_6_COMPUTER_COMPONENTS_H
#define JNP1_6_COMPUTER_COMPONENTS_H
#include <memory>
#include <unordered_map>
#include <string>
namespace computer {
// Class for memory storing data on which ooasm instructions operate.
class Memory {
public:
using word_t = int64_t;
using address_t = uint64_t;
using mem_size_t = uint64_t;
using id_t = std::string;
explicit Memory(mem_size_t size) : _size(size), mem(std::make_unique<mem_t>(size)) {
for (mem_size_t i = 0; i < _size; ++i) {
mem[i] = 0;
}
}
[[nodiscard]] word_t at(address_t i) const {
if (i >= size()) {
throw OutOfRangeMemoryAccessException();
}
return mem[i];
}
void set(address_t i, word_t new_val) {
if (i >= size()) {
throw OutOfRangeMemoryAccessException();
}
mem[i] = new_val;
}
[[nodiscard]] address_t get_variable_address(const id_t &var_name) const {
return vars.at(var_name);
}
void add_variable(const id_t &var_name, word_t word) {
if (variables_count == size()) {
throw TooManyVariablesException();
}
set(variables_count, word);
if (vars.count(var_name) == 0) {
vars[var_name] = variables_count;
}
variables_count++;
}
[[nodiscard]] mem_size_t size() const {
return _size;
}
void wipe() {
for (mem_size_t i = 0; i < size(); i++) {
mem[i] = 0;
}
vars.clear();
variables_count = 0;
}
private:
class OutOfRangeMemoryAccessException : public std::exception {
[[nodiscard]] const char *what() const noexcept override {
return "Address larger than size of memory cannot be accessed!";
}
};
class TooManyVariablesException : public std::exception {
[[nodiscard]] const char *what() const noexcept override {
return "Too many variable declarations!";
}
};
using mem_t = word_t[];
mem_size_t _size;
mem_size_t variables_count = 0;
std::unique_ptr<mem_t> mem;
std::unordered_map<id_t, mem_size_t> vars;
};
// Base class for processor, introduced in order to avoid circular file dependency.
class ProcessorAbstract {
public:
using flag_t = bool;
[[nodiscard]] flag_t getZF() const {
return ZF;
}
[[nodiscard]] flag_t getSF() const {
return SF;
}
void setZF(flag_t new_val) {
ZF = new_val;
}
void setSF(flag_t new_val) {
SF = new_val;
}
protected:
flag_t ZF, SF;
Memory &mem;
explicit ProcessorAbstract(Memory &memory)
: ZF(false), SF(false), mem(memory) {}
};
}
#endif //JNP1_6_COMPUTER_COMPONENTS_H