aboutsummaryrefslogtreecommitdiff
path: root/src/NDS.cpp
diff options
context:
space:
mode:
authorJesse Talavera-Greenberg <jesse@jesse.tg>2023-06-30 07:28:52 -0400
committerGitHub <noreply@github.com>2023-06-30 13:28:52 +0200
commitb659bce3c16519e15de6bacb588278691ac20ac8 (patch)
tree89eb6027427bafe4133164671e6b23cf9015c202 /src/NDS.cpp
parent7b948e6ec9cb67be5682b3759fbb69c4c05aa8f1 (diff)
Split the cart parsing and loading steps (#1707)
* Split ROMList into a .cpp file - Its definition in ROMList.h was causing multiple-definition linker errors - Introduce ROMListSize, since you can't take a sizeof() of an extern declaration - Mark ROMList and ROMListSize as const * Update ReadROMParams to accommodate ROMList changes * Split parsing and loading of NDS ROMs - Introduce an NDSCartData class for parsing a ROM file - Introduce InsertROM for loading a NDS cart - Refactor LoadROM to use NDSCartData and InsertROM under the hood * Reset cart state and initialize save memory in the NDSCartData constructor - Otherwise there's no way to know about SRAM-specific attributes before inserting the game * Add a comment to NDSCartData * First crack at splitting parsing and loading for GBACart * Add some logging calls for encrypting the secure area * Log the XXH64 hash of the inserted NDS ROM * Log the XXH64 hash of the secure area after decryption * Add some logging to Key1_LoadKeyBuf * Re-encrypt the secure area when inserting the cart, not when parsing it - This way, constructing a NDSCart doesn't imply a read from the filesystem (as is done in Key1_KeyBuf) * Load Key1_KeyBuf from memory, not from the file system - Now that the cart's secure area isn't re-encrypted until insertion, we can expect that the BIOS will be ready at this point * Add some helper query methods to NDSHeader * Query the DSi region directly from the header instead of checking the ROM again * Introduce a CartType enum - So CartCommon::Type doesn't have to return magic numbers * Reset the cart in NDSCart::InsertROM instead of the NDSCartData constructor - That way the constructor doesn't rely on the config or on file I/O when loading homebrew - This keeps the use of global state closer to one place * Add non-const getters for the carts * Add InsertROM overloads that accept unique_ptrs * Fix a comment * Rename member functions on NDSCartData and GBACartData to adhere to the convention * Rename members on NDSCartData and GBACartData to adhere to the convention * Fix build errors on some platforms * Add NDSHeader::IsDSiWare * Add a ROMListEntry parameter to the cart constructors - To allow for looking up details of SRAM or expected ROM size * Add some new getters to CartCommon * Use the Header/Banner members instead of globals * Make CartCommon abstract - It's not supposed to be instantiated anyway * Change the signature of CartCommon::Checksum - It's neither overridden nor mutating * Add some clarifying comments to NDSHeader * Delete CartCommon::ROM in its destructor - ParseROM copies its input and gives that copy to the cart object, so it's okay * Add some getters to CartCommon * Refactor NDSCart - Get rid of NDSCartData - Get rid of cart-specific global state within NDSCart (so registers are untouched) - Refactor uses of removed global variables to use the Cart pointer instead - Refactor ROMInfoDialog's icon functions to accept const arguments * Return the cart pointer - So *that's* why it was crashing. Whoops - Why is this even allowed? * Refactor GBACart - Delete CartGame::ROM in the destructor - Get rid of GBACartData - Remove some global state * Mark NDSCart::CartCommon::Type as const * Slightly refactor GBACart::CartCommon - Mark Type as const - Use enum constants - Make CartCommon itself abstract * Mark CRC32's data parameter as const * Mark GBACart::CartCommon::Checksum as const * Use assert.h instead of cassert - As demanded by the style guide * Fix some includes to adhere to the style guide * Get the ARM9 entry address directly from the header object * Use more Header fields directly * Rename some parameters to match the style guide * Remove some unused includes * Slightly change NDS_Header::IsHomebrew for clarity
Diffstat (limited to 'src/NDS.cpp')
-rw-r--r--src/NDS.cpp57
1 files changed, 31 insertions, 26 deletions
diff --git a/src/NDS.cpp b/src/NDS.cpp
index 7bf94f8..5caaaee 100644
--- a/src/NDS.cpp
+++ b/src/NDS.cpp
@@ -379,33 +379,37 @@ bool NeedsDirectBoot()
void SetupDirectBoot(const std::string& romname)
{
+ const NDSHeader& header = NDSCart::Cart->GetHeader();
+
if (ConsoleType == 1)
{
DSi::SetupDirectBoot();
}
else
{
+ u32 cartid = NDSCart::Cart->ID();
+ const u8* cartrom = NDSCart::Cart->GetROM();
MapSharedWRAM(3);
// setup main RAM data
for (u32 i = 0; i < 0x170; i+=4)
{
- u32 tmp = *(u32*)&NDSCart::CartROM[i];
+ u32 tmp = *(u32*)&cartrom[i];
ARM9Write32(0x027FFE00+i, tmp);
}
- ARM9Write32(0x027FF800, NDSCart::CartID);
- ARM9Write32(0x027FF804, NDSCart::CartID);
- ARM9Write16(0x027FF808, NDSCart::Header.HeaderCRC16);
- ARM9Write16(0x027FF80A, NDSCart::Header.SecureAreaCRC16);
+ ARM9Write32(0x027FF800, cartid);
+ ARM9Write32(0x027FF804, cartid);
+ ARM9Write16(0x027FF808, header.HeaderCRC16);
+ ARM9Write16(0x027FF80A, header.SecureAreaCRC16);
ARM9Write16(0x027FF850, 0x5835);
- ARM9Write32(0x027FFC00, NDSCart::CartID);
- ARM9Write32(0x027FFC04, NDSCart::CartID);
- ARM9Write16(0x027FFC08, NDSCart::Header.HeaderCRC16);
- ARM9Write16(0x027FFC0A, NDSCart::Header.SecureAreaCRC16);
+ ARM9Write32(0x027FFC00, cartid);
+ ARM9Write32(0x027FFC04, cartid);
+ ARM9Write16(0x027FFC08, header.HeaderCRC16);
+ ARM9Write16(0x027FFC0A, header.SecureAreaCRC16);
ARM9Write16(0x027FFC10, 0x5835);
ARM9Write16(0x027FFC30, 0xFFFF);
@@ -414,30 +418,30 @@ void SetupDirectBoot(const std::string& romname)
u32 arm9start = 0;
// load the ARM9 secure area
- if (NDSCart::Header.ARM9ROMOffset >= 0x4000 && NDSCart::Header.ARM9ROMOffset < 0x8000)
+ if (header.ARM9ROMOffset >= 0x4000 && header.ARM9ROMOffset < 0x8000)
{
u8 securearea[0x800];
NDSCart::DecryptSecureArea(securearea);
for (u32 i = 0; i < 0x800; i+=4)
{
- ARM9Write32(NDSCart::Header.ARM9RAMAddress+i, *(u32*)&securearea[i]);
+ ARM9Write32(header.ARM9RAMAddress+i, *(u32*)&securearea[i]);
arm9start += 4;
}
}
// CHECKME: firmware seems to load this in 0x200 byte chunks
- for (u32 i = arm9start; i < NDSCart::Header.ARM9Size; i+=4)
+ for (u32 i = arm9start; i < header.ARM9Size; i+=4)
{
- u32 tmp = *(u32*)&NDSCart::CartROM[NDSCart::Header.ARM9ROMOffset+i];
- ARM9Write32(NDSCart::Header.ARM9RAMAddress+i, tmp);
+ u32 tmp = *(u32*)&cartrom[header.ARM9ROMOffset+i];
+ ARM9Write32(header.ARM9RAMAddress+i, tmp);
}
- for (u32 i = 0; i < NDSCart::Header.ARM7Size; i+=4)
+ for (u32 i = 0; i < header.ARM7Size; i+=4)
{
- u32 tmp = *(u32*)&NDSCart::CartROM[NDSCart::Header.ARM7ROMOffset+i];
- ARM7Write32(NDSCart::Header.ARM7RAMAddress+i, tmp);
+ u32 tmp = *(u32*)&cartrom[header.ARM7ROMOffset+i];
+ ARM7Write32(header.ARM7RAMAddress+i, tmp);
}
ARM7BIOSProt = 0x1204;
@@ -472,20 +476,20 @@ void SetupDirectBoot(const std::string& romname)
NDSCart::SetupDirectBoot(romname);
- ARM9->R[12] = NDSCart::Header.ARM9EntryAddress;
+ ARM9->R[12] = header.ARM9EntryAddress;
ARM9->R[13] = 0x03002F7C;
- ARM9->R[14] = NDSCart::Header.ARM9EntryAddress;
+ ARM9->R[14] = header.ARM9EntryAddress;
ARM9->R_IRQ[0] = 0x03003F80;
ARM9->R_SVC[0] = 0x03003FC0;
- ARM7->R[12] = NDSCart::Header.ARM7EntryAddress;
+ ARM7->R[12] = header.ARM7EntryAddress;
ARM7->R[13] = 0x0380FD80;
- ARM7->R[14] = NDSCart::Header.ARM7EntryAddress;
+ ARM7->R[14] = header.ARM7EntryAddress;
ARM7->R_IRQ[0] = 0x0380FF80;
ARM7->R_SVC[0] = 0x0380FFC0;
- ARM9->JumpTo(NDSCart::Header.ARM9EntryAddress);
- ARM7->JumpTo(NDSCart::Header.ARM7EntryAddress);
+ ARM9->JumpTo(header.ARM9EntryAddress);
+ ARM7->JumpTo(header.ARM7EntryAddress);
PostFlag9 = 0x01;
PostFlag7 = 0x01;
@@ -978,7 +982,7 @@ void EjectCart()
bool CartInserted()
{
- return NDSCart::CartInserted;
+ return NDSCart::Cart != nullptr;
}
bool LoadGBACart(const u8* romdata, u32 romlen, const u8* savedata, u32 savelen)
@@ -1666,9 +1670,10 @@ void MonitorARM9Jump(u32 addr)
// checkme: can the entrypoint addr be THUMB?
// also TODO: make it work in DSi mode
- if ((!RunningGame) && NDSCart::CartROM)
+ if ((!RunningGame) && NDSCart::Cart)
{
- if (addr == *(u32*)&NDSCart::CartROM[0x24])
+ const NDSHeader& header = NDSCart::Cart->GetHeader();
+ if (addr == header.ARM9EntryAddress)
{
Log(LogLevel::Info, "Game is now booting\n");
RunningGame = true;