2012-03-22 103 views
0

我仍然有问题复制到另一个阵列的一部分。我试图检查数组的内容,以确保事情正常运行,但所有东西打印为零。我首先将我的数组初始化为零,当程序发生时,将不同的部分复制到名为dataBlock的数组中,这是数组的一部分,称为缓存,它由名为SlotNodes的对象组成。复制阵列的一部分到另一个 - Java

东西都在我的模拟类初始化为这样

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

不应该“while(cacheSize <8){”be“if(cacheSize <8){”以防止无限循环? – 2012-03-22 00:41:33

+0

已更新但所有东西仍然打印为零 – jackie 2012-03-22 00:44:00

+0

忘记我以前的评论... – Jochen 2012-03-22 00:49:34

回答

1

初始化的cachesize为8,但只复制而CACHESIZE < 8.这是不正确的。

对不起来砸这一点,但是这是通过使用你应该了解调试程序和步进程序,而不是让人们通过读取代码来调试代码。

+0

你是对的,那是一个愚蠢的错误XD也注意到我是在复制地址号而不是实际的内容。 – jackie 2012-03-22 01:06:48

相关问题