Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "cache.h"
- /* Steven Wagner & Travis Emery
- */
- void readFile(FILE * inFile, int blockSize, int lines, int sets);
- void discardRest(FILE * inFile);
- int getOffSet(int blockPower2, int address);
- int getIndex(int setsPower2, int blockPower2, int address);
- int getTag(int setsPower2, int blockPower2, int address);
- int returnPower(int num);
- void cacheFunk(unsigned int tag, unsigned int index, unsigned int offset, int lineCount, int sets);
- void enqueue(int index, setType * cache, int set);
- int dequeue(int index, setType * cache, int set);
- int position = 0;
- static int EMPTY = 0;
- static int FULL = 0;
- int hits, miss = 0;
- setType * cache;
- int QSIZE;
- int accesses=0;
- int main(int argv, char * args[])
- {
- int blockSize = 0;
- int lines = 0;
- int sets = 0;
- char filename[100];
- int i;//loop counter
- QSIZE = sets;
- if( argv <= 8 ){
- printf("\nusage: cacheSim -S s -E e -B b -F <filename>\n");
- printf("s - number of Sets\ne - number of Lines per Set\n");
- printf("b - number of Bytes per Block\n<filename> - name of file containing address trace\n");
- printf("s, e, b must be powers of two\n");
- exit(1);
- }
- for(i = 0; i < argv-1; i++){
- // Convert to int
- int val = atoi(args[i+1]);
- // logic for checking
- // if power of two
- int power = (val & (val-1));
- if(strcmp(args[i],"-F") == 0){
- // FILE
- strcpy( filename, args[i+1] );
- }
- else if(strcmp(args[i], "-E") == 0){
- // Lines per set
- if(power != 0){
- printf("\n-E parameter not power of 2.\n");
- exit(1);
- }
- lines = val;
- }
- else if(strcmp(args[i], "-S") == 0){
- // SETS
- if(power != 0){
- printf("\n-S parameter not power of 2.\n");
- exit(1);
- }
- sets = val;
- }
- else if(strcmp(args[i], "-B") == 0){
- // Bytes per Block
- if(power != 0){
- printf("\n-B parameter not power of 2.\n");
- exit(1);
- }
- blockSize = val;
- }
- }
- //Start Program
- cache = initCache(sets, lines);
- //printf("Block Size: %d\nLines: %d\nSets: %d\n", blockSize, lines, sets);
- readFile(fopen(filename, "r"), blockSize, lines, sets);
- printResults( accesses, hits, miss );
- }//end main
- void readFile(FILE * inFile, int blockSize, int lines, int sets)
- {
- int i; // loop counter
- int blockSizePower2 = returnPower(blockSize);
- int setsPower2 = returnPower(sets);
- unsigned int tag, index, offset = 0;
- // For Loop to clear out Comment Lines
- for(i = 0; i < 3; i++)
- {
- fscanf(inFile, "#");
- discardRest(inFile);
- }//end for
- // While loop to parse through instructions
- int type, address, value = 0;
- while(!feof(inFile)){
- fscanf(inFile, "%d %x %d", &type, &address, &value);
- offset = getOffSet(blockSizePower2, address);
- index = getIndex(setsPower2, blockSizePower2, address);
- tag = getTag(setsPower2, blockSizePower2, address);
- //printf("address = %x, tag = %x, index = %x, offset = %x\n", address, tag, index, offset);
- cacheFunk( tag, index, offset, lines, sets );
- discardRest(inFile);
- }//end while
- }//end method
- void discardRest(FILE * inFile)
- {
- char fileRead;
- while(fileRead != EOF){
- if(fileRead == '\n')
- return;
- fileRead = fgetc(inFile);
- }//end while
- }//end method
- int getOffSet(int blockPower2, int address)
- {
- int shiftCount = (32 - blockPower2);
- return ((unsigned int)address << shiftCount) >> shiftCount;
- }//end method
- int getIndex(int setsPower2, int blockPower2, int address)
- {
- if(setsPower2 == 0){ return 0; }
- int shiftCount = (32 - (blockPower2 + setsPower2));
- return ((unsigned int)address << shiftCount) >> (shiftCount + blockPower2);
- }//end method
- int getTag(int setsPower2, int blockPower2, int address)
- {
- int shiftCount = blockPower2 + setsPower2;
- return (unsigned int)address >> shiftCount;
- }//end method
- int returnPower(int num)
- {
- float tempNum = log2f( (float)num );
- return (int)tempNum;
- }//end method
- void cacheFunk(unsigned int tag, unsigned int index, unsigned int offset, int lineCount, int sets){
- accesses++;
- int i;
- //printf("Index: %x - %x \n", index, (int)index);
- // Looks for a hit
- for(i = 0; i < lineCount; i++){
- if(cache[index].lines[i].tag == tag && cache[index].lines[i].valid == 1){
- hits++;
- int item = dequeue( i, cache, index );
- enqueue( item, cache, index);
- return;
- }//end if
- }//end for
- //Look for a miss
- int j;
- for(j=0; j< lineCount; j++){
- if( cache[index].lines[j].valid == 0 ){
- miss++;
- cache[index].lines[j].tag = tag;
- cache[index].lines[j].valid = 1;
- int item = cache[index].lruQueue[i];
- enqueue( item, cache, index );
- return;
- } //end if
- } //end for
- // Just a miss
- int first = cache[index].lruQueue[0];
- int temp = dequeue( first, cache, index );
- enqueue( first, cache, index );
- cache[index].lines[first].tag = tag;
- miss++;
- return;
- }//end cacheFunk
- void enqueue(int unit, setType * cache, int set){
- //if(position == QSIZE){ FULL = 1; }
- if(FULL){ return; }
- cache[set].lruQueue[position] = unit;
- position++;
- if(position == QSIZE){ FULL = 1; }
- }//end method
- int dequeue(int index, setType * cache, int set){
- if(EMPTY){ return -1; }
- int i;
- int element = 0;
- for( i=0; i<QSIZE; i++ ){
- if( cache[set].lruQueue[i] == index ){
- element = i;
- break;
- }
- }
- int val = cache[set].lruQueue[element];
- for( i=element; i<QSIZE-1; i++ ){
- cache[set].lruQueue[i] = cache[set].lruQueue[i+1];
- }
- cache[set].lruQueue[position] = -1;
- position--;
- if(position == 0){ EMPTY = 1; }
- //printf("%d\n", val);
- return val;
- }//end method
Add Comment
Please, Sign In to add comment