Updated gamecart reset routines

thanks @profi200
This commit is contained in:
d0k3 2018-06-08 01:43:45 +02:00
parent a6305206e0
commit e4352d4cb0
3 changed files with 32 additions and 52 deletions

View File

@ -7,7 +7,7 @@
#include "ncch.h" #include "ncch.h"
#include "ncsd.h" #include "ncsd.h"
#define CART_INSERTED (!(REG_CARDCONF2 & 0x1)) #define CART_INSERTED (!(REG_CARDSTATUS & 0x1))
typedef struct { typedef struct {
NcsdHeader ncsd; NcsdHeader ncsd;
@ -72,8 +72,8 @@ u32 InitCardRead(CartData* cdata) {
Cart_Secure_Init((u32*) (void*) ncch_header, sec_keys); Cart_Secure_Init((u32*) (void*) ncch_header, sec_keys);
// NCSD header and CINFO // NCSD header and CINFO
Cart_Dummy(); // Cart_Dummy();
Cart_Dummy(); // Cart_Dummy();
CTR_CmdReadData(0, 0x200, 8, cdata->header); CTR_CmdReadData(0, 0x200, 8, cdata->header);
// safety checks, cart size // safety checks, cart size
@ -144,8 +144,8 @@ u32 ReadCartSectors(void* buffer, u32 sector, u32 count, CartData* cdata) {
const u32 max_read = 0x800; const u32 max_read = 0x800;
u8* buff = buffer8; u8* buff = buffer8;
for (u32 i = 0; i < count; i += max_read) { for (u32 i = 0; i < count; i += max_read) {
Cart_Dummy(); // Cart_Dummy();
Cart_Dummy(); // Cart_Dummy();
CTR_CmdReadData(sector + i, 0x200, min(max_read, count - i), buff); CTR_CmdReadData(sector + i, 0x200, min(max_read, count - i), buff);
buff += max_read * 0x200; buff += max_read * 0x200;
} }

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "protocol.h" #include "protocol.h"
#include "timer.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -42,38 +43,42 @@ u32 BSWAP32(u32 val) {
((val & 0xFF) << 24); ((val & 0xFF) << 24);
} }
// TODO: Verify // updated function by profi200
static void ResetCartSlot(void) static void ResetCardSlot(void)
{ {
REG_CARDCONF2 = 0x0C; REG_CARDCYCLES0 = 0x1988;
REG_CARDCONF &= ~3; REG_CARDCYCLES1 = 0x264C;
// boot9 waits here. Unnecessary?
if (REG_CARDCONF2 == 0xC) { REG_CARDSTATUS = 3u<<2; // Request power off
while (REG_CARDCONF2 != 0); while(REG_CARDSTATUS != 0); // Aotomatically changes to 0 (off)
} wait_msec(1*2);
if (REG_CARDCONF2 != 0) REG_CARDSTATUS = 1u<<2; // Prepare power on
return; wait_msec(10*2);
REG_CARDCONF2 = 0x4; REG_CARDSTATUS = 2u<<2; // Power on
while(REG_CARDCONF2 != 0x4); wait_msec(27*2);
REG_CARDCONF2 = 0x8; // Switch to NTRCARD controller.
while(REG_CARDCONF2 != 0x8); REG_CARDCTL = 0; // Select NTRCARD controller, eject IRQ off?
REG_NTRCARDMCNT = NTRCARD_CR1_ENABLE | NTRCARD_CR1_IRQ;
REG_NTRCARDROMCNT = 0x20000000;
wait_msec(120*2);
} }
static void SwitchToNTRCARD(void) static void SwitchToNTRCARD(void)
{ {
REG_NTRCARDROMCNT = 0x20000000; REG_NTRCARDROMCNT = 0x20000000;
REG_CARDCONF &= ~3; REG_CARDCTL &= ~3;
REG_CARDCONF &= ~0x100; REG_CARDCTL &= ~0x100;
REG_NTRCARDMCNT = NTRCARD_CR1_ENABLE; REG_NTRCARDMCNT = NTRCARD_CR1_ENABLE;
} }
static void SwitchToCTRCARD(void) static void SwitchToCTRCARD(void)
{ {
REG_CTRCARDCNT = 0x10000000; REG_CTRCARDCNT = 0x10000000;
REG_CARDCONF = (REG_CARDCONF & ~3) | 2; REG_CARDCTL = (REG_CARDCTL & ~3) | 2;
} }
int Cart_IsInserted(void) int Cart_IsInserted(void)
@ -86,38 +91,12 @@ u32 Cart_GetID(void)
return CartID; return CartID;
} }
void Cart_Reset(void)
{
ResetCartSlot(); //Seems to reset the cart slot?
REG_CTRCARDSECCNT &= 0xFFFFFFFB;
ioDelay2(0x40000);
SwitchToNTRCARD();
ioDelay2(0x40000);
}
void Cart_Init(void) void Cart_Init(void)
{ {
ResetCartSlot(); //Seems to reset the cart slot? ResetCardSlot(); //Seems to reset the cart slot?
REG_CTRCARDSECCNT &= 0xFFFFFFFB;
ioDelay(0x40000);
SwitchToNTRCARD();
ioDelay(0x40000);
REG_NTRCARDROMCNT = 0;
REG_NTRCARDMCNT &= 0xFF;
ioDelay(0x40000);
REG_NTRCARDMCNT |= (NTRCARD_CR1_ENABLE | NTRCARD_CR1_IRQ);
REG_NTRCARDROMCNT = NTRCARD_nRESET | NTRCARD_SEC_SEED;
while (REG_NTRCARDROMCNT & NTRCARD_BUSY);
// Reset // Reset
NTR_CmdReset(); NTR_CmdReset();
ioDelay(0x40000);
CartID = NTR_CmdGetCartId(); CartID = NTR_CmdGetCartId();
// 3ds // 3ds
@ -127,7 +106,6 @@ void Cart_Init(void)
NTR_CmdEnter16ByteMode(); NTR_CmdEnter16ByteMode();
SwitchToCTRCARD(); SwitchToCTRCARD();
ioDelay(0xF000);
REG_CTRCARDBLKCNT = 0; REG_CTRCARDBLKCNT = 0;
} }

View File

@ -5,8 +5,11 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#define REG_CARDCONF (*(vu16*)0x1000000C) #define REG_CARDCTL (*(vu16*)0x1000000C)
#define REG_CARDCONF2 (*(vu8*)0x10000010) #define REG_CARDSTATUS (*(vu8* )0x10000010)
#define REG_CARDCYCLES0 (*(vu16*)0x10000012)
#define REG_CARDCYCLES1 (*(vu16*)0x10000014)
#define LATENCY 0x822Cu #define LATENCY 0x822Cu
@ -17,4 +20,3 @@ int Cart_IsInserted(void);
u32 Cart_GetID(void); u32 Cart_GetID(void);
void Cart_Secure_Init(u32* buf, u32* out); void Cart_Secure_Init(u32* buf, u32* out);
void Cart_Dummy(void); void Cart_Dummy(void);
void Cart_Reset(void);