-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCache.h
111 lines (89 loc) · 2.46 KB
/
Cache.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef CACHE_H
#define CACHE_H
#include <vector>
#include "CacheSet.h"
#include "../HardwareStats.h"
class Cache
{
public:
int hits = 0;
int misses = 0;
// Create a new cache
// We only have 1 in our program but this can easily be expanded out to use data cache and a instruction cache
// so why not?
Cache(int totalSets, int setSize)
{
TotalSets = totalSets;
SetSize = setSize;
Sets = new vector<CacheSet*>; // initalize vector cache set
// let's create our sets
for (int i = 0; i < TotalSets; ++i)
{
Sets->push_back( new CacheSet(SetSize, i));
}
}
// deconstructor
// clear/free up memory
~Cache()
{
for (int i = 0; i < TotalSets; ++i)
{
delete Sets->at(i); // delete set
}
delete Sets; // delete our vector
}
// Get a entry from a set
bool GetEntry(int index, int tag)
{
return Sets->at(index)->GetEntry(tag);
}
// Add a entry to a set
void AddEntry(int index, int tag, int dirtyBit, int pfn)
{
Sets->at(index)->AddEntry(tag, dirtyBit, pfn);
}
// Update big inside a set of a entry
void UpdateDirtyEntry(int index, int tag)
{
Sets->at(index)->UpdateDirtyEntry(tag);
}
// Invalidates the entire cache at set->set cache entry
int Invalidate(int physicalFrameNmumber)
{
int TotalDirty = 0;
for (int i = 0; i < TotalSets; ++i)
{
TotalDirty += Sets->at(i)->Invalidate(physicalFrameNmumber);
}
return TotalDirty;
}
// GET POLICY
// 0: write-through no write allocate, 1: write-back, write-allocate
int GetPolicy()
{
return Policy;
}
// SET POLICY
// 0: write-through no write allocate, 1: write-back, write-allocate
void SetPolicy(int policy)
{
Policy = policy;
}
// Checks if the cache at index first entry is has a dirty bit
bool LRU_IsEntryDirtyBit(int index)
{
return Sets->at(index)->LRU_IsEntryDirtyBit();
}
// return hardware stats
HardwareStats GetStatistics()
{
HardwareStats stats(hits, misses);
return stats;
}
private:
vector<CacheSet*> *Sets; // vector of our CacheSets objects (class)
int TotalSets; // total ses
int SetSize; // set size
int Policy = 0; // 0: write-through no write allocate, 1: write-back, write-allocate
};
#endif