#include "simulator.h" #include #include using CacheSim::Simulator; using std::stringstream; using std::bitset; Simulator::Simulator(int machineBits, short setSize, short waySize, short tagSize){ this->setSize = setSize; this->tagSize = tagSize; this->machineBits = machineBits; for(int i=0; isets.push_back(Set(waySize, tagSize)); } } bool Simulator::read(string address){ vector addrBits = this->addressTranslate(address); for(int i=0; igetValue(addrBits, "index"); short offset = this->getValue(addrBits, "offset"); vector tag; tag.assign(addrBits.begin(), addrBits.begin()+this->tagSize); std::cout<<"更新 index: "<sets[index].read(tag, offset); // return 0; } bool Simulator::write(string address){ return true; } vector Simulator::addressTranslate(string address){ vector 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 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; }