在Java中使用System.arraycopy
所以我创建了一个直接映射回写缓存模拟器 在测试我的东西时,我将值“99”写入地址0x14c,它最初是一个值“4C” 然后我读入值0x348,这使得写回功能发生,因为下面两个地址都指向相同的插槽号,只是不同的标签 基本上,我需要将插槽中的所有数据从“cache”对象写入主_mem对象。我使用System.array复制 我可以看到值99已成功写入主内存阵列 但当我想再次读取地址14C时(这应该会把它的整个块包括99块),它会打印最初的内容,而不是反映我写的更改 System.arraycopy是否从特定索引开始从数组中提取数据?它只是从第一个索引的值开始计算吗 这是我的read()和write()方法以及输出在Java中使用System.arraycopy,java,arrays,copy,Java,Arrays,Copy,所以我创建了一个直接映射回写缓存模拟器 在测试我的东西时,我将值“99”写入地址0x14c,它最初是一个值“4C” 然后我读入值0x348,这使得写回功能发生,因为下面两个地址都指向相同的插槽号,只是不同的标签 基本上,我需要将插槽中的所有数据从“cache”对象写入主_mem对象。我使用System.array复制 我可以看到值99已成功写入主内存阵列 但当我想再次读取地址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();
}
如果startAddress是main_Mem中的地址,我想您需要:
System.arraycopy(main_Mem, startAddress, ...)
不是
尽管如此,其中一次运行的完整输出可能会使问题变得更加清楚。您的问题并不十分清楚,但一个简短但完整的程序演示该问题将使回答变得非常容易,我相信……
“这是一个脏槽!”
…一个元音就不好笑了!很抱歉我正在将数据从一个数组复制到另一个数组,当我尝试将数据重新加载到原始数组中时,这是不正确的。我更新了我的问题,以完全包含我的读写方法。问题是它说“332的值是76。”它应该是153(0x99)。它没有像应该的那样与我的副本一起保存到主内存中。问题是在第二次读取14c地址(索引)。它不再携带99。输出是4c(就像程序初始化时一样。):)我做了。直到不久前才回复或接受。现在快乐吗?
System.arraycopy(main_Mem, startAddress, ...)
System.arraycopy(main_Mem, main_Mem[startAddress], ...)