Demo entry 6745928

csim.c

   

Submitted by anonymous on May 28, 2018 at 04:32
Language: C. Code size: 4.9 kB.

//name: Yue Liu  
//student ID: 516030910397  
/*#include "cachelab.h" 
 
int main() 
{ 
    //printSummary(0, 0, 0); 
    return 0; 
}*/  
#include"cachelab.h"  
#include<getopt.h>  
#include<stdlib.h>  
#include<unistd.h>  
#include<string.h>  
#include<math.h>  
#include<malloc.h>  
#include<stdio.h>  
typedef struct {  
    long long addr;  
    int valid;  
    int time;  
}Cache;  
int main(int argc, char** argv){  
    Cache** cache;  
    int S, E, B;  
    int s, b;  
    int ch;  
    int hit_count=0, miss_count=0, eviction_count=0;  
  
    int print_info=0;  
    int index;  
    long long address;  
    long long tag;  
    int tmp;  
    char *file;  
    while((ch=getopt(argc, argv, "hvs:E:b:t:"))!=-1){  
        switch(ch){  
                    case'v':  
                        print_info = 1;  
                break;  
            case's':  
                s = atoi(optarg);  
                S = (int)pow(2,atoi(optarg));  
                break;  
            case'E':  
                E = atoi(optarg);  
                break;  
            case'b':  
                b = atoi(optarg);  
                B = (int) pow(2,atoi(optarg));  
                break;  
            case't':  
                file=optarg;  
                break;  
        }  
    }  
  
    //allocate space for cache  
    cache = (Cache**)malloc(sizeof(Cache*)*S);  
    for (int i = 0; i < S; ++i) {  
        cache[i] = (Cache*)malloc(sizeof(Cache)*E);  
        memset(cache[i], 0, sizeof(Cache)*E);  
    }  
  
  
    freopen(file,"r",stdin);  
    char c;  
    Cache *p = NULL;  
    int hit,miss,evict;  
    int empty;  
    int i;  
    int maxtime;  
    while(scanf("%c",&c)!=EOF)  
    {  
        hit=miss=evict=0;  
        empty = 0;  
        maxtime=-1;  
        Cache* q;  
        if(c=='I')scanf("%llx,%d",&address,&tmp);  
        else if( c=='L' || c=='M' || c=='S')  
        {  
            scanf("%llx,%d",&address,&tmp);//ignore size of data  
            index = (unsigned int)((address/B)%S);  
  
            tag = address>>(s+b);  
            printf("index: %d tag: %llx   ",index,tag);  
            for(i=0;i<E;++i)  
            {  
                p = &(cache[index][i]);  
                p->time++;  
                if(!hit&&(p->addr>>(s+b))==tag&&p->valid)  
                {  
                    hit=1;//printf("hello p-<addr %llx ",p->addr);  
                    p->time=0;  
                    //break;  
                }  
            }  
  
            if(!hit)  
            {  
                miss = 1;  
                for(i=0;i<E;++i)//find empty block  
                {  
                    p = &(cache[index][i]);  
                    if(!p->valid)  
                    {  
                        p->addr = address;  
                        p->valid = 1;  
                        empty = 1;  
                        p->time = 0;  
                        break;  
                    }  
                }  
  
                if(!empty)  
                {  
                    evict = 1;  
                    for(i=0;i<E;++i)//find the longest one that ha  
                    {  
                        p = &(cache[index][i]);  
                        if(p->time>maxtime){maxtime = p->time; q=p;}  
                    }  
                    printf(" object to evcit: %llx  ",q->addr);  
                    q ->addr = address;  
                    q-> valid = 1;  
                    q->time = 0;  
                }  
            }  
        if(hit) hit_count++;  
        if(miss) miss_count++;  
        if(evict) eviction_count++;  
        if(c=='M') hit_count++;  
        if(print_info)  
        {  
            printf("%c %llx ",c,address);  
            if(c=='S')  
            {  
                if(hit)printf("hit  ");  
                else printf("miss  ");  
                if(evict) printf("evict ");  
            }  
            else if(c=='L')  
            {  
                if(hit) printf("hit  ");  
                else{  
                    printf("miss  ");  
                    if(evict) printf("evict ");  
                }  
            }  
            else if(c=='M')  
            {  
                if(hit) printf("hit  ");  
                else{  
                    printf("miss  ");  
                    if(evict) printf("evict ");  
                }  
                printf("hit");  
            }  
            else{}  
            printf("\n");  
        }  
  
        }  
  
    }  
    printSummary(hit_count, miss_count, eviction_count);  
    for (i = 0;i < S; ++i){  
        free(cache[i]);  
    }  
    free(cache);  
    return 0;  
}  

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).