In creating my cache simulator, I realized that I need a structure that holds the block of data that is normally copied from main memory to the cache. In this case, it’s holding 8 numbers. I’ve setup my cache as an object so that I can set it’s Tag, Valid Bit, Dirty Bit, and finally the data block. So I’m thinking an array would be the best thing to do here. Would my getters and setters just be :
public int[] getDataBlock() {
return dataBlock;
}
public void setDataBlock(int[] dataBlock) {
this.dataBlock = dataBlock;
}
If they are, how do I initialize the cache to all 0?
//initialize cache slots to 0
for (int i = 0; i<cache.length; i++) {
cache[i] = new SlotNode();
cache[i].setValidBit(0);
cache[i].setTag(0);
for (int j = 0; j < cache.length; j++) {
cache[i].setDataBlock([0]);
}
//cache[i].setData(0);
cache[i].setDirty(0);
}
You can write a helper function to clear a cache slot like this:
To copy from main mem to a particular slot, use System.arraycopy:
Loop through your
cachearray and call the above functions accordinly for each element.