/* UC Santa Barbara ArchLab www.cs.ucsb.edu/~arch Wavelet-Based Phase Classification Source Code www.cs.ucsb.edu/~arch/wavelet Copyright (c) 2006 The Regents of the University of California. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice, this paragraph and the following three paragraphs appear in all copies. Permission to incorporate this software into commercial products may be obtained by contacting the University of California. For information about obtaining such a license contact: Tim Sherwood This software program and documentation are copyrighted by The Regents of the University of California. The software program and documentation are supplied "as is", without any accompanying services from The Regents. The Regents does not warrant that the operation of the program will be uninterrupted or error-free. The end-user understands that the program was developed for research purposes and is advised not to rely exclusively on the program for any reason. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. Ted Huffmire, (www.cs.ucsb.edu/~huffmire, huffmire at cs dot ucsb dot edu) Tim Sherwood (www.cs.ucsb.edu/~sherwood, sherwood at cs dot ucsb dot edu) 14 June 2006 File: cachesim.java Publication: Ted Huffmire and Tim Sherwood. Wavelet-Based Phase Classification. Proceedings of the Fifteenth International Conference on Parallel Architectures and Compilation Techniques (PACT'06), Seattle, Washington, September 16-20, 2006. */ class cachesim { public static final int L1TIME=1; public static final int L2TIME=2; public static final int MISSPENALTY=200; int tag1[], tag2[], L1size, L2size, L1blocksize, L2blocksize; int L1assoc, L2assoc, tagbits1, tagbits2, offsetbits1, offsetbits2; int whichblockbits1, whichblockbits2, waysize1, waysize2; public cachesim( int L1_size, /*bytes*/ int L1_assoc, /*1=directmap, 2=2-way, 4=4-way */ int L1_blocksize, /*bytes*/ int L2_size, /*bytes*/ int L2_assoc, /*1=directmap, 2=2-way, 4=4-way */ int L2_blocksize /*bytes*/ ) { L1size = L1_size; L2size = L2_size; L1assoc = L1_assoc; L2assoc = L2_assoc; L1blocksize = L1_blocksize; L2blocksize = L2_blocksize; if (L1size <= 0 || L2size <= 0 || L1assoc <= 0 || L2assoc <= 0 || L1blocksize <= 0 || L2blocksize <= 0) { System.err.println("error! inputs are not all positive values\n"); System.exit(1); } if (L1assoc > L1size || L2assoc > L2size){ System.err.println("error! associativity is too large.\n"); System.exit(1); } whichblockbits1 = (int)(Math.log((double)(L1size / L1assoc / L1blocksize)) / Math.log((2.0)) + .1); whichblockbits2 = (int)(Math.log((double)(L2size / L2assoc / L2blocksize)) / Math.log(2.0) + .1); offsetbits1 = (int)(Math.log((double)L1blocksize) / Math.log(2.0) + .1); offsetbits2 = (int)(Math.log((double)L2blocksize) / Math.log(2.0) + .1); tagbits1 = 32 - whichblockbits1 - offsetbits1; tagbits2 = 32 - whichblockbits2 - offsetbits2; waysize1 = pow(2, whichblockbits1); waysize2 = pow(2, whichblockbits2); tag1 = new int[L1size / L1blocksize]; tag2 = new int[L2size / L2blocksize]; for (int i = 0; i < L1size / L1blocksize; i++) { tag1[i] = -1; } for (int i = 0; i < L2size / L2blocksize; i++) { tag2[i] = -1; } } int max(int a, int b) { if (a > b) return a; else return b; } int pow(int x, int y) { int rv = 1; for (int i = 0; i < y; i++) { rv = rv * x; } return rv; } int mask(int a) { int rv = 0; for (int i = 0; i < a; i++) { rv = rv << 1 | 0x1; } return rv; } //if it is a hit in any level, return 1 for is_hit, else return 0 //return the access time in the accesstime variable void access(long pc, count is_l1hit, count is_l2hit, count accesstime) { int index, old; // WRITE ME is_l1hit.setvalue(0); is_l2hit.setvalue(0); accesstime.setvalue(0); //return access time in ns int L1tag = (int)((pc >> (offsetbits1 + whichblockbits1)) & mask(tagbits1)); int whichblock1 = (int)((pc >> offsetbits1) & mask(whichblockbits1)); int L2tag = (int)((pc >> (offsetbits2 + whichblockbits2)) & mask(tagbits2)); int whichblock2 = (int)((pc >> offsetbits2) & mask(whichblockbits2)); // Look for L1tag in the L1 cache for (int i = 0; i < L1assoc; i++) { int tag = tag1[whichblock1 + i * waysize1]; if (L1tag == tag) { is_l1hit.setvalue(1); is_l2hit.setvalue(1); accesstime.setvalue(L1TIME); return; } } // If it's not in the L1 cache, look for L2tag in the L2 cache for (int i = 0; i < L2assoc; i++) { int tag = tag2[whichblock2 + i * waysize2]; if (L2tag == tag) { is_l2hit.setvalue(1); accesstime.setvalue(L2TIME); // Must put it in L1 cache using LRU policy index = whichblock1; old = tag1[index]; tag1[index] = L1tag; for (int j = 0; j < L1assoc - 1; j++) { index = whichblock1 + (j + 1) * waysize1; int TAG = tag1[index]; tag1[index] = old; old = TAG; } return; } } // If it's not there, then put it into L1 and L2, set is_hit to 0 and return. //Put it in L1 using LRU replacement policy index = whichblock1; old = tag1[index]; tag1[index] = L1tag; for (int i = 0; i < L1assoc - 1; i++) { index = whichblock1 + (i + 1) * waysize1; int tag = tag1[index]; tag1[index] = old; old = tag; } // Put it in L2 using LRU replacement policy index = whichblock2; old = tag2[index]; tag2[index] = L2tag; for (int i = 0; i < L2assoc - 1; i++) { index = whichblock2 + (i + 1) * waysize2; int tag = tag2[index]; tag2[index] = old; old = tag; } is_l1hit.setvalue(0); is_l2hit.setvalue(0); accesstime.setvalue(MISSPENALTY); return; } }