2017-01-22 12:34:59 -07:00
|
|
|
|
/*
|
2022-01-08 18:15:50 -07:00
|
|
|
|
Copyright 2016-2022 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>
|
|
|
|
|
|
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"
|
2017-01-22 12:34:59 -07:00
|
|
|
|
|
|
|
|
|
namespace NDSCart
|
|
|
|
|
{
|
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
// CartCommon -- base code shared by all cart types
|
|
|
|
|
class CartCommon
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CartCommon(u8* rom, u32 len, u32 chipid);
|
|
|
|
|
virtual ~CartCommon();
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual u32 Type() { return 0x001; }
|
|
|
|
|
virtual u32 Checksum();
|
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
virtual void Reset();
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual void SetupDirectBoot(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
|
|
|
|
|
|
|
|
|
virtual int ROMCommandStart(u8* cmd, u8* data, u32 len);
|
|
|
|
|
virtual void ROMCommandFinish(u8* cmd, u8* data, u32 len);
|
|
|
|
|
|
|
|
|
|
virtual u8 SPIWrite(u8 val, u32 pos, bool last);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CartRetail -- regular retail cart (ROM, SPI SRAM)
|
|
|
|
|
class CartRetail : public CartCommon
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CartRetail(u8* rom, u32 len, u32 chipid);
|
|
|
|
|
virtual ~CartRetail() override;
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual u32 Type() override { return 0x101; }
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
virtual int ROMCommandStart(u8* cmd, u8* data, u32 len) override;
|
|
|
|
|
|
|
|
|
|
virtual u8 SPIWrite(u8 val, u32 pos, bool last) override;
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
CartRetailNAND(u8* rom, u32 len, u32 chipid);
|
|
|
|
|
~CartRetailNAND() override;
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual u32 Type() override { return 0x102; }
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
int ROMCommandStart(u8* cmd, u8* data, u32 len) override;
|
|
|
|
|
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:
|
|
|
|
|
CartRetailIR(u8* rom, u32 len, u32 chipid, u32 irversion);
|
|
|
|
|
~CartRetailIR() override;
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual u32 Type() override { return 0x103; }
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// CartRetailBT - Pok<6F>mon Typing Adventure (SPI BT controller)
|
|
|
|
|
class CartRetailBT : public CartRetail
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CartRetailBT(u8* rom, u32 len, u32 chipid);
|
|
|
|
|
~CartRetailBT() override;
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual u32 Type() override { return 0x104; }
|
|
|
|
|
|
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:
|
|
|
|
|
CartHomebrew(u8* rom, u32 len, u32 chipid);
|
|
|
|
|
~CartHomebrew() override;
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
virtual u32 Type() override { return 0x201; }
|
|
|
|
|
|
2021-04-24 16:48:02 -06:00
|
|
|
|
void Reset() override;
|
2022-01-07 06:00:43 -07:00
|
|
|
|
void SetupDirectBoot(std::string romname) override;
|
2021-04-24 16:48:02 -06:00
|
|
|
|
|
|
|
|
|
void DoSavestate(Savestate* file) override;
|
|
|
|
|
|
|
|
|
|
int ROMCommandStart(u8* cmd, u8* data, u32 len) override;
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
2017-01-22 12:34:59 -07:00
|
|
|
|
extern u16 SPICnt;
|
|
|
|
|
extern u32 ROMCnt;
|
|
|
|
|
|
|
|
|
|
extern u8 ROMCommand[8];
|
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
extern bool CartInserted;
|
2017-02-01 13:35:00 -07:00
|
|
|
|
extern u8* CartROM;
|
|
|
|
|
extern u32 CartROMSize;
|
|
|
|
|
|
2018-04-27 11:26:46 -06:00
|
|
|
|
extern u32 CartID;
|
|
|
|
|
|
2021-07-02 10:42:54 -06:00
|
|
|
|
extern NDSHeader Header;
|
|
|
|
|
extern NDSBanner Banner;
|
|
|
|
|
|
2017-02-07 14:23:46 -07:00
|
|
|
|
bool Init();
|
|
|
|
|
void DeInit();
|
2017-01-22 12:34:59 -07:00
|
|
|
|
void Reset();
|
|
|
|
|
|
2018-10-17 16:54:21 -06:00
|
|
|
|
void DoSavestate(Savestate* file);
|
|
|
|
|
|
2020-03-30 03:04:50 -06:00
|
|
|
|
void DecryptSecureArea(u8* out);
|
2020-11-11 05:38:05 -07:00
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
bool LoadROM(const u8* romdata, u32 romlen);
|
|
|
|
|
void LoadSave(const u8* savedata, u32 savelen);
|
|
|
|
|
void SetupDirectBoot(std::string romname);
|
2017-01-22 12:34:59 -07:00
|
|
|
|
|
2022-01-07 06:00:43 -07:00
|
|
|
|
void EjectCart();
|
2020-09-10 19:08:06 -06:00
|
|
|
|
|
2019-08-04 08:46:02 -06:00
|
|
|
|
void ResetCart();
|
|
|
|
|
|
2017-01-31 09:34:17 -07:00
|
|
|
|
void WriteROMCnt(u32 val);
|
|
|
|
|
u32 ReadROMData();
|
2020-09-03 12:28:07 -06:00
|
|
|
|
void WriteROMData(u32 val);
|
2017-01-22 12:34:59 -07:00
|
|
|
|
|
2017-01-31 09:34:17 -07:00
|
|
|
|
void WriteSPICnt(u16 val);
|
|
|
|
|
u8 ReadSPIData();
|
|
|
|
|
void WriteSPIData(u8 val);
|
|
|
|
|
|
2018-10-17 17:38:33 -06:00
|
|
|
|
void ROMPrepareData(u32 param);
|
|
|
|
|
void ROMEndTransfer(u32 param);
|
|
|
|
|
void SPITransferDone(u32 param);
|
|
|
|
|
|
2017-01-22 12:34:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|