melonDS/src/NDSCart.h

233 lines
5.4 KiB
C
Raw Normal View History

2017-01-22 12:34:59 -07:00
/*
Copyright 2016-2021 Arisotura
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
#include "types.h"
#include "NDS_Header.h"
2017-01-22 12:34:59 -07:00
namespace NDSCart
{
// CartCommon -- base code shared by all cart types
class CartCommon
{
public:
CartCommon(u8* rom, u32 len, u32 chipid);
virtual ~CartCommon();
virtual void Reset();
virtual void SetupDirectBoot();
virtual void DoSavestate(Savestate* file);
virtual void LoadSave(const char* path, u32 type);
virtual void RelocateSave(const char* path, bool write);
virtual int ImportSRAM(const u8* data, u32 length);
virtual void FlushSRAMFile();
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;
bool DSiMode;
u32 DSiBase;
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;
virtual void Reset() override;
virtual void DoSavestate(Savestate* file) override;
virtual void LoadSave(const char* path, u32 type) override;
virtual void RelocateSave(const char* path, bool write) override;
virtual int ImportSRAM(const u8* data, u32 length) override;
virtual void FlushSRAMFile() override;
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;
char SRAMPath[1024];
bool SRAMFileDirty;
u8 SRAMCmd;
u32 SRAMAddr;
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;
void Reset() override;
2021-05-03 06:36:21 -06:00
void DoSavestate(Savestate* file) override;
void LoadSave(const char* path, u32 type) override;
int ImportSRAM(const u8* data, u32 length) override;
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;
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;
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;
void Reset() override;
void DoSavestate(Savestate* file) override;
int ROMCommandStart(u8* cmd, u8* data, u32 len) override;
void ROMCommandFinish(u8* cmd, u8* data, u32 len) override;
private:
void ApplyDLDIPatch(const u8* patch, u32 len);
void ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset);
FILE* SDFile;
};
2017-01-22 12:34:59 -07:00
extern u16 SPICnt;
extern u32 ROMCnt;
extern u8 ROMCommand[8];
extern u8* CartROM;
extern u32 CartROMSize;
extern u32 CartID;
extern NDSHeader Header;
extern NDSBanner Banner;
bool Init();
void DeInit();
2017-01-22 12:34:59 -07:00
void Reset();
void DoSavestate(Savestate* file);
void DecryptSecureArea(u8* out);
bool LoadROM(const char* path, const char* sram, bool direct);
Polish up archive support (#930) * Fix directory path when extracting from archive * Don't create new dir in execution dir of melonds * Create it beside the archive instead Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * ArchiveUtil : Use QT functions for I/O * Make it more platform independent, cleaner * Fixes permission related crash on linux Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * NDSCart : Abstract out common code in LoadROM() Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Extract nds roms to memory * Some stuff is still broken in the frontend Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * GBACart : Abstract out common code in LoadROM() Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Extract gba roms to memory Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Integrate archive support with recent files Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * onClickRecentFile : Pause emu thread conditionally * Don't pause at start of the function * If user opens an archive and hits cancel, it won't pause Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Handle Resets when loading from archives * Ask user to pick the rom(s) again (i.e. GBA & NDS) when there are multiple files in the archive(s) * Directly load if only 1 file Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Archive support for drag-n-drop * Also recent files support for drag-n-drop Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * main : Allocate rombuffer objects on stack * Less messy, decreases chances of memory leaks * Underlying implementation of qbytearray uses heap (hopefully?) Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * GetSavestateName : Archive support * Construct ssname from srampath (since rompath has archive name) NOTE: In general, archive name != rom file name !!!!!!!!!! Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Add srl and dsi as "direct-load" formats * Direct-load = anything not in an archive Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Don't use static functions Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Remove QT stuff from Util_ROM * Also, during reset, directly load file from archive (no rom picker) Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Remove QT includes from FrontendUtil.h Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Util_ROM/LoadROM() : Use SetupDSiNAND() Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Util_ROM/Reset() : Use strrchr() Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Util_ROM : Put Archive stuff behind ifdefs Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * main: Set parent widget for archive dialog boxes Signed-off-by: Madhav Kanbur <abcdjdj@gmail.com> * Revert "Util_ROM/Reset() : Use strrchr()" This reverts commit c8af6f066f6aa15e5557e478417edb125cad0809.
2021-01-22 03:22:32 -07:00
bool LoadROM(const u8* romdata, u32 filelength, const char *sram, bool direct);
void FlushSRAMFile();
2018-10-23 16:24:36 -06:00
void RelocateSave(const char* path, bool write);
2017-01-22 12:34:59 -07:00
2020-09-10 19:08:06 -06:00
int ImportSRAM(const u8* data, u32 length);
void ResetCart();
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
void WriteSPICnt(u16 val);
u8 ReadSPIData();
void WriteSPIData(u8 val);
void ROMPrepareData(u32 param);
void ROMEndTransfer(u32 param);
void SPITransferDone(u32 param);
2017-01-22 12:34:59 -07:00
}
#endif