如果可以的话,寻求一些建议。我在我的 PlayStation 模拟器中有一个方法(基于 Java 的大学论文已经完成)。它需要一个整数内存地址,然后返回该地址的字节——根据地址将读取重定向到 RAM、BIOS ROM、给定的 I/O 端口等。目前,这是使用大量 if-else 案例来实现的,这些案例检查地址范围并相应地从正确的位置读取,返回字节。
这给我带来了大约 9% 的整体运行时间的性能损失。我想我可以使用调度表来改进这一点 - 本质上是一个带有自动装箱整数键的 HashMap 代表内存地址和一个 lambda 值来根据地址处理字节的返回。现在请记住,考虑到 PS1 的内存映射,大约有 260 万个不同的可能地址,这会使用更多的内存——这很好。
令我困惑的是,这比 if-else 语句的性能稍差——大约占总运行时间的 12%。有没有更好的方法来做我正在做的事情?我不能使用数组解决方案(地址作为原始 int 索引和存储在该索引处的 lambda),因为地址空间中存在间隙,如果没有太多的内存使用量,这将无法处理。
我很欣赏任何其他可能会降低这个数字的想法 - 我意识到 Java 不是一种很好的仿真语言,但我的部分论文证明它可以工作(确实如此)。非常感谢。
问候,菲尔
编辑:
下面是 readByte 方法的完整代码(地址被转换为 long 以允许将较低地址与较高地址进行比较,其值对于普通 int 为负):
/**
* This reads from the correct area depending on the address.
* @param address
* @return
*/
public byte readByte(int address) {
long tempAddress = address & 0xFFFFFFFFL;
byte retVal = 0;
if (tempAddress >= 0L && tempAddress < 0x200000L) { // RAM
retVal = ram[(int)tempAddress];
} else if (tempAddress >= 0x1F000000L && tempAddress < 0x1F800000L) { // Expansion Region 1
// do nothing for now
;
} else if (tempAddress >= 0x1F800000L && tempAddress < 0x1F800400L) { // Scratchpad
// read from data cache scratchpad if enabled
if (scratchpadEnabled()) {
tempAddress -= 0x1F800000L;
retVal = scratchpad[(int)tempAddress];
}
} else if (tempAddress >= 0x1F801000L && tempAddress < 0x1F802000L) { // I/O Ports
if (tempAddress >= 0x1F801000L && tempAddress < 0x1F801004L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(expansion1BaseAddress >>> 24);
break;
case 1:
retVal = (byte)(expansion1BaseAddress >>> 16);
break;
case 2:
retVal = (byte)(expansion1BaseAddress >>> 8);
break;
case 3:
retVal = (byte)expansion1BaseAddress;
break;
}
}
else if (tempAddress >= 0x1F801004L && tempAddress < 0x1F801008L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(expansion2BaseAddress >>> 24);
break;
case 1:
retVal = (byte)(expansion2BaseAddress >>> 16);
break;
case 2:
retVal = (byte)(expansion2BaseAddress >>> 8);
break;
case 3:
retVal = (byte)expansion2BaseAddress;
break;
}
} else if (tempAddress >= 0x1F801008L && tempAddress < 0x1F80100CL) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(expansion1DelaySize >>> 24);
break;
case 1:
retVal = (byte)(expansion1DelaySize >>> 16);
break;
case 2:
retVal = (byte)(expansion1DelaySize >>> 8);
break;
case 3:
retVal = (byte)expansion1DelaySize;
break;
}
} else if (tempAddress >= 0x1F80100CL && tempAddress < 0x1F801010L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(expansion3DelaySize >>> 24);
break;
case 1:
retVal = (byte)(expansion3DelaySize >>> 16);
break;
case 2:
retVal = (byte)(expansion3DelaySize >>> 8);
break;
case 3:
retVal = (byte)expansion3DelaySize;
break;
}
} else if (tempAddress >= 0x1F801010L && tempAddress < 0x1F801014L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(biosRomDelaySize >>> 24);
break;
case 1:
retVal = (byte)(biosRomDelaySize >>> 16);
break;
case 2:
retVal = (byte)(biosRomDelaySize >>> 8);
break;
case 3:
retVal = (byte)biosRomDelaySize;
break;
}
} else if (tempAddress >= 0x1F801014L && tempAddress < 0x1F801018L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(spuDelaySize >>> 24);
break;
case 1:
retVal = (byte)(spuDelaySize >>> 16);
break;
case 2:
retVal = (byte)(spuDelaySize >>> 8);
break;
case 3:
retVal = (byte)spuDelaySize;
break;
}
} else if (tempAddress >= 0x1F801018L && tempAddress < 0x1F80101CL) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(cdromDelaySize >>> 24);
break;
case 1:
retVal = (byte)(cdromDelaySize >>> 16);
break;
case 2:
retVal = (byte)(cdromDelaySize >>> 8);
break;
case 3:
retVal = (byte)cdromDelaySize;
break;
}
} else if (tempAddress >= 0x1F80101CL && tempAddress < 0x1F801020L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(expansion2DelaySize >>> 24);
break;
case 1:
retVal = (byte)(expansion2DelaySize >>> 16);
break;
case 2:
retVal = (byte)(expansion2DelaySize >>> 8);
break;
case 3:
retVal = (byte)expansion2DelaySize;
break;
}
} else if (tempAddress >= 0x1F801020L && tempAddress < 0x1F801024L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(commonDelay >>> 24);
break;
case 1:
retVal = (byte)(commonDelay >>> 16);
break;
case 2:
retVal = (byte)(commonDelay >>> 8);
break;
case 3:
retVal = (byte)commonDelay;
break;
}
} else if (tempAddress >= 0x1F801040L && tempAddress < 0x1F801050L) {
// read from ControllerIO object
retVal = cio.readByte((int)tempAddress);
} else if (tempAddress >= 0x1F801060L && tempAddress < 0x1F801064L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(ramSize >>> 24);
break;
case 1:
retVal = (byte)(ramSize >>> 16);
break;
case 2:
retVal = (byte)(ramSize >>> 8);
break;
case 3:
retVal = (byte)ramSize;
break;
}
}
else if (tempAddress >= 0x1F801070L && tempAddress < 0x1F801074L) { // Interrupt Status Register
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(interruptStatusReg >>> 24);
break;
case 1:
retVal = (byte)(interruptStatusReg >>> 16);
break;
case 2:
retVal = (byte)(interruptStatusReg >>> 8);
break;
case 3:
retVal = (byte)interruptStatusReg;
break;
}
}
else if (tempAddress >= 0x1F801074L && tempAddress < 0x1F801078L) { // Interrupt Mask Register
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(interruptMaskReg >>> 24);
break;
case 1:
retVal = (byte)(interruptMaskReg >>> 16);
break;
case 2:
retVal = (byte)(interruptMaskReg >>> 8);
break;
case 3:
retVal = (byte)interruptMaskReg;
break;
}
}
else if (tempAddress >= 0x1F801080L && tempAddress < 0x1F801100L) {
retVal = dma.readByte(address);
}
else if (tempAddress >= 0x1F801100L && tempAddress < 0x1F801104L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer0.counterValueRead() >>> 24);
break;
case 1:
retVal = (byte)(timer0.counterValueRead() >>> 16);
break;
case 2:
retVal = (byte)(timer0.counterValueRead() >>> 8);
break;
case 3:
retVal = (byte)timer0.counterValueRead();
break;
}
}
else if (tempAddress >= 0x1F801104L && tempAddress < 0x1F801108L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer0.counterModeRead(false) >>> 24);
break;
case 1:
retVal = (byte)(timer0.counterModeRead(false) >>> 16);
break;
case 2:
retVal = (byte)(timer0.counterModeRead(false) >>> 8);
break;
case 3:
retVal = (byte)timer0.counterModeRead(false);
break;
}
}
else if (tempAddress >= 0x1F801108L && tempAddress < 0x1F80110CL) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer0.counterTargetRead() >>> 24);
break;
case 1:
retVal = (byte)(timer0.counterTargetRead() >>> 16);
break;
case 2:
retVal = (byte)(timer0.counterTargetRead() >>> 8);
break;
case 3:
retVal = (byte)timer0.counterTargetRead();
break;
}
}
else if (tempAddress >= 0x1F801110L && tempAddress < 0x1F801114L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer1.counterValueRead() >>> 24);
break;
case 1:
retVal = (byte)(timer1.counterValueRead() >>> 16);
break;
case 2:
retVal = (byte)(timer1.counterValueRead() >>> 8);
break;
case 3:
retVal = (byte)timer1.counterValueRead();
break;
}
}
else if (tempAddress >= 0x1F801114L && tempAddress < 0x1F801118L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer1.counterModeRead(false) >>> 24);
break;
case 1:
retVal = (byte)(timer1.counterModeRead(false) >>> 16);
break;
case 2:
retVal = (byte)(timer1.counterModeRead(false) >>> 8);
break;
case 3:
retVal = (byte)timer1.counterModeRead(false);
break;
}
}
else if (tempAddress >= 0x1F801118L && tempAddress < 0x1F80111CL) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer1.counterTargetRead() >>> 24);
break;
case 1:
retVal = (byte)(timer1.counterTargetRead() >>> 16);
break;
case 2:
retVal = (byte)(timer1.counterTargetRead() >>> 8);
break;
case 3:
retVal = (byte)timer1.counterTargetRead();
break;
}
}
else if (tempAddress >= 0x1F801120L && tempAddress < 0x1F801124L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer2.counterValueRead() >>> 24);
break;
case 1:
retVal = (byte)(timer2.counterValueRead() >>> 16);
break;
case 2:
retVal = (byte)(timer2.counterValueRead() >>> 8);
break;
case 3:
retVal = (byte)timer2.counterValueRead();
break;
}
}
else if (tempAddress >= 0x1F801124L && tempAddress < 0x1F801128L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer2.counterModeRead(false) >>> 24);
break;
case 1:
retVal = (byte)(timer2.counterModeRead(false) >>> 16);
break;
case 2:
retVal = (byte)(timer2.counterModeRead(false) >>> 8);
break;
case 3:
retVal = (byte)timer2.counterModeRead(false);
break;
}
}
else if (tempAddress >= 0x1F801128L && tempAddress < 0x1F80112CL) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(timer2.counterTargetRead() >>> 24);
break;
case 1:
retVal = (byte)(timer2.counterTargetRead() >>> 16);
break;
case 2:
retVal = (byte)(timer2.counterTargetRead() >>> 8);
break;
case 3:
retVal = (byte)timer2.counterTargetRead();
break;
}
}
else if (tempAddress >= 0x1F801810L && tempAddress < 0x1F801814L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(gpu.readResponse() >>> 24);
break;
case 1:
retVal = (byte)(gpu.readResponse() >>> 16);
break;
case 2:
retVal = (byte)(gpu.readResponse() >>> 8);
break;
case 3:
retVal = (byte)gpu.readResponse();
break;
}
}
else if (tempAddress >= 0x1F801814L && tempAddress < 0x1F801818L) {
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(gpu.readStatus() >>> 24);
break;
case 1:
retVal = (byte)(gpu.readStatus() >>> 16);
break;
case 2:
retVal = (byte)(gpu.readStatus() >>> 8);
break;
case 3:
retVal = (byte)gpu.readStatus();
break;
}
}
else if (tempAddress >= 0x1F801800L && tempAddress < 0x1F801804L) { // CDROM
switch ((int)tempAddress & 0xF) {
case 0:
retVal = cdrom.read1800();
break;
case 1:
retVal = cdrom.read1801();
break;
case 2:
retVal = cdrom.read1802();
break;
case 3:
retVal = cdrom.read1803();
break;
}
}
else if (tempAddress >= 0x1F801C00L && tempAddress < 0x1F802000L) {
// fake SPU read
retVal = spu.readByte(address);
}
} else if (tempAddress >= 0x1F802000L && tempAddress < 0x1F803000L) { // Expansion Region 2 (I/O Ports)
// read from BIOS post register
if (tempAddress == 0x1F802041L) {
retVal = biosPost;
}
} else if (tempAddress >= 0x1FA00000L && tempAddress < 0x1FC00000L) { // Expansion Region 3 (Multipurpose)
// do nothing for now
;
} else if (tempAddress >= 0x1FC00000L && tempAddress < 0x1FC80000L) { // BIOS ROM
// read from memory mapped BIOS file
tempAddress -= 0x1FC00000L;
retVal = biosBuffer.get((int)tempAddress);
} else if (tempAddress >= 0xFFFE0000L && tempAddress < 0xFFFE0200L) { // I/O Ports (Cache Control)
if (tempAddress >= 0xFFFE0130L && tempAddress < 0xFFFE0134L) { // Cache Control Register
int shift = (int)(tempAddress & 0x3L);
switch (shift) {
case 0:
retVal = (byte)(cacheControlReg >>> 24);
break;
case 1:
retVal = (byte)(cacheControlReg >>> 16);
break;
case 2:
retVal = (byte)(cacheControlReg >>> 8);
break;
case 3:
retVal = (byte)cacheControlReg;
break;
}
}
}
return retVal;
}