-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.cc
401 lines (351 loc) · 9.38 KB
/
funcs.cc
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
#ifndef __X_H_INCLUDED__
#endif
#include <stdio.h>
#include <unistd.h> //for the getopt function
#include <stdlib.h> //for atoi
#include <iostream>
#include <string>
#include <strings.h>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <assert.h>
#include <set>
#include <getopt.h>
#include "common.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <strings.h>
#include <cstdlib>
#include <stdlib.h>
#include <sstream>
#include <string.h>
#include <iomanip>
#include <stddef.h>
#include "CacheSet.h"
#define KILOBYTE 1024
#define INT_SIZE 4
#define MEM_READ 0
#define MEM_WRITE 1
void updateDataRam(int index, unsigned int data) {
ramAllocation[index] = data;
}
unsigned int readDataRamInt(int index) {
return ramAllocation[index];
}
void cacheMemmoryAllocation() {
chacheAllocation = (CacheSet*) malloc(
((totalCacheSets) * sizeof(CacheSet)));
ptrdiff_t k = 0;
try {
for (; k < totalCacheSets; k++)
new (chacheAllocation + k) CacheSet(blockSize, tagbits_tot,
indexbits_tot, offsetbits_tot, associativity, k);
}
catch (...) {
for (; k > 0; k--)
(chacheAllocation + k)->~CacheSet();
throw;
}
if (split) {
chacheAllocationIns = (CacheSet*) malloc(
((totalCacheSets) * sizeof(CacheSet)));
ptrdiff_t k = 0;
try {
for (; k < totalCacheSets; k++)
new (chacheAllocationIns + k) CacheSet(blockSize, tagbits_tot,
indexbits_tot, offsetbits_tot, associativity, k);
}
catch (...) {
for (; k > 0; k--)
(chacheAllocationIns + k)->~CacheSet();
// free(chacheAllocation);
throw;
}
}
}
unsigned int readDataCache(int index, int blockNoInSet, int offset) {
unsigned int num = chacheAllocation[index].set[blockNoInSet].data[offset];
return num;
}
char* emptyoffset() {
char* emptyOffset = (char*) malloc(sizeof(char) * (offsetbits_tot + 1));
memset(emptyOffset, '0', offsetbits_tot + 1);
emptyOffset[offsetbits_tot] = '\0';
return emptyOffset;
}
void ramMemmoryAllocation() {
int i;
ramAllocation = (unsigned int*) malloc(ramSize * 4);
for (i = 0; i < (int) (ramSize / INT_SIZE); i++) {
updateDataRam(i, 0);
}
}
std::string wordAddress(int i) {
std::stringstream stream;
stream << "" << std::setfill('0') << std::setw(INT_SIZE * 2) << std::hex
<< i;
std::string t = stream.str();
stream.str("");
return t;
}
void displayStatistics() {
std::cout << "STATISTICS:" << std::endl;
int totalAccess = (total_Read + total_Write);
std::cout << "";
std::cout << "Total Mem Acces = " << totalAccess << std::endl;
if (!split) {
printf("Misses: Total %d DataReads %d DataWrites %d\n", (missRead+ missWrite),
missRead, missWrite);
printf("Miss Rate: %f %f %f\n", ((float) miss / totalAccess),
((float) missRead / totalAccess),
((float) missWrite / totalAccess));
printf("Number of Dirty Blocks Evicted From the Cache: %d\n",
dirtyBlockEvictionCounter);
}
if (split) {
printf("L1I Misses: Total %d InstructionReads %d\n", totalICReads,
missReadIC);
printf("L1I Miss Rate: %f\n", missReadIC / (float) totalICReads);
printf("L1D Misses: Total %d DataReads %d DataWrites %d\n", (missRead+ missWrite),
missRead, missWrite);
printf("L1D Miss Rate: %f %f %f\n", ((float) miss / totalAccess),
((float) missRead / totalAccess),
((float) missWrite / totalAccess));
printf("OverAll cache Misses %d\n", (missRead+ missWrite+missReadIC));
printf("Number of Dirty Blocks Evicted From L1D Cache: %d\n",
dirtyBlockEvictionCounter);
}
}
int binaryToInteger(char *bin) {
int b, k, m, n;
int len, sum;
sum = 0;
len = strlen(bin) - 1;
for (k = 0; k <= len; k++) {
n = (bin[k] - '0');
if ((n > 1) || (n < 0)) {
return 0;
}
for (b = 1, m = len; m > k; m--) {
b *= 2;
}
sum = sum + n * b;
}
return (sum);
}
void displayCache(CacheSet *cacheSet, int type) {
if (split) {
if (type == 0) {
std::cout << "L1 DATA CACHE CONTENTS:\n";
std::cout << "Set\tV\tTag\t\tDirty\t\tWords\n";
}
if (type == 1) {
std::cout << "L1 INSTRUCTION CACHE CONTENTS:\n";
std::cout << "Set\tV\tTag\t\tWords\n";
}
} else {
std::cout << "CACHE CONTENTS:\n";
std::cout << "Set\tV\tTag\t\tDirty\t\tWords\n";
}
for (int i = 0; i < (totalCacheSets); i++) {
for (int j = 0; j < associativity; j++) {
std::cout << std::hex << i << "\t" << cacheSet[i].set[j].v << "\t"
<< std::setfill('0') << std::setw(8) << std::hex
<< (int) binaryToInteger(cacheSet[i].set[j].tag) << "\t";
if (type != 1) {
std::cout << cacheSet[i].set[j].dirty << "\t" << "\t";
}
std::cout << " ";
for (int k = 0; k < blockSize; k++) {
std::cout << std::setfill('0') << std::setw(8) << std::hex
<< (int) readDataCache(i, j, k) << " ";
}
std::cout << std::endl;
}
}
}
void displayMainMemory() {
int begin_address = strtoul("003f7f00", NULL, 16);
std::cout << std::endl << "MAIN MEMORY:" << std::endl
<< "Address Words";
for (int i = begin_address / INT_SIZE;
i < (begin_address + KILOBYTE) / INT_SIZE; i++) {
if (i % 8 == 0)
std::cout << std::endl << std::setfill('0') << std::setw(8)
<< std::hex << i * INT_SIZE << " ";
std::cout << " " << std::setfill('0') << std::setw(8)
<< ramAllocation[i];
}
std::cout << std::endl;
}
void parseMemoryAddress(char *bformatted, char* tag, char* index,
char* offset) {
int i = 0;
assert(tag != NULL);
tag[tagbits_tot] = '\0';
for (i = 0; i < tagbits_tot; i++) {
tag[i] = bformatted[i];
}
assert(index != NULL);
index[indexbits_tot] = '\0';
for (i = tagbits_tot + 1; i < indexbits_tot + tagbits_tot + 1; i++) {
index[i - tagbits_tot - 1] = bformatted[i - 1];
}
assert(offset != NULL);
offset[offsetbits_tot] = '\0';
for (i = indexbits_tot + tagbits_tot + 2;
i < offsetbits_tot + indexbits_tot + tagbits_tot + 2; i++) {
offset[i - indexbits_tot - tagbits_tot - 2] = bformatted[i - 2];
}
// printf("Tag: %s (%i)\n", tag, binaryToInteger(tag));
// printf("Index: %s (%i)\n", index, binaryToInteger(index));
// printf("Offset: %s (%i)\n", offset, binaryToInteger(offset));
}
char *getBinary(unsigned int num) {
char* bstring;
int i;
/* Calculate the Binary String */
bstring = (char*) malloc(sizeof(char) * 33);
assert(bstring != NULL);
bstring[32] = '\0';
for (i = 0; i < 32; i++) {
bstring[32 - 1 - i] = (num == ((1 << i) | num)) ? '1' : '0';
}
return bstring;
}
bool checkMemSize(int memSize) {
switch (memSize) {
case 4:
case 8:
case 16:
case 32:
case 64:
return true;
break;
default:
break;
}
return false;
}
bool checkBlockSize(int blockSize) {
switch (blockSize) {
case 4:
case 8:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
return true;
break;
default:
break;
}
return false;
}
bool parseParams(int argc, char *argv[], int& mem_capacity, int& blockSize,
int &assosiativity, std::string &filename, bool &split) {
//needed for the parsing of command line options
int c;
bool c_flag, b_flag, a_flag, t_flag;
bool errflg = false;
c_flag = errflg = b_flag = a_flag = t_flag = false;
int digit_optind = 0;
extern char *optarg;
extern int optopt;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = { { "wbwa", 0, 0, 0 }, { "wtwn",
0, 0, 0 },{ "wbwn", 0, 0, 0 },{ "wtwa", 0, 0, 0 }, { 0, 0, 0, 0 } };
c = getopt_long(argc, argv, "c:a:b:s::0:1:2:3:", long_options,
&option_index);
if (c == -1)
break;
switch (c) {
case 0:
if (strcmp(long_options[option_index].name, "wbwa") == 0) {
writeBack = true;
writeThrough = false;
writeAllocate = true;
writeNoAllocate = false;
}
if (strcmp(long_options[option_index].name, "wtwn") == 0) {
writeThrough = true;
writeBack = false;
writeNoAllocate = true;
writeAllocate = false;
}
if (strcmp(long_options[option_index].name, "wbwn") == 0) {
writeThrough = false;
writeBack = true;
writeNoAllocate = true;
writeAllocate = false;
}
if (strcmp(long_options[option_index].name, "wtwa") == 0) {
writeThrough = true;
writeBack = false;
writeNoAllocate = false;
writeAllocate = true;
}
break;
case 's':
split = true;
break;
case 't':
filename = optarg;
t_flag = true;
break;
case 'c':
mem_capacity = atoi(optarg);
c_flag = true;
break;
case 'b':
blockSize = atoi(optarg);
b_flag = true;
break;
case 'a':
assosiativity = atoi(optarg);
a_flag = true;
break;
case ':': //: -c without operand
fprintf(stderr, "Option -%c requires an operand\n", optopt);
errflg++;
break;
case '?':
fprintf(stderr, "Unrecognised option: -%c\n", optopt);
errflg = true;
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
if (!checkBlockSize(blockSize)) {
std::cout
<< "\nPossible Block capacities are 4, 8, 16, 32, 64, 128, 256, or 512.\n";
errflg=true;
}
if (!checkMemSize(mem_capacity)) {
std::cout << "\nPossible Memmory capacities are 4, 8, 16, 32, or 64.";
errflg=true;
}
//check if we have all the options and have no illegal options
if (errflg || !c_flag || !b_flag || !a_flag) {
fprintf(stderr,
"usage: %s -c<capacity> -b<wordsize> -a<associativity> -s<for Split> < inputTrace.trace > outputFile.txt \nWrite policies are as follows: --wbwa/--wbwn/--wtwa/--wtwn",
argv[0]);
return false;
}
return true;
}