2017-01-22 12:34:59 -07:00
|
|
|
|
/*
|
2023-11-03 17:21:46 -06:00
|
|
|
|
Copyright 2016-2023 melonDS team
|
2017-01-22 12:34:59 -07:00
|
|
|
|
|
|
|
|
|
This file is part of melonDS.
|
|
|
|
|
|
|
|
|
|
melonDS is free software: you can redistribute it and/or modify it under
|
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
|
|
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
|
with melonDS. If not, see http://www.gnu.org/licenses/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef NDSCART_H
|
|
|
|
|
#define NDSCART_H
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
#include <string>
|
2023-06-30 05:28:52 -06:00
|
|
|
|
#include <memory>
|
2022-01-07 06:00:43 -07:00
|
|
|
|
|
2017-01-22 12:34:59 -07:00
|
|
|
|
#include "types.h"
|
2022-07-27 10:20:31 -06:00
|
|
|
|
#include "Savestate.h"
|
2021-07-02 10:42:54 -06:00
|
|
|
|
#include "NDS_Header.h"
|
2021-10-28 10:47:13 -06:00
|
|
|
|
#include "FATStorage.h"
|
2023-06-30 05:28:52 -06:00
|
|
|
|
#include "ROMList.h"
|
2017-01-22 12:34:59 -07:00
|
|
|
|
|
|
|
|
|
namespace NDSCart
|
|
|
|
|
{
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
enum CartType
|
|
|
|
|
{
|
|
|
|
|
Default = 0x001,
|
|
|
|
|
Retail = 0x101,
|
|
|
|
|
RetailNAND = 0x102,
|
|
|
|
|
RetailIR = 0x103,
|
|
|
|
|
RetailBT = 0x104,
|
|
|
|
|
Homebrew = 0x201,
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-09 10:57:16 -07:00
|
|
|
|
class NDSCartSlot;
|
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
// CartCommon -- base code shared by all cart types
|
|
|
|
|
class CartCommon
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-06-30 05:28:52 -06:00
|
|
|
|
CartCommon(u8* rom, u32 len, u32 chipid, bool badDSiDump, ROMListEntry romparams);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
virtual ~CartCommon();
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
virtual u32 Type() const = 0;
|
|
|
|
|
[[nodiscard]] u32 Checksum() const;
|
2022-01-07 06:00:43 -07:00
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
virtual void Reset();
|
2023-04-28 09:05:34 -06:00
|
|
|
|
virtual void SetupDirectBoot(const std::string& romname);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
|
|
|
|
virtual void DoSavestate(Savestate* file);
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual void SetupSave(u32 type);
|
|
|
|
|
virtual void LoadSave(const u8* savedata, u32 savelen);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
2023-11-09 10:57:16 -07:00
|
|
|
|
virtual int ROMCommandStart(NDSCart::NDSCartSlot& cartslot, u8* cmd, u8* data, u32 len);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
virtual void ROMCommandFinish(u8* cmd, u8* data, u32 len);
|
|
|
|
|
|
|
|
|
|
virtual u8 SPIWrite(u8 val, u32 pos, bool last);
|
|
|
|
|
|
2023-03-27 14:36:26 -06:00
|
|
|
|
virtual u8* GetSaveMemory() const;
|
|
|
|
|
virtual u32 GetSaveMemoryLength() const;
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
[[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; }
|
2021-04-24 16:48:02 -06:00
|
|
|
|
protected:
|
|
|
|
|
void ReadROM(u32 addr, u32 len, u8* data, u32 offset);
|
|
|
|
|
|
|
|
|
|
void SetIRQ();
|
|
|
|
|
|
|
|
|
|
u8* ROM;
|
|
|
|
|
u32 ROMLength;
|
|
|
|
|
u32 ChipID;
|
|
|
|
|
bool IsDSi;
|
2021-07-22 13:37:34 -06:00
|
|
|
|
bool DSiMode;
|
|
|
|
|
u32 DSiBase;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
|
|
|
|
u32 CmdEncMode;
|
|
|
|
|
u32 DataEncMode;
|
2023-06-30 05:28:52 -06:00
|
|
|
|
// Kept separate from the ROM data so we can decrypt the modcrypt area
|
|
|
|
|
// without touching the overall ROM data
|
|
|
|
|
NDSHeader Header;
|
|
|
|
|
ROMListEntry ROMParams;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CartRetail -- regular retail cart (ROM, SPI SRAM)
|
|
|
|
|
class CartRetail : public CartCommon
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-06-30 05:28:52 -06:00
|
|
|
|
CartRetail(u8* rom, u32 len, u32 chipid, bool badDSiDump, ROMListEntry romparams);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
virtual ~CartRetail() override;
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
virtual u32 Type() const override { return CartType::Retail; }
|
2022-01-07 06:00:43 -07:00
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
virtual void Reset() override;
|
|
|
|
|
|
|
|
|
|
virtual void DoSavestate(Savestate* file) override;
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual void SetupSave(u32 type) override;
|
|
|
|
|
virtual void LoadSave(const u8* savedata, u32 savelen) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
2023-11-09 10:57:16 -07:00
|
|
|
|
virtual int ROMCommandStart(NDSCart::NDSCartSlot& cartslot, u8* cmd, u8* data, u32 len) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
|
|
|
|
virtual u8 SPIWrite(u8 val, u32 pos, bool last) override;
|
|
|
|
|
|
2023-03-27 14:36:26 -06:00
|
|
|
|
virtual u8* GetSaveMemory() const override;
|
|
|
|
|
virtual u32 GetSaveMemoryLength() const override;
|
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
protected:
|
|
|
|
|
void ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset);
|
|
|
|
|
|
|
|
|
|
u8 SRAMWrite_EEPROMTiny(u8 val, u32 pos, bool last);
|
|
|
|
|
u8 SRAMWrite_EEPROM(u8 val, u32 pos, bool last);
|
|
|
|
|
u8 SRAMWrite_FLASH(u8 val, u32 pos, bool last);
|
|
|
|
|
|
|
|
|
|
u8* SRAM;
|
|
|
|
|
u32 SRAMLength;
|
|
|
|
|
u32 SRAMType;
|
|
|
|
|
|
|
|
|
|
u8 SRAMCmd;
|
|
|
|
|
u32 SRAMAddr;
|
2022-01-07 06:00:43 -07:00
|
|
|
|
u32 SRAMFirstAddr;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
u8 SRAMStatus;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CartRetailNAND -- retail cart with NAND SRAM (WarioWare DIY, Jam with the Band, ...)
|
|
|
|
|
class CartRetailNAND : public CartRetail
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-06-30 05:28:52 -06:00
|
|
|
|
CartRetailNAND(u8* rom, u32 len, u32 chipid, ROMListEntry romparams);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
~CartRetailNAND() override;
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
virtual u32 Type() const override { return CartType::RetailNAND; }
|
2022-01-07 06:00:43 -07:00
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void Reset() override;
|
|
|
|
|
|
2021-05-03 06:36:21 -06:00
|
|
|
|
void DoSavestate(Savestate* file) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
void LoadSave(const u8* savedata, u32 savelen) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
2023-11-09 10:57:16 -07:00
|
|
|
|
int ROMCommandStart(NDSCart::NDSCartSlot& cartslot, u8* cmd, u8* data, u32 len) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void ROMCommandFinish(u8* cmd, u8* data, u32 len) override;
|
|
|
|
|
|
|
|
|
|
u8 SPIWrite(u8 val, u32 pos, bool last) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void BuildSRAMID();
|
|
|
|
|
|
|
|
|
|
u32 SRAMBase;
|
|
|
|
|
u32 SRAMWindow;
|
|
|
|
|
|
|
|
|
|
u8 SRAMWriteBuffer[0x800];
|
|
|
|
|
u32 SRAMWritePos;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CartRetailIR -- SPI IR device and SRAM
|
|
|
|
|
class CartRetailIR : public CartRetail
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-06-30 05:28:52 -06:00
|
|
|
|
CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion, bool badDSiDump, ROMListEntry romparams);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
~CartRetailIR() override;
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
virtual u32 Type() const override { return CartType::RetailIR; }
|
2022-01-07 06:00:43 -07:00
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void Reset() override;
|
|
|
|
|
|
|
|
|
|
void DoSavestate(Savestate* file) override;
|
|
|
|
|
|
|
|
|
|
u8 SPIWrite(u8 val, u32 pos, bool last) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
u32 IRVersion;
|
|
|
|
|
u8 IRCmd;
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-28 09:05:34 -06:00
|
|
|
|
// CartRetailBT - Pok<6F>mon Typing Adventure (SPI BT controller)
|
2021-04-24 16:48:02 -06:00
|
|
|
|
class CartRetailBT : public CartRetail
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-06-30 05:28:52 -06:00
|
|
|
|
CartRetailBT(u8* rom, u32 len, u32 chipid, ROMListEntry romparams);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
~CartRetailBT() override;
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
virtual u32 Type() const override { return CartType::RetailBT; }
|
2022-01-07 06:00:43 -07:00
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void Reset() override;
|
|
|
|
|
|
|
|
|
|
void DoSavestate(Savestate* file) override;
|
|
|
|
|
|
|
|
|
|
u8 SPIWrite(u8 val, u32 pos, bool last) override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CartHomebrew -- homebrew 'cart' (no SRAM, DLDI)
|
|
|
|
|
class CartHomebrew : public CartCommon
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-06-30 05:28:52 -06:00
|
|
|
|
CartHomebrew(u8* rom, u32 len, u32 chipid, ROMListEntry romparams);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
~CartHomebrew() override;
|
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
virtual u32 Type() const override { return CartType::Homebrew; }
|
2022-01-07 06:00:43 -07:00
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void Reset() override;
|
2023-04-28 09:05:34 -06:00
|
|
|
|
void SetupDirectBoot(const std::string& romname) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
|
|
|
|
void DoSavestate(Savestate* file) override;
|
|
|
|
|
|
2023-11-09 10:57:16 -07:00
|
|
|
|
int ROMCommandStart(NDSCart::NDSCartSlot& cartslot, u8* cmd, u8* data, u32 len) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void ROMCommandFinish(u8* cmd, u8* data, u32 len) override;
|
|
|
|
|
|
|
|
|
|
private:
|
2022-03-12 13:52:29 -07:00
|
|
|
|
void ApplyDLDIPatchAt(u8* binary, u32 dldioffset, const u8* patch, u32 patchlen, bool readonly);
|
2021-10-28 10:47:13 -06:00
|
|
|
|
void ApplyDLDIPatch(const u8* patch, u32 patchlen, bool readonly);
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset);
|
|
|
|
|
|
2021-10-28 10:47:13 -06:00
|
|
|
|
FATStorage* SD;
|
|
|
|
|
bool ReadOnly;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
};
|
|
|
|
|
|
2023-11-09 10:57:16 -07:00
|
|
|
|
class NDSCartSlot
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
NDSCartSlot() noexcept;
|
|
|
|
|
~NDSCartSlot() noexcept;
|
|
|
|
|
void Reset() noexcept;
|
|
|
|
|
void ResetCart() noexcept;
|
|
|
|
|
void DoSavestate(Savestate* file) noexcept;
|
|
|
|
|
void DecryptSecureArea(u8* out) noexcept;
|
|
|
|
|
|
|
|
|
|
/// Loads a Nintendo DS cart object into the cart slot.
|
|
|
|
|
/// The cart slot 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) noexcept;
|
|
|
|
|
|
|
|
|
|
/// 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) noexcept;
|
|
|
|
|
void LoadSave(const u8* savedata, u32 savelen) noexcept;
|
|
|
|
|
void SetupDirectBoot(const std::string& romname) noexcept;
|
|
|
|
|
|
|
|
|
|
/// This function is intended to allow frontends to save and load SRAM
|
|
|
|
|
/// without using melonDS APIs.
|
|
|
|
|
/// Modifying the emulated SRAM for any other reason is strongly discouraged.
|
|
|
|
|
/// The returned pointer may be invalidated if the emulator is reset,
|
|
|
|
|
/// or when a new game is loaded.
|
|
|
|
|
/// Consequently, don't store the returned pointer for any longer than necessary.
|
|
|
|
|
/// @returns Pointer to this cart's SRAM if a cart is loaded and supports SRAM, otherwise \c nullptr.
|
|
|
|
|
[[nodiscard]] const u8* GetSaveMemory() const noexcept { return Cart ? Cart->GetSaveMemory() : nullptr; }
|
|
|
|
|
[[nodiscard]] u8* GetSaveMemory() noexcept { return Cart ? Cart->GetSaveMemory() : nullptr; }
|
|
|
|
|
|
|
|
|
|
/// @returns The length of the buffer returned by ::GetSaveMemory()
|
|
|
|
|
/// if a cart is loaded and supports SRAM, otherwise zero.
|
|
|
|
|
[[nodiscard]] u32 GetSaveMemoryLength() const noexcept { return Cart ? Cart->GetSaveMemoryLength() : 0; }
|
|
|
|
|
void EjectCart() noexcept;
|
|
|
|
|
u32 ReadROMData() noexcept;
|
|
|
|
|
void WriteROMData(u32 val) noexcept;
|
|
|
|
|
void WriteSPICnt(u16 val) noexcept;
|
|
|
|
|
void WriteROMCnt(u32 val) noexcept;
|
|
|
|
|
[[nodiscard]] u8 ReadSPIData() const noexcept;
|
|
|
|
|
void WriteSPIData(u8 val) noexcept;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] CartCommon* GetCart() noexcept { return Cart.get(); }
|
|
|
|
|
[[nodiscard]] const CartCommon* GetCart() const noexcept { return Cart.get(); }
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] u8 GetROMCommand(u8 index) const noexcept { return ROMCommand[index]; }
|
|
|
|
|
void SetROMCommand(u8 index, u8 val) noexcept { ROMCommand[index] = val; }
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] u32 GetROMCnt() const noexcept { return ROMCnt; }
|
|
|
|
|
[[nodiscard]] u16 GetSPICnt() const noexcept { return SPICnt; }
|
|
|
|
|
void SetSPICnt(u16 val) noexcept { SPICnt = val; }
|
|
|
|
|
private:
|
|
|
|
|
friend class CartCommon;
|
|
|
|
|
u16 SPICnt {};
|
|
|
|
|
u32 ROMCnt {};
|
|
|
|
|
std::array<u8, 8> ROMCommand {};
|
|
|
|
|
u8 SPIData;
|
|
|
|
|
u32 SPIDataPos;
|
|
|
|
|
bool SPIHold;
|
|
|
|
|
|
|
|
|
|
u32 ROMData;
|
|
|
|
|
|
|
|
|
|
std::array<u8, 0x4000> TransferData;
|
|
|
|
|
u32 TransferPos;
|
|
|
|
|
u32 TransferLen;
|
|
|
|
|
u32 TransferDir;
|
|
|
|
|
std::array<u8, 8> TransferCmd;
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CartCommon> Cart;
|
|
|
|
|
|
|
|
|
|
std::array<u32, 0x412> Key1_KeyBuf;
|
|
|
|
|
|
|
|
|
|
u64 Key2_X;
|
|
|
|
|
u64 Key2_Y;
|
|
|
|
|
|
|
|
|
|
void Key1_Encrypt(u32* data) noexcept;
|
|
|
|
|
void Key1_Decrypt(u32* data) noexcept;
|
|
|
|
|
void Key1_ApplyKeycode(u32* keycode, u32 mod) noexcept;
|
|
|
|
|
void Key1_LoadKeyBuf(bool dsi, bool externalBios, u8 *bios, u32 biosLength) noexcept;
|
|
|
|
|
void Key1_InitKeycode(bool dsi, u32 idcode, u32 level, u32 mod, u8 *bios, u32 biosLength) noexcept;
|
|
|
|
|
void Key2_Encrypt(u8* data, u32 len) noexcept;
|
|
|
|
|
void ROMEndTransfer(u32 param) noexcept;
|
|
|
|
|
void ROMPrepareData(u32 param) noexcept;
|
|
|
|
|
void AdvanceROMTransfer() noexcept;
|
|
|
|
|
void SPITransferDone(u32 param) noexcept;
|
|
|
|
|
};
|
2020-11-11 05:38:05 -07:00
|
|
|
|
|
2023-06-30 05:28:52 -06:00
|
|
|
|
/// 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);
|
2017-01-22 12:34:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|