-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.h
194 lines (190 loc) · 7.52 KB
/
simulator.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef H_SIM
#define H_SIM
#include <fstream>
// import modules
#include "branchPredictor.h"
// import stages
#include "common.h"
#include "fetch.h"
#include "decode.h"
#include "issue.h"
#include "execute.h"
class Simulator {
private:
// simulation
string inpFileName = "";
const int nf, ni, nw, nb, nr;
bool debugMode = false;
// system memory cache
unordered_map<int, double> memories;
unordered_map<string, int> branchLabelsTable;
int address = 0;
int cycleCount = 1;
int programCounter = 0;
int fStallCount = 0;
int dStallCount = 0;
int iStallCount = 0;
int eStallCount = 0;
// system instruction cache
deque<Instruction> instructions;
// fetch instruction queue (before decode stage)
deque<Instruction> fInstructionQueue;
// branch prediction
unordered_map<int, pair<int, int>> btb;
BranchPredictor * dbp;
// decode instruction queue (before issue stage)
deque<Instruction> dInstructionQueue;
// register renaming
unordered_map<string, string> mappingTable;
deque<string> freeList;
unordered_map<string, double> physicalRegs;
deque<unordered_map<string, string>> mappingTableHistory;
deque<deque<string>> freeListHistory;
// reservation stations
vector<RSStatus> rsUnitInt;
vector<RSStatus> rsUnitLoad;
vector<RSStatus> rsUnitStore;
vector<RSStatus> rsUnitFpAdd;
vector<RSStatus> rsUnitFpMult;
vector<RSStatus> rsUnitFpDiv;
vector<RSStatus> rsUnitBu;
deque<ROBStatus> rob;
unordered_map<string, double> cdb;
void initSimulatorPhysicalRegs();
public:
// stages
Fetch * f;
Decode * d;
Issue * i;
Execute * e;
void tokenizeMemory(char * inpStr);
void tokenizeInstruction(char * inpStr);
bool readInputFile(const char * inpFile);
int getMemoriesSize() {
return memories.size();
}
int getInstructionsSize() {
return instructions.size();
}
void tickCycleCount(bool isPositive) {
if (isPositive) cycleCount++;
else cycleCount--;
}
void cyclePipeline();
void cyclePipeline(int cycles);
void execute();
void execute(int inpCycleCount);
Simulator(
string inpFileName,
const int nf,
const int ni,
const int nw,
const int nb,
const int nr,
const bool debugMode
);
~Simulator();
// debug
void printSimulatorMemories() {
cout << "\n\nMEMORIES CACHE (size=" << this->memories.size() << ")\n\n";
for(const auto& elem : memories) {
cout << " " << elem.first << " " << elem.second << "\n";
}
}
void printSimulatorInstructions();
void printSimulatorBranchLabelsTable() {
cout << "\n\nBRANCH LABELS TABLE (size=" << this->branchLabelsTable.size() << ")\n";
for (const auto& elem : branchLabelsTable) {
cout << elem.first << " " << elem.second << "\n";
}
}
void printCurrentAddress() {
cout << "\n\nAddress = " << address << "\n";
}
void printSimulatorCurrentCycleCount() {
cout << "\n\ncycleCount = " << cycleCount << "\n";
}
void printSimulatorFetchInstructionQueue();
void printSimulatorDecodeInstructionQueue();
void printSimulatorBtbMap() {
cout << "\n\nBTB (size=" << this->btb.size() << ")\n";
for (auto & kv : btb) {
cout << "\naddress=" << kv.first <<
"\n branch translation={" << kv.second.first << ", " << kv.second.second <<
"}\n";
}
}
void printSimulatorPhysicalRegs() {
cout << "\n\nPHYSICAL REGS (size=" << this->physicalRegs.size() << ")\n";
for (auto & kv : physicalRegs) {
cout << "\n name=" << kv.first <<
"\n val=" << kv.second <<
"\n";
}
}
void printSimulatorMappingTable() {
cout << "\n\nMAPPING TABLE (size=" << this->mappingTable.size() << ")\n\n";
for (const auto& elem : mappingTable) {
cout << " " << elem.first << " " << elem.second << "\n";
}
}
void printSimulatorFreeList(){
cout << "\n\nFREE LIST (size=" << this->freeList.size() << ")\n";
for (const auto& elem : freeList) {
cout << "\n" << elem << "\n";
}
}
void printSimulatorMappingTableHistory() {
cout << "\n\nMAPPING TABLE HISTORY (size=" << this->mappingTableHistory.size() << ")\n";
int i = 0;
for (const auto& mt : mappingTableHistory) {
cout << "MAPPING TABLE index " << i++ << "\n";
for (const auto& elem : mt) {
cout << elem.first << " " << elem.second << "\n";
}
}
}
void printSimulatorFreeListHistory() {
cout << "\n\nFREE LIST HISTORY (size=" << this->freeListHistory.size() << ")\n";
int i = 0;
for (const auto& fl : freeListHistory) {
cout << "FREE LIST index " << i++ << "\n";
for (const auto& elem : fl) {
cout << "\n" << elem << "\n";
}
}
}
void printSimulatorROB() {
cout << "\n\nROB (size=" << this->rob.size() << ")\n";
for (auto & entry : this->rob) {
printROBStatus(entry);
}
}
void printSimulatorReservationStationUnit(vector<RSStatus> inpReservationStationUnit) {
for (auto & rs : inpReservationStationUnit)
printRSStatus(rs);
}
void printSimulatorReservationStations() {
cout << "\n\nRESERVATION STATION UNIT=INT (size=" << this->rsUnitInt.size() << ")\n";
printSimulatorReservationStationUnit(this->rsUnitInt);
cout << "\n\nRESERVATION STATION UNIT=LOAD (size=" << this->rsUnitLoad.size() << ")\n";
printSimulatorReservationStationUnit(this->rsUnitLoad);
cout << "\n\nRESERVATION STATION UNIT=STORE (size=" << this->rsUnitStore.size() << ")\n";
printSimulatorReservationStationUnit(this->rsUnitStore);
cout << "\n\nRESERVATION STATION UNIT=FPADD (size=" << this->rsUnitFpAdd.size() << ")\n";
printSimulatorReservationStationUnit(this->rsUnitFpAdd);
cout << "\n\nRESERVATION STATION UNIT=FPMULT (size=" << this->rsUnitFpMult.size() << ")\n";
printSimulatorReservationStationUnit(this->rsUnitFpMult);
cout << "\n\nRESERVATION STATION UNIT=FPDIV (size=" << this->rsUnitFpDiv.size() << ")\n";
printSimulatorReservationStationUnit(this->rsUnitFpDiv);
cout << "\n\nRESERVATION STATION UNIT=BU (size=" << this->rsUnitBu.size() << ")\n";
printSimulatorReservationStationUnit(this->rsUnitBu);
}
void printSimulatorStallCounts() {
cout << "\n\nFETCH STAGE STALLS= " << fStallCount << "\n" <<
"\nDECODE STAGE STALLS= " << dStallCount << "\n" <<
"\nISSUE STAGE STALLS= " << iStallCount << "\n" <<
"\nEXECUTE STAGE STALLS= " << eStallCount << "\n";
}
};
#endif