-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.cpp
More file actions
149 lines (132 loc) · 3.36 KB
/
cache.cpp
File metadata and controls
149 lines (132 loc) · 3.36 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
#include "cache.h"
QueryRecord* Cache::genQuery(char t, unsigned int memAddr, int f)
{
int block_num_in_mem = (int) (memAddr/(words_per_block*4));
int block_offset = (int)( memAddr % (words_per_block*4));
int index = block_num_in_mem % n_sets;
int tag = block_num_in_mem/n_sets;
QueryRecord* q = new QueryRecord(t, index, tag);
return q;
}
void Cache::execute (QueryRecord* q)
{
// std::cout << "entered execute fn\n";
if (q->isTypeRead)
{
handleRead(q);
}
else
{
handleWrite(q);
}
}
void Cache::handleRead (QueryRecord* q)
{
// std::cout << "entered handleRead fn\n";
Set* s = container[q->index];
int bl = s->search(q);
if (bl == -1)
{
handleReadMiss(q, s);
}
else
{
q->gotHit = true;
handleReadHit(q, s);
}
}
void Cache::handleWrite (QueryRecord* q)
{
// std::cout << "entered handleWrite fn\n";
Set* s = container[q->index];
int bl = s->search(q);
if (bl == -1)
{
handleWriteMiss(q, s);
}
else
{
q->gotHit = true;
handleWriteHit(q, s);
}
}
void Cache::handleReadHit (QueryRecord* q, Set* s)
{
// std::cout << "entered handleReadHit fn\n";
q->n_cycles += CACHE_RW_CYCLES_PER_WORD;
}
void Cache::handleReadMiss (QueryRecord* q, Set* s)
{
// std::cout << "entered handleReadMiss fn\n";
//correction
fetchFromMem(q);
// s->writeNewToCache(q);
bool dirtyEviction = s->writeNewToCache(q, false);
if (!isWriteThru && dirtyEviction) q->n_cycles += MEM_RW_CYCLES_PER_WORD*words_per_block;
q->n_cycles += CACHE_RW_CYCLES_PER_WORD;
}
void Cache::handleWriteHit (QueryRecord* q, Set* s)
{
// std::cout << "entered handleWriteHit fn\n";
if (isWriteThru)
{
s->writeExistingToCache(q);
q->n_cycles += CACHE_RW_CYCLES_PER_WORD;
writeToMem(q);
}
else
{
s->writeExistingToCache(q);
q->n_cycles += CACHE_RW_CYCLES_PER_WORD;
}
}
void Cache::handleWriteMiss (QueryRecord* q, Set* s)
{
// std::cout << "entered handleWriteMiss fn\n";
if(isWriteAlloc)
{
if(isWriteThru)
{
fetchFromMem(q);
s->writeNewToCache(q, true);
q->n_cycles += CACHE_RW_CYCLES_PER_WORD;
writeToMem(q);
}
else
{
//correction
writeBlockToMem(q);
bool dirtyEviction = s->writeNewToCache(q, true);
if (dirtyEviction) q->n_cycles += MEM_RW_CYCLES_PER_WORD*words_per_block;
q->n_cycles += CACHE_RW_CYCLES_PER_WORD;
}
}
else
{
writeToMem(q);
}
}
void Cache::fetchFromMem (QueryRecord* q)
{
// std::cout << "entered fetchFromMem fn\n";
q->n_cycles += MEM_RW_CYCLES_PER_WORD*words_per_block;
}
void Cache::writeToMem (QueryRecord* q)
{
// std::cout << "entered writeToMem fn\n";
q->n_cycles += MEM_RW_CYCLES_PER_WORD*1;
}
void Cache::writeBlockToMem (QueryRecord* q)
{
// std::cout << "entered writeToMem fn\n";
q->n_cycles += MEM_RW_CYCLES_PER_WORD*words_per_block;
}
Cache::~Cache()
{
for(int i = 0; i<n_sets; i++)
{
delete container[i];
container[i] = nullptr;
}
container.clear();
}