Repository navigation
Expand file tree
/
Copy pathcache.cc
More file actions
145 lines (122 loc) · 2.88 KB
/
Copy pathcache.cc
File metadata and controls
145 lines (122 loc) · 2.88 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdlib.h>
#include <assert.h>
#include "cache.h"
using namespace std;
Cache::Cache(int s,int a,int b )
{
ulong i, j;
reads = readMisses = writes = 0;
writeMisses = writeBacks = currentCycle = 0;
size = (ulong)(s);
lineSize = (ulong)(b);
assoc = (ulong)(a);
sets = (ulong)((s/b)/a);
numLines = (ulong)(s/b);
log2Sets = (ulong)(log2(sets));
log2Blk = (ulong)(log2(b));
ccTransfer = 0;
memTransact = 0;
invalidations = 0;
interventions = 0;
flushes = 0;
busRd = 0;
busRdX = 0;
busUpgr = 0;
tagMask =0;
for(i=0;i<log2Sets;i++)
{
tagMask <<= 1;
tagMask |= 1;
}
cache = new cacheLine*[sets];
for(i=0; i<sets; i++)
{
cache[i] = new cacheLine[assoc];
for(j=0; j<assoc; j++)
{
cache[i][j].invalidate();
}
}
}
void Cache::Access(ulong addr,uchar op)
{
currentCycle++;/*per cache global counter to maintain LRU order
among cache ways, updated on every cache access*/
if(op == 'w') writes++;
else reads++;
cacheLine * line = findLine(addr);
if(line == NULL)/*miss*/
{
if(op == 'w') writeMisses++;
else readMisses++;
cacheLine *newline = fillLine(addr);
//Write updates I->M state
if(op == 'w') newline->setFlags(MODIFIED);
}
else
{
//Hit updates M->S state
updateLRU(line);
if(op == 'w') line->setFlags(SHARED);
}
}
/*look up line*/
cacheLine * Cache::findLine(ulong addr)
{
ulong i, j, tag, pos;
pos = assoc;
tag = calcTag(addr);
i = calcIndex(addr);
for(j=0; j<assoc; j++)
if(cache[i][j].isValid())
if(cache[i][j].getTag() == tag)
{
pos = j; break;
}
if(pos == assoc)
return NULL;
else
return &(cache[i][pos]);
}
/*upgrade LRU line to be MRU line*/
void Cache::updateLRU(cacheLine *line)
{
line->setSeq(currentCycle);
}
/*return an invalid line as LRU, if any, otherwise return LRU line*/
cacheLine * Cache::getLRU(ulong addr)
{
ulong i, j, victim, min;
victim = assoc;
min = currentCycle;
i = calcIndex(addr);
for(j=0;j<assoc;j++)
{
if(cache[i][j].isValid() == 0) return &(cache[i][j]);
}
for(j=0;j<assoc;j++)
{
if(cache[i][j].getSeq() <= min) { victim = j; min = cache[i][j].getSeq();}
}
assert(victim != assoc);
return &(cache[i][victim]);
}
/*find a victim, move it to MRU position*/
cacheLine *Cache::findLineToReplace(ulong addr)
{
cacheLine * victim = getLRU(addr);
updateLRU(victim);
return (victim);
}
/*allocate a new line*/
cacheLine *Cache::fillLine(ulong addr)
{
ulong tag;
cacheLine *victim = findLineToReplace(addr);
assert(victim != 0);
if(victim->getFlags() == MODIFIED || victim->getFlags() == S_MODIFIED) writeBack(addr);
tag = calcTag(addr);
victim->setTag(tag);
victim->setFlags(MODIFIED);
return victim;
}