-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.cpp
58 lines (49 loc) · 1.77 KB
/
os.cpp
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
#include "OperatingSystem/Utility.h"
#include "OperatingSystem/RegisterBank.h"
#include "OperatingSystem/Processor.h"
string replaceString(string subject, const std::string &search, const string &replace) {
ulong pos = 0;
while ((pos = subject.find(search, pos)) != string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
int main() {
srand(time(NULL));
ifstream inFile("../stdio/input.txt");
ofstream outFile("../stdio/output.txt");
ofstream syslog("../stdio/syslog.txt");
bool instructionFlag = false;
int numLine = 0;
string line;
RegisterBank registerBank{};
Processor processor(®isterBank, &inFile, &outFile, &syslog);
//read till EoF
while (getline(inFile, line)) {
if (regex_match(line, regex("(\\$AMJ)(.*)"))) {
processor.init(line);
registerBank.initialisePageTable();
instructionFlag = true;
} else if (regex_match(line, regex("(\\$DTA)(.*)"))) {
processor.run();
instructionFlag = false;
numLine = 0;
} else if (regex_match(line, regex("(\\$END)(.*)"))) {
instructionFlag = false;
outFile << endl;
} else if (instructionFlag) {
line = replaceString(line, "H", "H000");
int realAddress = registerBank.getRealAddress(numLine);
if(realAddress == -1) {
registerBank.allocateMemory(numLine);
realAddress = registerBank.getRealAddress(numLine);
}
memcpy(®isterBank.memoryRegisters[realAddress][0], line.c_str(), line.size());
numLine += ceil(line.size() / 4);
}
}
inFile.close();
outFile.close();
return 0;
}