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
This commit is contained in:
Jesse Talavera-Greenberg
2023-06-30 07:28:52 -04:00
committed by GitHub
parent 7b948e6ec9
commit b659bce3c1
15 changed files with 7321 additions and 7127 deletions

View File

@ -20,24 +20,36 @@
#define NDSCART_H
#include <string>
#include <memory>
#include "types.h"
#include "Savestate.h"
#include "NDS_Header.h"
#include "FATStorage.h"
#include "ROMList.h"
namespace NDSCart
{
enum CartType
{
Default = 0x001,
Retail = 0x101,
RetailNAND = 0x102,
RetailIR = 0x103,
RetailBT = 0x104,
Homebrew = 0x201,
};
// CartCommon -- base code shared by all cart types
class CartCommon
{
public:
CartCommon(u8* rom, u32 len, u32 chipid, bool badDSiDump);
CartCommon(u8* rom, u32 len, u32 chipid, bool badDSiDump, ROMListEntry romparams);
virtual ~CartCommon();
virtual u32 Type() { return 0x001; }
virtual u32 Checksum();
virtual u32 Type() const = 0;
[[nodiscard]] u32 Checksum() const;
virtual void Reset();
virtual void SetupDirectBoot(const std::string& romname);
@ -55,6 +67,15 @@ public:
virtual u8* GetSaveMemory() const;
virtual u32 GetSaveMemoryLength() const;
[[nodiscard]] const NDSHeader& GetHeader() const { return Header; }
[[nodiscard]] NDSHeader& GetHeader() { return Header; }
/// @return The cartridge's banner if available, or \c nullptr if not.
[[nodiscard]] const NDSBanner* Banner() const;
[[nodiscard]] const ROMListEntry& GetROMParams() const { return ROMParams; };
[[nodiscard]] u32 ID() const { return ChipID; }
[[nodiscard]] const u8* GetROM() const { return ROM; }
[[nodiscard]] u32 GetROMLength() const { return ROMLength; }
protected:
void ReadROM(u32 addr, u32 len, u8* data, u32 offset);
@ -69,16 +90,20 @@ protected:
u32 CmdEncMode;
u32 DataEncMode;
// Kept separate from the ROM data so we can decrypt the modcrypt area
// without touching the overall ROM data
NDSHeader Header;
ROMListEntry ROMParams;
};
// CartRetail -- regular retail cart (ROM, SPI SRAM)
class CartRetail : public CartCommon
{
public:
CartRetail(u8* rom, u32 len, u32 chipid, bool badDSiDump);
CartRetail(u8* rom, u32 len, u32 chipid, bool badDSiDump, ROMListEntry romparams);
virtual ~CartRetail() override;
virtual u32 Type() override { return 0x101; }
virtual u32 Type() const override { return CartType::Retail; }
virtual void Reset() override;
@ -115,10 +140,10 @@ protected:
class CartRetailNAND : public CartRetail
{
public:
CartRetailNAND(u8* rom, u32 len, u32 chipid);
CartRetailNAND(u8* rom, u32 len, u32 chipid, ROMListEntry romparams);
~CartRetailNAND() override;
virtual u32 Type() override { return 0x102; }
virtual u32 Type() const override { return CartType::RetailNAND; }
void Reset() override;
@ -145,10 +170,10 @@ private:
class CartRetailIR : public CartRetail
{
public:
CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion, bool badDSiDump);
CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion, bool badDSiDump, ROMListEntry romparams);
~CartRetailIR() override;
virtual u32 Type() override { return 0x103; }
virtual u32 Type() const override { return CartType::RetailIR; }
void Reset() override;
@ -165,10 +190,10 @@ private:
class CartRetailBT : public CartRetail
{
public:
CartRetailBT(u8* rom, u32 len, u32 chipid);
CartRetailBT(u8* rom, u32 len, u32 chipid, ROMListEntry romparams);
~CartRetailBT() override;
virtual u32 Type() override { return 0x104; }
virtual u32 Type() const override { return CartType::RetailBT; }
void Reset() override;
@ -181,10 +206,10 @@ public:
class CartHomebrew : public CartCommon
{
public:
CartHomebrew(u8* rom, u32 len, u32 chipid);
CartHomebrew(u8* rom, u32 len, u32 chipid, ROMListEntry romparams);
~CartHomebrew() override;
virtual u32 Type() override { return 0x201; }
virtual u32 Type() const override { return CartType::Homebrew; }
void Reset() override;
void SetupDirectBoot(const std::string& romname) override;
@ -208,14 +233,8 @@ extern u32 ROMCnt;
extern u8 ROMCommand[8];
extern bool CartInserted;
extern u8* CartROM;
extern u32 CartROMSize;
extern u32 CartID;
extern NDSHeader Header;
extern NDSBanner Banner;
/// The currently loaded NDS cart.
extern std::unique_ptr<CartCommon> Cart;
bool Init();
void DeInit();
@ -225,6 +244,41 @@ void DoSavestate(Savestate* file);
void DecryptSecureArea(u8* out);
/// Parses the given ROM data and constructs a \c NDSCart::CartCommon subclass
/// that can be inserted into the emulator or used to extract information about the cart beforehand.
/// @param romdata The ROM data to parse.
/// The returned cartridge will contain a copy of this data,
/// so the caller may deallocate \c romdata after this function returns.
/// @param romlen The length of the ROM data in bytes.
/// @returns A \c NDSCart::CartCommon object representing the parsed ROM,
/// or \c nullptr if the ROM data couldn't be parsed.
std::unique_ptr<CartCommon> ParseROM(const u8* romdata, u32 romlen);
/// Loads a Nintendo DS cart object into the emulator.
/// The emulator takes ownership of the cart object and its underlying resources
/// and re-encrypts the ROM's secure area if necessary.
/// If a cartridge is already inserted, it is first ejected
/// and its state is discarded.
/// If the provided cart is not valid,
/// then the currently-loaded ROM will not be ejected.
///
/// @param cart Movable reference to the cart.
/// @returns \c true if the cart was successfully loaded,
/// \c false otherwise.
/// @post If the cart was successfully loaded,
/// then \c cart will be \c nullptr
/// and \c Cart will contain the object that \c cart previously pointed to.
/// Otherwise, \c cart and \c Cart will be both be unchanged.
bool InsertROM(std::unique_ptr<CartCommon>&& cart);
/// Parses a ROM image and loads it into the emulator.
/// This function is equivalent to calling ::ParseROM() and ::InsertROM() in sequence.
/// @param romdata Pointer to the ROM image.
/// The cart emulator maintains its own copy of this data,
/// so the caller is free to discard this data after calling this function.
/// @param romlen The length of the ROM image, in bytes.
/// @returns \c true if the ROM image was successfully loaded,
/// \c false if not.
bool LoadROM(const u8* romdata, u32 romlen);
void LoadSave(const u8* savedata, u32 savelen);
void SetupDirectBoot(const std::string& romname);