import java.util.Random; /** * Write a description of class MainDirectMap here. * * @author (your name) * @version (a version number or a date) */ public class MainDirectMap { Random Gen = new Random(); public void addRandomData() { //int rowspace = 0; // int colspace = 0; for (int i = 0; i < Memory.Row; i++) { for (int j = 0; j < Memory.Col; j++) { Memory.memory[i][j] = Gen.nextInt(); } } } /* * Select a line of data to transfer from memory to cache * X is the row of Memory and Y is the Column */ public void specificSlotToCache(int x, int y) throws NullPointerException { int cacheX = x; // no need to change the columns as both hold 8 int blocksize = 32; // will be used for rolling over memory rows if ( x < 0 || x >= 128 ) // Not an acceptable row number { throw new NullPointerException(); } if ( x < 32 ) // First block { Cache.cache[x][y] = Memory.memory[x][y]; // Direct move of memory into cache } else if ( x >= blocksize && x < blocksize * 2) // Second block { cacheX = x - blocksize; // X minus blocksize (32) maps it into // between 0 and 31 in cache Cache.cache[cacheX][y] = Memory.memory[x][y]; // ie [54][ } else if ( x >= ( blocksize * 2 ) && x < ( blocksize * 3) ) // third block { cacheX = x - blocksize * 2; // X minus blocksize * 2 (64) maps // between 0 and 31 in cache Cache.cache[cacheX][y] = Memory.memory[x][y]; } else if ( x >= (blocksize *3) && x < (blocksize * 4) ) // fourth block { cacheX = x - blocksize * 3; // X minus blocksize * 3 (96) maps // into between 0 and 31 in cache Cache.cache[cacheX][y] = Memory.memory[x][y]; } } /* * Enter a specific slot number from Memory (0-127) and how many subsequent slots next * to that slotnumber you wish to transfer (Max = 32). IE: * Slotnumber = 100 | slottoenter = 8 | Slots 92-108 are transferred. */ public void specificSlotAndSubsequentSlotsToCache(int slotnumber, int slotstoenter) throws NullPointerException { if ( slotnumber < 0 || slotnumber > 127 ) { throw new NullPointerException(); } if ( slotstoenter < 0 || slotstoenter > 16 ) // block size = 32, so mid section = 16. { // trying to enter 17+ slots will result in 34+ slots being move throw new IllegalArgumentException(); // into cache, which is pointless! } for ( int x=0; x < 8; x++) { for ( int i=0; i < slotstoenter; i++) { int minuscounter = slotnumber; int pluscounter = slotnumber; int minusmem = slotnumber; int plusmem = slotnumber; minuscounter--; pluscounter++; minusmem--; plusmem++; { if ( minuscounter == 0 ) { minuscounter = 32; } if ( pluscounter == 32 ) // Will loop round the memory { // eg, 29,30,31,32,0,1,2,3 pluscounter = 0; } if ( slotnumber < 32 ) { Cache.cache[minuscounter][x] = Memory.memory[minusmem][x]; Cache.cache[pluscounter][x] = Memory.memory[plusmem][x]; Cache.cache[slotnumber][x] = Cache.cache[slotnumber][x]; } else if ( slotnumber >= 32 && slotnumber < 64 ) { minuscounter = minuscounter - 32; pluscounter = pluscounter - 32; int block2 = slotnumber - 32; { Cache.cache[minuscounter][x] = Memory.memory[minusmem][x]; Cache.cache[pluscounter][x] = Memory.memory[plusmem][x]; Cache.cache[block2][x] = Memory.memory[slotnumber][x]; } } else if ( slotnumber >= 64 && slotnumber < 96 ) { minuscounter = minuscounter - 64; pluscounter = pluscounter - 64; int block3 = slotnumber - 64; { Cache.cache[minuscounter][x] = Memory.memory[minusmem][x]; Cache.cache[pluscounter][x] = Memory.memory[plusmem][x]; Cache.cache[block3][x] = Memory.memory[slotnumber][x]; } } else { minuscounter = minuscounter - 96; pluscounter = pluscounter - 96; int block4 = slotnumber - 96; { Cache.cache[minuscounter][x] = Memory.memory[minusmem][x]; Cache.cache[pluscounter][x] = Memory.memory[plusmem][x]; Cache.cache[block4][x] = Memory.memory[slotnumber][x]; } } } } } } /* * Pick a number 1 - 4 to move that block into cache */ public void moveEntireBlockToCache(int blockNumber) throws NullPointerException { if ( blockNumber < 1 || blockNumber > 4) { throw new NullPointerException(); } for (int j = 0; j < 8; j++) { if ( blockNumber == 1 ) { for (int i = 0; i < 32; i++) { Cache.cache[i][j] = Memory.memory[i][j]; } } if ( blockNumber == 2 ) { for (int i = 32; i < 64; i++ ) { int i2 = i - 32; Cache.cache[i2][j] = Memory.memory[i][j]; } } if ( blockNumber == 3 ) { for (int i = 64; i < 96; i++) { int i3 = i - 64; Cache.cache[i3][j] = Memory.memory[i][j]; } } else { for (int i = 96; i < 128; i++) { int i4 = i - 96; Cache.cache[i4][j] = Memory.memory[i][j]; } } } } /* * Choose a number between 0 and 127 to check if cache data is still * stored in memory, and delete if not. */ public void purgeSpecificTag(int tagline) throws IllegalArgumentException { if ( tagline < 0 || tagline >= 32 ) { throw new IllegalArgumentException(); } else if ( tagline > 0 && tagline < 32 ) { for ( int i = 0; i < 96; i++ ) { int block2 = i + 32; int block3 = block2 + 32; int block4 = block3 + 32; for ( int j = 0; j < 8; j++ ) { if ( Cache.cache[i][j] != Memory.memory[i][j] ) { if ( Cache.cache[i][j] !=Memory.memory[block2][j] ) { if ( Cache.cache[i][j] !=Memory.memory[block3][j] ) { if ( Cache.cache[i][j] !=Memory.memory[block4][j] ) { Cache.cache[tagline][j] = 0; } } } } else { // Cache Row x / Col y = Memory Row x / Col y = Don't need to delete :) } } } } } public void checkMemoryForBlockNumber(int RowNo, int ColNo) throws IllegalArgumentException { int block2Row = RowNo + 32; int block3Row = RowNo + 64; { if ( (RowNo < 0 || RowNo > 32) || (ColNo < 0 || ColNo > 8) ) { throw new IllegalArgumentException(); } else if ( Cache.cache[RowNo][ColNo] == Memory.memory[RowNo][ColNo] ) { System.out.println("This slot in cache belongs to the Memory Block 1"); } else if ( Cache.cache[RowNo][ColNo] == Memory.memory[block2Row][ColNo] ) { System.out.println("This slot in cache belongs to the Memory block 2"); } else if ( Cache.cache[RowNo][ColNo] == Memory.memory[block3Row][ColNo] ) { System.out.println("This slot in cache belongs to the Memory Block 3"); } else { System.out.println("This slot belongs to the memory block 4"); } } } public void findHitMissRatio() { int hit = 0; int miss = 0; int conflictmiss = 0; for ( int i = 0; i < 32; i++ ) { int block2 = i + 32; int block3 = i + 64; int block4 = i + 96; for ( int j = 0; j < 8; j++ ) { if ( Cache.cache[i][j] == Memory.memory[i][j] ) { hit++; } else if ( Cache.cache[i][j] != 0 ) //Test block 1 { conflictmiss++; } else { miss++; } if (Cache.cache[i][j] == Memory.memory[block2][j]) { hit++; } else if ( Cache.cache[i][j] != 0 ) // Test block 2 { conflictmiss++; } else { miss++; } if (Cache.cache[i][j] == Memory.memory[block3][j]) { hit++; // test block 3 } else if ( Cache.cache[i][j] != 0 ) { conflictmiss++; } else { miss++; } if( Cache.cache[i][j] == Memory.memory[block4][j]) { hit++; } else if ( Cache.cache[i][j] != 0 ) { conflictmiss++; //test block 4 } else { miss++; } } double hitmissratio = (double) hit / (miss + conflictmiss); { System.out.println(hit + " hits"); System.out.println(miss + " misses"); System.out.println(conflictmiss + " conflict misses"); System.out.println("Therefore the Hit/Miss ratio is: " + hitmissratio); } } } }