-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheCore.cpp
128 lines (117 loc) · 3.78 KB
/
CacheCore.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
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
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include "CacheCore.h"
CacheCore::CacheCore(uint32_t s, uint32_t a, uint32_t b, const char *pStr)
: size(s)
,lineSize(b)
,assoc(a)
,numLines(s/b)
,numRows(s/b/a)
{
if (strcasecmp(pStr, "RANDOM") == 0)
policy = RANDOM;
else if (strcasecmp(pStr, "LRU") == 0)
policy = LRU;
else {
assert(0);
}
content = new CacheLine[numLines + 1];
for(uint32_t i = 0; i < numLines; i++) {
content[i].initialize();
}
}
CacheCore::~CacheCore() {
delete [] content;
}
CacheLine *CacheCore::accessLine(uint32_t addr)
{
uint32_t offset_bit_len;
uint32_t row_bit_len;
uint32_t tag_bit_len;
//uint32_t offset_bit;
uint32_t row_bit;
uint32_t tag_bit;
offset_bit_len = log2i(lineSize);
row_bit_len = log2i(numRows);
tag_bit_len = 32 - offset_bit_len - row_bit_len;
//offset_bit = addr << (32 - offset_bit_len) >> (32 - offset_bit_len);
row_bit = addr << tag_bit_len >>(32 - row_bit_len);
tag_bit = addr >> (32 - tag_bit_len);
for(uint32_t i = 0; i < assoc; i++){
if(content[numRows * i + row_bit].getTag() == tag_bit && content[numRows * i + row_bit].isValid()){
for(uint32_t j = 0; j < assoc; j++){
if(content[numRows * j + row_bit].isValid() && i != j){
//printf("*(%d, %d) age increase\n", row_bit, j);
content[numRows * j + row_bit].incAge();
}
}
content[numRows * i + row_bit].resetAge();
return &content[numRows * i + row_bit];
}
}
//printf("content size: %d", content.size());
return NULL;
}
CacheLine *CacheCore::allocateLine(uint32_t addr, uint32_t *rplcAddr) {
// TODO: Implement
uint32_t offset_bit_len;
uint32_t row_bit_len;
uint32_t tag_bit_len;
//uint32_t offset_bit;
uint32_t row_bit;
uint32_t tag_bit;
int max_age;
uint32_t max_age_index;
offset_bit_len = log2i(lineSize);
row_bit_len = log2i(numRows);
tag_bit_len = 32 - offset_bit_len - row_bit_len;
//offset_bit = addr << (32 - offset_bit_len) >> (32 - offset_bit_len);
row_bit = (addr << tag_bit_len) >>(32 - row_bit_len);
tag_bit = addr >> (32 - tag_bit_len);
max_age = -1;
//if(size == 16384){
/*
printf("size: %d\n", (int)size);
printf("num of rows: %d\n", (int)numRows);
printf("tag_bit_len: %d\n", (int)tag_bit_len);
printf("row_bit_len: %d\n", (int)row_bit_len);
printf("tag_bit: %x\n", (int)tag_bit);
printf("row_bit: %x\n", (int)row_bit);
*/
//}
for(uint32_t i = 0; i < assoc; i++){
if(!content[numRows * i + row_bit].isValid()){
//printf("index: %d\n", numRows * i + row_bit);
content[numRows * i + row_bit].validate();
content[numRows * i + row_bit].setTag(tag_bit);
//printf("**************\n");
for(uint32_t j = 0; j < assoc; j++){
if(content[numRows * j + row_bit].isValid() && i != j){
//printf("*(%d, %d) age increase\n", row_bit, j);
content[numRows * j + row_bit].incAge();
}
}
return &content[numRows * i + row_bit];
}
if((int) content[numRows * i + row_bit].getAge() > max_age){
max_age = content[numRows * i + row_bit].getAge();
max_age_index = numRows * i + row_bit;
}
}
//printf("***************");
//printf("*(%d, %d) max_age\n", index2Row(max_age_index), index2Column(max_age_index));
if(content[max_age_index].isDirty()) rplcAddr = &addr;
content[max_age_index].initialize();
content[max_age_index].setTag(tag_bit);
content[max_age_index].validate();
for(uint32_t j = 0; j < assoc; j++){
if(content[numRows * j + row_bit].isValid() && index2Column(max_age_index) != j){
//printf("*(%d, %d) age increase\n", row_bit, j);
content[numRows * j + row_bit].incAge();
}
}
return &content[max_age_index];
}