Skip to content

Commit

Permalink
start moving ROM/firmware loading to the emuthread to avoid cursed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Arisotura committed Oct 23, 2024
1 parent 1428bfb commit 82f38f0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 48 deletions.
51 changes: 49 additions & 2 deletions src/frontend/qt_sdl/EmuThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ void EmuThread::handleMessages()
break;

case msg_EmuStop:
if (msg.stopExternal) emuInstance->nds->Stop();
if (msg.param.value<bool>())
emuInstance->nds->Stop();
emuStatus = emuStatus_Paused;
emuActive = false;

Expand Down Expand Up @@ -574,6 +575,26 @@ void EmuThread::handleMessages()
emuInstance->deinitOpenGL();
useOpenGL = false;
break;

case msg_BootROM:
bootResult = 0;
if (!emuInstance->loadROM(msg.param.value<QStringList>(), true))
break;

assert(emuInstance->nds != nullptr);
emuInstance->nds->Start();
bootResult = 1;
break;

case msg_BootFirmware:
bootResult = 0;
if (!emuInstance->bootToMenu())
break;

assert(emuInstance->nds != nullptr);
emuInstance->nds->Start();
bootResult = 1;
break;
}

msgSemaphore.release();
Expand Down Expand Up @@ -626,7 +647,7 @@ void EmuThread::emuTogglePause()

void EmuThread::emuStop(bool external)
{
sendMessage({.type = msg_EmuStop, .stopExternal = external});
sendMessage({.type = msg_EmuStop, .param = external});
waitMessage();
}

Expand Down Expand Up @@ -660,6 +681,32 @@ bool EmuThread::emuIsActive()
return emuActive;
}

int EmuThread::bootROM(QStringList filename)
{
sendMessage(msg_EmuPause);
sendMessage({.type = msg_BootROM, .param = filename});
waitMessage(2);
if (!bootResult)
return bootResult;

sendMessage(msg_EmuRun);
waitMessage();
return bootResult;
}

int EmuThread::bootFirmware()
{
sendMessage(msg_EmuPause);
sendMessage(msg_BootFirmware);
waitMessage(2);
if (!bootResult)
return bootResult;

sendMessage(msg_EmuRun);
waitMessage();
return bootResult;
}

void EmuThread::updateRenderer()
{
if (videoRenderer != lastVideoRenderer)
Expand Down
13 changes: 9 additions & 4 deletions src/frontend/qt_sdl/EmuThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ class EmuThread : public QThread

msg_InitGL,
msg_DeInitGL,

msg_BootROM,
msg_BootFirmware,
};

struct Message
{
MessageType type;
union
{
bool stopExternal;
};
QVariant param;
};

void sendMessage(Message msg);
Expand All @@ -100,6 +100,9 @@ class EmuThread : public QThread
void emuFrameStep();
void emuReset();

int bootROM(QStringList filename);
int bootFirmware();

bool emuIsRunning();
bool emuIsActive();

Expand Down Expand Up @@ -153,6 +156,8 @@ class EmuThread : public QThread
constexpr static int emuPauseStackPauseThreshold = 1;
int emuPauseStack;

int bootResult = 0;

QMutex msgMutex;
QSemaphore msgSemaphore;
QQueue<Message> msgQueue;
Expand Down
47 changes: 5 additions & 42 deletions src/frontend/qt_sdl/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,24 +1211,15 @@ void MainWindow::updateCartInserted(bool gba)

void MainWindow::onOpenFile()
{
emuThread->emuPause();

if (!verifySetup())
{
emuThread->emuUnpause();
return;
}

QStringList file = pickROM(false);
if (file.isEmpty())
{
emuThread->emuUnpause();
return;
}

if (!emuInstance->loadROM(file, true))

if (!emuThread->bootROM(file))
{
emuThread->emuUnpause();
return;
}

Expand All @@ -1237,10 +1228,6 @@ void MainWindow::onOpenFile()
recentFileList.prepend(filename);
updateRecentFilesMenu();

assert(emuInstance->nds != nullptr);
emuInstance->nds->Start();
emuThread->emuRun();

updateCartInserted(false);
}

Expand Down Expand Up @@ -1310,59 +1297,35 @@ void MainWindow::onClickRecentFile()
QAction *act = (QAction *)sender();
QString filename = act->data().toString();

emuThread->emuPause();

if (!verifySetup())
{
emuThread->emuUnpause();
return;
}

const QStringList file = splitArchivePath(filename, true);
if (file.isEmpty())
{
emuThread->emuUnpause();
return;
}

if (!emuInstance->loadROM(file, true))

if (!emuThread->bootROM(file))
{
emuThread->emuUnpause();
return;
}

recentFileList.removeAll(filename);
recentFileList.prepend(filename);
updateRecentFilesMenu();

assert(emuInstance->nds != nullptr);
emuInstance->nds->Start();
emuThread->emuRun();

updateCartInserted(false);
}

void MainWindow::onBootFirmware()
{
emuThread->emuPause();

if (!verifySetup())
{
emuThread->emuUnpause();
return;
}

if (!emuInstance->bootToMenu())
if (!emuThread->bootFirmware())
{
// TODO: better error reporting?
QMessageBox::critical(this, "melonDS", "This firmware is not bootable.");
emuThread->emuUnpause();
return;
}

assert(emuInstance->nds != nullptr);
emuInstance->nds->Start();
emuThread->emuRun();
}

void MainWindow::onInsertCart()
Expand Down

0 comments on commit 82f38f0

Please sign in to comment.