-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheSimRR.c
More file actions
95 lines (84 loc) · 2.05 KB
/
CacheSimRR.c
File metadata and controls
95 lines (84 loc) · 2.05 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
/**
* CacheSim.c
* This program is designed for class exercise only.
* It is provided as is. There is no warranty or support of any kind.
*
* Krerk Piromsopa, Ph.D.
* Department of Computer Engineering
* Chulalongkorn University
* Bangkok, Thailand.
*
* History
* 2013 - Initial design
* 2015 - Refactor/Clean code
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CacheSimAsso.h"
long MISS;
long HIT;
# define N 2
struct Cache cache[INDEX_SIZE][N];
int rr[INDEX_SIZE];
int init() {
MISS=0; HIT=0;
for(int i=0;i<INDEX_SIZE;i++){
for(int j=0;j<N;j++){
cache[i][j].valid=0;
cache[i][j].tag=0;
cache[i][j].dirty=0;
}
}
}
int calAddr(unsigned long addr,unsigned long *tag,unsigned long *idx,unsigned long *offset) {
unsigned long tmp;
*tag=addr>>(OFFSETLEN+INDEXLEN);
tmp=0;
for(int i=0;i<INDEXLEN;i++) { tmp<<=1; tmp|=0x01; }
*idx=addr>>OFFSETLEN & tmp;
tmp=0;
for(int i=0;i<OFFSETLEN;i++) { tmp<<=1; tmp|=0x01; }
*offset=addr & tmp;
}
int access(unsigned long addr){
unsigned long offset, tag, idx;
calAddr(addr,&tag,&idx,&offset);
int k=rr[idx];
int found=0;
for(int j=0;j<N;j++){
if(cache[idx][j].tag==tag && cache[idx][j].valid){
HIT++;
found=1;
break;
}
}
if(!found){
MISS++;
// replace
cache[idx][k].valid=1;
cache[idx][k].tag = tag;
cache[idx][k].lastuse = 0;
rr[idx] = (k+1)%N;
}
}
int main(int argc,char *argv[]){
printf("CacheSim v.2015\nThis program is designed for class exercise only.\nBy Krerk Piromsopa, Ph.D.\n");
init();
FILE *input;
char buff[1025];
unsigned long myaddr;
if (argc<2) {
fprintf(stderr,"Usage:\n\t%s address_file\n",argv[0]);
exit(-1);
}
input=fopen(argv[1],"r");
//read file
while (fgets(& buff[0],1024,input)) {
sscanf(buff,"0x%x",&myaddr);
access(myaddr);
}
printf("====RR====\nCachesize:%d\n",X);
printf("N: %d\n",N);
printf("HIT:%7d MISS: %7d\n",HIT,MISS);
}