-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheSim.cpp
More file actions
209 lines (183 loc) · 3.42 KB
/
cacheSim.cpp
File metadata and controls
209 lines (183 loc) · 3.42 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "cacheSim.h"
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
cacheSim::cacheSim()
{
numSets = 0;
numBlocks = 0;
blockSize = 0;
setSize = 0;
totalCount = 0;
hitCount = 0;
}
cacheSim::cacheSim(unsigned int& numSets, unsigned int& numBlocks, unsigned int& blockSize)
{
this->numSets = numSets;
this->numBlocks = numBlocks;
this->blockSize = blockSize;
this->setSize = numSets / numBlocks;
this->setFieldSize = log2(numSets);
this->offsetSize = log2(blockSize);
this->totalCount = 0;
this->hitCount = 0;
for (unsigned int i = 0; i < numSets; i++)
{
SetCache newSet;
newSet.setIdentity = i;
sets.push_back(newSet);
}
}
unsigned int cacheSim::b2Dec(string bin)
{
string num = bin;
unsigned int dec_value = 0;
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
string cacheSim::hex2B(string& hexNum)
{
string decodedNum;
for (unsigned int i = 2; i < hexNum.size(); i++)
{
char hexDigit = hexNum.at(i);
switch (hexDigit)
{
case 'a':
case 'A':
decodedNum += "1010";
break;
case 'b':
case 'B':
decodedNum += "1011";
break;
case 'c':
case 'C':
decodedNum += "1100";
break;
case 'd':
case 'D':
decodedNum += "1101";
break;
case 'e':
case 'E':
decodedNum += "1110";
break;
case 'f':
case 'F':
decodedNum += "1111";
break;
case '1':
decodedNum += "0001";
break;
case '2':
decodedNum += "0010";
break;
case '3':
decodedNum += "0011";
break;
case '4':
decodedNum += "0100";
break;
case '5':
decodedNum += "0101";
break;
case '6':
decodedNum += "0110";
break;
case '7':
decodedNum += "0111";
break;
case '8':
decodedNum += "1000";
break;
case '9':
decodedNum += "1001";
break;
default:
decodedNum += "0000";
}
}
return decodedNum;
}
void cacheSim::processAddress(char& option, string& address)
{
string binAddress = hex2B(address);
string tag;
string setField;
unsigned int setDec;
unsigned int tagSize = 32 - (setFieldSize + offsetSize); //Gets tag size
for (unsigned int i = 0; i < tagSize; i++) //Finds tag from after 0x to right before
{ //The offset
tag.push_back(binAddress.at(i));
}
if (setFieldSize == 0)
{
setField = "0";
}
for (unsigned int i = tagSize; i < 32 - offsetSize; i++)
{
setField.push_back(binAddress.at(i));
}
setDec = b2Dec(setField);
if (option == 's')
{
store(setDec, tag);
}
else if (option == 'l')
{
totalCount++;
bool hit = find(setDec, tag);
if (!hit)
{
store(setDec, tag);
}
}
}
bool cacheSim::find(unsigned int& set, string& tag) //Searching algorithm.
{
bool verify = false;
for (unsigned int i = 0; i < sets.at(set).blocks.size(); i++)
{
if (tag == sets.at(set).blocks[i])
{
hitCount++;
return true;
}
}
return verify;
}
void cacheSim::store(unsigned int& set, string & tag) //Loading algorithm, pretty simple for FA
{
for (int i = 0; i < numSets; i++)
{
if (sets.at(i).setIdentity == set)
{
if (sets.at(i).blocks.size() == numBlocks * (blockSize / 4)) //If full, pop out first to enter
{
sets.at(i).blocks.pop_front();
}
sets.at(i).blocks.push_back(tag);
break;
}
}
}
float cacheSim::getHitRate()
{
if (totalCount == 0)
{
return 0;
}
return (float)hitCount / totalCount;
}
int cacheSim::getTotalCount()
{
return totalCount;
}