我仍然无法将一个数组的一部分复制到另一个数组。我试图检查数组的内容,以确保一切正常,但所有内容都打印为零。我首先将我的数组初始化为零,并且当程序发生时,不同的部分被复制到名为dataBlock的数组中,该数组是名为cache的数组的一部分,该数组由称为SlotNodes的对象组成。
事情在我的模拟器类中初始化为
public static SlotNode[] cache = new SlotNode[8];
public static int cacheSize = 8;
public static int memSize = 2048;
public static byte[] main_Mem = new byte[memSize];
有一个菜单,如果我们尝试阅读地址,我们会解析地址:
public static void readAddress() {
System.out.println("What address? ");
//String tmpAddress = keyboard.next();
address = keyboard.nextInt(16);
System.out.println("After parsing the string to base 16, you get ");
System.out.printf("%X", 0xFF & address);
System.out.println(" ");
//get offset
offset = address & 0x7;
System.out.println(offset);
//get tag
int tmpTag = address >> 6;
tag = tmpTag & 0x1F;
System.out.println(tag);
//get slot
slot = (address >> 3) & 0x7;
System.out.println(slot);
//go to slot number and see what valid bit is
if (cache[slot].getValidBit() == 0) {
当validbit为0时,我们必须将main_Mem数组中的数据块复制到缓存数组的dataBlock数组中。
System.out.println("Miss");
//copy block in main memory, 8 bytes
//address/8 gets address of block in mainmemory
int startAddress = address & 0x7F8;
System.out.println("The start address of " + address + " should be " + startAddress);
cache[slot].setValidBit(1);
cache[slot].setTag(tag);
//Now need to copy from startAddress plus 8 bytes into cache[slot]
while (cacheSize < 8){
System.arraycopy(main_Mem, startAddress, cache[slot].dataBlock, 0, cacheSize);
}
System.out.println();
System.out.println("The block of data is: ");
for (int i=0; i<8; i++) {
for (int j=0; j<7; j++){
System.out.println("Value: " + cache[i].dataBlock[j]);
}
}
}
我设置为'1'的有效位打印出来。看起来main_mem数组中的数字似乎没有被复制到它们需要的位置。 :(
答案 0 :(得分:1)
您将cacheSize初始化为8,但仅在cacheSize&lt;那是不对的。
很抱歉打扰这个,但这是您应该通过使用调试器并逐步执行程序来找到的,而不是让人们通过阅读来调试代码。