-
Notifications
You must be signed in to change notification settings - Fork 3
/
mips_simulator.hpp
69 lines (55 loc) · 1.99 KB
/
mips_simulator.hpp
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
#ifndef MIPS_SIMULATOR_HPP
#define MIPS_SIMULATOR_HPP
#include "include_define.hpp"
#include "mips_text_parser.hpp"
#include "mips_entry_processor.hpp"
#include "mips_pipeline.hpp"
using namespace std;
class MIPS_Simulator {
private:
// memory management
int textMemoryTop, staticDataMemoryTop, dynamicDataMemoryTop;
int mainLabelAddr;
// registers and memory space
Word registers[registerNum];
byte memorySpace[maxMemoryByte];
// mips source text
string mipsText;
vector<Entry> entries;
Word ®(const string regName) {
#ifdef DEBUG_REGISTER_NAME
if (MIPS_Entry_Processor::instance().registerIdx.find(regName) == MIPS_Entry_Processor::instance().registerIdx.end()) {
cout << "Invalid register name: " << regName << endl;
system("pause");
}
#endif
return registers[MIPS_Entry_Processor::instance().registerIdx[regName]];
}
MIPS_Simulator() = default;
MIPS_Simulator(MIPS_Simulator const&);
MIPS_Simulator& operator=(MIPS_Simulator const&);
public:
static MIPS_Simulator& instance() {
static MIPS_Simulator ins;
return ins;
}
void SimulatorInit() {
MIPS_Entry_Processor::instance().RegisterIdxInit();
MIPS_Text_Parser::instance().TokenTypeInit();
reg("$sp") = maxMemoryByte;
textMemoryTop = 0;
for (int i = 0; i < registerNum; ++i) {
if (i == 29) continue; // $sp
registers[i] = 0;
}
}
void ProcessMIPSText(const string &str) {
mipsText = str;
MIPS_Text_Parser::instance().SplitToEntries(mipsText, entries);
MIPS_Entry_Processor::instance().ProcessEntries(entries, memorySpace, textMemoryTop, staticDataMemoryTop, dynamicDataMemoryTop, mainLabelAddr);
}
void RunPipeline() {
MIPS_Pipeline::instance().Run(memorySpace, registers, textMemoryTop, dynamicDataMemoryTop, mainLabelAddr);
}
};
#endif // MIPS_SIMULATOR_HPP