diff options
Diffstat (limited to 'src/citra_qt')
-rw-r--r-- | src/citra_qt/bootmanager.cpp | 103 | ||||
-rw-r--r-- | src/citra_qt/bootmanager.h | 68 | ||||
-rw-r--r-- | src/citra_qt/debugger/disassembler.cpp | 81 | ||||
-rw-r--r-- | src/citra_qt/debugger/disassembler.h | 7 | ||||
-rw-r--r-- | src/citra_qt/debugger/graphics_breakpoints_p.h | 2 | ||||
-rw-r--r-- | src/citra_qt/debugger/profiler.cpp | 4 | ||||
-rw-r--r-- | src/citra_qt/debugger/profiler.h | 2 | ||||
-rw-r--r-- | src/citra_qt/debugger/registers.cpp | 33 | ||||
-rw-r--r-- | src/citra_qt/debugger/registers.h | 4 | ||||
-rw-r--r-- | src/citra_qt/main.cpp | 125 | ||||
-rw-r--r-- | src/citra_qt/main.h | 23 | ||||
-rw-r--r-- | src/citra_qt/main.ui | 3 | ||||
-rw-r--r-- | src/citra_qt/util/spinbox.cpp | 3 |
13 files changed, 241 insertions, 217 deletions
diff --git a/src/citra_qt/bootmanager.cpp b/src/citra_qt/bootmanager.cpp index b81bd6167..a7f949411 100644 --- a/src/citra_qt/bootmanager.cpp +++ b/src/citra_qt/bootmanager.cpp @@ -10,6 +10,7 @@ #include "common/common.h" #include "bootmanager.h" +#include "main.h" #include "core/core.h" #include "core/settings.h" @@ -27,43 +28,33 @@ #define COPYRIGHT "Copyright (C) 2013-2014 Citra Team" EmuThread::EmuThread(GRenderWindow* render_window) : - filename(""), exec_cpu_step(false), cpu_running(false), - stop_run(false), render_window(render_window) -{ -} + exec_step(false), running(false), stop_run(false), render_window(render_window) { -void EmuThread::SetFilename(std::string filename) -{ - this->filename = filename; + connect(this, SIGNAL(started()), render_window, SLOT(moveContext())); } -void EmuThread::run() -{ +void EmuThread::run() { stop_run = false; // holds whether the cpu was running during the last iteration, // so that the DebugModeLeft signal can be emitted before the // next execution step bool was_active = false; - while (!stop_run) - { - if (cpu_running) - { + while (!stop_run) { + if (running) { if (!was_active) emit DebugModeLeft(); Core::RunLoop(); - was_active = cpu_running || exec_cpu_step; - if (!was_active) + was_active = running || exec_step; + if (!was_active && !stop_run) emit DebugModeEntered(); - } - else if (exec_cpu_step) - { + } else if (exec_step) { if (!was_active) emit DebugModeLeft(); - exec_cpu_step = false; + exec_step = false; Core::SingleStep(); emit DebugModeEntered(); yieldCurrentThread(); @@ -71,47 +62,10 @@ void EmuThread::run() was_active = false; } } - render_window->moveContext(); - - Core::Stop(); -} - -void EmuThread::Stop() -{ - if (!isRunning()) - { - LOG_WARNING(Frontend, "EmuThread::Stop called while emu thread wasn't running, returning..."); - return; - } - stop_run = true; - // Release emu threads from any breakpoints, so that this doesn't hang forever. - Pica::g_debug_context->ClearBreakpoints(); - - //core::g_state = core::SYS_DIE; - - // TODO: Waiting here is just a bad workaround for retarded shutdown logic. - wait(1000); - if (isRunning()) - { - LOG_WARNING(Frontend, "EmuThread still running, terminating..."); - quit(); - - // TODO: Waiting 50 seconds can be necessary if the logging subsystem has a lot of spam - // queued... This should be fixed. - wait(50000); - if (isRunning()) - { - LOG_CRITICAL(Frontend, "EmuThread STILL running, something is wrong here..."); - terminate(); - } - } - LOG_INFO(Frontend, "EmuThread stopped"); - - System::Shutdown(); + render_window->moveContext(); } - // This class overrides paintEvent and resizeEvent to prevent the GUI thread from stealing GL context. // The corresponding functionality is handled in EmuThread instead class GGLWidgetInternal : public QGLWidget @@ -133,13 +87,9 @@ private: GRenderWindow* parent; }; -EmuThread& GRenderWindow::GetEmuThread() -{ - return emu_thread; -} +GRenderWindow::GRenderWindow(QWidget* parent, EmuThread* emu_thread) : + QWidget(parent), emu_thread(emu_thread), keyboard_id(0) { -GRenderWindow::GRenderWindow(QWidget* parent) : QWidget(parent), emu_thread(this), keyboard_id(0) -{ std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc); setWindowTitle(QString::fromStdString(window_title)); @@ -160,7 +110,6 @@ GRenderWindow::GRenderWindow(QWidget* parent) : QWidget(parent), emu_thread(this layout->addWidget(child); layout->setMargin(0); setLayout(layout); - connect(&emu_thread, SIGNAL(started()), this, SLOT(moveContext())); OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size); @@ -180,29 +129,17 @@ void GRenderWindow::moveContext() // We need to move GL context to the swapping thread in Qt5 #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0) // If the thread started running, move the GL Context to the new thread. Otherwise, move it back. - child->context()->moveToThread((QThread::currentThread() == qApp->thread()) ? &emu_thread : qApp->thread()); + auto thread = (QThread::currentThread() == qApp->thread() && emu_thread != nullptr) ? emu_thread : qApp->thread(); + child->context()->moveToThread(thread); #endif } -GRenderWindow::~GRenderWindow() -{ - if (emu_thread.isRunning()) - emu_thread.Stop(); -} - void GRenderWindow::SwapBuffers() { // MakeCurrent is already called in renderer_opengl child->swapBuffers(); } -void GRenderWindow::closeEvent(QCloseEvent* event) -{ - if (emu_thread.isRunning()) - emu_thread.Stop(); - QWidget::closeEvent(event); -} - void GRenderWindow::MakeCurrent() { child->makeCurrent(); @@ -288,7 +225,7 @@ void GRenderWindow::mousePressEvent(QMouseEvent *event) void GRenderWindow::mouseMoveEvent(QMouseEvent *event) { auto pos = event->pos(); - this->TouchMoved(static_cast<unsigned>(pos.x()), static_cast<unsigned>(pos.y())); + this->TouchMoved(static_cast<unsigned>(std::max(pos.x(), 0)), static_cast<unsigned>(std::max(pos.y(), 0))); } void GRenderWindow::mouseReleaseEvent(QMouseEvent *event) @@ -335,3 +272,11 @@ void GRenderWindow::OnClientAreaResized(unsigned width, unsigned height) void GRenderWindow::OnMinimalClientAreaChangeRequest(const std::pair<unsigned,unsigned>& minimal_size) { setMinimumSize(minimal_size.first, minimal_size.second); } + +void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) { + this->emu_thread = emu_thread; +} + +void GRenderWindow::OnEmulationStopping() { + emu_thread = nullptr; +} diff --git a/src/citra_qt/bootmanager.h b/src/citra_qt/bootmanager.h index 288da45a1..715faf2d7 100644 --- a/src/citra_qt/bootmanager.h +++ b/src/citra_qt/bootmanager.h @@ -9,72 +9,58 @@ #include "common/common.h" #include "common/emu_window.h" +#include "common/thread.h" class QScreen; class QKeyEvent; class GRenderWindow; +class GMainWindow; class EmuThread : public QThread { Q_OBJECT public: - /** - * Set image filename - * - * @param filename - * @warning Only call when not running! - */ - void SetFilename(std::string filename); + EmuThread(GRenderWindow* render_window); /** * Start emulation (on new thread) - * * @warning Only call when not running! */ void run() override; /** - * Allow the CPU to process a single instruction (if cpu is not running) - * + * Steps the emulation thread by a single CPU instruction (if the CPU is not already running) * @note This function is thread-safe */ - void ExecStep() { exec_cpu_step = true; } + void ExecStep() { exec_step = true; } /** - * Allow the CPU to continue processing instructions without interruption - * + * Sets whether the emulation thread is running or not + * @param running Boolean value, set the emulation thread to running if true * @note This function is thread-safe */ - void SetCpuRunning(bool running) { cpu_running = running; } + void SetRunning(bool running) { this->running = running; } /** - * Allow the CPU to continue processing instructions without interruption - * - * @note This function is thread-safe - */ - bool IsCpuRunning() { return cpu_running; } - + * Check if the emulation thread is running or not + * @return True if the emulation thread is running, otherwise false + * @note This function is thread-safe + */ + bool IsRunning() { return running; } -public slots: /** - * Stop emulation and wait for the thread to finish. - * - * @details: This function will wait a second for the thread to finish; if it hasn't finished until then, we'll terminate() it and wait another second, hoping that it will be terminated by then. - * @note: This function is thread-safe. + * Requests for the emulation thread to stop running */ - void Stop(); + void RequestStop() { + stop_run = true; + running = false; + }; private: - friend class GRenderWindow; - - EmuThread(GRenderWindow* render_window); - - std::string filename; - - bool exec_cpu_step; - bool cpu_running; + bool exec_step; + bool running; std::atomic<bool> stop_run; GRenderWindow* render_window; @@ -100,10 +86,7 @@ class GRenderWindow : public QWidget, public EmuWindow Q_OBJECT public: - GRenderWindow(QWidget* parent = NULL); - ~GRenderWindow(); - - void closeEvent(QCloseEvent*) override; + GRenderWindow(QWidget* parent, EmuThread* emu_thread); // EmuWindow implementation void SwapBuffers() override; @@ -116,8 +99,6 @@ public: void restoreGeometry(const QByteArray& geometry); // overridden QByteArray saveGeometry(); // overridden - EmuThread& GetEmuThread(); - void keyPressEvent(QKeyEvent* event) override; void keyReleaseEvent(QKeyEvent* event) override; @@ -134,15 +115,18 @@ public: public slots: void moveContext(); // overridden + void OnEmulationStarting(EmuThread* emu_thread); + void OnEmulationStopping(); + private: void OnMinimalClientAreaChangeRequest(const std::pair<unsigned,unsigned>& minimal_size) override; QGLWidget* child; - EmuThread emu_thread; - QByteArray geometry; /// Device id of keyboard for use with KeyMap int keyboard_id; + + EmuThread* emu_thread; }; diff --git a/src/citra_qt/debugger/disassembler.cpp b/src/citra_qt/debugger/disassembler.cpp index f620687ae..08c6b49bd 100644 --- a/src/citra_qt/debugger/disassembler.cpp +++ b/src/citra_qt/debugger/disassembler.cpp @@ -18,8 +18,8 @@ #include "core/arm/disassembler/arm_disasm.h" -DisassemblerModel::DisassemblerModel(QObject* parent) : QAbstractListModel(parent), base_address(0), code_size(0), program_counter(0), selection(QModelIndex()) { - +DisassemblerModel::DisassemblerModel(QObject* parent) : + QAbstractListModel(parent), base_address(0), code_size(0), program_counter(0), selection(QModelIndex()) { } int DisassemblerModel::columnCount(const QModelIndex& parent) const { @@ -158,34 +158,28 @@ void DisassemblerModel::SetNextInstruction(unsigned int address) { emit dataChanged(prev_index, prev_index); } -DisassemblerWidget::DisassemblerWidget(QWidget* parent, EmuThread& emu_thread) : QDockWidget(parent), base_addr(0), emu_thread(emu_thread) -{ - disasm_ui.setupUi(this); +DisassemblerWidget::DisassemblerWidget(QWidget* parent, EmuThread* emu_thread) : + QDockWidget(parent), emu_thread(emu_thread), base_addr(0) { - model = new DisassemblerModel(this); - disasm_ui.treeView->setModel(model); + disasm_ui.setupUi(this); RegisterHotkey("Disassembler", "Start/Stop", QKeySequence(Qt::Key_F5), Qt::ApplicationShortcut); RegisterHotkey("Disassembler", "Step", QKeySequence(Qt::Key_F10), Qt::ApplicationShortcut); RegisterHotkey("Disassembler", "Step into", QKeySequence(Qt::Key_F11), Qt::ApplicationShortcut); RegisterHotkey("Disassembler", "Set Breakpoint", QKeySequence(Qt::Key_F9), Qt::ApplicationShortcut); - connect(disasm_ui.button_breakpoint, SIGNAL(clicked()), model, SLOT(OnSetOrUnsetBreakpoint())); connect(disasm_ui.button_step, SIGNAL(clicked()), this, SLOT(OnStep())); connect(disasm_ui.button_pause, SIGNAL(clicked()), this, SLOT(OnPause())); connect(disasm_ui.button_continue, SIGNAL(clicked()), this, SLOT(OnContinue())); - connect(disasm_ui.treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), - model, SLOT(OnSelectionChanged(const QModelIndex&))); - connect(GetHotkey("Disassembler", "Start/Stop", this), SIGNAL(activated()), this, SLOT(OnToggleStartStop())); connect(GetHotkey("Disassembler", "Step", this), SIGNAL(activated()), this, SLOT(OnStep())); connect(GetHotkey("Disassembler", "Step into", this), SIGNAL(activated()), this, SLOT(OnStepInto())); - connect(GetHotkey("Disassembler", "Set Breakpoint", this), SIGNAL(activated()), model, SLOT(OnSetOrUnsetBreakpoint())); + + setEnabled(false); } -void DisassemblerWidget::Init() -{ +void DisassemblerWidget::Init() { model->ParseFromAddress(Core::g_app_core->GetPC()); disasm_ui.treeView->resizeColumnToContents(0); @@ -197,25 +191,21 @@ void DisassemblerWidget::Init() disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); } -void DisassemblerWidget::OnContinue() -{ - emu_thread.SetCpuRunning(true); +void DisassemblerWidget::OnContinue() { + emu_thread->SetRunning(true); } -void DisassemblerWidget::OnStep() -{ +void DisassemblerWidget::OnStep() { OnStepInto(); // change later } -void DisassemblerWidget::OnStepInto() -{ - emu_thread.SetCpuRunning(false); - emu_thread.ExecStep(); +void DisassemblerWidget::OnStepInto() { + emu_thread->SetRunning(false); + emu_thread->ExecStep(); } -void DisassemblerWidget::OnPause() -{ - emu_thread.SetCpuRunning(false); +void DisassemblerWidget::OnPause() { + emu_thread->SetRunning(false); // TODO: By now, the CPU might not have actually stopped... if (Core::g_app_core) { @@ -223,17 +213,15 @@ void DisassemblerWidget::OnPause() } } -void DisassemblerWidget::OnToggleStartStop() -{ - emu_thread.SetCpuRunning(!emu_thread.IsCpuRunning()); +void DisassemblerWidget::OnToggleStartStop() { + emu_thread->SetRunning(!emu_thread->IsRunning()); } -void DisassemblerWidget::OnDebugModeEntered() -{ +void DisassemblerWidget::OnDebugModeEntered() { ARMword next_instr = Core::g_app_core->GetPC(); if (model->GetBreakPoints().IsAddressBreakPoint(next_instr)) - emu_thread.SetCpuRunning(false); + emu_thread->SetRunning(false); model->SetNextInstruction(next_instr); @@ -242,16 +230,35 @@ void DisassemblerWidget::OnDebugModeEntered() disasm_ui.treeView->selectionModel()->setCurrentIndex(model_index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); } -void DisassemblerWidget::OnDebugModeLeft() -{ - +void DisassemblerWidget::OnDebugModeLeft() { } -int DisassemblerWidget::SelectedRow() -{ +int DisassemblerWidget::SelectedRow() { QModelIndex index = disasm_ui.treeView->selectionModel()->currentIndex(); if (!index.isValid()) return -1; return disasm_ui.treeView->selectionModel()->currentIndex().row(); } + +void DisassemblerWidget::OnEmulationStarting(EmuThread* emu_thread) { + this->emu_thread = emu_thread; + + model = new DisassemblerModel(this); + disasm_ui.treeView->setModel(model); + + connect(disasm_ui.treeView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), + model, SLOT(OnSelectionChanged(const QModelIndex&))); + connect(disasm_ui.button_breakpoint, SIGNAL(clicked()), model, SLOT(OnSetOrUnsetBreakpoint())); + connect(GetHotkey("Disassembler", "Set Breakpoint", this), SIGNAL(activated()), model, SLOT(OnSetOrUnsetBreakpoint())); + + Init(); + setEnabled(true); +} + +void DisassemblerWidget::OnEmulationStopping() { + disasm_ui.treeView->setModel(nullptr); + delete model; + emu_thread = nullptr; + setEnabled(false); +} diff --git a/src/citra_qt/debugger/disassembler.h b/src/citra_qt/debugger/disassembler.h index 5e19d7c51..45b0a7e08 100644 --- a/src/citra_qt/debugger/disassembler.h +++ b/src/citra_qt/debugger/disassembler.h @@ -51,7 +51,7 @@ class DisassemblerWidget : public QDockWidget Q_OBJECT public: - DisassemblerWidget(QWidget* parent, EmuThread& emu_thread); + DisassemblerWidget(QWidget* parent, EmuThread* emu_thread); void Init(); @@ -65,6 +65,9 @@ public slots: void OnDebugModeEntered(); void OnDebugModeLeft(); + void OnEmulationStarting(EmuThread* emu_thread); + void OnEmulationStopping(); + private: // returns -1 if no row is selected int SelectedRow(); @@ -75,5 +78,5 @@ private: u32 base_addr; - EmuThread& emu_thread; + EmuThread* emu_thread; }; diff --git a/src/citra_qt/debugger/graphics_breakpoints_p.h b/src/citra_qt/debugger/graphics_breakpoints_p.h index 232bfc863..34e72e859 100644 --- a/src/citra_qt/debugger/graphics_breakpoints_p.h +++ b/src/citra_qt/debugger/graphics_breakpoints_p.h @@ -25,7 +25,7 @@ public: QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); + bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override; public slots: void OnBreakPointHit(Pica::DebugContext::Event event); diff --git a/src/citra_qt/debugger/profiler.cpp b/src/citra_qt/debugger/profiler.cpp index ae0568b6a..2ac1748b7 100644 --- a/src/citra_qt/debugger/profiler.cpp +++ b/src/citra_qt/debugger/profiler.cpp @@ -26,7 +26,7 @@ static QVariant GetDataForColumn(int col, const AggregatedDuration& duration) static const TimingCategoryInfo* GetCategoryInfo(int id) { const auto& categories = GetProfilingManager().GetTimingCategoriesInfo(); - if (id >= categories.size()) { + if ((size_t)id >= categories.size()) { return nullptr; } else { return &categories[id]; @@ -98,7 +98,7 @@ QVariant ProfilerModel::data(const QModelIndex& index, int role) const const TimingCategoryInfo* info = GetCategoryInfo(index.row() - 2); return info != nullptr ? QString(info->name) : QVariant(); } else { - if (index.row() - 2 < results.time_per_category.size()) { + if (index.row() - 2 < (int)results.time_per_category.size()) { return GetDataForColumn(index.column(), results.time_per_category[index.row() - 2]); } else { return QVariant(); diff --git a/src/citra_qt/debugger/profiler.h b/src/citra_qt/debugger/profiler.h index a6d87aa0f..fabf279b8 100644 --- a/src/citra_qt/debugger/profiler.h +++ b/src/citra_qt/debugger/profiler.h @@ -18,7 +18,7 @@ class ProfilerModel : public QAbstractItemModel public: ProfilerModel(QObject* parent); - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex& child) const override; int columnCount(const QModelIndex& parent = QModelIndex()) const override; diff --git a/src/citra_qt/debugger/registers.cpp b/src/citra_qt/debugger/registers.cpp index ab3666156..5527a2afd 100644 --- a/src/citra_qt/debugger/registers.cpp +++ b/src/citra_qt/debugger/registers.cpp @@ -7,8 +7,7 @@ #include "core/core.h" #include "core/arm/arm_interface.h" -RegistersWidget::RegistersWidget(QWidget* parent) : QDockWidget(parent) -{ +RegistersWidget::RegistersWidget(QWidget* parent) : QDockWidget(parent) { cpu_regs_ui.setupUi(this); tree = cpu_regs_ui.treeWidget; @@ -18,8 +17,7 @@ RegistersWidget::RegistersWidget(QWidget* parent) : QDockWidget(parent) registers->setExpanded(true); CSPR->setExpanded(true); - for (int i = 0; i < 16; ++i) - { + for (int i = 0; i < 16; ++i) { QTreeWidgetItem* child = new QTreeWidgetItem(QStringList(QString("R[%1]").arg(i, 2, 10, QLatin1Char('0')))); registers->addChild(child); } @@ -39,12 +37,16 @@ RegistersWidget::RegistersWidget(QWidget* parent) : QDockWidget(parent) CSPR->addChild(new QTreeWidgetItem(QStringList("C"))); CSPR->addChild(new QTreeWidgetItem(QStringList("Z"))); CSPR->addChild(new QTreeWidgetItem(QStringList("N"))); + + setEnabled(false); } -void RegistersWidget::OnDebugModeEntered() -{ +void RegistersWidget::OnDebugModeEntered() { ARM_Interface* app_core = Core::g_app_core; + if (app_core == nullptr) + return; + for (int i = 0; i < 16; ++i) registers->child(i)->setText(1, QString("0x%1").arg(app_core->GetReg(i), 8, 16, QLatin1Char('0'))); @@ -66,7 +68,22 @@ void RegistersWidget::OnDebugModeEntered() CSPR->child(14)->setText(1, QString("%1").arg((app_core->GetCPSR() >> 31) & 0x1)); // N - Negative/Less than } -void RegistersWidget::OnDebugModeLeft() -{ +void RegistersWidget::OnDebugModeLeft() { +} + +void RegistersWidget::OnEmulationStarting(EmuThread* emu_thread) { + setEnabled(true); +} + +void RegistersWidget::OnEmulationStopping() { + // Reset widget text + for (int i = 0; i < 16; ++i) + registers->child(i)->setText(1, QString("")); + + for (int i = 0; i < 15; ++i) + CSPR->child(i)->setText(1, QString("")); + + CSPR->setText(1, QString("")); + setEnabled(false); } diff --git a/src/citra_qt/debugger/registers.h b/src/citra_qt/debugger/registers.h index bf8955625..68e3fb908 100644 --- a/src/citra_qt/debugger/registers.h +++ b/src/citra_qt/debugger/registers.h @@ -8,6 +8,7 @@ #include <QTreeWidgetItem> class QTreeWidget; +class EmuThread; class RegistersWidget : public QDockWidget { @@ -20,6 +21,9 @@ public slots: void OnDebugModeEntered(); void OnDebugModeLeft(); + void OnEmulationStarting(EmuThread* emu_thread); + void OnEmulationStopping(); + private: Ui::ARMRegisters cpu_regs_ui; diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index e5ca04124..dd0e4de8f 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp @@ -15,6 +15,7 @@ #include "common/logging/log.h" #include "common/logging/backend.h" #include "common/logging/filter.h" +#include "common/make_unique.h" #include "common/platform.h" #include "common/scope_exit.h" @@ -46,7 +47,7 @@ #include "version.h" -GMainWindow::GMainWindow() +GMainWindow::GMainWindow() : emu_thread(nullptr) { Pica::g_debug_context = Pica::DebugContext::Construct(); @@ -55,14 +56,14 @@ GMainWindow::GMainWindow() ui.setupUi(this); statusBar()->hide(); - render_window = new GRenderWindow; + render_window = new GRenderWindow(this, emu_thread.get()); render_window->hide(); profilerWidget = new ProfilerWidget(this); addDockWidget(Qt::BottomDockWidgetArea, profilerWidget); profilerWidget->hide(); - disasmWidget = new DisassemblerWidget(this, render_window->GetEmuThread()); + disasmWidget = new DisassemblerWidget(this, emu_thread.get()); addDockWidget(Qt::BottomDockWidgetArea, disasmWidget); disasmWidget->hide(); @@ -138,14 +139,12 @@ GMainWindow::GMainWindow() connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode())); connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog())); - // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views before the CPU continues - connect(&render_window->GetEmuThread(), SIGNAL(DebugModeEntered()), disasmWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection); - connect(&render_window->GetEmuThread(), SIGNAL(DebugModeEntered()), registersWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection); - connect(&render_window->GetEmuThread(), SIGNAL(DebugModeEntered()), callstackWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection); - - connect(&render_window->GetEmuThread(), SIGNAL(DebugModeLeft()), disasmWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection); - connect(&render_window->GetEmuThread(), SIGNAL(DebugModeLeft()), registersWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection); - connect(&render_window->GetEmuThread(), SIGNAL(DebugModeLeft()), callstackWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection); + connect(this, SIGNAL(EmulationStarting(EmuThread*)), disasmWidget, SLOT(OnEmulationStarting(EmuThread*))); + connect(this, SIGNAL(EmulationStopping()), disasmWidget, SLOT(OnEmulationStopping())); + connect(this, SIGNAL(EmulationStarting(EmuThread*)), registersWidget, SLOT(OnEmulationStarting(EmuThread*))); + connect(this, SIGNAL(EmulationStopping()), registersWidget, SLOT(OnEmulationStopping())); + connect(this, SIGNAL(EmulationStarting(EmuThread*)), render_window, SLOT(OnEmulationStarting(EmuThread*))); + connect(this, SIGNAL(EmulationStopping()), render_window, SLOT(OnEmulationStopping())); // Setup hotkeys RegisterHotkey("Main Window", "Load File", QKeySequence::Open); @@ -196,32 +195,76 @@ void GMainWindow::OnDisplayTitleBars(bool show) } } -void GMainWindow::BootGame(std::string filename) -{ +void GMainWindow::BootGame(std::string filename) { LOG_INFO(Frontend, "Citra starting...\n"); + + // Initialize the core emulation System::Init(render_window); - // Load a game or die... + // Load the game if (Loader::ResultStatus::Success != Loader::LoadFile(filename)) { LOG_CRITICAL(Frontend, "Failed to load ROM!"); + System::Shutdown(); + return; } - disasmWidget->Init(); + // Create and start the emulation thread + emu_thread = Common::make_unique<EmuThread>(render_window); + emit EmulationStarting(emu_thread.get()); + emu_thread->start(); + + // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views before the CPU continues + connect(emu_thread.get(), SIGNAL(DebugModeEntered()), disasmWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection); + connect(emu_thread.get(), SIGNAL(DebugModeEntered()), registersWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection); + connect(emu_thread.get(), SIGNAL(DebugModeEntered()), callstackWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection); + connect(emu_thread.get(), SIGNAL(DebugModeLeft()), disasmWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection); + connect(emu_thread.get(), SIGNAL(DebugModeLeft()), registersWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection); + connect(emu_thread.get(), SIGNAL(DebugModeLeft()), callstackWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection); + + // Update the GUI registersWidget->OnDebugModeEntered(); callstackWidget->OnDebugModeEntered(); - - render_window->GetEmuThread().SetFilename(filename); - render_window->GetEmuThread().start(); - render_window->show(); + OnStartGame(); } +void GMainWindow::ShutdownGame() { + emu_thread->RequestStop(); + + // Release emu threads from any breakpoints + // This belongs after RequestStop() and before wait() because if emulation stops on a GPU + // breakpoint after (or before) RequestStop() is called, the emulation would never be able + // to continue out to the main loop and terminate. Thus wait() would hang forever. + // TODO(bunnei): This function is not thread safe, but it's being used as if it were + Pica::g_debug_context->ClearBreakpoints(); + + emit EmulationStopping(); + + // Wait for emulation thread to complete and delete it + emu_thread->wait(); + emu_thread = nullptr; + + // Shutdown the core emulation + System::Shutdown(); + + // Update the GUI + ui.action_Start->setEnabled(false); + ui.action_Pause->setEnabled(false); + ui.action_Stop->setEnabled(false); + render_window->hide(); +} + void GMainWindow::OnMenuLoadFile() { QString filename = QFileDialog::getOpenFileName(this, tr("Load File"), QString(), tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.bin *.cci *.cxi)")); - if (filename.size()) - BootGame(filename.toLatin1().data()); + if (filename.size()) { + // Shutdown previous session if the emu thread is still active... + if (emu_thread != nullptr) + ShutdownGame(); + + BootGame(filename.toLatin1().data()); + } } void GMainWindow::OnMenuLoadSymbolMap() { @@ -232,7 +275,7 @@ void GMainWindow::OnMenuLoadSymbolMap() { void GMainWindow::OnStartGame() { - render_window->GetEmuThread().SetCpuRunning(true); + emu_thread->SetRunning(true); ui.action_Start->setEnabled(false); ui.action_Pause->setEnabled(true); @@ -241,21 +284,15 @@ void GMainWindow::OnStartGame() void GMainWindow::OnPauseGame() { - render_window->GetEmuThread().SetCpuRunning(false); + emu_thread->SetRunning(false); ui.action_Start->setEnabled(true); ui.action_Pause->setEnabled(false); ui.action_Stop->setEnabled(true); } -void GMainWindow::OnStopGame() -{ - render_window->GetEmuThread().SetCpuRunning(false); - // TODO: Shutdown core - - ui.action_Start->setEnabled(true); - ui.action_Pause->setEnabled(false); - ui.action_Stop->setEnabled(false); +void GMainWindow::OnStopGame() { + ShutdownGame(); } void GMainWindow::OnOpenHotkeysDialog() @@ -265,24 +302,22 @@ void GMainWindow::OnOpenHotkeysDialog() } -void GMainWindow::ToggleWindowMode() -{ - bool enable = ui.action_Single_Window_Mode->isChecked(); - if (!enable && render_window->parent() != nullptr) - { - ui.horizontalLayout->removeWidget(render_window); - render_window->setParent(nullptr); - render_window->setVisible(true); - render_window->RestoreGeometry(); - render_window->setFocusPolicy(Qt::NoFocus); - } - else if (enable && render_window->parent() == nullptr) - { +void GMainWindow::ToggleWindowMode() { + if (ui.action_Single_Window_Mode->isChecked()) { + // Render in the main window... render_window->BackupGeometry(); ui.horizontalLayout->addWidget(render_window); render_window->setVisible(true); render_window->setFocusPolicy(Qt::ClickFocus); render_window->setFocus(); + + } else { + // Render in a separate window... + ui.horizontalLayout->removeWidget(render_window); + render_window->setParent(nullptr); + render_window->setVisible(true); + render_window->RestoreGeometry(); + render_window->setFocusPolicy(Qt::NoFocus); } } @@ -303,6 +338,8 @@ void GMainWindow::closeEvent(QCloseEvent* event) settings.setValue("firstStart", false); SaveHotkeys(settings); + ShutdownGame(); + render_window->close(); QWidget::closeEvent(event); diff --git a/src/citra_qt/main.h b/src/citra_qt/main.h index 9b57c5772..3e29534fb 100644 --- a/src/citra_qt/main.h +++ b/src/citra_qt/main.h @@ -5,12 +5,14 @@ #ifndef _CITRA_QT_MAIN_HXX_ #define _CITRA_QT_MAIN_HXX_ +#include <memory> #include <QMainWindow> #include "ui_main.h" class GImageInfo; class GRenderWindow; +class EmuThread; class ProfilerWidget; class DisassemblerWidget; class RegistersWidget; @@ -34,8 +36,27 @@ public: GMainWindow(); ~GMainWindow(); +signals: + + /** + * Signal that is emitted when a new EmuThread has been created and an emulation session is + * about to start. At this time, the core system emulation has been initialized, and all + * emulation handles and memory should be valid. + * + * @param emu_thread Pointer to the newly created EmuThread (to be used by widgets that need to + * access/change emulation state). + */ + void EmulationStarting(EmuThread* emu_thread); + + /** + * Signal that is emitted when emulation is about to stop. At this time, the EmuThread and core + * system emulation handles and memory are still valid, but are about become invalid. + */ + void EmulationStopping(); + private: void BootGame(std::string filename); + void ShutdownGame(); void closeEvent(QCloseEvent* event) override; @@ -55,6 +76,8 @@ private: GRenderWindow* render_window; + std::unique_ptr<EmuThread> emu_thread; + ProfilerWidget* profilerWidget; DisassemblerWidget* disasmWidget; RegistersWidget* registersWidget; diff --git a/src/citra_qt/main.ui b/src/citra_qt/main.ui index a3752ac1e..689806465 100644 --- a/src/citra_qt/main.ui +++ b/src/citra_qt/main.ui @@ -90,6 +90,9 @@ </property> </action> <action name="action_Start"> + <property name="enabled"> + <bool>false</bool> + </property> <property name="text"> <string>&Start</string> </property> diff --git a/src/citra_qt/util/spinbox.cpp b/src/citra_qt/util/spinbox.cpp index 2e2076a27..de4060116 100644 --- a/src/citra_qt/util/spinbox.cpp +++ b/src/citra_qt/util/spinbox.cpp @@ -29,6 +29,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include <cstdlib> #include <QLineEdit> #include <QRegExpValidator> @@ -206,7 +207,7 @@ QString CSpinBox::TextFromValue() { return prefix + QString(HasSign() ? ((value < 0) ? "-" : "+") : "") - + QString("%1").arg(abs(value), num_digits, base, QLatin1Char('0')).toUpper() + + QString("%1").arg(std::abs(value), num_digits, base, QLatin1Char('0')).toUpper() + suffix; } |