I was working on part of my project which has to do with direct mapped caches. In the following I created a structure for my cache elements:
typedef struct node{
int *tagBits;
int *setBits;
int *blockOff;
} cacheNode;
I want to store the corresponding block offset bits, set bits and tag bits within each element.
For example, the binary of a memory address given is: 000000000000000000001001110010110011110101000100. I want the first two bits to be the block offset and the next two to be the set bits followed by the remaining 44 to be the tag. The result should be blockOff = 00, setBit = 01 tag = rest. The parameters: blockOff, setBits and tagBits represent how many bits belong to what in the binary (blockOff =2, set = 2 and tag = 44). Here is what I tried so far:
void directMap(cacheNode** cache,int blockOff, int setBits, int
tagBits,int* binary)
{
int tagDec, blockDec, setDec, i= 0;
cacheNode* element = (cacheNode*)malloc(sizeof(cacheNode));
element->blockOff = (int*)malloc(blockOff * sizeof(int));
element->setBits = (int*)malloc(setBits * sizeof(int));
element->tagBits = (int*)malloc(tagBits * sizeof(int));
for(i = 0; i< 48; i++){
if(i = 0 && i < blockOff){
element->blocOff[i] = binary[i];
}
else if(i = (blockOff) && i < (blockOff + setBits)){
element->setBits[i] = binary[i];
}
}
printString(element->blockOff,blockOff);
printString(element->setBits,setBits);
}
My problem is that when I print out both integer arrays from my struct it gives me '00' for set and blockOff. The answer is correct for blockOff but not set, set should be '01'. Any HINTS as to why?