jackie
jackie

Reputation: 644

Copying part of an array to another - Java

I am still having issues copying the part of one array to another. I am trying to check to contents of the array to make sure things are functioning but everything prints to zero. I initialize my array to zero first and as things happen in the program, different parts are copied into array called dataBlock which is part of the array called cache which is made of objects called SlotNodes.

Things are initialized in my simulator class as so

  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];

There's a menu, if we try to read an address, we dissect the address:

      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) {

When the validbit is 0, then we have to copy a block of data from the main_Mem array into the cache array's dataBlock array.

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]);
                      }
                  }

          }

The valid bit that i set to '1' prints out fine. It just seems like the numbers from the main_mem array aren't being copied to where they need to be. :(

Upvotes: 0

Views: 1076

Answers (1)

Jochen
Jochen

Reputation: 2295

You initialize cacheSize to 8, but you copy only while cacheSize < 8. That is never true.

Sorry to bash on this, but this is something you are supposed to find out about by using a debugger and stepping through your program, rather having people debug you code by reading it.

Upvotes: 1

Related Questions