From 50d040a3a15b694a6cfeb7c54dfef1f428730254 Mon Sep 17 00:00:00 2001 From: rogerman Date: Wed, 18 Dec 2013 08:15:33 +0000 Subject: [PATCH] Core (v0.9.10): - Backport changes from r4946 to the 0.9.10 branch. --- desmume/src/NDSSystem.cpp | 164 ++++++++++++---------- desmume/src/addons/slot1comp_protocol.cpp | 9 ++ desmume/src/addons/slot1comp_rom.cpp | 2 +- desmume/src/types.h | 4 +- desmume/src/utils/decrypt/header.cpp | 12 +- 5 files changed, 111 insertions(+), 80 deletions(-) diff --git a/desmume/src/NDSSystem.cpp b/desmume/src/NDSSystem.cpp index 7ef8501b5..71b811f5f 100644 --- a/desmume/src/NDSSystem.cpp +++ b/desmume/src/NDSSystem.cpp @@ -179,61 +179,10 @@ void NDS_DeInit(void) NDS_header * NDS_getROMHeader(void) { - NDS_header * header = new NDS_header; - - memcpy(header, &gameInfo.header, sizeof(gameInfo.header)); - - //endian swap necessary fields. It would be better if we made accessors for these. I wonder if you could make a macro for a field accessor that would take the bitsize and do the swap on the fly - struct FieldSwap { - size_t offset; - int bytes; - }; - - static const FieldSwap fieldSwaps[] = { - { offsetof(NDS_header,makerCode), 2}, - - { offsetof(NDS_header,ARM9src), 4}, - { offsetof(NDS_header,ARM9exe), 4}, - { offsetof(NDS_header,ARM9cpy), 4}, - { offsetof(NDS_header,ARM7src), 4}, - { offsetof(NDS_header,ARM7exe), 4}, - { offsetof(NDS_header,ARM7cpy), 4}, - { offsetof(NDS_header,ARM7binSize), 4}, - { offsetof(NDS_header,FNameTblOff), 4}, - { offsetof(NDS_header,FNameTblSize), 4}, - { offsetof(NDS_header,FATOff), 4}, - { offsetof(NDS_header,FATSize), 4}, - { offsetof(NDS_header,ARM9OverlayOff), 4}, - { offsetof(NDS_header,ARM9OverlaySize), 4}, - { offsetof(NDS_header,ARM7OverlayOff), 4}, - { offsetof(NDS_header,ARM7OverlaySize), 4}, - { offsetof(NDS_header,normalCmd), 4}, - { offsetof(NDS_header,Key1Cmd), 4}, - { offsetof(NDS_header,IconOff), 4}, - - { offsetof(NDS_header,CRC16), 2}, - { offsetof(NDS_header,ROMtimeout), 2}, - - { offsetof(NDS_header,ARM9autoload), 4}, - { offsetof(NDS_header,ARM7autoload), 4}, - { offsetof(NDS_header,endROMoffset), 4}, - { offsetof(NDS_header,HeaderSize), 4}, - - { offsetof(NDS_header, ARM9module), 4}, - { offsetof(NDS_header, ARM7module), 4}, - - { offsetof(NDS_header,logoCRC16), 2}, - { offsetof(NDS_header,headerCRC16), 2}, - }; - - for(int i=0;i>1); @@ -421,14 +425,24 @@ bool GameInfo::loadROM(std::string fname, u32 type) { delete [] romdata; romdata = NULL; romsize = 0; - + return false; } - + if(hasRomBanner()) + { memcpy(&banner, romdata + header.IconOff, sizeof(RomBanner)); - - _isDSiEnhanced = ((*(u32*)(romdata + 0x180) == 0x8D898581U) && (*(u32*)(romdata + 0x184) == 0x8C888480U)); + + banner.version = LE_TO_LOCAL_16(banner.version); + banner.crc16 = LE_TO_LOCAL_16(banner.crc16); + + for(size_t i = 0; i < ARRAY_SIZE(banner.palette); i++) + { + banner.palette[i] = LE_TO_LOCAL_16(banner.palette[i]); + } + } + + _isDSiEnhanced = (LE_TO_LOCAL_32(*(u32*)(romdata + 0x180) == 0x8D898581U) && LE_TO_LOCAL_32(*(u32*)(romdata + 0x184) == 0x8C888480U)); fclose(fROM); fROM = NULL; return true; } @@ -437,6 +451,14 @@ bool GameInfo::loadROM(std::string fname, u32 type) { fseek(fROM, header.IconOff + headerOffset, SEEK_SET); fread(&banner, 1, sizeof(RomBanner), fROM); + + banner.version = LE_TO_LOCAL_16(banner.version); + banner.crc16 = LE_TO_LOCAL_16(banner.crc16); + + for(size_t i = 0; i < ARRAY_SIZE(banner.palette); i++) + { + banner.palette[i] = LE_TO_LOCAL_16(banner.palette[i]); + } } fseek(fROM, headerOffset, SEEK_SET); lastReadPos = 0; @@ -471,10 +493,10 @@ u32 GameInfo::readROM(u32 pos) fseek(fROM, pos + headerOffset, SEEK_SET); u32 num = fread(&data, 1, 4, fROM); lastReadPos = (pos + num); - return data; + return LE_TO_LOCAL_32(data); } else - return *(u32*)(romdata + pos); + return LE_TO_LOCAL_32(*(u32*)(romdata + pos)); } static int rom_init_path(const char *filename, const char *physicalName, const char *logicalFilename) @@ -2175,7 +2197,7 @@ bool NDS_FakeBoot() u32 dst = header->ARM9cpy; for(u32 i = 0; i < header->ARM9binSize; i+=4) { - u32 tmp = (hasSecureArea && ((src >= 0x4000) && (src < 0x8000)))?*(u32*)(gameInfo.secureArea + (src - 0x4000)):gameInfo.readROM(src); + u32 tmp = (hasSecureArea && ((src >= 0x4000) && (src < 0x8000)))?LE_TO_LOCAL_32(*(u32*)(gameInfo.secureArea + (src - 0x4000))):gameInfo.readROM(src); _MMU_write32(dst, tmp); @@ -2215,12 +2237,12 @@ bool NDS_FakeBoot() { //dsi needs this copied later in memory. there are probably a number of things that get copied to a later location in memory.. thats where the NDS consoles tend to stash stuff. for (int i = 0; i < (0x170); i+=4) - _MMU_write32(0x027FFE00 + i, LE_TO_LOCAL_32(gameInfo.readROM(i))); + _MMU_write32(0x027FFE00 + i, gameInfo.readROM(i)); } else { for (int i = 0; i < (0x170); i+=4) - _MMU_write32(0x027FFE00 + i, LE_TO_LOCAL_32(gameInfo.readROM(i))); + _MMU_write32(0x027FFE00 + i, gameInfo.readROM(i)); } //the firmware will be booting to these entrypoint addresses via BX (well, the arm9 at least; is unverified for the arm7) diff --git a/desmume/src/addons/slot1comp_protocol.cpp b/desmume/src/addons/slot1comp_protocol.cpp index f8f4b3efe..d99266c7a 100644 --- a/desmume/src/addons/slot1comp_protocol.cpp +++ b/desmume/src/addons/slot1comp_protocol.cpp @@ -95,7 +95,11 @@ void Slot1Comp_Protocol::write_command_KEY1(GC_Command command) //TODO - more endian-safe way of doing this (theres examples in R4) { +#ifdef LOCAL_LE u64 cmd64 = bswap64(*(u64*)command.bytes); +#else + u64 cmd64 = *(u64*)command.bytes; +#endif //todo - parse into blocknumber address = (u32)((cmd64 >> 32) & 0xF000); } @@ -129,7 +133,12 @@ void Slot1Comp_Protocol::write_command_NORMAL(GC_Command command) operation = eSlot1Operation_B7_Read; //TODO - more endian-safe way of doing this (theres examples in R4) +#ifdef LOCAL_LE u64 cmd64 = bswap64(*(u64*)command.bytes); +#else + u64 cmd64 = *(u64*)command.bytes; +#endif + address = (u32)((cmd64 >> 24)); length = 0x200; diff --git a/desmume/src/addons/slot1comp_rom.cpp b/desmume/src/addons/slot1comp_rom.cpp index cac3068fc..a381e1584 100644 --- a/desmume/src/addons/slot1comp_rom.cpp +++ b/desmume/src/addons/slot1comp_rom.cpp @@ -42,7 +42,7 @@ u32 Slot1Comp_Rom::read() { //see B7 for details address &= gameInfo.mask; //sanity check - u32 ret = *(u32*)(gameInfo.secureArea + (address - 0x4000)); + u32 ret = LE_TO_LOCAL_32(*(u32*)(gameInfo.secureArea + (address - 0x4000))); address = (address&~0xFFF) + ((address+4)&0xFFF); return ret; } diff --git a/desmume/src/types.h b/desmume/src/types.h index 39c416589..6942c6f8d 100644 --- a/desmume/src/types.h +++ b/desmume/src/types.h @@ -256,10 +256,10 @@ typedef int desmume_BOOL; #ifdef LOCAL_BE /* local arch is big endian */ # define LE_TO_LOCAL_16(x) ((((x)&0xff)<<8)|(((x)>>8)&0xff)) # define LE_TO_LOCAL_32(x) ((((x)&0xff)<<24)|(((x)&0xff00)<<8)|(((x)>>8)&0xff00)|(((x)>>24)&0xff)) -# define LE_TO_LOCAL_64(x) ((((x)&0xff)<<56)|(((x)&0xff00)<<40)|(((x)&0xff0000)<<24)|(((x)&0xff000000)<<8)|(((x)>>8)&0xff000000)|(((x)>>24)&0xff00)|(((x)>>40)&0xff00)|(((x)>>56)&0xff)) +# define LE_TO_LOCAL_64(x) ((((x)&0xff)<<56)|(((x)&0xff00)<<40)|(((x)&0xff0000)<<24)|(((x)&0xff000000)<<8)|(((x)>>8)&0xff000000)|(((x)>>24)&0xff0000)|(((x)>>40)&0xff00)|(((x)>>56)&0xff)) # define LOCAL_TO_LE_16(x) ((((x)&0xff)<<8)|(((x)>>8)&0xff)) # define LOCAL_TO_LE_32(x) ((((x)&0xff)<<24)|(((x)&0xff00)<<8)|(((x)>>8)&0xff00)|(((x)>>24)&0xff)) -# define LOCAL_TO_LE_64(x) ((((x)&0xff)<<56)|(((x)&0xff00)<<40)|(((x)&0xff0000)<<24)|(((x)&0xff000000)<<8)|(((x)>>8)&0xff000000)|(((x)>>24)&0xff00)|(((x)>>40)&0xff00)|(((x)>>56)&0xff)) +# define LOCAL_TO_LE_64(x) ((((x)&0xff)<<56)|(((x)&0xff00)<<40)|(((x)&0xff0000)<<24)|(((x)&0xff000000)<<8)|(((x)>>8)&0xff000000)|(((x)>>24)&0xff0000)|(((x)>>40)&0xff00)|(((x)>>56)&0xff)) #else /* local arch is little endian */ # define LE_TO_LOCAL_16(x) (x) # define LE_TO_LOCAL_32(x) (x) diff --git a/desmume/src/utils/decrypt/header.cpp b/desmume/src/utils/decrypt/header.cpp index 5edc948a5..8204b9a34 100644 --- a/desmume/src/utils/decrypt/header.cpp +++ b/desmume/src/utils/decrypt/header.cpp @@ -68,14 +68,14 @@ */ int DetectRomType(const Header& header, char* secure) { - unsigned int * data = (unsigned int*)(secure); - + const u64 data = LE_TO_LOCAL_64(*(u64 *)secure); + //this is attempting to check for an utterly invalid nds header - if(header.unitcode < 0 && header.unitcode > 3) return ROMTYPE_INVALID; - + if(header.unitcode > 3) return ROMTYPE_INVALID; + if (header.arm9_rom_offset < 0x4000) return ROMTYPE_HOMEBREW; - if (data[0] == 0x00000000 && data[1] == 0x00000000) return ROMTYPE_MULTIBOOT; - if (data[0] == 0xE7FFDEFF && data[1] == 0xE7FFDEFF) return ROMTYPE_NDSDUMPED; + else if (data == 0x0000000000000000ULL) return ROMTYPE_MULTIBOOT; + else if (data == 0xE7FFDEFFE7FFDEFFULL) return ROMTYPE_NDSDUMPED; //TODO //for (int i=0x200; i<0x4000; i++) // if (romdata[i]) return ROMTYPE_MASKROM; // found something odd ;)