-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
161 lines (149 loc) · 4.72 KB
/
main.c
File metadata and controls
161 lines (149 loc) · 4.72 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <time.h>
typedef struct {
long tag;
bool valid;
int index;
int accesses;
long time;
} line;
void killCache(line** cache) {
free(*cache);
free(cache);
}
bool check_hit(long address, line* l, long tagMask) {
bool ret_value = (l->valid && (l->tag == (address & tagMask)));
if (ret_value) {
l->accesses++;
l->time = clock();
}
return ret_value;
}
int main() {
clock_t start = clock();
int setNum;
int lineNum;
int blockSize;
int addrBitsNum;
char rp[3];
int hTime;
int mPenalty;
int s;
int b;
line** cache;
float references = 0;
float misses = 0;
float hits = 0;
printf("What kind of cache are we dealing with here?\n");
scanf("%d", &setNum);
s = log2(setNum);
scanf("%d", &lineNum);
scanf("%d", &blockSize);
b = log2(blockSize);
scanf("%d", &addrBitsNum);
scanf("%s", rp);
scanf("%d", &hTime);
scanf("%d", &mPenalty);
cache = malloc(sizeof(line *) * setNum);
for (int i = 0; i < setNum; i++) {
cache[i] = malloc(sizeof(line *) * lineNum);
}
for (int i = 0; i < setNum; i++) {
for (int j = 0; j < lineNum; j++) {
cache[i][j].tag = 0;
cache[i][j].valid = false;
cache[i][j].index = 0;
cache[i][j].accesses = 0;
cache[i][j].time = 0;
}
}
long bMask = ~(~0 << b);
long sMask = ~(~0 << s) << b;
long tMask = ~0 << (s + b);
printf("Please enter your addresses in hex form, or -1 to quit\n");
char input[9];
unsigned long address = 0;
while(strcmp("-1", input) != 0) {
scanf("%s", input);
if (strcmp("-1", input) == 0) {
break;
} else {
sscanf(input, "%lx", &address);
}
bool hit = false;
for (int i = 0; i < lineNum; i++) {
if (check_hit(address, &cache[(address & sMask) >> b][i], tMask)) {
hit = true;
break;
}
}
if (hit) {
references++;
hits++;
printf("%lx H\n", address);
}
else {
references++;
misses++;
bool done = false;
while (!done) {
for (int i = 0; i < lineNum; i++) {
if (!cache[(address & sMask) >> b][i].valid) {
cache[(address & sMask) >> b][i].valid = true;
cache[(address & sMask) >> b][i].tag = (address & tMask);
printf("%lx M\n", address);
done = true;
break;
}
}
if (done) {
break;
}
if (strcmp(rp, "LFU") != 0) {
int temp = 100000;
for (int i = 0; i < lineNum; i++) {
if (!(temp < cache[(address & sMask) >> b][i].accesses)) {
temp = cache[(address & sMask) >> b][i].accesses;
}
}
for (int i = 0; i < lineNum; i++) {
if (temp == cache[(address & sMask) >> b][i].accesses) {
cache[(address & sMask) >> b][i].valid = true;
cache[(address & sMask) >> b][i].tag = (address & tMask);
printf("%lx M\n", address);
done = true;
break;
}
}
} else {
long temp = 99999999999;
for (int i = 0; i < lineNum; i++) {
if (!(temp < cache[(address & sMask) >> b][i].time)) {
temp = cache[(address & sMask) >> b][i].time;
}
}
for (int i = 0; i < lineNum; i++) {
if (temp == cache[(address & sMask) >> b][i].time) {
cache[(address & sMask) >> b][i].valid = true;
cache[(address & sMask) >> b][i].tag = (address & tMask);
printf("%lx M\n", address);
done = true;
break;
}
}
}
}
}
}
clock_t end = clock();
int total = (hTime*hits) + ((hTime+mPenalty)*misses);
float mRate = ((misses/references) * 100);
int final = (int)(mRate < 0 ? (mRate - 0.5) : (mRate + 0.5));
printf("%d %d\n", final, total);
killCache(cache);
return 0;
}