-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsim.c
194 lines (185 loc) · 4.5 KB
/
csim.c
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
#include "cachelab.h"
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#define MAXLEN 100
int s,S,E,b;
int hits=0, misses=0, evictions=0;
typedef struct _Line
{
int v;
int tag;
int LRU_p; // priority for LRU logic
} Line;
typedef enum _Exec_Rst
{HIT, MISS, MISS_HIT, MISS_EVICTION, MISS_EVICTION_HIT} Exec_Rst;
long long int get_Set_Index(long long int address)
{
long long int s_mask = (1<<s)-1;
return (address>>b)&s_mask;
}
long long int get_Tag(long long int address)
{
long long int tag_mask = (1<<(64-s-b))-1; // 64-bit system
return (address>>(s+b))&tag_mask;
}
void update_LRU(long long int set_index, int id,
long long int tag, Line** cache)
{
cache[set_index][id].v=1;
cache[set_index][id].tag=tag;
int i;
for(i=0;i<E;i++)
{
if(cache[set_index][i].v==1
&&cache[set_index][i].LRU_p>cache[set_index][id].LRU_p)
{
cache[set_index][i].LRU_p--;
}
}
cache[set_index][id].LRU_p=E-1;
}
Exec_Rst exec_Instr(char instr[], Line** cache)
{
long long int address;
int size;
char type;
sscanf(instr, " %c %llx,%d", &type, &address, &size);
long long int set_index=get_Set_Index(address);
long long int tag=get_Tag(address);
int i;
for(i=0;i<E;i++)
{
if(cache[set_index][i].v==1
&& cache[set_index][i].tag==tag)
{// find a hit
hits++;
update_LRU(set_index,i,tag,cache);
if(type=='M')
{// Modify hits twice
hits++;
}
return HIT;
}
}
misses++; // not a hit
for(i=0;i<E;i++)
{
if(cache[set_index][i].v==0)
{// find vacancy, no eviction needed
update_LRU(set_index,i,tag,cache);
if(type=='M')
{// Modify hits after misses
hits++;
return MISS_HIT;
}
return MISS;
}
}
evictions++; // no vacancy, meaning there should be an eviction
for(i=0;i<E;i++)
{
if(cache[set_index][i].LRU_p==0)
{
update_LRU(set_index,i,tag,cache);
if(type=='M')
{
hits++;
return MISS_EVICTION_HIT;
}
return MISS_EVICTION;
}
}
return -1;
}
Line** init_Cache()
{
Line **cache=(Line **)malloc(S*sizeof(Line *));
int i,j;
for(i=0;i<S;i++)
{
cache[i]=(Line *)malloc(E*sizeof(Line));
for(j=0;j<E;j++)
{
cache[i][j].v=0; // set all lines unwarmed
cache[i][j].tag=-1;
cache[i][j].LRU_p=-1;
}
}
return cache;
}
int main(int argc, char *argv[])
{
int verbose=0;
char t[MAXLEN];
int opt;
while((opt=getopt(argc,argv,"hvs:E:b:t:"))!=-1)
{// see "man 3 getopt" for help
switch (opt)
{
case 'v':
verbose=1;
break;
case 's':
s=atoi(optarg);
S=1<<s;
break;
case 'E':
E=atoi(optarg);
break;
case 'b':
b=atoi(optarg);
break;
case 't':
strcpy(t,optarg);
break;
default:
fprintf(stderr,"Usage: %s \n",argv[0]);
exit(1);
}
}
Line **cache=init_Cache();
FILE* fin;
char instr[MAXLEN];
int exec_rst;
if((fin=fopen(t,"r"))==NULL)
{
printf("Error: File not found.\n");
exit(1);
}
while(fgets(instr,MAXLEN,fin))
{
if(instr[0]==' ')
{// not an instruction load
exec_rst=exec_Instr(instr, cache);
if(verbose)
{
switch (exec_rst)
{
case HIT:
printf("%s hit\n",instr+1);
break;
case MISS:
printf("%s miss\n",instr+1);
break;
case MISS_HIT:
printf("%s miss hit\n",instr+1);
break;
case MISS_EVICTION:
printf("%s miss eviction\n",instr+1);
break;
case MISS_EVICTION_HIT:
printf("%s miss eviction hit\n",instr+1);
break;
default:
printf("Error.\n");
}
}
}
}
printSummary(hits,misses,evictions);
return 0;
}