forked from jiangxincode/CacheSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.cpp
447 lines (387 loc) · 11.6 KB
/
base.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
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
/** \brief definition of almost all globle variables.
*
* \param
* \param
* \return
*
*/
#include "base.h"
/******************************************/
unsigned int long i_cache_size = 0; //cache size
unsigned int long i_cache_line_size = 0; //cacheline size
unsigned int long i_cache_set = 0; //cache set
unsigned int long i_num_line = 0; //How many lines of the cache.
unsigned int long i_num_set = 0; //How many sets of the cache.
ASSOC t_assoc = direct_mapped; //associativity method,default direct_mapped
REPLACE t_replace = none; //replacement policy,default Random
WRITE t_write = write_back; //write policy,default write_back
/******************************************/
/******************************************/
short unsigned int bit_block = 0; //How many bits of the block.
short unsigned int bit_line = 0; //How many bits of the line.
short unsigned int bit_tag = 0; //How many bits of the tag.
short unsigned int bit_set = 0; //How many bits of the set.
/******************************************/
/******************************************/
unsigned long int i_num_access = 0; //Number of cache access
unsigned long int i_num_load = 0; //Number of cache load
unsigned long int i_num_store = 0; //Number of cache store
unsigned long int i_num_space = 0; //Number of space line
unsigned long int i_num_hit = 0; //Number of cache hit
unsigned long int i_num_load_hit = 0; //Number of load hit
unsigned long int i_num_store_hit = 0; //Number of store hit
float f_ave_rate = 0.0; //Average cache hit rate
float f_load_rate = 0.0; //Cache hit rate for loads
float f_store_rate = 0.0; //Cache hit rate for stores
/******************************************/
std::bitset<32> cache_item[MAX_CACHE_LINE]; // [31]:valid,[30]:hit,[29]:dirty,[28]-[0]:data
unsigned long int LRU_priority[MAX_CACHE_LINE]; //For LRU policy's priority
unsigned long int current_line = 0; // The line num which is processing
unsigned long int current_set = 0; // The set num which is processing
unsigned long int i=0,j=0; //For loop
unsigned long int temp = 0; //A temp varibale
/*************************************************
// functions.cpp
// 1.Defination of almost all functions.
*************************************************/
using namespace std;
bool GetHitNum(char *address)
{
bool is_store = false;
bool is_load = false;
bool is_space = false;
bool hit = false;
switch(address[0])
{
case 's':
is_store = true;
break;
case 'l':
is_load = true;
break;
//case ' ':break; //Waring if a line has nothing,the first of it is a '\0' nor a ' '
case '\0':
is_space = true;
break; //In case of space lines
default:
cout << "The address[0] is:" <<address[0] << endl;
cout << "ERROR IN JUDGE!" << endl;
return false;
}
temp = strtoul(address+2,NULL,16);
bitset<32> flags(temp); // flags if the binary of address
#ifndef NDEBUG
cout << flags << endl;
#endif // NDEBUG
hit = IsHit(flags);
if(hit && is_load) // 命中,读操作
{
i_num_access++;
i_num_load++;
i_num_load_hit++;
i_num_hit++;
#ifndef NDEBUG
cout << "Loading" << endl;
cout << "Hit" << endl;
cout << "Read from Cache!" << endl;
#endif // NDEBUG
if(t_replace == LRU)
{
LruHitProcess();
}
}
else if(hit && is_store) // 命中,写操作
{
i_num_access++;
i_num_store++;
i_num_store_hit++;
i_num_hit++;
#ifndef NDEBUG
cout << "Storing" << endl;
cout << "Hit" << endl;
cout << "Write to Cache" << endl;
#endif // NDEBUG
cache_item[current_line][29] = true; //设置dirty为true
if(t_replace == LRU)
{
LruHitProcess();
}
}
else if((!hit) && is_load) // 没命中,读操作
{
i_num_access++;
i_num_load++;
#ifndef NDEBUG
cout << "Loading" << endl;
cout << "Not Hit" << endl;
#endif // NDEBUG
GetRead(flags); // read data from memory
#ifndef NDEBUG
cout << "Read from Cache!" << endl;
#endif // NDEBUG
if(t_replace == LRU)
{
LruUnhitSpace();
}
}
else if((!hit) && is_store) // 没命中,写操作
{
i_num_access++;
i_num_store++;
#ifndef NDEBUG
cout << "Storing" << endl;
cout << "Not Hit" << endl;
#endif // NDEBUG
GetRead(flags); // read data from memory
#ifndef NDEBUG
cout << "Write to Cache" << endl;
#endif // NDEBUG
cache_item[current_line][29] = true; //设置dirty为true
if(t_replace == LRU)
{
LruUnhitSpace();
}
}
else if(is_space)
{
i_num_space++;
}
else
{
cerr << "Something ERROR" << endl;
return false;
}
if(i_num_space != 0)
{
#ifndef NDEBUG
cout << "There have " << i_num_space << " space lines" << endl;
#endif // NDEBUG
}
return true;
}
bool IsHit(bitset<32> flags)
{
bool ret = false;
if(t_assoc == direct_mapped)
{
bitset<32> flags_line; // a temp variable
for(j=0,i=(bit_block); i<(bit_block+bit_line); j++,i++) //判断在cache多少行
{
flags_line[j] = flags[i];
}
current_line = flags_line.to_ulong();
assert(cache_item[current_line][31] == true);
if(cache_item[current_line][30]==true) //判断hit位是否为真
{
ret = true;
for(i=31,j=28; i>(31ul-bit_tag); i--,j--) //判断标记是否相同,i:address,j:cache
{
if(flags[i] != cache_item[current_line][j])
{
ret = false;
break;
}
}
}
}
else if(t_assoc == full_associative)
{
for(temp=0; temp<i_num_line; temp++)
{
if(cache_item[temp][30]==true) //判断hit位是否为真
{
ret = true;
for(i=31,j=28; i>(31ul-bit_tag); i--,j--) //判断标记是否相同,i:address,j:cache
{
if(flags[i] != cache_item[temp][j])
{
ret = false;
break;
}
}
}
if(ret == true)
{
current_line = temp;
break;
}
}
}
else if(t_assoc == set_associative)
{
bitset<32> flags_set;
for(j=0,i=(bit_block); i<(bit_block+bit_set); j++,i++) //判断在cache多少组
{
flags_set[j] = flags[i];
}
current_set = flags_set.to_ulong();
for(temp=(current_set*i_cache_set); temp<((current_set+1)*i_cache_set); temp++)
{
if(cache_item[temp][30]==true) //判断hit位是否为真
{
ret = true;
for(i=31,j=28; i>(31ul-bit_tag); i--,j--) //判断标记是否相同,i:address,j:cache
{
if(flags[i] != cache_item[temp][j])
{
ret = false;
break;
}
}
}
if(ret == true)
{
current_line = temp;
break;
}
}
}
return ret;
}
void GetRead(bitset<32> flags)
{
if(t_assoc == direct_mapped)
{
if(cache_item[current_line][30] == false) //hit is false
{
#ifndef NDEBUG
cout << "Read from Main Memory to Cache!" << endl;
#endif // NDEBUG
for(i=31,j=28; i>(31ul-bit_tag); i--,j--) //设置标记
{
cache_item[current_line][j] = flags[i];
assert(j>0);
}
cache_item[current_line][30] = true; //设置hit位为true
}
else
{
GetReplace(flags);
}
}
else if(t_assoc == full_associative)
{
bool space = false;
for(temp=0; temp<i_num_line; temp++)
{
if(cache_item[temp][30] == false) //find a space line
{
space = true;
break;
}
}
if(space == true)
{
current_line = temp; // 此处,temp不需减1,因为一旦发现空行,上面for循环会break,此时temp尚未++
#ifndef NDEBUG
cout << "Read from Main Memory to Cache!" << endl;
#endif // NDEBUG
for(i=31,j=28; i>(31ul-bit_tag); i--,j--) //设置标记
{
cache_item[current_line][j] = flags[i];
assert(j>0);
}
cache_item[current_line][30] = true; //设置hit位为true.
if(t_replace == LRU)
{
LruUnhitSpace();
}
}
else
{
GetReplace(flags);
}
}
else if(t_assoc == set_associative)
{
bool space = false;
for(temp=(current_set*i_cache_set); temp<((current_set+1)*i_cache_set); temp++)
{
if(cache_item[temp][30] == false) //find a space line
{
space = true;
break;
}
}
if(space == true)
{
current_line = temp; // 此处,temp不需减1,因为一旦发现空行,上面for循环会break,此时temp尚未++
#ifndef NDEBUG
cout << "Read from Main Memory to Cache!" << endl;
#endif // NDEBUG
for(i=31,j=28; i>(31ul-bit_tag); i--,j--) //设置标记
{
cache_item[current_line][j] = flags[i];
assert(j>0);
}
cache_item[current_line][30] = true; //设置hit位为true.
if(t_replace == LRU)
{
LruUnhitSpace();
}
}
else
{
GetReplace(flags);
}
}
}
void GetReplace(bitset<32> flags)
{
if(t_assoc == direct_mapped)
{
}
else if(t_assoc == full_associative)
{
if(t_replace == Random)
{
current_line = rand()/(RAND_MAX/i_num_line+1); // a random line in(0,i_num_line-1)
}
else if(t_replace == LRU)
{
LruUnhitUnspace();
}
}
else if(t_assoc == set_associative) // 从本组中任选一行,进行替换
{
if(t_replace == Random)
{
temp = rand()/(RAND_MAX/i_cache_set+1); // a random line in(0,i_cache_set-1)
current_line = current_set*i_cache_set+temp; // a random line in current_set
}
else if(t_replace == LRU)
{
LruUnhitUnspace();
}
}
if(cache_item[current_line][29] == true) //dirty位必须为1才写入
{
GetWrite(); //写入内存
}
#ifndef NDEBUG
cout << "Read from Main Memory to Cache: " << endl;
#endif // NDEBUG
for(i=31,j=28; i>(31ul-bit_tag); i--,j--) //设置标记
{
cache_item[current_line][j] = flags[i];
assert(j>0);
}
cache_item[current_line][30] = true; //设置hit位为true
}
void GetWrite() //写入内存
{
#ifndef NDEBUG
cout << "Writing to the Main Memory!" <<endl;
#endif
cache_item[current_line][29] = false; //设置dirty为false
cache_item[current_line][30] = false; //设置hit为false
}
void GetHitRate()
{
assert(i_num_access != 0);
assert(i_num_load != 0);
assert(i_num_store != 0);
f_ave_rate = ((double)i_num_hit)/i_num_access; //Average cache hit rate
f_load_rate = ((double)i_num_load_hit)/i_num_load; //Cache hit rate for loads
f_store_rate = ((double)i_num_store_hit)/i_num_store; //Cache hit rate for stores
}