mirror of
https://github.com/melonDS-emu/melonDS.git
synced 2025-07-26 07:39:56 -06:00
get this started: refactor SPI in OOP
This commit is contained in:
159
src/SPI.h
159
src/SPI.h
@ -28,61 +28,150 @@
|
||||
#include "Savestate.h"
|
||||
#include "SPI_Firmware.h"
|
||||
|
||||
namespace SPI_Firmware
|
||||
enum
|
||||
{
|
||||
SPIDevice_PowerMan = 0,
|
||||
SPIDevice_FirmwareMem,
|
||||
SPIDevice_TSC,
|
||||
|
||||
SPIDevice_MAX
|
||||
};
|
||||
|
||||
u16 CRC16(const u8* data, u32 len, u32 start);
|
||||
void SetupDirectBoot(bool dsi);
|
||||
|
||||
u32 FixFirmwareLength(u32 originalLength);
|
||||
class SPIHost;
|
||||
|
||||
/// @return A pointer to the installed firmware blob if one exists, otherwise \c nullptr.
|
||||
/// @warning The pointer refers to memory that melonDS owns. Do not deallocate it yourself.
|
||||
/// @see InstallFirmware
|
||||
const Firmware* GetFirmware();
|
||||
|
||||
bool IsLoadedFirmwareBuiltIn();
|
||||
bool InstallFirmware(Firmware&& firmware);
|
||||
bool InstallFirmware(std::unique_ptr<Firmware>&& firmware);
|
||||
void RemoveFirmware();
|
||||
}
|
||||
|
||||
namespace SPI_Powerman
|
||||
class SPIDevice
|
||||
{
|
||||
public:
|
||||
SPIDevice(SPIHost* host) : Host(host), Hold(false), DataPos(0) {}
|
||||
virtual ~SPIDevice() {}
|
||||
|
||||
bool GetBatteryLevelOkay();
|
||||
void SetBatteryLevelOkay(bool okay);
|
||||
virtual void Reset() = 0;
|
||||
|
||||
}
|
||||
virtual void DoSavestate(Savestate* file) = 0;
|
||||
|
||||
namespace SPI_TSC
|
||||
virtual u8 Read() { return Data; }
|
||||
virtual void Write(u8 val) = 0;
|
||||
virtual void Release() { Hold = false; DataPos = 0; }
|
||||
|
||||
protected:
|
||||
SPIHost* Host;
|
||||
|
||||
bool Hold;
|
||||
u32 DataPos;
|
||||
u8 Data;
|
||||
};
|
||||
|
||||
class FirmwareMem : public SPIDevice
|
||||
{
|
||||
public:
|
||||
FirmwareMem(SPIHost* host);
|
||||
~FirmwareMem() override;
|
||||
|
||||
void SetTouchCoords(u16 x, u16 y);
|
||||
void MicInputFrame(s16* data, int samples);
|
||||
void Reset() override;
|
||||
|
||||
u8 Read();
|
||||
void Write(u8 val, u32 hold);
|
||||
void DoSavestate(Savestate* file) override;
|
||||
|
||||
}
|
||||
void SetupDirectBoot(bool dsi);
|
||||
|
||||
namespace SPI
|
||||
const class Firmware* GetFirmware();
|
||||
bool IsLoadedFirmwareBuiltIn();
|
||||
bool InstallFirmware(class Firmware&& firmware);
|
||||
bool InstallFirmware(std::unique_ptr<class Firmware>&& firmware);
|
||||
void RemoveFirmware();
|
||||
|
||||
void Write(u8 val) override;
|
||||
void Release() override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<Firmware> Firmware;
|
||||
|
||||
u8 CurCmd;
|
||||
|
||||
u8 StatusReg;
|
||||
u32 Addr;
|
||||
|
||||
bool VerifyCRC16(u32 start, u32 offset, u32 len, u32 crcoffset);
|
||||
};
|
||||
|
||||
class PowerMan : public SPIDevice
|
||||
{
|
||||
public:
|
||||
PowerMan(SPIHost* host);
|
||||
~PowerMan() override;
|
||||
|
||||
extern u16 Cnt;
|
||||
void Reset() override;
|
||||
|
||||
bool Init();
|
||||
void DeInit();
|
||||
void Reset();
|
||||
void DoSavestate(Savestate* file);
|
||||
void DoSavestate(Savestate* file) override;
|
||||
|
||||
void WriteCnt(u16 val);
|
||||
bool GetBatteryLevelOkay();
|
||||
void SetBatteryLevelOkay(bool okay);
|
||||
|
||||
u8 ReadData();
|
||||
void WriteData(u8 val);
|
||||
void Write(u8 val) override;
|
||||
|
||||
void TransferDone(u32 param);
|
||||
private:
|
||||
u8 Index;
|
||||
|
||||
}
|
||||
u8 Registers[8];
|
||||
u8 RegMasks[8];
|
||||
};
|
||||
|
||||
class TSC : public SPIDevice
|
||||
{
|
||||
public:
|
||||
TSC(SPIHost* host);
|
||||
virtual ~TSC() override;
|
||||
|
||||
virtual void Reset() override;
|
||||
|
||||
virtual void DoSavestate(Savestate* file) override;
|
||||
|
||||
virtual void SetTouchCoords(u16 x, u16 y);
|
||||
virtual void MicInputFrame(s16* data, int samples);
|
||||
|
||||
virtual void Write(u8 val) override;
|
||||
|
||||
protected:
|
||||
u8 ControlByte;
|
||||
|
||||
u16 ConvResult;
|
||||
|
||||
u16 TouchX, TouchY;
|
||||
|
||||
s16 MicBuffer[1024];
|
||||
int MicBufferLen;
|
||||
};
|
||||
|
||||
|
||||
class SPIHost
|
||||
{
|
||||
public:
|
||||
SPIHost();
|
||||
~SPIHost();
|
||||
|
||||
void Reset();
|
||||
|
||||
void DoSavestate(Savestate* file);
|
||||
|
||||
FirmwareMem* GetFirmwareMem() { return (FirmwareMem*)Devices[SPIDevice_FirmwareMem]; }
|
||||
PowerMan* GetPowerMan() { return (PowerMan*)Devices[SPIDevice_PowerMan]; }
|
||||
TSC* GetTSC() { return (TSC*)Devices[SPIDevice_TSC]; }
|
||||
|
||||
const Firmware* GetFirmware() { return GetFirmwareMem()->GetFirmware(); }
|
||||
|
||||
u16 ReadCnt() { return Cnt; }
|
||||
void WriteCnt(u16 val);
|
||||
|
||||
u8 ReadData();
|
||||
void WriteData(u8 val);
|
||||
|
||||
void TransferDone(u32 param);
|
||||
|
||||
private:
|
||||
u16 Cnt;
|
||||
|
||||
SPIDevice* Devices[3];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user