2012-03-23 4 views
0

Итак, я создал имитатор кэша обратной записи с прямым отображением.Использование System.arraycopy в Java

При тестировании моих вещей я написал адрес 0x14c значение «99», изначально это было значение «4C» Затем я прочитал значение 0x348, что делает функцию обратной записи, потому что оба этих адреса ниже тот же номер слота, только другой тег.

В принципе, мне тогда необходимо записать все данные в слот из объекта «cache» в объект main_mem. Я использую копию System.array.

Я могу видеть, что значение 99 IS успешно записано в массив основной памяти.

Но когда я хочу снова прочитать адрес 14C (который должен вытащить весь блок в том числе и 99), он печатает то, что было там первоначально, не отражая это изменение, которое я написал.

Неужели System.arraycopy не вытаскивает данные из массива, начиная с определенного индекса? Он просто подсчитывает значение первого индекса?

Вот мой читать() и записи() методы и выход

 public static void readAddress() { 
      System.out.println("What address? "); 

      address = keyboard.nextInt(16); 
      startAddress = address & 0x758; 
      tag = (address >> 6) & 0x1F; 
      slot = (address >> 3) & 0x7; 

      //Valid bit is 0, empty slot 
      if (cache[slot].getValidBit() == 0) {    

       cache[slot].setValidBit(1); 
       cache[slot].setTag(tag); 
       cache[slot].setStartAddress(startAddress); 

       System.arraycopy(main_Mem, main_Mem[startAddress], cache[slot].dataBlock, 0, cacheSize); 

       System.out.println("Cache Miss"); 
       System.out.print("The value at that address is: "); 
       System.out.printf("%X", 0xFF & address); 
       System.out.println(); 
       System.out.println(); 
      } 
      //Valid bit 1 but tags don't match 
      else if (cache[slot].getValidBit() == 1 && cache[slot].getTag() != tag) { 
       System.out.println("Cache Miss "); 

       if (cache[slot].getDirty() == 1) { 
        System.out.println("This is a dirty slot!"); 
        //copy contents of slot back into main memory before loading new block 
        System.out.println("Slot is dirty and will be written back, val for 14c is " +cache[slot].dataBlock[4]); 
        System.arraycopy(main_Mem, cache[slot].getStartAddress(), cache[slot].dataBlock, 0, cacheSize); 
        System.out.println("Everything should have been copied to main by now. The value for 332 is " + main_Mem[332]); 
       } 

       startAddress = address & 0x7F8; 
       cache[slot].setTag(tag); 
       cache[slot].setStartAddress(startAddress); 
       //set dirty back to 0, incase it was 1 
       cache[slot].setDirty(0); 

       for (int i = 0; i < cacheSize; i++) { 
        for (int j = cache[slot].getStartAddress(); j<cacheSize; j ++) { 
         cache[slot].dataBlock[i] = main_Mem[j]; 
        } 
       } 
       //System.arraycopy(main_Mem, main_Mem[startAddress], cache[slot].dataBlock, 0, cacheSize); 
       System.out.print("The value at that address is: "); 
       System.out.printf("%X", 0xFF & address); 
       System.out.println(); 
       System.out.println(); 
      } 
      //Valid bit 1 and tags match, hit 
      else if (cache[slot].getValidBit() == 1 && tag == cache[slot].getTag()) { 
       System.out.println("Cache Hit"); 
       System.out.print("The value at that address is: "); 
       System.out.printf("%X", 0xFF & address); 
       System.out.println(); 
       System.out.println(); 
      } 

      menu(); 
    } 

    public static void writeAddress() { 

     System.out.println("What address do you want to write to? "); 
     address = keyboard.nextInt(16); 
     System.out.println("And what value do you want to write to it? "); 
     int input = keyboard.nextInt(16); 

     startAddress = address & 0x758; 
     tag = (address >> 6) & 0x1F; 
     slot = (address >> 3) & 0x7; 
     //Valid bit 1, tag matches, hit, just modify value 
     if (cache[slot].getValidBit() != 0 && cache[slot].getTag() == tag) { 
      System.out.println("Cache Hit"); 
      System.out.printf("%X", 0xFF & address); 

      for (int i = 0; i <8; i++) { 
       if (cache[slot].dataBlock[i] == (0xFF & address)) { 
        cache[slot].dataBlock[i] = input; 
        cache[slot].setDirty(1); 
       } 
      } 
     } 
     //Valid bit 1, tags don't match-Check dirty bit and write back first if valid 
     else if (cache[slot].getValidBit() == 1 && cache[slot].getTag() != tag) { 

      if (cache[slot].getDirty() ==1) { 
       //copy contents of slot back into main memory before loading new block 
       for (int i = 0; i < cacheSize; i++) { 
        for (int j = startAddress; j<cacheSize; j++) { 
         cache[slot].dataBlock[i] = main_Mem[j]; 
        } 
       } 
       //System.arraycopy(cache[slot].dataBlock, 0, main_Mem, cache[slot].getStartAddress(), cacheSize); 
      } 

      System.out.println("Cache Miss"); 
      cache[slot].setValidBit(1); 
      cache[slot].setTag(tag); 
      cache[slot].setDirty(1); 
      cache[slot].setStartAddress(startAddress); 
      //copy new block into cache now 
      System.arraycopy(main_Mem, main_Mem[startAddress], cache[slot].dataBlock, 0, cacheSize); 

      for (int i = 0; i <8; i++) { 
       if (cache[slot].dataBlock[i] == (0xFF & address)) { 
        System.out.println("Writing over the value of that address now..."); 
        cache[slot].dataBlock[i] = input; 
       } 
      } 

     } 
     //Empty slot, no need to write back 
     else if (cache[slot].getValidBit() == 0) { 
      System.out.println("Cache Miss"); 
      System.out.println("Setting the dirty bit to 1"); 
      System.out.println("Dirty bit was " + cache[slot].getDirty()); 

      cache[slot].setValidBit(1); 
      cache[slot].setTag(tag); 
      cache[slot].setDirty(1); 
      System.out.println("And is now " +cache[slot].getDirty()); 
      cache[slot].setStartAddress(startAddress); 
      //copy from main mem to cache 
      System.arraycopy(main_Mem, main_Mem[startAddress], cache[slot].dataBlock, 0, cacheSize); 

      //writes to selected value in the cache 
      for (int i = 0; i <8; i++) { 
       if (cache[slot].dataBlock[i] == (0xFF & address)) { 
        System.out.println("Writing over the value of that address now..."); 
        cache[slot].dataBlock[i] = input; 
       } 
      } 
     } 
     menu(); 
    } 

Выход:

This is a cache simulator, type in what you want to do and press enter 

[r] to read [w] to write [d] to display 
w 
What address do you want to write to? 
14c 
And what value do you want to write to it? 
99 
Cache Miss 
Setting the dirty bit to 1 
Dirty bit was 0 
And is now 1 
Writing over the value of that address now... 
This is a cache simulator, type in what you want to do and press enter 

[r] to read [w] to write [d] to display 
r 
What address? 
348 
Cache Miss 
This is a dirty slot! 
Slot is dirty and will be written back, val for 14c is 153 
Everything should have been copied to main by now. The value for 332 is 76 
The value at that address is: 48 

This is a cache simulator, type in what you want to do and press enter 

[r] to read [w] to write [d] to display 
+1

Ваш вопрос не очень ясен, но короткая, но полная программа, демонстрирующая проблему, заставит ее очень легко ответить, я уверен ... –

+2

'' Это грязный слот! "' ... one гласный от веселья! – mre

+0

Извините. Я копирую данные из одного массива в другой, и когда я пытаюсь перезагрузить эти данные обратно в исходный массив, это неверно. – jackie

ответ

1

Если StartAddress это адрес в main_Mem, я думаю, что вы хотите:

System.arraycopy(main_Mem, startAddress, ...) 

System.arraycopy(main_Mem, main_Mem[startAddress], ...) 

Хотя полный вывод одного из прогонов, скорее всего, сделает его более понятным, что происходит не так и почему.

+0

Я обновил свой вопрос, чтобы полностью включить мои методы чтения и записи. Проблема заключается в том, где говорится: «Значение для 332 равно 76». Это должно быть 153 (0x99). Это не сохранение в основной памяти с моей копией, как должно. – jackie

+0

Проблема заключается во чтении 14c адреса (индекса) во второй раз. Он больше не переносит 99. Выход 4c (например, когда программа инициализируется.) – jackie

+0

@JackieAldama: Вы даже прочитали этот ответ? – ruakh