diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f12ba0c --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +CC = g++ + +all: sim + +sim: main.cpp block.cpp set.cpp simulator.cpp + $(CC) main.cpp block.cpp set.cpp simulator.cpp -o sim + +clean: + rm sim diff --git a/main.cpp b/main.cpp index 36eae17..4548282 100644 --- a/main.cpp +++ b/main.cpp @@ -27,10 +27,25 @@ int main(){ CacheSim::Simulator sim(machineBits, setSize, waySize, tagSize); std::string action, address; + bool status; + std::vector> result; while(std::cin >> action >> address){ std::cout << "action: "<< action << " address: " << address << std::endl; - sim.read(address); + status = sim.read(address); + result.push_back(std::make_pair(address, status)); std::cout << std::endl << std::endl; } - + + int allCount = 0, hitCount = 0; + for(int i=0; i tag, short offset){ // HIT 的情況 + short baseAccessTime; // 被替換掉的 block ,它的 access time for(int i=0; iwaySize; i++){ if(this->blocks[i].getValid() && this->blocks[i].getTag() == tag){ // valid bit == true 且 tag 相同 - this->blocks[i].access(); // 重設 access 的時間點 - this->countAccess(); // 更新計算所有 way access 的時間點 + baseAccessTime= this->blocks[i].getAccessTime(); + this->blocks[i].access(); + this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點 cout << "HIT: 在 " << i << " way 找到!" << endl; return true; } @@ -35,6 +37,7 @@ bool Set::read(vector tag, short offset){ cout << "MISS: 在 " << i <<" way 寫入資料" << endl; this->blocks[i].update(tag); // 更新 tag this->blocks[i].access(); + baseAccessTime = this->waySize; // 每個都要更新 break; } } @@ -43,7 +46,7 @@ bool Set::read(vector tag, short offset){ short maxIndex = -1, maxTime = -1; short tempTime; - // 找最久沒被 access 的 index + // 找最久沒被 access 的 block index for(int i=0; iwaySize; i++){ tempTime = this->blocks[i].getAccessTime(); if(tempTime > maxTime){ @@ -54,17 +57,28 @@ bool Set::read(vector tag, short offset){ // update cout << "MISS: way 滿了,找到 " << maxIndex << " 在 " << maxTime << " 前更新" << endl; + baseAccessTime = this->blocks[maxIndex].getAccessTime(); this->blocks[maxIndex].update(tag); this->blocks[maxIndex].access(); + + for(int i=0; iwaySize; i++){ + vector t = this->blocks[i].getTag(); + for(int j=0; jcountAccess(); + this->countAccess(baseAccessTime); return false; } -void Set::countAccess(){ +void Set::countAccess(short baseAccessTime){ for(int i=0; iwaySize; i++){ - this->blocks[i].countAccess(); + if(this->blocks[i].getAccessTime() < baseAccessTime){ // 小於 basetime 就 ++,補足 basetime 缺的 hole + this->blocks[i].countAccess(); + } } for(int i=0; i<20; i++){ cout << "=" ; diff --git a/set.h b/set.h index 6559087..e96a276 100644 --- a/set.h +++ b/set.h @@ -15,7 +15,7 @@ namespace CacheSim{ public: Set(short waySize, short tagSize); bool read(vector tag, short offset); - void countAccess(); + void countAccess(short baseAccessTime); }; } \ No newline at end of file diff --git a/simulator.cpp b/simulator.cpp index 13264e6..a148a02 100644 --- a/simulator.cpp +++ b/simulator.cpp @@ -22,11 +22,9 @@ bool Simulator::read(string address){ std::cout << std::endl; short index = this->getValue(addrBits, "index"); short offset = this->getValue(addrBits, "offset"); - vector tag; - tag.assign(addrBits.begin(), addrBits.begin()+this->tagSize); + vector tag(addrBits.begin(), addrBits.begin()+this->tagSize); std::cout<<"更新 index: "<sets[index].read(tag, offset); - // return 0; } bool Simulator::write(string address){