feat: 可模擬 read
This commit is contained in:
parent
9a545ccc37
commit
7eff1a23bf
32
block.cpp
32
block.cpp
@ -1,7 +1,37 @@
|
|||||||
#include "block.h"
|
#include "block.h"
|
||||||
|
using CacheSim::Block;
|
||||||
|
|
||||||
CacheSim::Block::Block(short tagSize) {
|
Block::Block(short tagSize) {
|
||||||
this->valid = false;
|
this->valid = false;
|
||||||
this->tag.assign(tagSize, 0);
|
this->tag.assign(tagSize, 0);
|
||||||
this->tagSize = tagSize;
|
this->tagSize = tagSize;
|
||||||
|
this->recentlyAccess = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Block::getValid(){
|
||||||
|
return this->valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<bool> Block::getTag(){
|
||||||
|
return this->tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Block::update(vector<bool> 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;
|
||||||
}
|
}
|
||||||
8
block.h
8
block.h
@ -8,10 +8,18 @@ namespace CacheSim{
|
|||||||
bool valid;
|
bool valid;
|
||||||
vector<bool> tag;
|
vector<bool> tag;
|
||||||
short tagSize;
|
short tagSize;
|
||||||
|
short recentlyAccess;
|
||||||
// data;
|
// data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Block(){};
|
Block(){};
|
||||||
Block(short tagSize);
|
Block(short tagSize);
|
||||||
|
bool getValid();
|
||||||
|
vector<bool> getTag();
|
||||||
|
short getAccessTime();
|
||||||
|
void update(vector<bool> tag);
|
||||||
|
void countAccess();
|
||||||
|
void access();
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
1
main.cpp
1
main.cpp
@ -30,6 +30,7 @@ int main(){
|
|||||||
while(std::cin >> action >> address){
|
while(std::cin >> action >> address){
|
||||||
std::cout << "action: "<< action << " address: " << address << std::endl;
|
std::cout << "action: "<< action << " address: " << address << std::endl;
|
||||||
sim.read(address);
|
sim.read(address);
|
||||||
|
std::cout << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
74
set.cpp
74
set.cpp
@ -1,13 +1,87 @@
|
|||||||
#include "set.h"
|
#include "set.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
using CacheSim::Block;
|
using CacheSim::Block;
|
||||||
using CacheSim::Set;
|
using CacheSim::Set;
|
||||||
|
|
||||||
|
using std::cout;
|
||||||
|
using std::endl;
|
||||||
|
|
||||||
Set::Set(short waySize, short tagSize){
|
Set::Set(short waySize, short tagSize){
|
||||||
this->waySize = waySize;
|
this->waySize = waySize;
|
||||||
|
this->emptySize = waySize;
|
||||||
|
|
||||||
for(int i=0; i<waySize; i++){
|
for(int i=0; i<waySize; i++){
|
||||||
this->blocks.push_back(Block(tagSize));
|
this->blocks.push_back(Block(tagSize));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Set::read(vector<bool> tag, short offset){
|
||||||
|
// HIT 的情況
|
||||||
|
for(int i=0; i<this->waySize; 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; i<this->waySize; 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; i<this->waySize; 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; i<this->waySize; i++){
|
||||||
|
this->blocks[i].countAccess();
|
||||||
|
}
|
||||||
|
for(int i=0; i<20; i++){
|
||||||
|
cout << "=" ;
|
||||||
|
}
|
||||||
|
cout << "way 狀態";
|
||||||
|
for(int i=0; i<20; i++){
|
||||||
|
cout << "=";
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
|
|
||||||
|
for(int i=0; i<this->waySize; i++){
|
||||||
|
cout << this->blocks[i].getAccessTime() << " ";
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
|
|
||||||
|
for(int i=0; i<45; i++){
|
||||||
|
cout << "=";
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
}
|
}
|
||||||
4
set.h
4
set.h
@ -9,9 +9,13 @@ namespace CacheSim{
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
short waySize;
|
short waySize;
|
||||||
|
short emptySize;
|
||||||
vector<Block> blocks;
|
vector<Block> blocks;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Set(short waySize, short tagSize);
|
Set(short waySize, short tagSize);
|
||||||
|
bool read(vector<bool> tag, short offset);
|
||||||
|
void countAccess();
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -19,10 +19,14 @@ bool Simulator::read(string address){
|
|||||||
for(int i=0; i<addrBits.size(); i++){
|
for(int i=0; i<addrBits.size(); i++){
|
||||||
std::cout << addrBits[i] << " ";
|
std::cout << addrBits[i] << " ";
|
||||||
}
|
}
|
||||||
// std::cout << std::endl << addrBits.size() << std::endl;
|
std::cout << std::endl;
|
||||||
short index = this->getIndex(addrBits);
|
short index = this->getValue(addrBits, "index");
|
||||||
// std::cout<<index<<std::endl;
|
short offset = this->getValue(addrBits, "offset");
|
||||||
return true;
|
vector<bool> tag;
|
||||||
|
tag.assign(addrBits.begin(), addrBits.begin()+this->tagSize);
|
||||||
|
std::cout<<"更新 index: "<<index<<std::endl;
|
||||||
|
return this->sets[index].read(tag, offset);
|
||||||
|
// return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Simulator::write(string address){
|
bool Simulator::write(string address){
|
||||||
@ -46,15 +50,21 @@ vector<bool> Simulator::addressTranslate(string address){
|
|||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
short Simulator::getIndex(vector<bool> addrBits){
|
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;
|
short ans = 0 , base = 1;
|
||||||
// std::cout<<"hi"<<std::endl;
|
for(int i=start; i>=end; i--){
|
||||||
for(int i=(this->tagSize+log2(this->setSize)-1); i>=this->tagSize; i--){
|
|
||||||
// std::cout<<i<<std::endl;
|
|
||||||
ans += base * addrBits[i];
|
ans += base * addrBits[i];
|
||||||
base *= 2;
|
base *= 2;
|
||||||
}
|
}
|
||||||
// std::cout<<"bye"<<std::endl;
|
|
||||||
|
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
@ -19,7 +19,6 @@ namespace CacheSim{
|
|||||||
bool read(string address);
|
bool read(string address);
|
||||||
bool write(string address);
|
bool write(string address);
|
||||||
vector<bool> addressTranslate(string address);
|
vector<bool> addressTranslate(string address);
|
||||||
short getIndex(vector<bool> addrBits);
|
short getValue(vector<bool> addrBits, string type);
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user