-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.java
252 lines (205 loc) · 6.15 KB
/
Cache.java
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
import java.util.ArrayList;
import java.util.HashMap;
// Direct Mapping
public class Cache {
private Disk disk;
private int[][] cache;
private String byteInstr;
private int wordsPerBlock;
private int bytesPerBlock;
private int numCacheBlocks;
private double numTagBits;
private double numIndexBits;
private double numOffSetBits;
private float cacheAccess;
private float hit, miss;
private float hitTime = 1.0f; // default
private float missPenalty;
public Cache() {
disk = new Disk();
byteInstr = "0x00";
hit = 0;
miss = 0;
wordsPerBlock = 1; // default
missPenalty = 10 + wordsPerBlock;
numCacheBlocks = 4; // default
bytesPerBlock = wordsPerBlock * 4;
cacheAccess = 0;
cache = new int[numCacheBlocks][2 + wordsPerBlock];
if (wordsPerBlock == 1) {
numOffSetBits = 0;
} else {
numOffSetBits = (int) Math.ceil(Math.log(wordsPerBlock));
}
numIndexBits = (int) Math.ceil(Math.log(numCacheBlocks));
numTagBits = 32 - numIndexBits - numOffSetBits;
cacheInfo();
}
/* USER FUNCTIONS */
// addr -> 0xFF
public void fetchAddr(String addr) {
// Direct Map Cache
cacheAccess += 1.0;
byteInstr = addr;
String[] tio = TIO(hexToBinary(addr));
int tag = Integer.parseInt(tio[0], 2);
int index = Integer.parseInt(tio[1], 2) % numCacheBlocks;
int offset = 0;
if (tio[2].length() > 0) {
offset = Integer.parseInt(tio[2], 2);
}
System.out.println("Fetch Addr TIO: " + tag + " " + index + " " + offset);
// MISS
if (cache[index][1] != tag || cache[index][0] == 0 || cache[index][2 + offset] == 0) {
// STORE DATA THEN RETURN
miss += 1.0;
storeTIO(tio); // store data in cache
System.out.println("MISS at ADDR " + addr + " : "+ cache[index][2 + offset]);
}
// HIT
else if (cache[index][1] == tag && cache[index][0] != 0) {
// RETURN DATA
hit += 1.0;
System.out.println("HIT at ADDR " + addr + " : "+ cache[index][2 + offset]);
}
return;
}
// convert hex into an Integer using the parseInt() radix parameter
// calculate the input's Block Address and return it for parsing
private String hexToBinary(String hex) {
String binary = "";
int blockAddr = calculateBlockAddress(Integer.parseInt(hex.substring(2, hex.length()), 16));
// "binary" is the binary string of the Block Address used to generate TIO
binary = padLeft(Integer.toBinaryString(blockAddr), 32); // default value of 8 bits
return binary;
}
private String padLeft(String s, int i) {
if (s.length() >= i) {
return s;
} else {
while (s.length() != i) {
s = "0" + s;
}
return s;
}
}
private int getInt(String s, int i) {
return Integer.parseInt(Character.toString(s.charAt(i)), 16);
}
// Block Addr = Byte Addr / Bytes per Block -> (1 word == 4 bytes == 32 bits)
// Index = Block Addr % # of Blocks in Cache
// Used to has index into the Cache
private int calculateBlockAddress(int byteAddress) {
int blockAddress = byteAddress / bytesPerBlock;
return blockAddress;
}
private String[] TIO(String binary) {
String offset = "";
String index, tag;
if (numOffSetBits != 0) {
offset = binary.substring((int) (numTagBits + numIndexBits), 32);
}
tag = binary.substring(0, (int) numTagBits);
index = binary.substring((int) numTagBits, (int) (numTagBits + numIndexBits));
return new String[] {tag, index, offset};
}
private void storeTIO(String[] tio) {
int tag, index, offset;
tag = Integer.parseInt(tio[0], 2);
index = Integer.parseInt(tio[1], 2) % numCacheBlocks;
offset = 0;
if (tio[2].length() > 0) {
offset = Integer.parseInt(tio[2], 2);
}
int data = disk.getData(byteInstr);
cache[index][1] = tag;
cache[index][0] = 1; // valid bit
cache[index][2 + offset] = data;
}
private class Disk {
private HashMap<String, Integer> testInstructions;
private String[] addr;
private int index;
public Disk() {
index = 0;
testInstructions = new HashMap<>(); //
addr = new String[] {"0x00000000", "0x00000004", "0x00000008", "0x0000000c", "0x00000010"};
int[] testData = new int[] {0, 1, 2, 3, 4};
for (int i = 0; i < addr.length; i++) {
testInstructions.put(addr[i], testData[i]);
}
}
public int getData(String address) {
int ret = testInstructions.get(address);
index++;
if (index % addr.length == 0) {
index = 0;
}
return ret;
}
}
/* DEBUGGING AND TESTING FUNCTIONS */
private void cacheInfo() {
System.out.println("Cache Info: ");
System.out.println();
System.out.println("Number of Blocks in Cache: " + numCacheBlocks + ", WordsPerBlock: " + wordsPerBlock + " ");
System.out.println("Bytes Per Block: " + bytesPerBlock + ", Bits Per Block: " + (bytesPerBlock * 8));
System.out.println("Number of Tag Bits: " + numTagBits + ", Number of Index Bits: " + numIndexBits + ", Number of Offset Bits: " + numOffSetBits);
System.out.println();
}
public void printCache() {
System.out.println("v T D");
for (int i = 0; i < cache.length; i++) {
for (int j = 0; j < cache[i].length; j++) {
System.out.print(cache[i][j] + " ");
}
System.out.println();
}
}
public void AMAT_Info() {
System.out.println("Miss Penalty: " + missPenalty + ", Hit Time: " + hitTime);
}
public void cacheAccess() {
System.out.println("Number of Cache Accesses: " + cacheAccess);
}
public void miss() {
System.out.println("Misses: " + miss);
}
public void hits() {
System.out.println("Hits: " + hit);
}
public void hitRate() {
if (cacheAccess == 0) {
return;
}
float x = (hit / cacheAccess);
System.out.println("Hit Rate: " + x);
}
public void missRate() {
if (cacheAccess == 0) {
return;
}
float x = (miss / cacheAccess);
System.out.println("Miss Rate: " + x);
}
public void AMAT() {
if (cacheAccess == 0) {
return;
}
System.out.println("AMAT: " + (hitTime + ((miss / cacheAccess) * missPenalty)));
}
public static void main(String[] args) {
Cache cache = new Cache();
String[] addr = new String[] {"0x00000000", "0x00000004", "0x00000008", "0x0000000c", "0x00000010", "0x00000010", "0x00000000"};
for (String s : addr) {
cache.fetchAddr(s);
/*cache.cacheAccess();
cache.hits();
cache.miss();*/
}
//cache.hitRate();
//cache.missRate();
//cache.AMAT();
cache.printCache();
}
}