-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
150 lines (135 loc) · 5.46 KB
/
common.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
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
#include "common.h"
bool operator==(Instruction const &lhs, Instruction const &rhs) {
return lhs.address == rhs.address and
lhs.opcode == rhs.opcode and
lhs.rd == rhs.rd and
lhs.rs == rhs.rs and
lhs.rt == rhs.rt and
lhs.immediate == rhs.immediate;
}
bool operator!=(Instruction const &lhs, Instruction const &rhs) {
return lhs.address != rhs.address or
lhs.opcode != rhs.opcode or
lhs.rd != rhs.rd or
lhs.rs != rhs.rs or
lhs.rt != rhs.rt or
lhs.immediate != rhs.immediate;
}
string toUpper(string inpString) {
string temp = inpString;
transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
return temp;
}
InstructionType getInstructionTypeFromString(const string inpString) {
if (inpString == "FLD") return InstructionType::FLD;
else if (inpString == "FSD") return InstructionType::FSD;
else if (inpString == "ADD") return InstructionType::ADD;
else if (inpString =="ADDI") return InstructionType::ADDI;
else if (inpString =="SLT") return InstructionType::SLT;
else if (inpString =="FADD") return InstructionType::FADD;
else if (inpString =="FSUB") return InstructionType::FSUB;
else if (inpString =="FMUL") return InstructionType::FMUL;
else if (inpString =="FDIV") return InstructionType::FDIV;
else if (inpString =="BNE") return InstructionType::BNE;
else throw invalid_argument(
"common_getInstructionTypeFromString: the provided string was not recognized as an InstructionType. Check to ensure that input parameters are not null or unequal"
);
};
string getStringFromInstructionType(const InstructionType inpInstrType) {
switch (inpInstrType) {
case InstructionType::FLD: return "FLD";
case InstructionType::FSD: return "FSD";
case InstructionType::ADD: return "ADD";
case InstructionType::ADDI: return "ADDI";
case InstructionType::SLT: return "SLT";
case InstructionType::FADD: return "FADD";
case InstructionType::FSUB: return "FSUB";
case InstructionType::FMUL: return "FMUL";
case InstructionType::FDIV: return "FDIV";
case InstructionType::BNE: return "BNE";
default: throw invalid_argument(
"common_getStringFromInstructionType: the provided InstructionType was not recognized. Check to ensure that input parameters are not null or invalid"
);
}
};
void printInstruction(Instruction inpInstr) {
cout << "\n Instruction= {" <<
"\n address=" << inpInstr.address <<
"\n opcode=" << getStringFromInstructionType(inpInstr.opcode) << " (" << inpInstr.opcode << ")" <<
"\n rd=" << inpInstr.rd <<
"\n rs=" << inpInstr.rs <<
"\n rt=" << inpInstr.rt <<
"\n immediate=" << inpInstr.immediate <<
"\n }\n";
}
void printInstructions(deque<Instruction> inpInstrs) {
for(int i = 0; i < inpInstrs.size(); i++) {
printInstruction(inpInstrs[i]);
}
}
bool containsOffset(const string inpString) {
int pos1 = inpString.find("(");
int pos2 = inpString.find(")");
return (pos1 != -1 && pos2 != -1);
}
int getOffset(const string inpString) {
int offset = 0;
int pos1 = inpString.find("(");
if (pos1 != -1) {
string offsetStr = inpString.substr(0, pos1);
offset = stoi(offsetStr);
return offset;
}
return offset;
}
string trimOffset(const string inpString) {
string newStr = inpString;
int pos1 = inpString.find("(");
int pos2 = inpString.find(")");
if (pos1 != -1 && pos2 != -1) {
newStr = inpString.substr(pos1 + 1, pos2-pos1-1);
return newStr;
}
return newStr;
}
string appendOffset(const string inpString, const int offset) {
return to_string(offset) + "(" + inpString + ")";
}
void printROBStatus(ROBStatus inpEntry) {
cout << " REORDER BUFFER ENTRY =\n" <<
" entry name=" << inpEntry.entryName << "\n" <<
" busy=" << inpEntry.busy << "\n" <<
" status=" << inpEntry.status << "\n" <<
" destination=" << inpEntry.instruction.rd << "\n" <<
" value=" << inpEntry.value << "\n" <<
" countLatency=" << inpEntry.countLatency;
printInstruction(inpEntry.instruction);
}
void printRSStatus(RSStatus inpRs) {
cout << " RESERVATION STATION ENTRY =\n" <<
" busy=" << inpRs.busy << "\n" <<
" vj=" << inpRs.vj << "\n" <<
" vk=" << inpRs.vk << "\n" <<
" qj=" << inpRs.qj << "\n" <<
" qk=" << inpRs.qk << "\n" <<
" destination=" << inpRs.qk << "\n" <<
" a=" << inpRs.a << "\n";
printInstruction(inpRs.instruction);
}
string getRSNameFromInstructionType(InstructionType inpInstrType) {
switch (inpInstrType) {
case InstructionType::FLD: return "LOAD";
case InstructionType::FSD: return "STORE";
case InstructionType::ADD: return "INT";
case InstructionType::ADDI: return "INT";
case InstructionType::SLT: return "INT";
case InstructionType::FADD: return "FPADD";
case InstructionType::FSUB: return "FPADD";
case InstructionType::FMUL: return "FPMULT";
case InstructionType::FDIV: return "FPDIV";
case InstructionType::BNE: return "BU";
default: throw invalid_argument(
"common_getRSNameFromInstructionType: the provided InstructionType was not recognized. Check to ensure that input parameters are not null or invalid"
);
}
}