|
| 1 | + |
| 2 | +public class Cache { |
| 3 | + |
| 4 | + public boolean debug = false; |
| 5 | + |
| 6 | + public AssocLevel [] memoryIndex; |
| 7 | + |
| 8 | + public int LRU; |
| 9 | + int STATE = 0; |
| 10 | + int LOC = 1; |
| 11 | + int BLOCKNUMBER = 2; |
| 12 | + int set = 0; |
| 13 | + int assoc = 0; |
| 14 | + int blockSize = 0; |
| 15 | + long tag = 0; |
| 16 | + int index = 0; |
| 17 | + int blockSet = 0; |
| 18 | + int indexBits = 0; |
| 19 | + int blockBits = 0; |
| 20 | + int tagBits = 0; |
| 21 | + String cacheName; |
| 22 | + String debugNumber = new String(); |
| 23 | + long instruction = 0; |
| 24 | + int number; |
| 25 | + boolean SBAccess = false; |
| 26 | + int blockNumber = 0; |
| 27 | + |
| 28 | + |
| 29 | + Cache nextLevel = null; |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + public int memoryTraffic = 0; |
| 35 | + public int read = 0; |
| 36 | + public int write = 0; |
| 37 | + public int readMiss = 0; |
| 38 | + public int writeMiss = 0; |
| 39 | + public int writeBacks = 0; |
| 40 | + public int SBReadMiss = 0; |
| 41 | + public int SBRead = 0; |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + public Cache(int SetSize, int Assoc, int BlockSize, String name) |
| 48 | + { |
| 49 | + // TODO Auto-generated constructor stub |
| 50 | + memoryIndex = new AssocLevel[SetSize]; |
| 51 | + set = SetSize; |
| 52 | + assoc = Assoc; |
| 53 | + blockSize = BlockSize; |
| 54 | + nextLevel = null; |
| 55 | + indexBits = log2(set); |
| 56 | + blockBits = log2(blockSize); |
| 57 | + tagBits = (int) (32 - indexBits - blockBits); |
| 58 | + cacheName = name; |
| 59 | + |
| 60 | + for(int index = 0; index < SetSize; index++) |
| 61 | + { |
| 62 | + memoryIndex[index] = new AssocLevel(Assoc, BlockSize); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public int log2(int value) |
| 69 | + { |
| 70 | + return (short)(Math.log10(value)/Math.log10(2)); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + public void controler(long[] result) |
| 76 | + { |
| 77 | + // TODO Auto-generated method stub |
| 78 | + long [] output = new long[2]; |
| 79 | + instruction = (result[1] >> blockBits) << blockBits; |
| 80 | + output = processInput(instruction); |
| 81 | + tag = output[0]; |
| 82 | + index = (int)output[1]; |
| 83 | + if(result[0] == 2) |
| 84 | + { |
| 85 | + result[0] = 0; |
| 86 | + SBAccess = true; |
| 87 | + } |
| 88 | + |
| 89 | + if(result[0] == 0) |
| 90 | + { |
| 91 | + read(); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + write(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public long [] processInput(long input) |
| 100 | + { |
| 101 | + long [] output = new long[2]; |
| 102 | + output[1] = (long)(( input >> blockBits) & ((long)Math.pow(2, indexBits) - 1 )); |
| 103 | + output[0] = (long)(( input >> indexBits + blockBits) & (long)(Math.pow(2, tagBits) - 1 )); |
| 104 | + return output; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + private void write() |
| 109 | + { |
| 110 | + int result; |
| 111 | + long evict; |
| 112 | + int lru; |
| 113 | + long [] input = new long[2]; |
| 114 | + write++; |
| 115 | + result = memoryIndex[index].search(tag); |
| 116 | + debugDisplay(cacheName + " write : " + Long.toHexString(instruction) + "(tag "+ Long.toHexString(tag) +", index "+ index+ ")"); |
| 117 | + if(result > -1) |
| 118 | + { |
| 119 | + debugDisplay(cacheName+" write hit"); |
| 120 | + memoryIndex[index].write(result); |
| 121 | + debugDisplay(cacheName+ " update LRU"); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + debugDisplay(cacheName+ " miss"); |
| 126 | + evict = recon(); |
| 127 | + if(memoryIndex[index].memorySlot[assoc-1].dirty) |
| 128 | + { |
| 129 | + debugDisplay(cacheName+" victim: " + Long.toHexString(evict)); |
| 130 | + writeBacks++; |
| 131 | + if(nextLevel == null) |
| 132 | + { |
| 133 | + memoryTraffic++; |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + input[0] = 1; |
| 138 | + input[1] = evict; |
| 139 | + nextLevel.controler(input); |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + debugDisplay(cacheName+" victim: none"); |
| 146 | + |
| 147 | + } |
| 148 | + writeMiss++; |
| 149 | + if(nextLevel == null) |
| 150 | + { |
| 151 | + memoryTraffic++; |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + input[0] = 0; |
| 156 | + input[1] = instruction; |
| 157 | + nextLevel.controler(input); |
| 158 | + } |
| 159 | + lru = memoryIndex[index].findLRU(); |
| 160 | + memoryIndex[index].updateMemory(tag, lru); |
| 161 | + debugDisplay(cacheName + " Update LRU"); |
| 162 | + memoryIndex[index].writeUpdate(); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + private void read() |
| 169 | + { |
| 170 | + int result; |
| 171 | + long evict; |
| 172 | + int lru; |
| 173 | + long [] input = new long[2]; |
| 174 | + if(!SBAccess) |
| 175 | + read++; |
| 176 | + else |
| 177 | + SBRead++; |
| 178 | + debugDisplay(cacheName + " read : " + Long.toHexString(instruction) + "(tag "+ Long.toHexString(tag) +", index "+ index+ ")"); |
| 179 | + result = memoryIndex[index].search(tag); |
| 180 | + if(result > -1) |
| 181 | + { |
| 182 | + debugDisplay(cacheName+" read hit"); |
| 183 | + memoryIndex[index].read(result); |
| 184 | + debugDisplay(cacheName+ " update LRU"); |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + debugDisplay(cacheName+ " miss"); |
| 189 | + evict = recon(); |
| 190 | + if(memoryIndex[index].memorySlot[assoc-1].dirty) |
| 191 | + { |
| 192 | + debugDisplay(cacheName+" victim: " + Long.toHexString(evict)); |
| 193 | + writeBacks++; |
| 194 | + if(nextLevel == null) |
| 195 | + { |
| 196 | + memoryTraffic++; |
| 197 | + } |
| 198 | + else |
| 199 | + { |
| 200 | + input[0] = 1; |
| 201 | + input[1] = evict; |
| 202 | + nextLevel.controler(input); |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + debugDisplay(cacheName+" victim: none"); |
| 210 | + |
| 211 | + } |
| 212 | + if(!SBAccess) |
| 213 | + { |
| 214 | + readMiss++; |
| 215 | + } |
| 216 | + else |
| 217 | + { |
| 218 | + SBReadMiss++; |
| 219 | + } |
| 220 | + |
| 221 | + if(nextLevel == null) |
| 222 | + { |
| 223 | + memoryTraffic++; |
| 224 | + } |
| 225 | + else |
| 226 | + { |
| 227 | + input[0] = 0; |
| 228 | + input[1] = instruction; |
| 229 | + nextLevel.controler(input); |
| 230 | + } |
| 231 | + lru = memoryIndex[index].findLRU(); |
| 232 | + memoryIndex[index].updateMemory(tag, lru); |
| 233 | + debugDisplay(cacheName + " Update LRU"); |
| 234 | + } |
| 235 | + SBAccess = false; |
| 236 | + } |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | + private long recon() |
| 241 | + { |
| 242 | + // TODO Auto-generated method stub |
| 243 | + return (index << blockBits) + ( memoryIndex[index].memorySlot[assoc-1].tag << (indexBits + blockBits)); |
| 244 | + } |
| 245 | + |
| 246 | + |
| 247 | + public String toString() |
| 248 | + { |
| 249 | + String string = new String(); |
| 250 | + for(int i = 0; i < memoryIndex.length; i++) |
| 251 | + { |
| 252 | + string = string + "Set " + i + ": "; |
| 253 | + string=string + memoryIndex[i].toString(); |
| 254 | + } |
| 255 | + return string; |
| 256 | + } |
| 257 | + |
| 258 | + |
| 259 | + public void debugDisplay(Object object) |
| 260 | + { |
| 261 | + if(debug) |
| 262 | + { |
| 263 | + System.out.println(object); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + |
| 268 | + /** |
| 269 | + * @param args |
| 270 | + */ |
| 271 | + public static void main(String[] args) { |
| 272 | + // TODO Auto-generated method stub |
| 273 | + new Cache(16, 1, 4, "L"); |
| 274 | + } |
| 275 | + |
| 276 | + |
| 277 | +} |
0 commit comments