Skip to content

Commit

Permalink
better, less hacky, more OOP-friendly scheduler design
Browse files Browse the repository at this point in the history
  • Loading branch information
Arisotura committed Nov 2, 2023
1 parent 5ccd391 commit 70c6750
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 218 deletions.
16 changes: 11 additions & 5 deletions src/DSi_Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const u32 kTransferStart = 60000;

bool Init()
{
NDS::RegisterEventFunc(NDS::Event_DSi_CamIRQ, 0, IRQ);
NDS::RegisterEventFunc(NDS::Event_DSi_CamTransfer, 0, TransferScanline);

Camera0 = new Camera(0);
Camera1 = new Camera(1);

Expand All @@ -66,6 +69,9 @@ void DeInit()

Camera0 = nullptr;
Camera1 = nullptr;

NDS::UnregisterEventFunc(NDS::Event_DSi_CamIRQ, 0);
NDS::UnregisterEventFunc(NDS::Event_DSi_CamTransfer, 0);
}

void Reset()
Expand All @@ -85,7 +91,7 @@ void Reset()
BufferNumLines = 0;
CurCamera = nullptr;

NDS::ScheduleEvent(NDS::Event_DSi_CamIRQ, false, kIRQInterval, IRQ, 0);
NDS::ScheduleEvent(NDS::Event_DSi_CamIRQ, false, kIRQInterval, 0, 0);
}

void Stop()
Expand Down Expand Up @@ -132,11 +138,11 @@ void IRQ(u32 param)
BufferWritePos = 0;
BufferNumLines = 0;
CurCamera = activecam;
NDS::ScheduleEvent(NDS::Event_DSi_CamTransfer, false, kTransferStart, TransferScanline, 0);
NDS::ScheduleEvent(NDS::Event_DSi_CamTransfer, false, kTransferStart, 0, 0);
}
}

NDS::ScheduleEvent(NDS::Event_DSi_CamIRQ, true, kIRQInterval, IRQ, 0);
NDS::ScheduleEvent(NDS::Event_DSi_CamIRQ, true, kIRQInterval, 0, 0);
}

void TransferScanline(u32 line)
Expand All @@ -162,7 +168,7 @@ void TransferScanline(u32 line)
if (line < ystart || line > yend)
{
if (!CurCamera->TransferDone())
NDS::ScheduleEvent(NDS::Event_DSi_CamTransfer, false, delay, TransferScanline, line+1);
NDS::ScheduleEvent(NDS::Event_DSi_CamTransfer, false, delay, 0, line+1);

return;
}
Expand Down Expand Up @@ -242,7 +248,7 @@ void TransferScanline(u32 line)
if (CurCamera->TransferDone())
return;

NDS::ScheduleEvent(NDS::Event_DSi_CamTransfer, false, delay, TransferScanline, line+1);
NDS::ScheduleEvent(NDS::Event_DSi_CamTransfer, false, delay, 0, line+1);
}


Expand Down
6 changes: 5 additions & 1 deletion src/DSi_DSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ void AudioCb(std::array<s16, 2> frame)

bool Init()
{
NDS::RegisterEventFunc(NDS::Event_DSi_DSP, 0, DSPCatchUpU32);

TeakraCore = new Teakra::Teakra();
SCFG_RST = false;

Expand Down Expand Up @@ -171,6 +173,8 @@ void DeInit()
//PDATAReadFifo = NULL;
//PDATAWriteFifo = NULL;
TeakraCore = NULL;

NDS::UnregisterEventFunc(NDS::Event_DSi_DSP, 0);
}

void Reset()
Expand Down Expand Up @@ -579,7 +583,7 @@ void Run(u32 cycles)

NDS::CancelEvent(NDS::Event_DSi_DSP);
NDS::ScheduleEvent(NDS::Event_DSi_DSP, false,
16384/*from citra (TeakraSlice)*/, DSPCatchUpU32, 0);
16384/*from citra (TeakraSlice)*/, 0, 0);
}

void DoSavestate(Savestate* file)
Expand Down
58 changes: 26 additions & 32 deletions src/DSi_NWifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ const u8 CIS1[256] =
};


DSi_NWifi* Ctx = nullptr;


DSi_NWifi::DSi_NWifi(DSi_SDHost* host)
: DSi_SDDevice(host),
Mailbox
Expand All @@ -133,16 +130,17 @@ DSi_NWifi::DSi_NWifi(DSi_SDHost* host)
DynamicFIFO<u8>(0x8000)
}
{
NDS::RegisterEventFunc(NDS::Event_DSi_NWifi, 0, MemberEventFunc(DSi_NWifi, MSTimer));

// this seems to control whether the firmware upload is done
EEPROMReady = 0;

Ctx = this;
}

DSi_NWifi::~DSi_NWifi()
{
NDS::CancelEvent(NDS::Event_DSi_NWifi);
Ctx = nullptr;

NDS::UnregisterEventFunc(NDS::Event_DSi_NWifi, 0);
}

void DSi_NWifi::Reset()
Expand Down Expand Up @@ -908,7 +906,7 @@ void DSi_NWifi::HTC_Command()
SendWMIEvent(1, 0x1006, regdomain_evt, 4);

BootPhase = 2;
NDS::ScheduleEvent(NDS::Event_DSi_NWifi, false, 33611, MSTimer, 0);
NDS::ScheduleEvent(NDS::Event_DSi_NWifi, false, 33611, 0, 0);
}
break;

Expand Down Expand Up @@ -1549,7 +1547,26 @@ void DSi_NWifi::WindowWrite(u32 addr, u32 val)
}


void DSi_NWifi::_MSTimer()
void DSi_NWifi::DrainRXBuffer()
{
while (Mailbox[8].Level() >= 6)
{
u16 len = Mailbox[8].Peek(2) | (Mailbox[8].Peek(3) << 8);
u32 totallen = len + 6;
u32 required = (totallen + 0x7F) & ~0x7F;

if (!Mailbox[4].CanFit(required))
break;

u32 i = 0;
for (; i < totallen; i++) Mailbox[4].Write(Mailbox[8].Read());
for (; i < required; i++) Mailbox[4].Write(0);
}

UpdateIRQ_F1();
}

void DSi_NWifi::MSTimer(u32 param)
{
BeaconTimer++;

Expand Down Expand Up @@ -1587,29 +1604,6 @@ void DSi_NWifi::_MSTimer()
//if (Mailbox[4].IsEmpty())
CheckRX();
}
}

void DSi_NWifi::DrainRXBuffer()
{
while (Mailbox[8].Level() >= 6)
{
u16 len = Mailbox[8].Peek(2) | (Mailbox[8].Peek(3) << 8);
u32 totallen = len + 6;
u32 required = (totallen + 0x7F) & ~0x7F;

if (!Mailbox[4].CanFit(required))
break;

u32 i = 0;
for (; i < totallen; i++) Mailbox[4].Write(Mailbox[8].Read());
for (; i < required; i++) Mailbox[4].Write(0);
}

UpdateIRQ_F1();
}

void DSi_NWifi::MSTimer(u32 param)
{
Ctx->_MSTimer();
NDS::ScheduleEvent(NDS::Event_DSi_NWifi, true, 33611, MSTimer, 0);
NDS::ScheduleEvent(NDS::Event_DSi_NWifi, true, 33611, 0, 0);
}
4 changes: 1 addition & 3 deletions src/DSi_NWifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class DSi_NWifi : public DSi_SDDevice

void SetIRQ_F1_Counter(u32 n);

void _MSTimer();

static void MSTimer(u32 param);
void MSTimer(u32 param);

private:
u32 TransferCmd;
Expand Down
42 changes: 27 additions & 15 deletions src/DSi_SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ using namespace Platform;

#define SD_DESC Num?"SDIO":"SD/MMC"

enum
{
Transfer_TX = 0,
Transfer_RX,
};


DSi_SDHost::DSi_SDHost(u32 num)
{
Num = num;

NDS::RegisterEventFunc(Num ? NDS::Event_DSi_SDIOTransfer : NDS::Event_DSi_SDMMCTransfer,
Transfer_TX, MemberEventFunc(DSi_SDHost, FinishTX));
NDS::RegisterEventFunc(Num ? NDS::Event_DSi_SDIOTransfer : NDS::Event_DSi_SDMMCTransfer,
Transfer_RX, MemberEventFunc(DSi_SDHost, FinishRX));

Ports[0] = nullptr;
Ports[1] = nullptr;
}
Expand All @@ -61,6 +72,11 @@ DSi_SDHost::~DSi_SDHost()
{
if (Ports[0]) delete Ports[0];
if (Ports[1]) delete Ports[1];

NDS::UnregisterEventFunc(Num ? NDS::Event_DSi_SDIOTransfer : NDS::Event_DSi_SDMMCTransfer,
Transfer_TX);
NDS::UnregisterEventFunc(Num ? NDS::Event_DSi_SDIOTransfer : NDS::Event_DSi_SDMMCTransfer,
Transfer_RX);
}

void DSi_SDHost::CloseHandles()
Expand Down Expand Up @@ -281,14 +297,12 @@ void DSi_SDHost::SendResponse(u32 val, bool last)

void DSi_SDHost::FinishRX(u32 param)
{
DSi_SDHost* host = (param & 0x1) ? DSi::SDIO : DSi::SDMMC;

host->CheckSwapFIFO();
CheckSwapFIFO();

if (host->DataMode == 1)
host->UpdateFIFO32();
if (DataMode == 1)
UpdateFIFO32();
else
host->SetIRQ(24);
SetIRQ(24);
}

u32 DSi_SDHost::DataRX(u8* data, u32 len)
Expand All @@ -308,30 +322,28 @@ u32 DSi_SDHost::DataRX(u8* data, u32 len)
// we need a delay because DSi boot2 will send a command and then wait for IRQ0
// but if IRQ24 is thrown instantly, the handler clears IRQ0 before the
// send-command function starts polling IRQ status
u32 param = Num | (last << 1);
NDS::ScheduleEvent(Num ? NDS::Event_DSi_SDIOTransfer : NDS::Event_DSi_SDMMCTransfer,
false, 512, FinishRX, param);
false, 512, Transfer_RX, 0);

return len;
}

void DSi_SDHost::FinishTX(u32 param)
{
DSi_SDHost* host = (param & 0x1) ? DSi::SDIO : DSi::SDMMC;
DSi_SDDevice* dev = host->Ports[host->PortSelect & 0x1];
DSi_SDDevice* dev = Ports[PortSelect & 0x1];

if (host->BlockCountInternal == 0)
if (BlockCountInternal == 0)
{
if (host->StopAction & (1<<8))
if (StopAction & (1<<8))
{
if (dev) dev->SendCMD(12, 0);
}

// CHECKME: presumably IRQ2 should not trigger here, but rather
// when the data transfer is done
//SetIRQ(0);
host->SetIRQ(2);
host->TXReq = false;
SetIRQ(2);
TXReq = false;
}
else
{
Expand Down Expand Up @@ -392,7 +404,7 @@ u32 DSi_SDHost::DataTX(u8* data, u32 len)
BlockCountInternal--;

NDS::ScheduleEvent(Num ? NDS::Event_DSi_SDIOTransfer : NDS::Event_DSi_SDMMCTransfer,
false, 512, FinishTX, Num);
false, 512, Transfer_TX, 0);

return len;
}
Expand Down
4 changes: 2 additions & 2 deletions src/DSi_SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class DSi_SDHost

void DoSavestate(Savestate* file);

static void FinishRX(u32 param);
static void FinishTX(u32 param);
void FinishRX(u32 param);
void FinishTX(u32 param);
void SendResponse(u32 val, bool last);
u32 DataRX(u8* data, u32 len);
u32 DataTX(u8* data, u32 len);
Expand Down
27 changes: 22 additions & 5 deletions src/GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ namespace GPU
#define HBLANK_CYCLES (48+(256*6))
#define FRAME_CYCLES (LINE_CYCLES * 263)

enum
{
LCD_StartHBlank = 0,
LCD_StartScanline,
LCD_FinishFrame,
};

u16 VCount;
u32 NextVCount;
u16 TotalScanlines;
Expand Down Expand Up @@ -151,6 +158,11 @@ std::unique_ptr<GLCompositor> CurGLCompositor = {};

bool Init()
{
NDS::RegisterEventFunc(NDS::Event_LCD, LCD_StartHBlank, StartHBlank);
NDS::RegisterEventFunc(NDS::Event_LCD, LCD_StartScanline, StartScanline);
NDS::RegisterEventFunc(NDS::Event_LCD, LCD_FinishFrame, FinishFrame);
NDS::RegisterEventFunc(NDS::Event_DisplayFIFO, 0, DisplayFIFO);

GPU2D_Renderer = std::make_unique<GPU2D::SoftRenderer>();
if (!GPU3D::Init()) return false;

Expand Down Expand Up @@ -180,6 +192,11 @@ void DeInit()
#ifdef OGLRENDERER_ENABLED
CurGLCompositor = nullptr;
#endif

NDS::UnregisterEventFunc(NDS::Event_LCD, LCD_StartHBlank);
NDS::UnregisterEventFunc(NDS::Event_LCD, LCD_StartScanline);
NDS::UnregisterEventFunc(NDS::Event_LCD, LCD_FinishFrame);
NDS::UnregisterEventFunc(NDS::Event_DisplayFIFO, 0);
}

void ResetVRAMCache()
Expand Down Expand Up @@ -1022,7 +1039,7 @@ void DisplayFIFO(u32 x)
{
// transfer the next 8 pixels
NDS::CheckDMAs(0, 0x04);
NDS::ScheduleEvent(NDS::Event_DisplayFIFO, true, 6*8, DisplayFIFO, x+8);
NDS::ScheduleEvent(NDS::Event_DisplayFIFO, true, 6*8, 0, x+8);
}
else
GPU2D_A.SampleFIFO(253, 3); // sample the remaining pixels
Expand Down Expand Up @@ -1077,9 +1094,9 @@ void StartHBlank(u32 line)
if (DispStat[1] & (1<<4)) NDS::SetIRQ(1, NDS::IRQ_HBlank);

if (VCount < 262)
NDS::ScheduleEvent(NDS::Event_LCD, true, (LINE_CYCLES - HBLANK_CYCLES), StartScanline, line+1);
NDS::ScheduleEvent(NDS::Event_LCD, true, (LINE_CYCLES - HBLANK_CYCLES), LCD_StartScanline, line+1);
else
NDS::ScheduleEvent(NDS::Event_LCD, true, (LINE_CYCLES - HBLANK_CYCLES), FinishFrame, line+1);
NDS::ScheduleEvent(NDS::Event_LCD, true, (LINE_CYCLES - HBLANK_CYCLES), LCD_FinishFrame, line+1);
}

void FinishFrame(u32 lines)
Expand Down Expand Up @@ -1164,7 +1181,7 @@ void StartScanline(u32 line)
}

if (RunFIFO)
NDS::ScheduleEvent(NDS::Event_DisplayFIFO, false, 32, DisplayFIFO, 0);
NDS::ScheduleEvent(NDS::Event_DisplayFIFO, false, 32, 0, 0);
}

if (VCount == 262)
Expand Down Expand Up @@ -1210,7 +1227,7 @@ void StartScanline(u32 line)
}
}

NDS::ScheduleEvent(NDS::Event_LCD, true, HBLANK_CYCLES, StartHBlank, line);
NDS::ScheduleEvent(NDS::Event_LCD, true, HBLANK_CYCLES, LCD_StartHBlank, line);
}


Expand Down
Loading

0 comments on commit 70c6750

Please sign in to comment.