camera: model FIFO more accurately (avoids weird bugs when DMA fails)

This commit is contained in:
Arisotura
2025-07-07 02:30:01 +02:00
parent 16d1464d19
commit 2cb07bf126
4 changed files with 29 additions and 25 deletions

View File

@ -67,9 +67,7 @@ void DSi_CamModule::Reset()
CropStart = 0;
CropEnd = 0;
memset(DataBuffer, 0, 512*sizeof(u32));
BufferReadPos = 0;
BufferWritePos = 0;
DataBuffer.Clear();
BufferNumLines = 0;
CurCamera = nullptr;
@ -89,6 +87,8 @@ void DSi_CamModule::DoSavestate(Savestate* file)
file->Var16(&ModuleCnt);
file->Var16(&Cnt);
// TODO: should other stuff be savestated? (pixel buffer, etc)
/*file->VarArray(FrameBuffer, sizeof(FrameBuffer));
file->Var32(&TransferPos);
file->Var32(&FrameLength);*/
@ -113,8 +113,6 @@ void DSi_CamModule::IRQ(u32 param)
if (Cnt & (1<<15))
{
BufferReadPos = 0;
BufferWritePos = 0;
BufferNumLines = 0;
CurCamera = activecam;
DSi.ScheduleEvent(Event_DSi_CamTransfer, false, kTransferStart, 0, 0);
@ -126,8 +124,10 @@ void DSi_CamModule::IRQ(u32 param)
void DSi_CamModule::TransferScanline(u32 line)
{
u32* dstbuf = &DataBuffer[BufferWritePos];
int maxlen = 512 - BufferWritePos;
if (Cnt & (1<<4))
return;
int maxlen = DataBuffer.FreeSpace();
u32 tmpbuf[512];
int lines_next;
@ -195,28 +195,28 @@ void DSi_CamModule::TransferScanline(u32 line)
u32 col1 = (r1 >> 3) | ((g1 >> 3) << 5) | ((b1 >> 3) << 10) | 0x8000;
u32 col2 = (r2 >> 3) | ((g2 >> 3) << 5) | ((b2 >> 3) << 10) | 0x8000;
dstbuf[i] = col1 | (col2 << 16);
DataBuffer.Write(col1 | (col2 << 16));
}
}
else
{
// return raw data
memcpy(dstbuf, &tmpbuf[copystart], copylen*sizeof(u32));
for (u32 i = 0; i < copylen; i++)
{
u32 val = tmpbuf[copystart + i];
DataBuffer.Write(val);
}
}
numscan = Cnt & 0x000F;
if (BufferNumLines >= numscan)
{
BufferReadPos = 0; // checkme
BufferWritePos = 0;
BufferNumLines = 0;
DSi.CheckNDMAs(0, 0x0B);
}
else
{
BufferWritePos += copylen;
if (BufferWritePos > 512) BufferWritePos = 512;
BufferNumLines++;
}
@ -225,10 +225,9 @@ skip_line:
{
// when the frame is finished, transfer any remaining data if needed
// (if the frame height isn't a multiple of the DMA interval)
// (TODO: should it be done earlier?)
if (BufferNumLines > 0)
{
BufferReadPos = 0;
BufferWritePos = 0;
BufferNumLines = 0;
DSi.CheckNDMAs(0, 0x0B);
}
@ -266,14 +265,19 @@ u32 DSi_CamModule::Read32(u32 addr)
{
case 0x04004204:
{
u32 ret = DataBuffer[BufferReadPos];
u32 ret;
if (Cnt & (1<<15))
{
if (BufferReadPos < 511)
BufferReadPos++;
// CHECKME!!!!
// also presumably we should set bit4 in Cnt if there's no new data to be read
if (DataBuffer.IsEmpty())
{
ret = 0;
Cnt |= (1<<4);
}
else
ret = DataBuffer.Read();
}
else
ret = DataBuffer.Peek();
return ret;
}
@ -339,8 +343,7 @@ void DSi_CamModule::Write16(u32 addr, u16 val)
if (val & (1<<5))
{
Cnt &= ~(1<<4);
BufferReadPos = 0;
BufferWritePos = 0;
DataBuffer.Clear();
}
if ((val & (1<<15)) && !(Cnt & (1<<15)))

View File

@ -22,6 +22,7 @@
#include "types.h"
#include "Savestate.h"
#include "DSi_I2C.h"
#include "FIFO.h"
namespace melonDS
{
@ -119,8 +120,7 @@ private:
u32 CropStart, CropEnd;
// pixel data buffer holds a maximum of 512 words, regardless of how long scanlines are
u32 DataBuffer[512];
u32 BufferReadPos, BufferWritePos;
FIFO<u32, 512> DataBuffer;
u32 BufferNumLines;
DSi_Camera* CurCamera;

View File

@ -90,6 +90,7 @@ public:
}
u32 Level() const { return NumOccupied; }
u32 FreeSpace() const { return NumEntries - NumOccupied; }
bool IsEmpty() const { return NumOccupied == 0; }
bool IsFull() const { return NumOccupied >= NumEntries; }

View File

@ -979,7 +979,7 @@ void MainWindow::keyPressEvent(QKeyEvent* event)
if (event->isAutoRepeat()) return;
// TODO!! REMOVE ME IN RELEASE BUILDS!!
//if (event->key() == Qt::Key_F11) emuThread->NDS->debug(0);
//if (event->key() == Qt::Key_F11) emuInstance->getNDS()->debug(0);
emuInstance->onKeyPress(event);
}