2012-03-21 38 views
1

在創建我的高速緩存模擬器時,我意識到我需要一種結構來保存通常從主內存複製到高速緩存的數據塊。在這種情況下,它擁有8個數字。我將緩存設置爲對象,以便我可以將它設置爲標記,有效位,髒位,最後是數據塊。所以我想在這裏做一個數組是最好的選擇。我的getter和setter只是:Java中的高速緩存模擬器,實現數據塊

public int[] getDataBlock() { 
    return dataBlock; 
} 
public void setDataBlock(int[] dataBlock) { 
    this.dataBlock = dataBlock; 
} 

如果他們是,我如何初始化緩存爲全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); 
} 

回答

1

你可以寫一個輔助函數來清除緩存插槽這樣的:

public void clearData(){ 
    Arrays.fill(this.dataBlock,0); 
} 

從主存複製到一個特定的插槽,使用System.arraycopy

public void copyToCacheSlot(int[] mainMem, int baseIndex, int length){ 
    if (length <= 8) 
     System.arraycopy(mainMem, baseIndex, this.dataBlock, 0, length); 
    else 
     //Copied too much 
} 

遍歷你的cache數組並且按照的每個元素調用上述函數。

+0

'cache [i] .setDataBlock([0]);'應該已被修改,以表明我不知道該怎麼做才能將數組的值初始化爲0.並且對於n00bness感到抱歉但是我設置了構造函數與cacheNode類中的其他值一起使用。我背後的想法是每個SLOT都需要一個單獨的數組或數據塊。 – jackie 2012-03-21 20:46:28

+0

'public class SlotNode {public int validBit; public int tag; public int [] dataBlock = new int [8]; public int dirty; public int [] getDataBlock(){ \t \t return dataBlock; \t} \t公共無效setDataBlock(INT []數據塊){ \t \t this.dataBlock =數據塊; \t} \t public int getDirty(){ return dirty; } public void setDirty(int dirty){this.dirty = dirty; } } – jackie 2012-03-21 20:47:05

+0

所以你說,緩存有多個插槽,每個插槽是一個數組? – 2012-03-21 20:47:44