Original Post
#include #include #include int main() { int numRows; int numCols; FILE *fp; int *resptr; int ct=0; int i,x,y,j=0; double k=32.00; printf("\tTHE K VALUE IS %d",k); char temc; unsigned long int hex_addr,temptag,tempindex; int counter=0; double ans=log (2.0); int rows=0,columns=0; double b=0; double c=0; double p=0; double c_size=0; double blk_t=0; double as=1.00000; double blk_size=0; double set_t=0; double set_bit=0; //double i=0; double blk_off=0; char ch; int pol=0; int rep=0; int hit=0; int miss=0; double tagbit=0; printf("\nEnter size of cache:\t"); scanf("%lf",&c_size); printf("\nEnter associativity of cache:\t"); scanf("%lf",&as);//1=direct mapped printf ("\nAssociativity is %lf",as); printf("\nEnter Block size of cache:\t"); scanf("%lf",&blk_size); blk_t=c_size/blk_size; printf("\nTotal no of cache blocks: %lf",blk_t); b=blk_size; p=(log(b)/ans); blk_off=p; c=(blk_t/as);//Total no of sets in cache printf("\nTotal no of sets: %lf",c); printf("\nNo of Block offset bits: %lf",blk_off); /*Set BIT Calculation*/ ans=log (2.0); p=(log(c)/ans); set_bit=p; /*TRACE READ*/ printf("\n"); fp=fopen("go_trace.txt","r"); printf("\nTotal bits for set = %lf",set_bit); //HERE IT GIVES SEGMENTATION ERROR while (!feof(fp)) { fscanf(fp,"%c %x\n",&ch,&hex_addr); //printf("mode - %c\taddr - %x\n",&ch,hex_addr); ct++; //i++; } fclose(fp); rows=ct; columns=as; printf("\nNumber of lines is %d Enter the policies for write and read\n",ct); printf("1=WTNA ,2=WTBA"); scanf("%d",&pol); printf("1=LRU 2=FIFO 3=OPTIMAL 4=Random"); scanf("%d",&rep); //ROWS ARE THE NUMBER OF ADDRESSES IN TRACE; // COLUMNS IS THE ASSOCIATIVITY /*Allocating memory for tag and count array*/ printf("%lf bits to subtract\n",blk_off); numRows=(int)c; numCols=(int)as; printf("ROWS= %d \nCOLUMNS= %d\n",numRows,numCols); tagbit=32.00000-blk_off; tagbit=tagbit-set_bit; int **tag = (int **)malloc( numRows * sizeof( int * ) ); for ( i = 0; i < numRows; ++i ) { tag[ i ] = (int *)malloc( numCols * sizeof( int ) ); } int **count = (int **)malloc( numRows * sizeof( int * ) ); for ( i = 0; i < numRows; ++i ) {//printf("%d",i); count[ i ] = (int *)malloc( numCols * sizeof( int ) ); } printf("Tagbits= %lf \n block offset bits= %lf\n",tagbit,blk_off); printf("helloo\n"); fp=fopen("go_trace.txt","r"); printf("setbits= %lf \n block offset bits= %lf\n",set_bit,blk_off); while (!feof(fp)) { fscanf(fp,"%c %x\n",ch,&hex_addr); printf("%x is address\n",hex_addr); tempindex=hex_addr>>(int)blk_off; temptag=tempindex>>(int)set_bit; //printf("\n%x is tag[%d]",temptag,i); counter++; //if (counter==10) break;// FOR FIRST 10 addresses for (i=0;i[j] = count[j]+1; if (tag[j]==temptag) { break; hit++; printf ("\n%d",hit); } else if (tag[j]==0) { tag[j]=temptag; count[j]=1; } else if (tag[j]!=temptag) { miss++; switch(pol) case 1: for (x=0;x[j]>count[j+1]) x=i;y=j+1; } } } } } fclose(fp); return 0; } }