-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMT2019030_DirectMappedCache.cpp
59 lines (56 loc) · 1.98 KB
/
IMT2019030_DirectMappedCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "IMT2019030_DirectMappedCache.h"
//Constructors and Destructors
DMCache::DMCache(){}
DMCache::DMCache(int CacheSize,int BlockSize,int AddressSize){
this->CacheSize=CacheSize;
this->BlockSize=BlockSize;
this->AddressLength=AddressSize;
this->init();
}
DMCache::~DMCache(){
this->Cache.clear();
}
DMCache::DMCache(const DMCache&oldObj){
this->CacheSize=oldObj.CacheSize;
this->BlockSize=oldObj.BlockSize;
this->NumberOfLines=oldObj.NumberOfLines;
this->AddressLength=oldObj.AddressLength;
this->IndexSize=oldObj.IndexSize;
this->TagSize=oldObj.TagSize;
this->DataSize=oldObj.DataSize;
this->ByteOffset=oldObj.ByteOffset;
this->Cache=oldObj.Cache;
}
//Member functions
void DMCache::init(){
this->ByteOffset=ceil(log2(this->BlockSize));
this->NumberOfLines=this->CacheSize/this->BlockSize;
this->IndexSize=ceil(log2(this->NumberOfLines));
this->TagSize=this->AddressLength-(this->ByteOffset+this->IndexSize);
this->DataSize=4*this->BlockSize;
for(int i=0;i<this->NumberOfLines;i++){
this->Cache.push_back(Block(this->BlockSize,this->DataSize));
}
}
//Simply does the check for index and tag match
int DMCache::CheckAndHit(string HexAddress){
HexAddress=HexAddress.substr(2,HexAddress.size()-1);
Bitset BinAddress=Bitset::HexToBin(HexAddress);
int index=Bitset::BinToDec(BinAddress.extract(this->ByteOffset,this->ByteOffset+this->IndexSize-1));
Bitset tag=BinAddress.extract(this->ByteOffset+this->IndexSize,this->AddressLength-1);
//If the valid bit is zero
if(Cache[index].BValidBit().stringform()=="0"){
Bitset validbit(1);
validbit.setBitset("1");
Cache[index].setValidBit(validbit);
Cache[index].setTag(tag);
return 0;
}
//If there is a tag match and index match
if(Cache[index].BTag().stringform()==tag.stringform()){
return 1;
}
//If the valid bit is one and there is no tag match
Cache[index].setTag(tag);
return 0;
}