-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.cpp
More file actions
316 lines (303 loc) · 7.63 KB
/
Cache.cpp
File metadata and controls
316 lines (303 loc) · 7.63 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//Author: Ernesto Soltero
//File: Cache.cpp
#include "Cache.h"
using namespace std;
Cache::Cache(int numBits, bool prefetch=true):
i0("i0",numBits),
i1("i1", numBits),
i2("i2", numBits),
i3("i3", numBits),
i4("i4", numBits),
d0("d0", numBits),
d1("d1", numBits),
d2("d2", numBits),
d3("d3", numBits),
d4("d4", numBits),
prefetchAddr("pre", numBits),
cacheBus("CACHE", numBits),
temp("temp", numBits)
{
CACHE_DATA *instructionArray = new CACHE_DATA[5];
CACHE_DATA *dataArray = new CACHE_DATA[5];
//initialize the arrays to empty
for(int i = 0; i < 5; i++){
instructionArray[i].data = 0;
instructionArray[i].address = 0;
instructionArray[i].reg = 0;
dataArray[i].data = 0;
dataArray[i].address = 0;
dataArray[i].reg = 0;
}
prefetch = prefetch;
size = numBits;
}
bool Cache::contains(long address, bool type){
//check if prefetching is on
if(!prefetch){return false;}
//figure out if its a data or instruction that we're looking for
//means its instruction type if true
//use 4 LSB bits as hash
int index = (address & 0xF) % 5;
if(type){
if(instructionArray[index].address == address){
return true;
} else{
return false;
}
} else{
if(dataArray[index].address == address){
return true;
} else {
return false;
}
}
}
void Cache::setupConnections(Counter s3, Memory m, BusALU alu){
d0.connectsTo(cacheBus.OUT());
d0.connectsTo(cacheBus.IN());
d1.connectsTo(cacheBus.OUT());
d1.connectsTo(cacheBus.IN());
d2.connectsTo(cacheBus.OUT());
d2.connectsTo(cacheBus.IN());
d3.connectsTo(cacheBus.OUT());
d3.connectsTo(cacheBus.IN());
d4.connectsTo(cacheBus.OUT());
d4.connectsTo(cacheBus.IN());
i0.connectsTo(cacheBus.OUT());
i0.connectsTo(cacheBus.IN());
i1.connectsTo(cacheBus.OUT());
i1.connectsTo(cacheBus.IN());
i2.connectsTo(cacheBus.OUT());
i2.connectsTo(cacheBus.IN());
i3.connectsTo(cacheBus.OUT());
i3.connectsTo(cacheBus.IN());
i4.connectsTo(cacheBus.OUT());
i4.connectsTo(cacheBus.IN());
//scratch register for moving to memory
temp.connectsTo(cacheBus.IN());
temp.connectsTo(cacheBus.OUT());
temp.connectsTo(m.READ());
temp.connectsTo(m.WRITE());
prefetchAddr.connectsTo(m.WRITE());
//memory
m.MAR().connectsTo(cacheBus.OUT());
//s3 connections
s3.connectsTo(cacheBus.IN());
s3.connectsTo(cacheBus.OUT());
//Bus alu
prefetchAddr.connectsTo(alu.OUT());
}
void Cache::preFetch(bool type, Memory m){
long address = prefetchAddr(size - 1, 0);
//first check if this is already in the cache
if(contains(address, type)){
return;
}
//check if prefetching is on
if(!prefetch){
return;
}
//grab from memory
cacheBus.IN().pullFrom(prefetchAddr);
m.MAR().latchFrom(cacheBus.OUT());
Clock::tick();
m.read();
temp.latchFrom(m.READ());
Clock::tick();
cacheBus.IN().pullFrom(temp);
prefetchAddr.latchFrom(cacheBus.OUT());
Clock::tick();
int index = (address & 0xF) % 5;
//figure out where to put it
//instruction type
if(type){
//we dont really care about swapping instructions so just overwrite
instructionArray[index].address = address;
instructionArray[index].data = prefetchAddr(size - 1, 0);
instructionArray[index].reg = index;
//figure out the register to store
cacheBus.IN().pullFrom(prefetchAddr);
switch(index){
case 0:
i0.latchFrom(cacheBus.OUT());
instructionArray[index].reg = 0;
break;
case 1:
i1.latchFrom(cacheBus.OUT());
instructionArray[index].reg = 1;
break;
case 2:
i2.latchFrom(cacheBus.OUT());
instructionArray[index].reg = 2;
break;
case 3:
i3.latchFrom(cacheBus.OUT());
instructionArray[index].reg = 3;
break;
case 4:
i4.latchFrom(cacheBus.OUT());
instructionArray[index].reg = 4;
break;
}
} else{
//we really care about swapping data so we have to write back into
//memory if its not the same
if(dataArray[index].address != 0){
//write into mem
switch(index){
case 0:
cacheBus.IN().pullFrom(d0);
break;
case 1:
cacheBus.IN().pullFrom(d1);
break;
case 2:
cacheBus.IN().pullFrom(d2);
break;
case 3:
cacheBus.IN().pullFrom(d3);
break;
case 4:
cacheBus.IN().pullFrom(d4);
break;
}
temp.latchFrom(cacheBus.OUT());
//want to avoiding having to add in another register so
//kind of cheating and setting the address value using backdoor
m.MAR().backDoor(dataArray[index].address);
Clock::tick();
m.WRITE().pullFrom(temp);
m.write();
Clock::tick();
//wrote data now insert into reg
dataArray[index].address = address;
dataArray[index].data = prefetchAddr(size - 1, 0);
cacheBus.IN().pullFrom(prefetchAddr);
switch(index){
case 0:
dataArray[index].reg = 0;
d0.latchFrom(cacheBus.OUT());
break;
case 1:
dataArray[index].reg = 1;
d1.latchFrom(cacheBus.OUT());
break;
case 2:
dataArray[index].reg = 2;
d2.latchFrom(cacheBus.OUT());
break;
case 3:
dataArray[index].reg = 3;
d3.latchFrom(cacheBus.OUT());
break;
case 4:
dataArray[index].reg = 4;
d4.latchFrom(cacheBus.OUT());
break;
}
} else{
//safe to write nothing in register
dataArray[index].address = address;
dataArray[index].data = prefetchAddr(size - 1, 0);
cacheBus.IN().pullFrom(prefetchAddr);
switch(index){
case 0:
dataArray[index].reg = 0;
d0.latchFrom(cacheBus.OUT());
break;
case 1:
dataArray[index].reg = 1;
d1.latchFrom(cacheBus.OUT());
break;
case 2:
dataArray[index].reg = 2;
d2.latchFrom(cacheBus.OUT());
break;
case 3:
dataArray[index]. reg = 3;
d3.latchFrom(cacheBus.OUT());
break;
case 4:
dataArray[index].reg = 4;
d4.latchFrom(cacheBus.OUT());
break;
}
}
}
}
bool Cache::retrieve(long address, bool type, Counter s3){
//check if prefetching is on
if(!prefetch){
return false;
}
//check if we have what were looking for
if(!contains(address, type)){
return false;
}
//We do have address so find it and place it in s3
int index = (address & 0xF) % 5;
if(type){
switch(index){
case 0:
cacheBus.IN().pullFrom(i0);
break;
case 1:
cacheBus.IN().pullFrom(i1);
case 2:
cacheBus.IN().pullFrom(i2);
break;
case 3:
cacheBus.IN().pullFrom(i3);
break;
case 4:
cacheBus.IN().pullFrom(i4);
break;
}
s3.latchFrom(cacheBus.OUT());
} else{
switch(index){
case 0:
cacheBus.IN().pullFrom(d0);
break;
case 1:
cacheBus.IN().pullFrom(d1);
case 2:
cacheBus.IN().pullFrom(d2);
break;
case 3:
cacheBus.IN().pullFrom(d3);
break;
case 4:
cacheBus.IN().pullFrom(d4);
break;
}
s3.latchFrom(cacheBus.OUT());
}
Clock::tick();
return true;
}
void Cache::debugInfo(){
printf("Printing put the Cache contents\n\n");
printf("Instruction cache contents:\n");
printf("i0: %16x address: %16x\n", instructionArray[0].data,
instructionArray[0].address);
printf("i1: %16x address: %16x\n", instructionArray[1].data,
instructionArray[1].address);
printf("i2: %16x address: %16x\n", instructionArray[2].data,
instructionArray[2].address);
printf("i3: %16x address: %16x\n", instructionArray[3].data,
instructionArray[3].address);
printf("i4: %16x address: %16x\n\n", instructionArray[4].data,
instructionArray[4].address);
printf("Data cache contents:\n");
printf("d0: %16x address: %16x\n", dataArray[0].data,
dataArray[0].address);
printf("d1: %16x address: %16x\n", dataArray[1].data,
dataArray[1].address);
printf("d0: %16x address: %16x\n", dataArray[2].data,
dataArray[2].address);
printf("d0: %16x address: %16x\n", dataArray[3].data,
dataArray[3].address);
printf("d0: %16x address: %16x\n\n", dataArray[4].data,
dataArray[4].address);
}