-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.cc
More file actions
192 lines (183 loc) · 4.87 KB
/
cache.cc
File metadata and controls
192 lines (183 loc) · 4.87 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "cache.h"
#include "def.h"
#include <memory.h>
void Cache::HandleRequest(uint64_t addr, int bytes, int read,
char *content, int &hit, int &time) {
//hit = 0;
//time = 0;
//printf("debug1 %d\n", dirty[0][0]);
// Bypass?
if (!BypassDecision()) {
//printf("in\n");
bool ok = PartitionAlgorithm(addr, bytes, read, content);
// Miss?
if (ReplaceDecision(ok)) {
// Choose victim
ReplaceAlgorithm(addr, bytes, read, content, hit, time);
} else {
// return hit & time
hit = 1;
time += latency_.bus_latency + latency_.hit_latency;
stats_.access_time += latency_.bus_latency + latency_.hit_latency;
return;
}
}
// Prefetch?
if (PrefetchDecision()) {
PrefetchAlgorithm();
} else {
// Fetch from lower layer
int lower_hit, lower_time;
lower_->HandleRequest(addr, bytes, read, content,
lower_hit, lower_time);
hit = 0;
time += latency_.bus_latency + lower_time;
stats_.access_time += latency_.bus_latency;
}
}
int Cache::BypassDecision() {
return FALSE;
}
bool Cache::PartitionAlgorithm(uint64_t addr, int bytes, int read, char *content) {
lint tag = (addr >> (indexBits + offsetBits));
lint index = ((addr >> offsetBits) & ((1 << indexBits) - 1));
lint offset = (addr & ((1 << offsetBits) - 1));
for(int i = 0; i < way; i++)
{
if(tags[index][i] == tag && valid[index][i])
{
if(read)
{
memcpy(content, blocks[index][i]+offset, bytes);
nowTime ++;
lastTimes[index][i] = nowTime;
return true;
}
else
{
if(policy == 1)
{
memcpy(blocks[index][i]+offset, content, bytes);
nowTime ++;
lastTimes[index][i] = nowTime;
dirty[index][i] = TRUE;
return true;
}
else
{
memcpy(blocks[index][i]+offset, content, bytes);
nowTime ++;
lastTimes[index][i] = nowTime;
dirty[index][i] = TRUE;
int h, t;
lower_->HandleRequest(addr, bytes, read, content, h, t);
return true;
}
}
}
}
return false;
}
int Cache::ReplaceDecision(bool ok) {
return !ok;
//return FALSE;
}
void Cache::ReplaceAlgorithm(uint64_t addr, int bytes, int read,
char *content, int &hit, int &time){
//printf("replace\n", addr);
//printf("debug %d\n", dirty[0][0]);
lint tag = (addr >> (indexBits + offsetBits));
lint index = ((addr >> offsetBits) & ((1 << indexBits) - 1));
lint offset = (addr & ((1 << offsetBits) - 1));
//printf("replace %lu %lu %lu\n", tag, index, offset);
if(read)
{
//printf("read\n");
int w = 0;
lint lt = 10000000000;
for(int i = 0; i < way; i++)
{
if(!valid[index][i])
{
w = i;
break;
}
if(lastTimes[index][i] < lt)
{
lt = lastTimes[index][i];
w = i;
}
}
//printf("read-in index: %d w: %d addr: %llx\n", index, w, addr);
//printf("re1 %d %d %d\n", dirty[index][w], index, w);
if(dirty[index][w])
{
uint64_t raddr = 0;
raddr += (index << offsetBits);
raddr += (tags[index][w] << (offsetBits + indexBits));
//printf("raddr: %llx index: %lu tag: %llx\n", raddr, index, tags[index][w]);
int h, t;
lower_->HandleRequest(raddr, block_size, 0, blocks[index][w], h, time);
}
//printf("%llx %lu\n", addr, offset);
//printf("before\n");
int h;
lower_->HandleRequest(addr-offset, block_size, 1, blocks[index][w], hit, time);
//printf("re2\n");
nowTime ++;
lastTimes[index][w] = nowTime;
tags[index][w] = tag;
valid[index][w] = TRUE;
dirty[index][w] = FALSE;
memcpy(content, blocks[index][w]+offset, bytes);
}
else
{
//printf("write\n");
if(policy == 1)
{
int w = 0;
lint lt = 10000000000;
for(int i = 0; i < way; i++)
{
if(!valid[index][i])
{
w = i;
break;
}
if(lastTimes[index][i] < lt)
{
lt = lastTimes[index][i];
w = i;
}
}
if(dirty[index][w])
{
uint64_t raddr = 0;
raddr += (index << offsetBits);
raddr += (tags[index][w] << (offsetBits + indexBits));
int h, t;
lower_->HandleRequest(raddr, block_size, 0, blocks[index][w], h, time);
}
//printf("before\n");
int h;
lower_->HandleRequest(addr-offset, block_size, 1, blocks[index][w], hit, time);
nowTime ++;
lastTimes[index][w] = nowTime;
tags[index][w] = tag;
valid[index][w] = TRUE;
dirty[index][w] = TRUE;
memcpy(blocks[index][w]+offset, content, bytes);
}
else
{
int h;
lower_->HandleRequest(addr, bytes, 0, content, h, time);
}
}
}
int Cache::PrefetchDecision() {
return FALSE;
}
void Cache::PrefetchAlgorithm() {
}