Skip to content

Commit f0fa9d4

Browse files
committed
Add addCache and revert setState functions, also allow invalidated cache to go through the bus
1 parent d15d1f2 commit f0fa9d4

13 files changed

+77
-15
lines changed

bus.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Bus {
1515
virtual void attachProcessor(shared_ptr<Processor> proc) = 0;
1616
virtual void pushRequestToBus(shared_ptr<Request> request) = 0;
1717
virtual void pushRequestToMemory(shared_ptr<Request> request) = 0;
18-
virtual void issueInvalidation(unsigned int address) = 0;
18+
virtual void issueInvalidation(unsigned int pid,unsigned int address) = 0;
1919
virtual void executeCycle() = 0;
2020
virtual bool isCurrentRequestDone(int pid) = 0;
2121
virtual void printProgress() = 0;

bus_impl.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,26 @@ bool BusImpl::isCurrentRequestDone(int pid) {
5353
return currentRequests[pid]->done;
5454
}
5555

56-
void BusImpl::issueInvalidation(unsigned int pid) {
56+
void BusImpl::issueInvalidation(unsigned int pid,unsigned int address) {
57+
cout << "invalidating cache! " << endl;
5758
for (auto processor : this->processors) {
5859
if (processor->getPID() == pid) {
5960
continue;
6061
}
61-
processor->invalidateCache(); // Not sure if this will send through the bus
62+
processor->invalidateCache(address); // Not sure if this will send through the bus
6263
}
6364
}
6465

6566
void BusImpl::processBusRd(shared_ptr<Request> request) {
6667
if (bus_debug) cout << "processing BusRd" << endl;
68+
69+
// if processor is -1 then just pass it through the bus and disappear
70+
if (request-> pid == -1){
71+
// countdown of passing through the bus only
72+
request->countdown = 2 * wordsPerBlock;
73+
request->isToMemOrCache = false;
74+
return;
75+
}
6776
// check if any other processor has it in M state
6877
// sanity check : if there exists the block in M state then they need to flush it
6978
// otherwise, everyone who has the block MUST match memory. so 100 + 2n

bus_impl.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BusImpl : public Bus {
2626
public:
2727
BusImpl(int wordsPerBlock) : wordsPerBlock(wordsPerBlock){};
2828
void attachProcessor(shared_ptr<Processor> proc) override;
29-
void issueInvalidation(unsigned int pid) override;
29+
void issueInvalidation(unsigned int pid,unsigned int address) override;
3030
void pushRequestToBus(shared_ptr<Request> request) override;
3131
void pushRequestToMemory(shared_ptr<Request> request) override;
3232
bool isCurrentRequestDone(int pid) override;

cache.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ size_t Cache::getIndex(size_t address) {
1212
// this returns the index of the memory
1313
return (address / blockSize) % cacheSize;
1414
}
15+
size_t Cache::getTag(size_t address) { return (address / (blockSize * cacheSize)); }
1516

1617
size_t Cache::getIndexWithTag(size_t address) {
1718
// this returns the index of the memory
1819
return address / cacheSize;
1920
}
2021

21-
size_t Cache::getTag(size_t address) { return (address / (blockSize * cacheSize)); }
22+
size_t Cache::reverseGeneralAddress(size_t tag, size_t index){
23+
return ((tag*(blockSize * cacheSize)) + (index)) * blockSize;
24+
}
2225

2326
bool Cache::checkCacheLine(size_t address) {
2427
// get the respective address
@@ -40,7 +43,9 @@ void Cache::addCacheLine(size_t address, State state) {
4043
// performs a addCacheLine
4144
size_t index = getIndex(address);
4245
size_t tag = getTag(address);
46+
// this will only give the tag, need to add in the address and then pad with the block size
4347
cache[index].addCacheLine(tag, state);
48+
4449
}
4550

4651
bool Cache::readCacheLine(size_t address) {
@@ -64,10 +69,10 @@ State Cache::getCacheLineState(size_t address) {
6469
return cache[index].checkCacheLineState(tag);
6570
}
6671

67-
void Cache::setCacheLineState(size_t address, State state) {
72+
bool Cache::setCacheLineState(size_t address, State state) {
6873
size_t index = getIndex(address);
6974
size_t tag = getTag(address);
70-
cache[index].setCacheLineState(tag, state);
75+
return cache[index].setCacheLineState(tag, state);
7176
}
7277

7378
State Cache::getLRUCacheLineState(size_t address) {
@@ -78,3 +83,15 @@ State Cache::getLRUCacheLineState(size_t address) {
7883
}
7984
return I;
8085
}
86+
87+
bool Cache::checkCacheLineFull(size_t address){
88+
size_t index = getIndex(address);
89+
return associativity == cache[index].size();
90+
}
91+
92+
93+
unsigned int Cache::getLRUCacheLineAddress(size_t address){
94+
size_t index = getIndex(address);
95+
unsigned int tag = cache[index].getFirst().getTag();
96+
return reverseGeneralAddress(tag,index);
97+
}

cache.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Cache {
2929

3030
size_t getIndexWithTag(size_t address);
3131

32+
size_t reverseGeneralAddress(size_t tag,size_t index);
33+
3234
bool checkCacheLine(size_t address);
3335

3436
void invalidateCacheLine(size_t address);
@@ -41,7 +43,12 @@ class Cache {
4143

4244
State getCacheLineState(size_t address);
4345

44-
void setCacheLineState(size_t address, State state);
46+
bool setCacheLineState(size_t address, State state);
4547

4648
State getLRUCacheLineState(size_t address);
49+
50+
bool checkCacheLineFull(size_t address);
51+
52+
unsigned int getLRUCacheLineAddress(size_t address);
53+
4754
};

cache_line.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ CacheLine::CacheLine(unsigned int tag, State state) : tag(tag), state(state){};
55

66
State CacheLine::getState() { return state; };
77

8+
unsigned int CacheLine::getTag() { return tag; };
9+
810
void CacheLine::setState(State state) { this->state = state; }
11+

cache_line.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstddef>
55

66
class CacheLine {
7+
public:
78
State state; // The states currently will corresponds to 0,1,2,3
89
// M -- 0, E -- 1, S -- 2, I -- 3 or M -- 0 E -- 1 Sc -- 2 Sm -- 3
910
unsigned int tag;
@@ -12,5 +13,6 @@ class CacheLine {
1213
public:
1314
CacheLine(unsigned int tag, State state);
1415
State getState();
16+
unsigned int getTag();
1517
void setState(State state);
1618
};

cache_set.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ State CacheSet::checkCacheLineState(size_t tag) {
7979
return I; // this will be similar to invalid state
8080
}
8181

82-
void CacheSet::setCacheLineState(size_t tag, State state) {
82+
bool CacheSet::setCacheLineState(size_t tag, State state) {
8383
for (auto &cacheLine : cacheSet) {
8484
if (cacheLine.tag == tag) {
8585
cacheLine.setState(state);
86+
return true;
8687
}
8788
}
89+
return false;
8890
}
8991

9092
bool CacheSet::checkCacheSetFull() { return cacheSet.size() == maxSize; }

cache_set.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ class CacheSet {
2828

2929
State checkCacheLineState(size_t tag);
3030

31-
void setCacheLineState(size_t tag, State state);
31+
bool setCacheLineState(size_t tag, State state);
3232

3333
bool checkCacheSetFull();
3434

3535
bool isEmpty();
3636

37+
unsigned int size(){return cacheSet.size();}
38+
3739
CacheLine getFirst();
3840

3941
friend class Cache;

dragon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ CacheResultType DragonProtocol::onStore(int pid,unsigned int address, shared_ptr
5252
if (dragon_debug) cout << "Sc: store hit" << endl;
5353
cache->updateCacheLine(address,Sm);
5454

55-
shared_ptr<Request> busRdRequest = make_shared<Request>(pid, BusUpd, address);
55+
shared_ptr<Request> busRdRequest = make_shared<Request>(-1, BusUpd, address);
5656
bus->pushRequestToBus(busRdRequest);
5757
// TODO: use bus command to change the state of other caches
5858
bus->updateOtherCachesToSc(address,pid);

processor.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
class Processor {
66
public:
77
virtual void executeCycle() = 0;
8-
virtual void invalidateCache() = 0;
8+
virtual void invalidateCache(unsigned int address) = 0;
99
virtual State getState(unsigned int address) = 0;
1010
virtual bool isDone() = 0;
1111
virtual void setState(unsigned int address, State state) = 0;
12+
virtual void addCacheLine(unsigned int address, State state) = 0;
1213
virtual int getPID() = 0;
1314
virtual void printProgressInline() = 0;
1415
};

processor_impl.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,32 @@ bool ProcessorImpl::execute(unsigned int type, unsigned int value) {
110110
return true;
111111
}
112112

113-
void ProcessorImpl::invalidateCache() {}
113+
void ProcessorImpl::invalidateCache(unsigned int address) {cache->invalidateCacheLine(address);}
114114

115115
State ProcessorImpl::getState(unsigned int address) { return cache->getCacheLineState(address); }
116116

117117
bool ProcessorImpl::isDone() { return done; }
118118

119119
void ProcessorImpl::setState(unsigned int address, State state) {
120-
cache->setCacheLineState(address, state);
120+
cache->setCacheLineState(address,state);
121+
}
122+
123+
void ProcessorImpl::addCacheLine(unsigned int address, State state){
124+
//TODO: this function suppose to update cache if it exist else do other things
125+
bool isStateSet = cache->setCacheLineState(address, state);
126+
if (!isStateSet){
127+
// this means that cache is not inside the cacheline
128+
if (cache->checkCacheLineFull(address)){// if address is full, then will need to invalidate cache
129+
//get the cacheline and then add it to the bus // TODO: ask adam
130+
unsigned int old_cache_address = cache->getLRUCacheLineAddress(address);
131+
// put into the bus to send to cache
132+
// create a request
133+
shared_ptr<Request> invalidateRequest = make_shared<Request>(-1,BusRd,address);
134+
// put it into the bus
135+
bus->pushRequestToBus(invalidateRequest);
136+
}
137+
cache->addCacheLine(address,state); // will automatically remove LRU cache
138+
}
121139
}
122140

123141
void ProcessorImpl::printProgressInline() {

processor_impl.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ class ProcessorImpl : public Processor {
3939
ProcessorImpl(int pid, string filepath, unsigned int cacheSize, unsigned int associativity,
4040
unsigned int blockSize, shared_ptr<Bus> bus, shared_ptr<Protocol> protocol);
4141
void executeCycle() override;
42-
void invalidateCache() override;
42+
void invalidateCache(unsigned int address) override;
4343
State getState(unsigned int address) override;
4444
bool isDone() override;
4545
void setState(unsigned int address, State state) override;
46+
void addCacheLine(unsigned int address, State state) override;
4647
int getPID() override { return pid; };
4748
void printProgressInline() override;
4849
};

0 commit comments

Comments
 (0)