diff --git a/desmume/src/NDSSystem.cpp b/desmume/src/NDSSystem.cpp index 253e48dc6..3dc9f1047 100644 --- a/desmume/src/NDSSystem.cpp +++ b/desmume/src/NDSSystem.cpp @@ -545,24 +545,49 @@ void GameInfo::closeROM() u32 GameInfo::readROM(u32 pos) { + u32 num; + u32 data; if (!romdata) { - u32 data; if (lastReadPos != pos) fseek(fROM, pos + headerOffset, SEEK_SET); - u32 num = fread(&data, 1, 4, fROM); + num = fread(&data, 1, 4, fROM); lastReadPos = (pos + num); - return LE_TO_LOCAL_32(data); } else { - if(pos + 4 > romsize) + if(pos + 4 <= romsize) { - printf("Panic! GameInfo reading out of buffer!\n"); - exit(-1); + //fast path + data = LE_TO_LOCAL_32(*(u32*)(romdata + pos)); + num = 4; + } + else + { + data = 0; + num = 0; + for(int i=0;i<4;i++) + { + if(pos >= romsize) + break; + data |= (romdata[pos]<<(i*8)); + pos++; + num++; + } } - return LE_TO_LOCAL_32(*(u32*)(romdata + pos)); } + + + //in case we didn't read enough data, pad the remainder with 0xFF + u32 pad = 0; + while(num<4) + { + pad >>= 8; + pad |= 0xFF000000; + num++; + } + + return LE_TO_LOCAL_32(data) & ~pad | pad; } bool GameInfo::isDSiEnhanced() diff --git a/desmume/src/addons/slot1comp_rom.cpp b/desmume/src/addons/slot1comp_rom.cpp index e7139096b..aeb35c1e6 100644 --- a/desmume/src/addons/slot1comp_rom.cpp +++ b/desmume/src/addons/slot1comp_rom.cpp @@ -71,10 +71,10 @@ u32 Slot1Comp_Rom::read() //if (address > gameInfo.header.endROMoffset) // ... the cart hardware doesnt know anything about the rom header. if it has a totally bogus endROMoffset, the cart will probably work just fine. and, the +4 is missing anyway: //3. this is better: it just allows us to read 0xFF anywhere we dont have rom data. forget what the header says + //note: we allow the reading to proceed anyway, because the readROM method is built to allow jaggedy reads off the end of the rom to support trimmed roms if(address+4 > gameInfo.romsize) { DEBUG_Notify.ReadBeyondEndOfCart(address,gameInfo.romsize); - return 0xFFFFFFFF; } //actually read from the ROM provider