Skip to content

Commit

Permalink
get this started: refactor SPI in OOP
Browse files Browse the repository at this point in the history
  • Loading branch information
Arisotura committed Nov 3, 2023
1 parent 70c6750 commit 440b356
Show file tree
Hide file tree
Showing 17 changed files with 795 additions and 786 deletions.
11 changes: 6 additions & 5 deletions src/DSi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ void SetupDirectBoot()
NDSHeader& header = NDSCart::Cart->GetHeader();
const u8* cartrom = NDSCart::Cart->GetROM();
u32 cartid = NDSCart::Cart->ID();
DSi_TSC* tsc = (DSi_TSC*)NDS::SPI->GetTSC();

// TODO: add controls for forcing DS or DSi mode?
if (!(header.UnitCode & 0x02))
Expand All @@ -429,7 +430,7 @@ void SetupDirectBoot()

NDS::MapSharedWRAM(3);

DSi_SPI_TSC::SetMode(0x00);
tsc->SetMode(0x00);
Set_SCFG_Clock9(0x0000);
}
else
Expand Down Expand Up @@ -481,7 +482,7 @@ void SetupDirectBoot()
NDS::MapSharedWRAM(mbk[11] >> 24);

if (!(header.AppFlags & (1<<0)))
DSi_SPI_TSC::SetMode(0x00);
tsc->SetMode(0x00);
}

// setup main RAM data
Expand Down Expand Up @@ -552,12 +553,12 @@ void SetupDirectBoot()
}
}

SPI_Firmware::WifiBoard nwifiver = SPI_Firmware::GetFirmware()->Header().WifiBoard;
Firmware::WifiBoard nwifiver = NDS::SPI->GetFirmware()->GetHeader().WifiBoard;
ARM9Write8(0x020005E0, static_cast<u8>(nwifiver));

// TODO: these should be taken from the wifi firmware in NAND
// but, hey, this works too.
if (nwifiver == SPI_Firmware::WifiBoard::W015)
if (nwifiver == Firmware::WifiBoard::W015)
{
ARM9Write16(0x020005E2, 0xB57E);
ARM9Write32(0x020005E4, 0x00500400);
Expand Down Expand Up @@ -642,7 +643,7 @@ void SetupDirectBoot()

NDS::ARM7BIOSProt = 0x20;

SPI_Firmware::SetupDirectBoot(true);
NDS::SPI->GetFirmwareMem()->SetupDirectBoot(true);

NDS::ARM9->CP15Write(0x100, 0x00056078);
NDS::ARM9->CP15Write(0x200, 0x0000004A);
Expand Down
2 changes: 1 addition & 1 deletion src/DSi_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ u8 GetBatteryLevel() { return Registers[0x20] & 0xF; }
void SetBatteryLevel(u8 batteryLevel)
{
Registers[0x20] = ((Registers[0x20] & 0xF0) | (batteryLevel & 0x0F));
SPI_Powerman::SetBatteryLevelOkay(batteryLevel > batteryLevel_Low ? true : false);
//SPI_Powerman::SetBatteryLevelOkay(batteryLevel > batteryLevel_Low ? true : false);

if (batteryLevel <= 1)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DSi_NAND.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ union DSiFirmwareSystemSettings
u32 ConfigFlags;
u8 Zero02;
u8 CountryCode;
SPI_Firmware::Language Language;
Firmware::Language Language;
u8 RTCYear;
u32 RTCOffset;
u8 Zero3[4];
Expand Down
16 changes: 8 additions & 8 deletions src/DSi_NWifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ DSi_NWifi::~DSi_NWifi()

void DSi_NWifi::Reset()
{
using namespace SPI_Firmware;
TransferCmd = 0xFFFFFFFF;
RemSize = 0;

Expand All @@ -162,26 +161,28 @@ void DSi_NWifi::Reset()
for (int i = 0; i < 9; i++)
Mailbox[i].Clear();

MacAddress mac = GetFirmware()->Header().MacAddress;
const Firmware* fw = NDS::SPI->GetFirmware();

MacAddress mac = fw->GetHeader().MacAddress;
Log(LogLevel::Info, "NWifi MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

WifiBoard type = GetFirmware()->Header().WifiBoard;
Firmware::WifiBoard type = fw->GetHeader().WifiBoard;
switch (type)
{
case WifiBoard::W015: // AR6002
case Firmware::WifiBoard::W015: // AR6002
ROMID = 0x20000188;
ChipID = 0x02000001;
HostIntAddr = 0x00500400;
break;

case WifiBoard::W024: // AR6013
case Firmware::WifiBoard::W024: // AR6013
ROMID = 0x23000024;
ChipID = 0x0D000000;
HostIntAddr = 0x00520000;
break;

case WifiBoard::W028: // AR6014 (3DS)
case Firmware::WifiBoard::W028: // AR6014 (3DS)
ROMID = 0x2300006F;
ChipID = 0x0D000001;
HostIntAddr = 0x00520000;
Expand Down Expand Up @@ -893,9 +894,8 @@ void DSi_NWifi::HTC_Command()

case 0x0004: // setup complete
{
SPI_Firmware::MacAddress mac = SPI_Firmware::GetFirmware()->Header().MacAddress;
u8 ready_evt[12];
memcpy(&ready_evt[0], &mac, mac.size());
memcpy(&ready_evt[0], &EEPROM[0xA], 6); // MAC address
ready_evt[6] = 0x02;
ready_evt[7] = 0;
*(u32*)&ready_evt[8] = 0x2300006C;
Expand Down
61 changes: 21 additions & 40 deletions src/DSi_SPI_TSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,25 @@
#include <stdio.h>
#include <string.h>
#include "DSi.h"
#include "SPI.h"
#include "DSi_SPI_TSC.h"
#include "Platform.h"

using Platform::Log;
using Platform::LogLevel;

namespace DSi_SPI_TSC
{

u32 DataPos;
u8 Index;
u8 Bank;
u8 Data;

u8 Bank3Regs[0x80];
u8 TSCMode;

u16 TouchX, TouchY;


bool Init()
DSi_TSC::DSi_TSC(SPIHost* host) : TSC(host)
{
return true;
}

void DeInit()
DSi_TSC::~DSi_TSC()
{
}

void Reset()
void DSi_TSC::Reset()
{
TSC::Reset();

DataPos = 0;

Bank = 0;
Expand All @@ -72,8 +59,10 @@ void Reset()
TSCMode = 0x01; // DSi mode
}

void DoSavestate(Savestate* file)
void DSi_TSC::DoSavestate(Savestate* file)
{
TSC::DoSavestate(file);

file->Section("SPTi");

file->Var32(&DataPos);
Expand All @@ -85,19 +74,14 @@ void DoSavestate(Savestate* file)
file->Var8(&TSCMode);
}

void SetMode(u8 mode)
void DSi_TSC::SetMode(u8 mode)
{
TSCMode = mode;
}

void SetTouchCoords(u16 x, u16 y)
void DSi_TSC::SetTouchCoords(u16 x, u16 y)
{
if (TSCMode == 0x00)
{
if (y == 0xFFF) NDS::KeyInput |= (1 << (16+6));
else NDS::KeyInput &= ~(1 << (16+6));
return SPI_TSC::SetTouchCoords(x, y);
}
if (TSCMode == 0x00) return TSC::SetTouchCoords(x, y);

TouchX = x;
TouchY = y;
Expand Down Expand Up @@ -135,24 +119,17 @@ void SetTouchCoords(u16 x, u16 y)
}
}

void MicInputFrame(s16* data, int samples)
void DSi_TSC::MicInputFrame(s16* data, int samples)
{
if (TSCMode == 0x00) return SPI_TSC::MicInputFrame(data, samples);
if (TSCMode == 0x00) return TSC::MicInputFrame(data, samples);

// otherwise we don't handle mic input
// TODO: handle it where it needs to be
}

u8 Read()
{
if (TSCMode == 0x00) return SPI_TSC::Read();

return Data;
}

void Write(u8 val, u32 hold)
void DSi_TSC::Write(u8 val)
{
if (TSCMode == 0x00) return SPI_TSC::Write(val, hold);
if (TSCMode == 0x00) return TSC::Write(val);

#define READWRITE(var) { if (Index & 0x01) Data = var; else var = val; }

Expand Down Expand Up @@ -233,8 +210,12 @@ void Write(u8 val, u32 hold)
Index += (1<<1); // increment index
}

if (hold) DataPos++;
else DataPos = 0;
DataPos++;
}

void DSi_TSC::Release()
{
if (TSCMode == 0x00) return TSC::Release();

DataPos = 0;
}
33 changes: 20 additions & 13 deletions src/DSi_SPI_TSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,33 @@

#include "types.h"
#include "Savestate.h"
#include "SPI.h"

namespace DSi_SPI_TSC
class DSi_TSC : public TSC
{
public:
DSi_TSC(SPIHost* host);
~DSi_TSC() override;

extern u32 DataPos;
void Reset() override;

bool Init();
void DeInit();
void Reset();
void DoSavestate(Savestate* file);
void DoSavestate(Savestate* file) override;

// 00=DS-mode 01=normal
void SetMode(u8 mode);
// 00=DS-mode 01=normal
void SetMode(u8 mode);

void SetTouchCoords(u16 x, u16 y);
void MicInputFrame(s16* data, int samples);
void SetTouchCoords(u16 x, u16 y) override;
void MicInputFrame(s16* data, int samples) override;

u8 Read();
void Write(u8 val, u32 hold);
void Write(u8 val) override;
void Release() override;

}
private:
u8 Index;
u8 Bank;

u8 Bank3Regs[0x80];
u8 TSCMode;
};

#endif // DSI_SPI_TSC
Loading

0 comments on commit 440b356

Please sign in to comment.