-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
66 lines (62 loc) · 1.49 KB
/
parse.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
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include "thumbsim.hpp"
void parseMem(ifstream & in, string type) {
string s;
unsigned int data, addr;
in >> s;
transform(s.begin(), s.end(), s.begin(), (int(*)(int))tolower);
transform(type.begin(), type.end(), type.begin(),
(int(*)(int))std::tolower);
if (s == "ta") {
s = "data"; // hack
}
if (s != type) {
cerr << "Bad type in parseMem: " << s << endl;
exit(1);
}
if (type == "instruction") {
addr = imem.getBase();
} else if (type == "data") {
addr = dmem.getBase();
}
in >> s;
transform(s.begin(), s.end(), s.begin(), (int(*)(int))tolower);
if (s != "memory") {
cerr << "Bad type (memory) in parseMem: " << s << endl;
exit(1);
}
while (in >> addr >> data) {
//cout << "Addr: " << hex << addr << " Data: " << data << endl;
// data is in native format
if (type == "instruction") {
imem.write(addr, data);
addr += 2;
} else if (type == "data") {
dmem.write(addr, data);
addr += 4;
} else {
cerr << "Bad type in while loop in parseMem: " << s << endl;
exit(1);
}
}
in.clear();
}
void parse(const char * file) {
ifstream in(file);
string s;
unsigned int addr;
in >> hex; // in takes hex numbers
in >> s;
if (s != "PC") {
exit(1);
}
in >> addr;
pc = addr;
parseMem(in, "instruction");
parseMem(in, "data");
}