-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFully_Associative_Mapping.java
More file actions
269 lines (246 loc) · 8.32 KB
/
Fully_Associative_Mapping.java
File metadata and controls
269 lines (246 loc) · 8.32 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
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;
public class Fully_Associative_Mapping{ // FULLY ASSOCIATIVE CACHE
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
System.out.println("Enter Block size:");
int b = scn.nextInt(); // block size/ cache line size
while (!ispowerof2(b) || b<=0) {
System.out.println("Enter valid block size"); b = scn.nextInt();
}
System.out.println("Enter the number of cache lines:");
int cl = scn.nextInt(); // no of cache lines
while (!ispowerof2(cl) || cl<= 0) {
System.out.println("Enter valid number of cache lines"); cl = scn.nextInt();
}
int cols = b;
ArrayList<Integer> freq1 = new ArrayList<>();
ArrayList<Integer> freq2 = new ArrayList<>();
System.out.println("Enter the number of test cases:");
int tc = scn.nextInt();
HashMap<Integer, ArrayList<Integer>> L1 = new HashMap<>();
HashMap<Integer, ArrayList<Integer>> L2 = new HashMap<>();
for (int i = 0; i < tc; i++) {
System.out.println("Enter the address:");
int address = scn.nextInt();
while (address > 65535 || address < 0) {
System.out.println("Please enter a valid address, i.e. an integer from 0 to 65535");
address = scn.nextInt();
}
get_address(address,b);
int r = address / cols; // block no // cache independent
int c = address % cols; // index no
freq1.add(r);
freq2.add(r);
System.out.println("Enter read/write command");
String str = scn.next();
boolean cmd1 = str.toUpperCase().equals("READ");
boolean cmd2 = str.toUpperCase().equals("WRITE");
if (cmd1 || cmd2) {
int data = Integer.MAX_VALUE;
if (cmd2) {
System.out.println("Enter data to be stored");
data = scn.nextInt();
}
if (L1.containsKey(r)) {
System.out.println("Hit in Level1...!");
if (cmd1) { // if read
if (L1.get(r).get(c) == Integer.MAX_VALUE) {
System.out.println("EMPTY!");
} else {
System.out.println(L1.get(r).get(c));
}}
else { // if write
L1.get(r).set(c, data);
L2.get(r).set(c, data);
} }
else if (L2.containsKey(r)) {
System.out.println("Hit in Level2...!"); // read/write
if (cmd1) {
if (L2.get(r).get(c) == Integer.MAX_VALUE) {
System.out.println("EMPTY!");
} else {
System.out.println(L2.get(r).get(c));
}}
else { // if write
L2.get(r).set(c, data);
}
if (L1.size() < cl / 2) { // no replacement needed
L1.put(r, L2.get(r));
continue;
}
// replacement in L1
int tempdis = Integer.MIN_VALUE;
for (Entry<Integer, ArrayList<Integer>> mapElement : L1.entrySet()) {
int stackdis = 0;
for (int h = freq1.size() - 1; h >= 0; h--) {
if (freq1.get(h) == Integer.parseInt((mapElement.getKey())+"")) {
break;
}
stackdis++;
}
if (tempdis < stackdis) {
tempdis = stackdis;
}
}
int u = freq1.size() - tempdis - 1;
int val = freq1.get(u);
System.out.print("Block "); getbinary(val, b); System.out.println(" gets replaced in L1 cache");
L1.remove(val);
freq1 = removal(freq1, val);
// replacement done
L1.put(r, L2.get(r));
} else {
System.out.println("Address not found");
if (L1.size() == cl / 2 && L2.size() == cl) { // replace both
int tempdis = Integer.MIN_VALUE;
for (Entry<Integer, ArrayList<Integer>> mapElement : L1.entrySet()) {
int stackdis = 0;
for (int h = freq1.size() - 1; h >= 0; h--) {
if (freq1.get(h) == Integer.parseInt((mapElement.getKey())+"")) {
break;
}
stackdis++;
}if (tempdis < stackdis) {
tempdis = stackdis;
}
}
int u = freq1.size() - tempdis - 1;
int val = freq1.get(u);
System.out.print("Block "); getbinary(val, b); System.out.println(" gets replaced in L1 cache");
L1.remove(val);
freq1 = removal(freq1, val);
int tempdis2 = Integer.MIN_VALUE;
for (Entry<Integer, ArrayList<Integer>> mapElement : L2.entrySet()) {
int stackdis = 0;
for (int h = freq2.size() - 1; h >= 0; h--) {
if (freq2.get(h) == Integer.parseInt((mapElement.getKey())+"")) {
break;
}
stackdis++;
}
if (tempdis2 < stackdis) {
tempdis2 = stackdis;
}
}
int u1 = freq2.size() - tempdis2 - 1;
int val1 = freq2.get(u1);
L2.remove(val1);
System.out.print("Block "); getbinary(val1, b); System.out.println(" gets replaced in L2 cache");
freq2 = removal(freq2, val1);
ArrayList<Integer> helper = new ArrayList<>();
for (int j = 0; j < b; j++) {
helper.add(Integer.MAX_VALUE);
}
if (cmd2) {
helper.set(c, data);
}
L1.put(r, helper);
L2.put(r, helper);
// replace in both
} else if (L1.size() < cl / 2 && L2.size() < cl) { // no replacement
ArrayList<Integer> helper = new ArrayList<>();
for (int j = 0; j < b; j++) {
helper.add(Integer.MAX_VALUE); }
if (cmd2) {
helper.set(c, data);
}
L1.put(r, helper);
L2.put(r, helper);
// add in both
} else { // space in L2 but not in L1
int tempdis1 = Integer.MIN_VALUE;
for (Entry<Integer, ArrayList<Integer>> mapElement : L1.entrySet()) {
int stackdis = 0;
for (int h = freq1.size() - 1; h >= 0; h--) {
if (freq1.get(h) == Integer.parseInt((mapElement.getKey())+"")) {
break;
}
stackdis++;
}
if (tempdis1 < stackdis) {
tempdis1 = stackdis;
}
}
int u = freq1.size() - tempdis1 - 1;
int val = freq1.get(u);
L1.remove(val);
System.out.print("Block "); getbinary(val, b); System.out.println(" gets replaced in L1 cache");
freq1 = removal(freq1, val);
// replacement done
ArrayList<Integer> helper = new ArrayList<>();
for (int j = 0; j < b; j++) {
helper.add(Integer.MAX_VALUE);
}
if (cmd2) {
helper.set(c, data);
}
L1.put(r, helper);
L2.put(r, helper);
}
}
} else {
System.out.println("INVALID COMMAND");
}
}
System.out.println("--------------------------------------OUTPUT--------------------------------------");
System.out.println("LEVEL ONE CACHE:");
if (L1.size() == 0) {System.out.println("EMPTY CACHE");}
else{printfamap(L1, b);}
System.out.println();
System.out.println("LEVEL TWO CACHE:");
if (L2.size() == 0) {System.out.println("EMPTY CACHE");}
else {printfamap(L2, b);}
}
public static ArrayList<Integer> removal(ArrayList<Integer> freq, int item) {
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < freq.size(); i++) {
if (freq.get(i) != item) {
arr.add(freq.get(i));}
} return arr;
}
public static void printfamap(HashMap<Integer, ArrayList<Integer>> hp, int b) {
System.out.println("BLOCK NUMBER BLOCK ADDRESS BLOCK CONTENT");
for (Entry<Integer, ArrayList<Integer>> mapElement : hp.entrySet()) {
System.out.print(" " + mapElement.getKey() + " \t "); getbinary(mapElement.getKey(),b);
System.out.print(" \t");
for (int i = 0; i < mapElement.getValue().size(); i++) {
if (mapElement.getValue().get(i) == Integer.MAX_VALUE) {
System.out.print("Empty" + " ");
} else {System.out.print(mapElement.getValue().get(i) + " ");
}
}
System.out.println();
}
}
public static boolean ispowerof2(int n) {
if (n == 0) {
return false;
}else {
return ((int) (Math.floor(((Math.log(n) / Math.log(2))))))+1 == ((int) (Math.ceil((Math.log(n) / Math.log(2)))))+1;
}
}
public static void getbinary(int n, int b) {
b = getn(b);
String str = Integer.toBinaryString(n);
while (str.length()<16-b) {
str='0' + str;
}
System.out.print(str);
}
public static int getn(int n) {
return (int)(Math.log(n) / Math.log(2));}
public static void get_address(int address, int b) {
String str = Integer.toBinaryString(address);
while (str.length()<16) {
str='0' + str;
}
int blocksize = getn(b);
String offset = str.substring(str.length()-blocksize);
String tag = str.substring(0,str.length()-blocksize);
System.out.println("Physical Address: "+ str + ", Tag: "+ tag+", Word-Offset: "+ offset +" for both L1 and L2 caches");
}
}