#include "set.h" #include using std::vector; using CacheSim::Block; using CacheSim::Set; using std::cout; using std::endl; Set::Set(short waySize, short tagSize){ this->waySize = waySize; this->emptySize = waySize; for(int i=0; iblocks.push_back(Block(tagSize)); } } bool Set::read(vector tag, short offset){ // HIT 的情況 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 的時間點 cout << "HIT: 在 " << i << " way 找到!" << endl; return true; } } // MISS 的情況,需要寫入 $ if(this->emptySize > 0){ for(int i=0; iwaySize; i++){ if(this->blocks[i].getValid() == false){ cout << "MISS: 在 " << i <<" way 寫入資料" << endl; this->blocks[i].update(tag); // 更新 tag this->blocks[i].access(); break; } } this->emptySize--; }else{ // 沒有空位,需要 replace short maxIndex = -1, maxTime = -1; short tempTime; // 找最久沒被 access 的 index for(int i=0; iwaySize; i++){ tempTime = this->blocks[i].getAccessTime(); if(tempTime > maxTime){ maxTime = tempTime; maxIndex = i; } } // update cout << "MISS: way 滿了,找到 " << maxIndex << " 在 " << maxTime << " 前更新" << endl; this->blocks[maxIndex].update(tag); this->blocks[maxIndex].access(); } this->countAccess(); return false; } void Set::countAccess(){ for(int i=0; iwaySize; i++){ this->blocks[i].countAccess(); } for(int i=0; i<20; i++){ cout << "=" ; } cout << "way 狀態"; for(int i=0; i<20; i++){ cout << "="; } cout<waySize; i++){ cout << this->blocks[i].getAccessTime() << " "; } cout<