-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
81 lines (66 loc) · 1.42 KB
/
memory.cpp
File metadata and controls
81 lines (66 loc) · 1.42 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
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
#include "memory.h"
#include <iostream>
Memory::Memory() {
pc = 0;
}
Memory& Memory::operator>>(numtype& x) {
x = ops.at(0).at(pc);
++pc;
return (*this);
}
void
Memory::load(std::string file) {
std::ifstream read(file, std::ios::binary);
std::istreambuf_iterator<char> i(read), e;
std::vector<unsigned char> inf (i,e);
ops.push_back(std::vector<numtype>());
for(auto i = 0; i < inf.size(); i += 4) {
ops.at(0).push_back((inf.at(i) << 24) | (inf.at(i +1) << 16) | (inf.at(i +2) << 8) | (inf.at(i +3)));
}
}
Memory::Memory(std::string file) {
load(file);
pc = 0;
}
void
Memory::setPC(int x) {
pc = x;
}
int
Memory::getPC() {
return pc;
}
int
Memory::getMem(int array, int pos) {
return ops.at(array).at(pos);
}
std::vector<numtype>
Memory::getMem(int array) {
return ops.at(array);
}
void
Memory::setMem(int array, std::vector<numtype> vec) {
ops.at(array) = vec;
}
void
Memory::setMem(int array, int pos, int value) {
ops.at(array).at(pos) = value;
}
int
Memory::newArray(int size) {
std::vector<numtype> vec(size, 0);
// Try to find an empty array in the memory
for(int i = 1; i < ops.size(); ++i) {
if(ops.at(i).size() == 0) {
ops.at(i) = vec;
return i;
}
}
ops.push_back(vec);
return ops.size()-1;
}
void
Memory::clearArray(int pos) {
std::vector<numtype> vec(0,0);
ops.at(pos) = vec;
}