Guest User

Untitled

a guest
Jul 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "cache.h"
  6.  
  7.  
  8. /* Steven Wagner & Travis Emery
  9. */
  10.  
  11.  
  12.  
  13. void readFile(FILE * inFile, int blockSize, int lines, int sets);
  14. void discardRest(FILE * inFile);
  15. int getOffSet(int blockPower2, int address);
  16. int getIndex(int setsPower2, int blockPower2, int address);
  17. int getTag(int setsPower2, int blockPower2, int address);
  18. int returnPower(int num);
  19. void cacheFunk(unsigned int tag, unsigned int index, unsigned int offset, int lineCount, int sets);
  20. void enqueue(int index, setType * cache, int set);
  21. int dequeue(int index, setType * cache, int set);
  22. int position = 0;
  23. static int EMPTY = 0;
  24. static int FULL = 0;
  25.  
  26. int hits, miss = 0;
  27. setType * cache;
  28. int QSIZE;
  29. int accesses=0;
  30.  
  31.  
  32.  
  33. int main(int argv, char * args[])
  34. {
  35.   int blockSize   = 0;
  36.   int lines       = 0;
  37.   int sets        = 0;
  38.   char filename[100];
  39.   int i;//loop counter
  40.  
  41.   QSIZE = sets;
  42.  
  43.   if( argv <= 8 ){
  44.     printf("\nusage: cacheSim -S s -E e -B b -F <filename>\n");
  45.     printf("s - number of Sets\ne - number of Lines per Set\n");
  46.     printf("b - number of Bytes per Block\n<filename> - name of file containing address trace\n");
  47.     printf("s, e, b must be powers of two\n");
  48.     exit(1);
  49.   }
  50.  
  51.  
  52.   for(i = 0; i < argv-1; i++){
  53.     // Convert to int
  54.     int val = atoi(args[i+1]);
  55.     // logic for checking
  56.     // if power of two
  57.     int power = (val & (val-1));
  58.     if(strcmp(args[i],"-F") == 0){
  59.       // FILE
  60.       strcpy( filename, args[i+1] );
  61.     }
  62.     else if(strcmp(args[i], "-E") == 0){
  63.       // Lines per set
  64.       if(power != 0){
  65.         printf("\n-E parameter not power of 2.\n");
  66.         exit(1);
  67.       }
  68.       lines = val;
  69.     }
  70.     else if(strcmp(args[i], "-S") == 0){
  71.       // SETS
  72.       if(power != 0){
  73.         printf("\n-S parameter not power of 2.\n");
  74.         exit(1);
  75.       }
  76.       sets = val;
  77.     }
  78.     else if(strcmp(args[i], "-B") == 0){
  79.       // Bytes per Block
  80.       if(power != 0){
  81.         printf("\n-B parameter not power of 2.\n");
  82.         exit(1);
  83.       }
  84.       blockSize = val;
  85.     }
  86.   }
  87.  
  88.   //Start Program
  89.   cache = initCache(sets, lines);
  90.   //printf("Block Size: %d\nLines: %d\nSets: %d\n", blockSize, lines, sets);
  91.   readFile(fopen(filename, "r"), blockSize, lines, sets);
  92.  
  93.   printResults( accesses, hits, miss );
  94. }//end main
  95.  
  96. void readFile(FILE * inFile, int blockSize, int lines, int sets)
  97. {
  98.   int i; // loop counter
  99.   int blockSizePower2 = returnPower(blockSize);
  100.   int setsPower2 = returnPower(sets);
  101.   unsigned int tag, index, offset = 0;
  102.  
  103.   // For Loop to clear out Comment Lines
  104.   for(i = 0; i < 3; i++)
  105.     {
  106.       fscanf(inFile, "#");
  107.       discardRest(inFile);
  108.     }//end for
  109.  
  110.   // While loop to parse through instructions
  111.   int type, address, value = 0;
  112.   while(!feof(inFile)){
  113.     fscanf(inFile, "%d %x %d", &type, &address, &value);
  114.  
  115.     offset = getOffSet(blockSizePower2, address);
  116.     index  = getIndex(setsPower2, blockSizePower2, address);
  117.     tag    = getTag(setsPower2, blockSizePower2, address);
  118.  
  119.     //printf("address = %x, tag = %x, index = %x, offset = %x\n", address, tag, index, offset);
  120.     cacheFunk( tag, index, offset, lines, sets );
  121.     discardRest(inFile);
  122.   }//end while
  123.  
  124. }//end method
  125.  
  126. void discardRest(FILE * inFile)
  127. {
  128.   char fileRead;
  129.   while(fileRead != EOF){
  130.     if(fileRead == '\n')
  131.       return;
  132.     fileRead = fgetc(inFile);
  133.   }//end while
  134. }//end method
  135.  
  136. int getOffSet(int blockPower2, int address)
  137. {
  138.   int shiftCount = (32 - blockPower2);
  139.   return ((unsigned int)address << shiftCount) >> shiftCount;
  140. }//end method
  141.  
  142. int getIndex(int setsPower2, int blockPower2, int address)
  143. {
  144.     if(setsPower2 == 0){ return 0; }
  145.   int shiftCount = (32 - (blockPower2 + setsPower2));
  146.   return ((unsigned int)address << shiftCount) >> (shiftCount + blockPower2);
  147. }//end method
  148.  
  149. int getTag(int setsPower2, int blockPower2, int address)
  150. {
  151.   int shiftCount = blockPower2 + setsPower2;
  152.   return (unsigned int)address >> shiftCount;
  153. }//end method
  154.  
  155. int returnPower(int num)
  156. {
  157.   float tempNum = log2f( (float)num );
  158.   return (int)tempNum;
  159. }//end method
  160.  
  161. void cacheFunk(unsigned int tag, unsigned int index, unsigned int offset, int lineCount, int sets){
  162.   accesses++;
  163.   int i;
  164.     //printf("Index: %x - %x \n", index, (int)index);
  165.   // Looks for a hit
  166.   for(i = 0; i < lineCount; i++){
  167.     if(cache[index].lines[i].tag == tag && cache[index].lines[i].valid == 1){
  168.       hits++;
  169.       int item = dequeue( i, cache, index );
  170.       enqueue( item, cache, index);
  171.       return;
  172.     }//end if
  173.   }//end for
  174.  
  175.   //Look for a miss
  176.   int j;
  177.   for(j=0; j< lineCount; j++){
  178.     if( cache[index].lines[j].valid == 0 ){
  179.       miss++;
  180.       cache[index].lines[j].tag = tag;
  181.       cache[index].lines[j].valid = 1;
  182.  
  183.       int item = cache[index].lruQueue[i];
  184.       enqueue( item, cache, index );
  185.       return;
  186.     } //end if
  187.   } //end for
  188.  
  189.   // Just a miss
  190.   int first = cache[index].lruQueue[0];
  191.   int temp = dequeue( first, cache, index );
  192.   enqueue( first, cache, index );
  193.   cache[index].lines[first].tag = tag;
  194.   miss++;
  195.   return;
  196. }//end cacheFunk
  197.  
  198. void enqueue(int unit, setType * cache, int set){
  199.   //if(position == QSIZE){ FULL = 1; }
  200.  
  201.   if(FULL){ return; }
  202.   cache[set].lruQueue[position] = unit;
  203.   position++;
  204.   if(position == QSIZE){ FULL = 1; }
  205. }//end method
  206.  
  207.  
  208. int dequeue(int index, setType * cache, int set){
  209.   if(EMPTY){ return -1; }
  210.   int i;
  211.   int element = 0;
  212.   for( i=0; i<QSIZE; i++ ){
  213.     if( cache[set].lruQueue[i] == index ){
  214.       element = i;
  215.       break;
  216.     }
  217.   }
  218.   int val = cache[set].lruQueue[element];
  219.  
  220.   for( i=element; i<QSIZE-1; i++ ){
  221.     cache[set].lruQueue[i] = cache[set].lruQueue[i+1];
  222.   }
  223.   cache[set].lruQueue[position] = -1;
  224.   position--;
  225.   if(position == 0){ EMPTY = 1; }
  226.   //printf("%d\n", val);
  227.   return val;
  228. }//end method
Add Comment
Please, Sign In to add comment