I have to program a cache simulator in C for my computer architecture class. I have no problems with what is required of me and my program is nearly finished, however I am having an issue with my CreateCache method. For some reason AFTER I create my cache, when I then go to insert a single address to the cache via index and tag, each set/block in the cache is identical. If I add one entry to a the 1st set in a 4 set cache, all 4 sets will then have that same tag. My cache needs to be able to support associative (n-ways) and direct, and also must include L1 L2 and L3 memory.
If anyone could please take a look at my method and struct and tell me if there are any apparent errors I would greatly appreciate it. I have been stuck on this for days and the assignment is due tomorrow. Thank you all in advance!!
Here is the structs I am using, as well as the createCache method.
typedef struct block {
unsigned long tag;
int valid; //0 for invalid, 1 valid
}block;
typedef struct set {
int numBlocks;
block *blks;
int alg;
}set;
typedef struct Cache{
int FIFO, LRU; //index for alg
int hits, misses, reads, writes;
unsigned long accesses;
int cacheSize;
int numSets;
int numBlocks; //total blocks
set *sets;
int blockSize;
int assoc; //number of lines each in set
int alg;
}Cache;
And here is the createCache method I am using. I think the error lies in how I used pointers and set up the arrays of sets and blocks.
Cache createCache(int cacheSize, int blockSize, int alg, int assoc){
if(cacheSize<=0 || blockSize <=0 || (alg!=0 && alg!=1)){
perror("Invalid input!");
exit;
}
Cache *new = (Cache*)malloc(sizeof(Cache));
//initialize all values
new->hits=0;
new->misses=0;
new->reads=0;
new->writes=0;
new->alg=alg;
new->assoc = assoc;
new->cacheSize=cacheSize;
new->blockSize=blockSize;
new->numBlocks=(int)(cacheSize/blockSize);
new->numSets=(int)(new->numBlocks/assoc);
// new->set->blk = (block*)malloc(sizeof(block)* new->numSets);
//creates array with n sets
if(assoc >= 1){
set *s = malloc(sizeof(set));
block *b = malloc(sizeof(block));
new->sets = s;
new->sets->blks = b;
//for each set
for(int x=0; x < new->numSets; x++){
new->sets[x] = *s;
new->sets[x].alg=alg;
new->sets[x].numBlocks = new->numBlocks;
// new->sets[x] = (set*)malloc(sizeof((set* new->numLines);
for(int y=0; y < assoc; y++){
//add for blks[y]
new->sets[x].blks[y] = *b;
new->sets[x].blks[y].valid = -1;
new->sets[x].blks[y].tag = NULL; //null?
}
}
}//end if
return *new;
}
To make it easier to visualize, this is what happens when I print my cache. For this example I made a direct cache and added a single address to the cache.
Set 0
Block 0
tag 0
Set 1
Block 0
tag 0
Set 2
Block 0
tag 0
Set 3
Block 0
tag 0
Adding address: 7fff006891a8
Set 0
Block 0
tag 7fff006891a
Set 1
Block 0
tag 7fff006891a
Set 2
Block 0
tag 7fff006891a
Set 3
Block 0
tag 7fff006891a
I have to program a cache simulator in C for my computer architecture class. I have no problems with what is required of me and my program is nearly finished, however I am having an issue with my CreateCache method. For some reason AFTER I create my cache, when I then go to insert a single address to the cache via index and tag, each set/block in the cache is identical. If I add one entry to a the 1st set in a 4 set cache, all 4 sets will then have that same tag. My cache needs to be able to support associative (n-ways) and direct, and also must include L1 L2 and L3 memory.
If anyone could please take a look at my method and struct and tell me if there are any apparent errors I would greatly appreciate it. I have been stuck on this for days and the assignment is due tomorrow. Thank you all in advance!!
Here is the structs I am using, as well as the createCache method.
typedef struct block {
unsigned long tag;
int valid; //0 for invalid, 1 valid
}block;
typedef struct set {
int numBlocks;
block *blks;
int alg;
}set;
typedef struct Cache{
int FIFO, LRU; //index for alg
int hits, misses, reads, writes;
unsigned long accesses;
int cacheSize;
int numSets;
int numBlocks; //total blocks
set *sets;
int blockSize;
int assoc; //number of lines each in set
int alg;
}Cache;
And here is the createCache method I am using. I think the error lies in how I used pointers and set up the arrays of sets and blocks.
Cache createCache(int cacheSize, int blockSize, int alg, int assoc){
if(cacheSize<=0 || blockSize <=0 || (alg!=0 && alg!=1)){
perror("Invalid input!");
exit;
}
Cache *new = (Cache*)malloc(sizeof(Cache));
//initialize all values
new->hits=0;
new->misses=0;
new->reads=0;
new->writes=0;
new->alg=alg;
new->assoc = assoc;
new->cacheSize=cacheSize;
new->blockSize=blockSize;
new->numBlocks=(int)(cacheSize/blockSize);
new->numSets=(int)(new->numBlocks/assoc);
// new->set->blk = (block*)malloc(sizeof(block)* new->numSets);
//creates array with n sets
if(assoc >= 1){
set *s = malloc(sizeof(set));
block *b = malloc(sizeof(block));
new->sets = s;
new->sets->blks = b;
//for each set
for(int x=0; x < new->numSets; x++){
new->sets[x] = *s;
new->sets[x].alg=alg;
new->sets[x].numBlocks = new->numBlocks;
// new->sets[x] = (set*)malloc(sizeof((set* new->numLines);
for(int y=0; y < assoc; y++){
//add for blks[y]
new->sets[x].blks[y] = *b;
new->sets[x].blks[y].valid = -1;
new->sets[x].blks[y].tag = NULL; //null?
}
}
}//end if
return *new;
}
To make it easier to visualize, this is what happens when I print my cache. For this example I made a direct cache and added a single address to the cache.
Set 0
Block 0
tag 0
Set 1
Block 0
tag 0
Set 2
Block 0
tag 0
Set 3
Block 0
tag 0
Adding address: 7fff006891a8
Set 0
Block 0
tag 7fff006891a
Set 1
Block 0
tag 7fff006891a
Set 2
Block 0
tag 7fff006891a
Set 3
Block 0
tag 7fff006891a
0 commentaires:
Enregistrer un commentaire