-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetAssociative.java
More file actions
176 lines (154 loc) · 3.1 KB
/
setAssociative.java
File metadata and controls
176 lines (154 loc) · 3.1 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cache;
import java.util.*;
public class setAssociative {
static int [][] cacheMemory;
static String []tagarr;
static int MM; //main memory size
static int cs; //cache size
static int cl; //no of cache lines
static int bs; //blocks
static int pa; //bits for physical address
static int cm; // bits for cache memory
static int Off; //bits for block offset
static int tb; //no of tag bits
static int sets; // no of sets in set associative mapping
static int index; //no of bits for set Index
static void Read(String address)
{
String tag = "";
for (int i=0; i<=tb-1;i++)
{
tag=tag+address.charAt(i);
}
String Si= "";
for(int i = tb ; i<pa-Off;i++)
{
Si=Si+address.charAt(i);
}
String bo = "";
for (int i=pa-Off;i<pa;i++)
{
bo=bo+address.charAt(i);
}
boolean ans = true;
int start=(Integer.parseInt(Si,2)*2);
int end=start+2;
for (int i=start ; i<end;i++)
{
if (tag.equals(tagarr[i]))
{
ans=false;
System.out.print("\nCache Hit");
System.out.print("\n Data in the address : "
+ cacheMemory[i][Integer.parseInt(bo,2)] );
print();
break;
}
}
if(ans)
{
System.out.print("\nCache Miss");
print();
}
}
static void Write(String address,int data)
{
String tag = "";
for (int i=0; i<=tb-1;i++)
{
tag=tag+address.charAt(i);
}
String z= "";
for(int i = tb ; i<pa-Off;i++)
{
z=z+address.charAt(i);
}
String Bo = "";
for (int i=pa-Off;i<pa;i++)
{
Bo=Bo+address.charAt(i);
}
boolean ans =true;
int start=(Integer.parseInt(z,2)*2);
int end=start+2;
for (int i=start ; i<end;i++)
{
if (tagarr[i].equals("") || tag.equals(tagarr[i]))
{
ans=false;
tagarr[i]=tag;
cacheMemory[i][Integer.parseInt(Bo,2)]=data;
break;
}
}
if (ans)
{
for (int i = 0 ; i<Off;i++)
{
cacheMemory[start][i]=0;
}
tagarr[start]=tag;
cacheMemory[start][Integer.parseInt(Bo,2)]=data;
}
}
static void print()
{
System.out.println("\n**********CACHE MEMORY**********");
for (int i=0 ; i<cl;i++)
{
for (int j = 0 ; j<bs ; j++)
{
System.out.print(cacheMemory[i][j]+" ");
}
System.out.println();
}
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter Main Memory size : ");
MM=scan.nextInt();
System.out.print("Enter Cache size : ");
cs = scan.nextInt();
System.out.print("Enter Block Size : ");
bs = scan.nextInt();
scan.close();
cl=cm/bs;
pa=0;
int x = MM;
while(x!=1)
{
x = x/2;
pa++;
}
cm=0;
int y = cm;
while(y!=1)
{
y = y/2;
cm++;
}
Off=0;
int z=bs;
while(z!=1)
{
z = z/2;
Off++;
}
sets=cl/2;
index=0;
int w=sets;
while(w!=1)
{
w=w/2;
index++;
}
tb=pa-(Off+index);
cacheMemory=new int[cl][bs];
tagarr=new String[cl];
for (int i=0;i<cl ; i++)
{
tagarr[i]="";
}
}
}