From 2f24f37e7b96054cc364eca4013a1e5f1b1bbe47 Mon Sep 17 00:00:00 2001 From: Balint Kovacs Date: Mon, 30 Sep 2019 01:00:42 +0100 Subject: [PATCH] Temporarily disable IR carts And massively simplify chip detection --- arm9/source/gamecart/card_spi.c | 39 +++++++++++++++++---------------- arm9/source/gamecart/card_spi.h | 4 ++-- arm9/source/gamecart/gamecart.c | 2 +- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/arm9/source/gamecart/card_spi.c b/arm9/source/gamecart/card_spi.c index 958a929..87e270c 100644 --- a/arm9/source/gamecart/card_spi.c +++ b/arm9/source/gamecart/card_spi.c @@ -438,24 +438,28 @@ int _SPIIsDataMirrored(CardSPIType type, int size, bool* mirrored) { return 0; } -int CardSPIGetCardSPIType(CardSPIType* type, int infrared) { +int CardSPIGetCardSPIType(CardSPIType* type, bool infrared) { u8 sr = 0; u32 jedec = 0; - u32 tries = 0; - CardSPIType t = { &FLASH_DUMMY, infrared == 1 }; + CardSPIType t = {NO_CHIP, infrared}; int res; - u32 maxTries = (infrared == -1) ? 2 : 1; // note: infrared = -1 fails 1/3 of the time - while(tries < maxTries){ - res = CardSPIReadJEDECIDAndStatusReg(t, &jedec, &sr); // dummy - if (res) return res; - - if ((sr & 0xfd) == 0x00 && (jedec != 0x00ffffff)) { break; } - if ((sr & 0xfd) == 0xF0 && (jedec == 0x00ffffff)) { *type = (CardSPIType) { EEPROM_512B, false }; return 0; } - if ((sr & 0xfd) == 0x00 && (jedec == 0x00ffffff)) { t = (CardSPIType) { &EEPROM_DUMMY, false }; break; } - - ++tries; - t.infrared = true; + if(infrared) { + // Infrared carts currently not supported, need additional handling! + *type = (CardSPIType) {NO_CHIP, true}; + return 0; + } + + res = CardSPIReadJEDECIDAndStatusReg(t, &jedec, &sr); + if (res) return res; + + if ((sr & 0xfd) == 0x00 && (jedec != 0x00ffffff)) { t.chip = &FLASH_DUMMY; } + if ((sr & 0xfd) == 0xF0 && (jedec == 0x00ffffff)) { *type = (CardSPIType) { EEPROM_512B, false }; return 0; } + if ((sr & 0xfd) == 0x00 && (jedec == 0x00ffffff)) { t = (CardSPIType) { &EEPROM_DUMMY, false }; } + + if(t.chip == NO_CHIP) { + *type = (CardSPIType) {NO_CHIP, false}; + return 0; } if (t.chip == &EEPROM_DUMMY) { @@ -473,17 +477,14 @@ int CardSPIGetCardSPIType(CardSPIType* type, int infrared) { return 0; } - if (infrared == 0 && t.infrared) *type = (CardSPIType) { NO_CHIP, false }; // did anything go wrong? - if (infrared == 1 && !t.infrared) *type = (CardSPIType) { NO_CHIP, true }; - for(size_t i = 0; i < sizeof(flashTypes) / sizeof(CardSPITypeData); i++) { if (flashTypes[i].jedecId == jedec) { - *type = (CardSPIType) { flashTypes + i, t.infrared }; + *type = (CardSPIType) { flashTypes + i, infrared }; return 0; } } - *type = (CardSPIType) { NO_CHIP, t.infrared }; + *type = (CardSPIType) { NO_CHIP, infrared }; return 0; } diff --git a/arm9/source/gamecart/card_spi.h b/arm9/source/gamecart/card_spi.h index 581fcef..bec772e 100644 --- a/arm9/source/gamecart/card_spi.h +++ b/arm9/source/gamecart/card_spi.h @@ -60,7 +60,7 @@ const CardSPITypeData * const FLASH_256KB_2; const CardSPITypeData * const FLASH_512KB_1; const CardSPITypeData * const FLASH_512KB_2; const CardSPITypeData * const FLASH_1MB; -const CardSPITypeData * const FLASH_8MB; // <- can't restore savegames, and maybe not read them atm +const CardSPITypeData * const FLASH_8MB; const CardSPITypeData * const FLASH_128KB_CTR; // Most common, including Ocarina of time 3D const CardSPITypeData * const FLASH_512KB_CTR; // Also common, including Detective Pikachu @@ -70,7 +70,7 @@ int CardSPIWriteRead(CardSPIType type, const void* cmd, u32 cmdSize, void* answe int CardSPIWaitWriteEnd(CardSPIType type); int CardSPIEnableWriting(CardSPIType type); int CardSPIReadJEDECIDAndStatusReg(CardSPIType type, u32* id, u8* statusReg); -int CardSPIGetCardSPIType(CardSPIType* type, int infrared); +int CardSPIGetCardSPIType(CardSPIType* type, bool infrared); u32 CardSPIGetPageSize(CardSPIType type); u32 CardSPIGetCapacity(CardSPIType type); u32 CardSPIGetEraseSize(CardSPIType type); diff --git a/arm9/source/gamecart/gamecart.c b/arm9/source/gamecart/gamecart.c index c5fb467..0c88461 100644 --- a/arm9/source/gamecart/gamecart.c +++ b/arm9/source/gamecart/gamecart.c @@ -134,7 +134,7 @@ u32 InitCartRead(CartData* cdata) { if (cdata->data_size > cdata->cart_size) return 1; // save data - u32 infrared = (*(nds_header->game_code) == 'I') ? 1 : 0; + bool infrared = *(nds_header->game_code) == 'I'; if (CardSPIGetCardSPIType(&(cdata->save_type), infrared) != 0) { cdata->save_type = (CardSPIType) { NO_CHIP, false }; }