66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
#include "simulator.h"
|
|
#include <sstream>
|
|
#include <bitset>
|
|
using CacheSim::Simulator;
|
|
using std::stringstream;
|
|
using std::bitset;
|
|
|
|
Simulator::Simulator(int machineBits, short setSize, short waySize, short tagSize, short replacePolicy){
|
|
this->setSize = setSize;
|
|
this->tagSize = tagSize;
|
|
this->machineBits = machineBits;
|
|
this->replacePolicy = replacePolicy;
|
|
for(int i=0; i<setSize; i++){
|
|
this->sets.push_back(Set(waySize, tagSize, replacePolicy));
|
|
}
|
|
}
|
|
|
|
tuple<bool, short, short> Simulator::read(string address){
|
|
vector<bool> addrBits = this->addressTranslate(address);
|
|
short index = this->getValue(addrBits, "index");
|
|
short offset = this->getValue(addrBits, "offset");
|
|
vector<bool> tag(addrBits.begin(), addrBits.begin()+this->tagSize);
|
|
|
|
tuple<bool, short> setResult = this->sets[index].read(tag, offset);
|
|
return std::make_tuple(
|
|
std::get<0>(setResult),
|
|
index,
|
|
std::get<1>(setResult)
|
|
);
|
|
}
|
|
|
|
vector<bool> Simulator::addressTranslate(string address){
|
|
vector<bool> ans;
|
|
ans.reserve(this->tagSize);
|
|
|
|
stringstream ss;
|
|
ss << std::hex << address;
|
|
unsigned long long number;
|
|
ss >> number;
|
|
|
|
bitset<64> bits(number);
|
|
|
|
for(int i=this->machineBits-1; i>=0; i--){
|
|
ans.push_back(bits[i]);
|
|
}
|
|
return ans;
|
|
}
|
|
|
|
short Simulator::getValue(vector<bool> addrBits, string type){
|
|
int start, end;
|
|
if(type == "index"){
|
|
start = this->tagSize + log2(this->setSize) - 1;
|
|
end = this->tagSize;
|
|
} else if(type == "offset"){
|
|
start = this->machineBits-1;
|
|
end = this->tagSize + log2(this->setSize);
|
|
}
|
|
|
|
short ans = 0 , base = 1;
|
|
for(int i=start; i>=end; i--){
|
|
ans += base * addrBits[i];
|
|
base *= 2;
|
|
}
|
|
|
|
return ans;
|
|
} |