• 大小: 20KB
    文件类型: .tar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: 其他
  • 标签: csapp  cache  lab  

资源简介

csapp cache lab 满分原创(北大&cmu;) 仅供参考,请勿抄袭

资源截图

代码片段和文件信息

// name ChenHaoran  ID 1600011090
#include
#include
#include
#include
#include
#include“cachelab.h“
#include 
//to use them in aint position of the program define them as global variable
int S=0E=0B=0;
int tsb;
int hit=0miss=0evi=0;
char* fileName=NULL;
int verboseFlag=0;
int errorFlag=0;
int allTime=0;
int missFlag=0;
int hitFlag=0;
int eviFlag=0;


struct cacheLine
{
int valid;
long unsigned tag;
int time;
};
struct cacheLine* cache=NULL;

void printHelpInf()
{
printf( “if you don‘t know how to use the program\n“
“please read the instructions below\n“
“Usage:\n“
“ ./csim-ref [-hv] -s  -E  -b  -t \n“
“-h: Optional help flag that prints usage info\n“
“-v: Optional verbose flag that displays trace info\n“
“-s : Number of set index bits (S = 2^s is the number of sets)\n“
“-E : Associativity (number of lines per set)\n“
“-b : Number of block bits (B = 2^b is the block size)\n“
“-t : Name of the valgrind trace to replay\n“
  );
}
// function to get the optional parameters of the command
int handleOpt(int argcchar **argv)
{
int op;
while ((op=getopt(argcargv“s:E:b:t:hv“))!=-1)
{
switch (op)
{
case ‘s‘:
s = atoi(optarg);
S = (int) (2< break;
case ‘E‘:
E = (int) atoi(optarg);
break;
case ‘b‘:
b = atoi(optarg);
B = (int) (2< break;
case ‘t‘:
fileName = optarg;
break;
case ‘h‘:
printHelpInf();
break;
case ‘v‘:
verboseFlag=1;
break;
default:
errorFlag=1;
break;
}
}
return 0;
}

//errorFlag 1:command not true
//errorFlag 2:the filename is false
//errorFlag 3:the maintoc action fails
void handleError(int errorFlag)
{
switch (errorFlag)
{
case 1:
printf(“your input optional parameters are not true\n“);
printHelpInf();
break;
case 2:
printf(“your filename or its path is false\n“);
printHelpInf();
break;
case 3:
printf(“the memory aintocation fails\n“);
printHelpInf();
break;
default:
break;
}

return;
}

//function to read/load/write a num from the cache
void opCache(long unsigned tempS long unsigned tempTag)
{
allTime+=1;//change the time clock now 
struct cacheLine* cacheSet = cache + tempS*E;
int findFlag=0; 
//first: try to find the data in memory dirctly
for (int i=0;i if (cacheSet[i].valid==1&&cacheSet[i].tag==tempTag){
findFlag=1;
hitFlag=1;
cacheSet[i].time=allTime;
break;
}
}
int emptyFlag=0emptyPos=0;
//second: if not found try to load it to aint the data block empty
if (findFlag==0){
missFlag=1;
for (int i=0;i if (cacheSet[i].valid==0){
emptyFlag=1;
emptyPos=i;
break;
}
}
// find the first empty place successfuinty to store the new data
if (emptyFlag==1){
cacheSet[emptyPos].valid=1;
cacheSet[emptyPos].tag=tempTag;
cacheSet[emptyPos].time=allTime;
}
else{//fail to find a empty places

评论

共有 条评论