-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft.cpp
More file actions
499 lines (435 loc) · 15.5 KB
/
draft.cpp
File metadata and controls
499 lines (435 loc) · 15.5 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdlib.h>
#include <iomanip>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;
#define vacant -1
#define ADDRESS_SIZE 32
unsigned long L1_BLOCKSIZE;
unsigned long L1_SIZE;
unsigned long L1_ASSOC;
unsigned long VC_SIZE;
unsigned long VC_NUM_BLOCKS;
unsigned long VC_ASSOC;
unsigned long L2_BLOCKSIZE;
unsigned long L2_SIZE;
unsigned long L2_ASSOC;
char *Trace_file;
struct DATASTORE
{
bool isValid;
bool isDirty;
};
struct TAGSTORE
{
long long *TAG; // TAG value
int *lru;
DATASTORE *dataStore;
};
class CACHE
{
private:
unsigned long SIZE;
unsigned long ASSOC;
unsigned long BLOCKSIZE;
unsigned long NUM_SETS;
unsigned long INDEX_SIZE;
unsigned long block_offset_size;
unsigned long tag_bits;
unsigned long TAG_MASK = 0;
unsigned long INDEX_MASK = 0;
unsigned long BLOCK_OFFSET_MASK = 0;
unsigned long CACHE_BLOCK_MISS = 0;
unsigned long SECTOR_MISS = 0;
// vector<vector<TAGSTORE>> tagStore;
struct TAGSTORE **tagStore;
bool isPowerOfTwo(unsigned long x)
{
return (x > 0) && ((x & (x - 1)) == 0);
}
// void resetlru(unsigned long index, unsigned long lru)
// {
// for (unsigned long j = 0; j < ASSOC; j++)
// {
// if (tagStore[index][j].lru < lru)
// {
// tagStore[index][j].lru++;
// }
// else if (tagStore[index][j].lru == lru)
// {
// tagStore[index][j].lru = 0;
// }
// }
// }
void resetlru(unsigned long index, unsigned long lruIndex)
{
for (unsigned long j = 0; j < ASSOC; j++)
{
// Dereference lru pointer to compare and update its value
if (*(tagStore[index]->lru + j) < lruIndex)
{
(*(tagStore[index]->lru + j))++;
}
else if (*(tagStore[index]->lru + j) == lruIndex)
{
*(tagStore[index]->lru + j) = 0; // Reset the LRU counter for this entry
}
}
}
// void checkMissType(unsigned long index, unsigned long assoc)
// {
// bool isBlockMiss = false;
// bool isSectorMiss = true;
// for (unsigned long k = 0; k < BLOCKSIZE; k++)
// {
// if (tagStore[index][assoc].dataStore.isValid[k])
// {
// isBlockMiss = true;
// isSectorMiss = false;
// break;
// }
// }
// if (isBlockMiss)
// {
// CACHE_BLOCK_MISS++;
// }
// else if (isSectorMiss)
// {
// SECTOR_MISS++;
// }
// }
public:
unsigned long READ_HIT = 0;
unsigned long READ_MISS = 0;
unsigned long Reads = 0;
unsigned long writes = 0;
unsigned long WRITE_HIT = 0;
unsigned long WRITE_MISS = 0;
CACHE() = default;
CACHE(unsigned long size, unsigned long assoc, unsigned long blockSize)
: SIZE(size), ASSOC(assoc), BLOCKSIZE(blockSize)
{
if (!isPowerOfTwo(BLOCKSIZE))
{
cerr << "Error: BLOCKSIZE must be a power of two." << endl;
exit(EXIT_FAILURE);
}
NUM_SETS = SIZE / (BLOCKSIZE * ASSOC);
if (NUM_SETS != 1 && !isPowerOfTwo(NUM_SETS))
{
cerr << "Error: Number of sets must be a power of two." << endl;
exit(EXIT_FAILURE);
}
INDEX_SIZE = log2(NUM_SETS);
block_offset_size = log2(BLOCKSIZE);
tag_bits = 32 - INDEX_SIZE - block_offset_size;
// Generate BLOCK_OFFSET_MASK
for (unsigned long i = 0; i < block_offset_size; i++)
{
BLOCK_OFFSET_MASK = (BLOCK_OFFSET_MASK << 1) + 1;
}
// Generate INDEX_MASK
for (unsigned long i = 0; i < INDEX_SIZE; i++)
{
INDEX_MASK = (INDEX_MASK << 1) + 1;
}
INDEX_MASK <<= block_offset_size;
// Generate TAG_MASK
for (unsigned long i = 0; i < tag_bits; i++)
{
TAG_MASK = (TAG_MASK << 1) + 1;
}
TAG_MASK <<= (INDEX_SIZE + block_offset_size);
tagStore = new TAGSTORE *[NUM_SETS];
for (unsigned long i = 0; i < NUM_SETS; i++) {
tagStore[i] = new TAGSTORE();
// tagStore[i]->TAG = (unsigned*)malloc(ASSOC * sizeof(long));
// tagStore[i]->lru = (unsigned*)malloc(ASSOC * sizeof(long));
tagStore[i]->TAG = (long long*)malloc(ASSOC * sizeof(long long)); // Correct: Use long long type for TAG
tagStore[i]->lru = (int*)malloc(ASSOC * sizeof(int));
tagStore[i]->dataStore = new DATASTORE[ASSOC]; // Allocate array of DATASTORE for each set
for (int j = 0; j < ASSOC; j++) {
tagStore[i]->dataStore[j].isValid = false;
tagStore[i]->dataStore[j].isDirty = false;
}
// tagStore[i]->dataStore.isValid = false;
// tagStore[i]->dataStore.isDirty = false;
fill(tagStore[i]->lru, tagStore[i]->lru + ASSOC, -1);
}
// Initialize cache
// tagStore.resize(NUM_SETS, vector<TAGSTORE>(ASSOC));
// for (unsigned long i = 0; i < NUM_SETS; i++)
// {
// for (unsigned long j = 0; j < ASSOC; j++)
// {
// tagStore[i][j].TAG = vacant;
// tagStore[i][j].lru = j;
// tagStore[i][j].dataStore.TagSelect.resize(BLOCKSIZE, vacant);
// tagStore[i][j].dataStore.isValid.resize(BLOCKSIZE, false);
// tagStore[i][j].dataStore.isDirty.resize(BLOCKSIZE, false);
// }
// }
}
void writeToAddress(unsigned long address)
{
unsigned long TAG = (address & TAG_MASK) >> (32 - tag_bits);
unsigned long index = (address & INDEX_MASK) >> block_offset_size;
unsigned long blockoffset = address & BLOCK_OFFSET_MASK;
writes++;
// for (unsigned long j = 0; j < ASSOC; j++)
// {
bool hit = false;
for (unsigned long k = 0; k < ASSOC; k++)
{
if ((tagStore[index]->TAG[k]) == TAG )
{
WRITE_HIT++;
hit = true;
// resetFrequency(index, tagStore[index][j].frequency);
// resetlru(index, *(tagStore[index][j].lru));
// hit = true;
tagStore[index]->dataStore[k].isValid = true;
tagStore[index]->dataStore[k].isDirty = true;
// hitUpdateLru(index,TAG,k);
updatelru(index,TAG,true);
break;
}
}
if(!hit){
WRITE_MISS++;
// (tagStore[index]->TAG[j]) = TAG;
updatelru(index,TAG,true);
// tagStore[index]->dataStore[k].isValid = true;
// tagStore[index]->dataStore[k].isDirty = true;
// cout << "Trace_file woooooooow:\t" << (tagStore[index]->TAG[j]) << endl;
}
}
void readFromAddress(unsigned long address)
{
unsigned long TAG = (address & TAG_MASK) >> (32 - tag_bits);
unsigned long index = (address & INDEX_MASK) >> block_offset_size;
unsigned long blockoffset = address & BLOCK_OFFSET_MASK;
bool hit = false;
Reads++;
for (unsigned long j = 0; j < ASSOC; j++)
{
if ((tagStore[index]->TAG[j]) == TAG )
{
READ_HIT++;
hit = true;
hitUpdateLru(index, TAG, j);
// tagStore[index]->dataStore[k].isValid = true;
// tagStore[index]->dataStore[k].isDirty = false;
break;
}
}
// else {
if (!hit)
{
READ_MISS++;
// cout << dec <<*(tagStore[index]->lru ) << endl;
updatelru(index,TAG,false);
// (tagStore[maxIndex]->TAG[j]) = TAG;
// cout << "Trace_file woooooooow:\t" << (tagStore[index]->TAG[j]) << endl;
}
// if (!hit)
// {
// READ_MISS++;
// for (unsigned long j = 0; j < ASSOC; j++)
// {
// if (tagStore[index][j].TAG == vacant)
// {
// tagStore[index][j].TAG = TAG;
// tagStore[index][j].dataStore.isValid[blockoffset] = true;
// resetlru(index, tagStore[index][j].lru);
// break;
// }
// }
// }
}
void hitUpdateLru(int index, unsigned tag, int hit_cell){
int old_count = tagStore[index]->lru[hit_cell];
tagStore[index]->lru[hit_cell]=0;
tagStore[index]->TAG[hit_cell]=tag;
int replaced_cell = hit_cell;
for(int j=0;j<ASSOC;j++){
if(tagStore[index]->lru[j]>=0 && replaced_cell!=j && tagStore[index]->lru[j]<old_count)
tagStore[index]->lru[j] = tagStore[index]->lru[j] + 1;
}
}
void updatelru(int index, unsigned tag,bool write){
int* maxElementPtr = max_element(tagStore[index]->lru, tagStore[index]->lru + ASSOC);
int* minElementPtr = min_element(tagStore[index]->lru, tagStore[index]->lru + ASSOC);
int maxIndex = maxElementPtr - tagStore[index]->lru;
// cout << "maxindex\n" << minElementPtr;
// int minIndex = minElementPtr - tagstore[index].lru;
int replaced_cell;
for(int j=0;j<L1_ASSOC;j++){
if(tagStore[index]->TAG[j]==tag){
hitUpdateLru(index, tag, j);
return;
}
}
if(*minElementPtr < 0){
for(int i=0;i<ASSOC;i++){
if(tagStore[index]->lru[i]<0){
tagStore[index]->lru[i]=0;
tagStore[index]->TAG[i]=tag;
replaced_cell = i;
// cout << "lru\t" << tagStore[index]->lru ;
break;
}
}
}
// end
else{
// cout<<"Inside after cache"<<endl;
tagStore[index]->lru[maxIndex]=0;
tagStore[index]->TAG[maxIndex]=tag;
if(write){
tagStore[index]->dataStore[maxIndex].isValid = true;
tagStore[index]->dataStore[maxIndex].isDirty = true;
}
if(write ==false){
tagStore[index]->dataStore[maxIndex].isDirty = false;
}
replaced_cell =maxIndex;
}
for(int j=0;j<ASSOC;j++){
if(tagStore[index]->lru[j]>=0 && replaced_cell!=j)
tagStore[index]->lru[j] = tagStore[index]->lru[j] + 1;
}
}
void CacheStatus()
{
for (unsigned long i = 0; i < NUM_SETS; i++)
{
cout << "\nSet " << dec << i << ":\t";
for (unsigned long j = 0; j < ASSOC; j++)
{
// Find the correct block by lru
// for (unsigned long k = 0; k < ASSOC; k++)
// {
// if (tagStore[i].lru == j)
// {
// Print the tag
if (tagStore[i]->TAG[j] == vacant)
{
cout << "[0]";
}
else
{
cout << hex << tagStore[i]->TAG[j] ;
}
// Print the dirty bit
if (tagStore[i]->dataStore[j].isDirty == true) // Using [0] assuming first block for simplicity
{
cout << " D ";
}
if(tagStore[i]->dataStore[j].isDirty == false)
{
cout << " \t";
}
}
}
// for(int i=0;i<ASSOC;i++)
// cout<<setw(15)<<"BLOCK"<<i
// <<setw(15)<<"LRU"<<i;
// cout<<"\n\n";
// for(int i=0;i<NUM_SETS;i++){
// // cout<<"SET "<<i<<endl;
// cout<<"SET "<<i<<"\t";
// // cout<<"SET "<<i<<"\t"<<setw(15)<<tagstore[i].tag<<setw(15)<<tagstore[i].index<<setw(15)<<tagstore[i].ofst<<setw(15);
// // cout<<"Tag : "<<tagstore[i].tag<<"\n"<<"Index : "<<tagstore[i].index<<"\n"<<"OFST : "<<tagstore[i].ofst<<"\n"<<"BLOCKS : ";
// for(int j=0;j<ASSOC;j++){
// cout<<hex<<setw(15)<<tagStore[i]->TAG[j]
// <<dec<<setw(15)<<tagStore[i]->lru[j];
// }
// cout<<"\n";
// }
// READ_MISS = Reads - READ_HIT;
// WRITE_MISS = writes - WRITE_HIT;
cout<< dec <<"\nno of reads : "<<Reads
<<"\nno of read misses : "<<READ_MISS
<<"\nno of read hits : "<<READ_HIT
<<"\n\nno of Writes : "<<writes
<<"\n\nno of Writes misses : "<<WRITE_MISS << "\n";
}
};
int main(int argc, char *argv[])
{
if (argc < 8)
{
cerr << "Usage: " << argv[0] << " <L1_SIZE> <L1_ASSOC> <L1_BLOCKSIZE> <VC_NUM_BLOCKS> <L2_SIZE> <L2_ASSOC> <Trace_file>" << endl;
return 1;
}
L1_SIZE = strtoul(argv[1], 0, 10);
L1_ASSOC = strtoul(argv[2], 0, 10);
L1_BLOCKSIZE = strtoul(argv[3], 0, 10);
L2_BLOCKSIZE = L1_BLOCKSIZE;
VC_NUM_BLOCKS = strtoul(argv[4], 0, 10);
L2_SIZE = strtoul(argv[5], 0, 10);
L2_ASSOC = strtoul(argv[6], 0, 10);
Trace_file = argv[7];
cout << "===== Simulator configuration =====" << endl;
cout << "L1_SIZE:\t" << L1_SIZE << endl;
cout << "L1_ASSOC:\t" << L1_ASSOC << endl;
cout << "L1_BLOCKSIZE:\t" << L1_BLOCKSIZE << endl;
cout << "VC_NUM_BLOCKS:\t" << VC_NUM_BLOCKS << endl;
cout << "L2_SIZE:\t" << L2_SIZE << endl;
cout << "L2_ASSOC:\t" << L2_ASSOC << endl;
cout << "Trace_file:\t" << Trace_file << endl;
// VC_SIZE=VC_NUM_BLOCKS * L1_BLOCKSIZE;
CACHE L1;
CACHE VC;
CACHE L2;
// if (L2_SIZE != 0 && VC_NUM_BLOCKS != 0)
// {
// L1 = CACHE(L1_SIZE, L1_ASSOC, L1_BLOCKSIZE);
// VC = CACHE(VC_NUM_BLOCKS * L1_BLOCKSIZE, VC_NUM_BLOCKS, L1_BLOCKSIZE);
// L2 = CACHE(L2_SIZE, L2_ASSOC, L1_BLOCKSIZE);
// }
// else if (L2_SIZE == 0 && VC_NUM_BLOCKS != 0)
// {
// L1 = CACHE(L1_SIZE, L1_ASSOC, L1_BLOCKSIZE);
// VC = CACHE(VC_NUM_BLOCKS * L1_BLOCKSIZE, VC_NUM_BLOCKS, L1_BLOCKSIZE);
// }
// else if (L2_SIZE != 0 && VC_NUM_BLOCKS == 0)
// {
// L1 = CACHE(L1_SIZE, L1_ASSOC, L1_BLOCKSIZE);
// L2 = CACHE(L2_SIZE, L2_ASSOC, L1_BLOCKSIZE);
// }
// else
// {
L1 = CACHE(L1_SIZE, L1_ASSOC, L1_BLOCKSIZE);
// }
FILE *fp = fopen(Trace_file, "r");
if (fp == nullptr)
{
cerr << "Error opening trace file." << endl;
return 1;
}
char operation;
unsigned long address;
stringstream ss;
while (fscanf(fp, " %c %lx", &operation, &address) != EOF)
{
if (operation == 'r' || operation == 'R')
{
L1.readFromAddress(address);
// cout << "Trace_file woooooooow:\t" << endl;
}
if (operation == 'w' || operation == 'w')
{
L1.writeToAddress(address);
}
}
fclose(fp);
cout << "\n===== L1 contents =====";
L1.CacheStatus();
return 0;
}