From 7eff1a23bf4b17f064a6d900c9536608eadd7b0d Mon Sep 17 00:00:00 2001 From: Ting-Jun Wang Date: Fri, 31 Dec 2021 21:53:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=AF=E6=A8=A1=E6=93=AC=20read?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- block.cpp | 32 +++++++++++++++++++++- block.h | 8 ++++++ main.cpp | 1 + set.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ set.h | 4 +++ simulator.cpp | 28 ++++++++++++------- simulator.h | 3 +-- 7 files changed, 138 insertions(+), 12 deletions(-) diff --git a/block.cpp b/block.cpp index 5de18a4..438d4e4 100644 --- a/block.cpp +++ b/block.cpp @@ -1,7 +1,37 @@ #include "block.h" +using CacheSim::Block; -CacheSim::Block::Block(short tagSize) { +Block::Block(short tagSize) { this->valid = false; this->tag.assign(tagSize, 0); this->tagSize = tagSize; + this->recentlyAccess = 0; +} + +bool Block::getValid(){ + return this->valid; +} + +vector Block::getTag(){ + return this->tag; +} + +void Block::update(vector tag){ + this->valid = true; + this->tag.clear(); + this->tag.assign(tag.begin(), tag.end()); +} + +void Block::countAccess(){ + if(this->valid){ + this->recentlyAccess++; + } +} + +void Block::access(){ + this->recentlyAccess = 0; +} + +short Block::getAccessTime(){ + return this->recentlyAccess; } \ No newline at end of file diff --git a/block.h b/block.h index fa56d5e..ead26e3 100644 --- a/block.h +++ b/block.h @@ -8,10 +8,18 @@ namespace CacheSim{ bool valid; vector tag; short tagSize; + short recentlyAccess; // data; public: Block(){}; Block(short tagSize); + bool getValid(); + vector getTag(); + short getAccessTime(); + void update(vector tag); + void countAccess(); + void access(); + }; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index e6de4f1..36eae17 100644 --- a/main.cpp +++ b/main.cpp @@ -30,6 +30,7 @@ int main(){ while(std::cin >> action >> address){ std::cout << "action: "<< action << " address: " << address << std::endl; sim.read(address); + std::cout << std::endl << std::endl; } } diff --git a/set.cpp b/set.cpp index 73e35ea..384eb89 100644 --- a/set.cpp +++ b/set.cpp @@ -1,13 +1,87 @@ #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<getIndex(addrBits); - // std::cout<getValue(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){ @@ -46,15 +50,21 @@ vector Simulator::addressTranslate(string address){ return ans; } -short Simulator::getIndex(vector addrBits){ +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; - // std::cout<<"hi"<tagSize+log2(this->setSize)-1); i>=this->tagSize; i--){ - // std::cout<=end; i--){ ans += base * addrBits[i]; base *= 2; } - // std::cout<<"bye"< addressTranslate(string address); - short getIndex(vector addrBits); - + short getValue(vector addrBits, string type); }; } \ No newline at end of file