File size: 6,093 Bytes
8be366a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | //Alan Macedo Esparza A0136288
//Karen Bolaños A0134733
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#define CACHESIZE 16
struct cache{
unsigned int address;
char state;
};
struct cacheFA{ //Cache with time attribute for FA
unsigned int address;
unsigned int time;
char state;
};
unsigned int events = 0;
//declare caches
struct cache myCache[CACHESIZE];
struct cacheFA myCacheFA[CACHESIZE];
enum values {VALID, INVALID, EMPTY };
/**********************************************************************/
/* */
/* Name: GetAddr */
/* */
/* Description: This function will read the input file and returns an */
/* hexadecimal number. It will skip over comments, which */
/* begin the line with a # */
/* */
/* Inputs: The name of the input file */
/* */
/* Outputs: The address that will be presented to the cache */
/* */
/**********************************************************************/
unsigned int GetAddr (FILE *fp)
{
char c; /* Character read */
unsigned int address;
do {
c = getc (fp); /* Get next character */
if ( c == '#' ) /* Skip the comment */
do {
c = getc (fp);
} while ( c != '\n');
} while (!isxdigit(c) && !feof(fp));
ungetc(c, fp); /* Return the character to the file */
fscanf(fp, "%x\n",&address);
return (address);
}
/**********************************************************************/
/* */
/* Name: CacheAccess */
/* */
/* Description: This function will simulate a direct mapped cache */
/* */
/* Inputs: An address in hexadecimal format */
/* */
/* Outputs: The hit rate (p) */
/* */
/**********************************************************************/
int CacheAccess (unsigned int address){
static unsigned int hit = 0;
int end = address & 0x000f; //get last 4 bits of address
if(myCache[end].state==VALID && myCache[end].address == address)
{
hit++; //Address found correctly, no further action necessary
}else{
myCache[end].state=VALID;//Set state to valid (checking not really necessary)
myCache[end].address=address; //set cache address to searched address
}
return hit;
}
/**********************************************************************/
/* */
/* Name: CacheAccessFA */
/* */
/* Description: This function will simulate a fully associative cache */
/* */
/* Inputs: An address in hexadecimal format */
/* */
/* Outputs: The hit rate (p) */
/* */
/**********************************************************************/
int CacheAccessFA (unsigned int address){
static unsigned int hit = 0;
unsigned int minTime=99999; //Set min time to very high number
bool found=false;//found flag set to false
unsigned int minAddress;
for(int i = 0;i < CACHESIZE;i++)
{
if (myCacheFA[i].state==VALID && myCacheFA[i].address == address) //find hit
{
hit++;
found = true; //change found flag
myCacheFA[i].time= events+1; //events plus 1, as is end of last event.
break;
}
}
if (!found) //hit not found
{
for(int i = 0;i < CACHESIZE;i++){
if (myCacheFA[i].time <minTime){ //try to find lowest time
minTime = myCacheFA[i].time;
minAddress = i;
}
}
myCacheFA[minAddress].state = VALID;
myCacheFA[minAddress].address = address;//set element to relevant info
myCacheFA[minAddress].time = events+1;
}
return hit;
}
int main (int argc, const char * argv[]) {
FILE *fp; /* Pointer to the input file */
unsigned int ref,hitsDM,hitsFA;
/* Initialize the cache */
for (int i=0; i < CACHESIZE; i++){
myCache[i].state = INVALID;
myCache[i].address = EMPTY;
myCacheFA[i].state = INVALID;
myCacheFA[i].address = EMPTY;
myCacheFA[i].time = 0;
}
fp = fopen ("trace.txt","r"); /* Open file for read operation */
while (!feof(fp)) {
ref = GetAddr(fp);
hitsDM = CacheAccess(ref); //Continuously update hits for both types
hitsFA = CacheAccessFA(ref);
events++;
}
/* Print the hit rate of the cache */
printf("Percentage hits Direct Mapped : %2.2f%% (%d/%d)\n",hitsDM/(float)events*100,hitsDM,events);
printf("Percentage hits Fully Associative : %2.2f%% (%d/%d)\n",hitsFA/(float)events*100,hitsFA,events);
return 0;
} |