summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2016-11-04 23:14:38 -0400
committerGravatar bunnei2016-12-21 23:29:04 -0500
commit198b6c9bdd58d76303f75a8577303907967a143e (patch)
tree0dba084e5c3418f917880fd4fc1efad37f127e9b /src
parentloader: Remove duplicate docstrings. (diff)
downloadyuzu-198b6c9bdd58d76303f75a8577303907967a143e.tar.gz
yuzu-198b6c9bdd58d76303f75a8577303907967a143e.tar.xz
yuzu-198b6c9bdd58d76303f75a8577303907967a143e.zip
core: Consolidate top-level system state into a singleton.
Diffstat (limited to 'src')
-rw-r--r--src/citra/citra.cpp35
-rw-r--r--src/citra_qt/bootmanager.cpp2
-rw-r--r--src/citra_qt/configure_general.cpp2
-rw-r--r--src/citra_qt/configure_graphics.cpp2
-rw-r--r--src/citra_qt/configure_system.cpp2
-rw-r--r--src/citra_qt/main.cpp81
-rw-r--r--src/core/system.cpp61
-rw-r--r--src/core/system.h82
8 files changed, 164 insertions, 103 deletions
diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index 3114a71db..5e2829b54 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -64,7 +64,7 @@ int main(int argc, char** argv) {
64 return -1; 64 return -1;
65 } 65 }
66#endif 66#endif
67 std::string boot_filename; 67 std::string filepath;
68 68
69 static struct option long_options[] = { 69 static struct option long_options[] = {
70 {"gdbport", required_argument, 0, 'g'}, 70 {"gdbport", required_argument, 0, 'g'},
@@ -97,9 +97,9 @@ int main(int argc, char** argv) {
97 } 97 }
98 } else { 98 } else {
99#ifdef _WIN32 99#ifdef _WIN32
100 boot_filename = Common::UTF16ToUTF8(argv_w[optind]); 100 filepath = Common::UTF16ToUTF8(argv_w[optind]);
101#else 101#else
102 boot_filename = argv[optind]; 102 filepath = argv[optind];
103#endif 103#endif
104 optind++; 104 optind++;
105 } 105 }
@@ -115,7 +115,7 @@ int main(int argc, char** argv) {
115 MicroProfileOnThreadCreate("EmuThread"); 115 MicroProfileOnThreadCreate("EmuThread");
116 SCOPE_EXIT({ MicroProfileShutdown(); }); 116 SCOPE_EXIT({ MicroProfileShutdown(); });
117 117
118 if (boot_filename.empty()) { 118 if (filepath.empty()) {
119 LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified"); 119 LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
120 return -1; 120 return -1;
121 } 121 }
@@ -127,27 +127,20 @@ int main(int argc, char** argv) {
127 Settings::values.use_gdbstub = use_gdbstub; 127 Settings::values.use_gdbstub = use_gdbstub;
128 Settings::Apply(); 128 Settings::Apply();
129 129
130 std::unique_ptr<EmuWindow_SDL2> emu_window = std::make_unique<EmuWindow_SDL2>(); 130 std::unique_ptr<EmuWindow_SDL2> emu_window{ std::make_unique<EmuWindow_SDL2>() };
131 131
132 std::unique_ptr<Loader::AppLoader> loader = Loader::GetLoader(boot_filename); 132 Core::System& system{ Core::System::GetInstance() };
133 if (!loader) {
134 LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", boot_filename.c_str());
135 return -1;
136 }
137
138 boost::optional<u32> system_mode = loader->LoadKernelSystemMode();
139 133
140 if (!system_mode) { 134 SCOPE_EXIT({ system.Shutdown(); });
141 LOG_CRITICAL(Frontend, "Failed to load ROM (Could not determine system mode)!");
142 return -1;
143 }
144 135
145 System::Init(emu_window.get(), system_mode.get()); 136 const Core::System::ResultStatus load_result{ system.Load(emu_window.get(), filepath) };
146 SCOPE_EXIT({ System::Shutdown(); });
147 137
148 Loader::ResultStatus load_result = loader->Load(); 138 switch (load_result) {
149 if (Loader::ResultStatus::Success != load_result) { 139 case Core::System::ResultStatus::ErrorGetLoader:
150 LOG_CRITICAL(Frontend, "Failed to load ROM (Error %i)!", load_result); 140 LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filepath.c_str());
141 return -1;
142 case Core::System::ResultStatus::ErrorLoader:
143 LOG_CRITICAL(Frontend, "Failed to load ROM!");
151 return -1; 144 return -1;
152 } 145 }
153 146
diff --git a/src/citra_qt/bootmanager.cpp b/src/citra_qt/bootmanager.cpp
index c7eb2aafc..5e8ae3066 100644
--- a/src/citra_qt/bootmanager.cpp
+++ b/src/citra_qt/bootmanager.cpp
@@ -60,7 +60,7 @@ void EmuThread::run() {
60 } 60 }
61 61
62 // Shutdown the core emulation 62 // Shutdown the core emulation
63 System::Shutdown(); 63 Core::System::GetInstance().Shutdown();
64 64
65#if MICROPROFILE_ENABLED 65#if MICROPROFILE_ENABLED
66 MicroProfileOnThreadExit(); 66 MicroProfileOnThreadExit();
diff --git a/src/citra_qt/configure_general.cpp b/src/citra_qt/configure_general.cpp
index 27139fb30..f576f6f7a 100644
--- a/src/citra_qt/configure_general.cpp
+++ b/src/citra_qt/configure_general.cpp
@@ -14,7 +14,7 @@ ConfigureGeneral::ConfigureGeneral(QWidget* parent)
14 ui->setupUi(this); 14 ui->setupUi(this);
15 this->setConfiguration(); 15 this->setConfiguration();
16 16
17 ui->toggle_cpu_jit->setEnabled(!System::IsPoweredOn()); 17 ui->toggle_cpu_jit->setEnabled(!Core::System::GetInstance().IsPoweredOn());
18} 18}
19 19
20ConfigureGeneral::~ConfigureGeneral() {} 20ConfigureGeneral::~ConfigureGeneral() {}
diff --git a/src/citra_qt/configure_graphics.cpp b/src/citra_qt/configure_graphics.cpp
index 36f10c8d7..1e6f7f880 100644
--- a/src/citra_qt/configure_graphics.cpp
+++ b/src/citra_qt/configure_graphics.cpp
@@ -13,7 +13,7 @@ ConfigureGraphics::ConfigureGraphics(QWidget* parent)
13 ui->setupUi(this); 13 ui->setupUi(this);
14 this->setConfiguration(); 14 this->setConfiguration();
15 15
16 ui->toggle_vsync->setEnabled(!System::IsPoweredOn()); 16 ui->toggle_vsync->setEnabled(!Core::System::GetInstance().IsPoweredOn());
17} 17}
18 18
19ConfigureGraphics::~ConfigureGraphics() {} 19ConfigureGraphics::~ConfigureGraphics() {}
diff --git a/src/citra_qt/configure_system.cpp b/src/citra_qt/configure_system.cpp
index 873d314ec..545261c01 100644
--- a/src/citra_qt/configure_system.cpp
+++ b/src/citra_qt/configure_system.cpp
@@ -24,7 +24,7 @@ ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::
24ConfigureSystem::~ConfigureSystem() {} 24ConfigureSystem::~ConfigureSystem() {}
25 25
26void ConfigureSystem::setConfiguration() { 26void ConfigureSystem::setConfiguration() {
27 enabled = !System::IsPoweredOn(); 27 enabled = !Core::System::GetInstance().IsPoweredOn();
28 28
29 if (!enabled) { 29 if (!enabled) {
30 ReadSystemSettings(); 30 ReadSystemSettings();
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index e16d3196c..0c7723b0a 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -274,7 +274,7 @@ void GMainWindow::OnDisplayTitleBars(bool show) {
274 } 274 }
275} 275}
276 276
277bool GMainWindow::InitializeSystem(u32 system_mode) { 277bool GMainWindow::LoadROM(const std::string& filename) {
278 // Shutdown previous session if the emu thread is still active... 278 // Shutdown previous session if the emu thread is still active...
279 if (emu_thread != nullptr) 279 if (emu_thread != nullptr)
280 ShutdownGame(); 280 ShutdownGame();
@@ -284,79 +284,50 @@ bool GMainWindow::InitializeSystem(u32 system_mode) {
284 284
285 if (!gladLoadGL()) { 285 if (!gladLoadGL()) {
286 QMessageBox::critical(this, tr("Error while starting Citra!"), 286 QMessageBox::critical(this, tr("Error while starting Citra!"),
287 tr("Failed to initialize the video core!\n\n" 287 tr("Failed to initialize the video core!\n\n"
288 "Please ensure that your GPU supports OpenGL 3.3 and that you " 288 "Please ensure that your GPU supports OpenGL 3.3 and that you "
289 "have the latest graphics driver.")); 289 "have the latest graphics driver."));
290 return false; 290 return false;
291 } 291 }
292 292
293 // Initialize the core emulation 293 Core::System& system{ Core::System::GetInstance() };
294 System::Result system_result = System::Init(render_window, system_mode);
295 if (System::Result::Success != system_result) {
296 switch (system_result) {
297 case System::Result::ErrorInitVideoCore:
298 QMessageBox::critical(this, tr("Error while starting Citra!"),
299 tr("Failed to initialize the video core!\n\n"
300 "Please ensure that your GPU supports OpenGL 3.3 and that you "
301 "have the latest graphics driver."));
302 break;
303 294
304 default: 295 const Core::System::ResultStatus result{ system.Load(render_window, filename) };
305 QMessageBox::critical(this, tr("Error while starting Citra!"),
306 tr("Unknown error (please check the log)!"));
307 break;
308 }
309 return false;
310 }
311 return true;
312}
313 296
314bool GMainWindow::LoadROM(const std::string& filename) { 297 if (result != Core::System::ResultStatus::Success) {
315 std::unique_ptr<Loader::AppLoader> app_loader = Loader::GetLoader(filename); 298 switch (result) {
316 if (!app_loader) { 299 case Core::System::ResultStatus::ErrorGetLoader:
317 LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filename.c_str()); 300 LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filename.c_str());
318 QMessageBox::critical(this, tr("Error while loading ROM!"), 301 QMessageBox::critical(this, tr("Error while loading ROM!"),
319 tr("The ROM format is not supported.")); 302 tr("The ROM format is not supported."));
320 return false; 303 break;
321 }
322
323 boost::optional<u32> system_mode = app_loader->LoadKernelSystemMode();
324 if (!system_mode) {
325 LOG_CRITICAL(Frontend, "Failed to load ROM!");
326 QMessageBox::critical(this, tr("Error while loading ROM!"),
327 tr("Could not determine the system mode."));
328 return false;
329 }
330
331 if (!InitializeSystem(system_mode.get()))
332 return false;
333 304
334 Loader::ResultStatus result = app_loader->Load(); 305 case Core::System::ResultStatus::ErrorSystemMode:
335 if (Loader::ResultStatus::Success != result) { 306 LOG_CRITICAL(Frontend, "Failed to load ROM!");
336 System::Shutdown(); 307 QMessageBox::critical(this, tr("Error while loading ROM!"),
337 LOG_CRITICAL(Frontend, "Failed to load ROM!"); 308 tr("Could not determine the system mode."));
309 break;
338 310
339 switch (result) { 311 case Core::System::ResultStatus::ErrorLoader_ErrorEncrypted:
340 case Loader::ResultStatus::ErrorEncrypted: { 312 {
341 // Build the MessageBox ourselves to have clickable link 313 // Build the MessageBox ourselves to have clickable link
342 QMessageBox popup_error; 314 QMessageBox popup_error;
343 popup_error.setTextFormat(Qt::RichText); 315 popup_error.setTextFormat(Qt::RichText);
344 popup_error.setWindowTitle(tr("Error while loading ROM!")); 316 popup_error.setWindowTitle(tr("Error while loading ROM!"));
345 popup_error.setText( 317 popup_error.setText(
346 tr("The game that you are trying to load must be decrypted before being used with " 318 tr("The game that you are trying to load must be decrypted before being used with "
347 "Citra.<br/><br/>" 319 "Citra.<br/><br/>"
348 "For more information on dumping and decrypting games, please see: <a " 320 "For more information on dumping and decrypting games, please see: <a "
349 "href='https://citra-emu.org/wiki/Dumping-Game-Cartridges'>https://" 321 "href='https://citra-emu.org/wiki/Dumping-Game-Cartridges'>https://"
350 "citra-emu.org/wiki/Dumping-Game-Cartridges</a>")); 322 "citra-emu.org/wiki/Dumping-Game-Cartridges</a>"));
351 popup_error.setIcon(QMessageBox::Critical); 323 popup_error.setIcon(QMessageBox::Critical);
352 popup_error.exec(); 324 popup_error.exec();
353 break; 325 break;
354 } 326 }
355 case Loader::ResultStatus::ErrorInvalidFormat: 327 case Core::System::ResultStatus::ErrorLoader_ErrorInvalidFormat:
356 QMessageBox::critical(this, tr("Error while loading ROM!"), 328 QMessageBox::critical(this, tr("Error while loading ROM!"),
357 tr("The ROM format is not supported.")); 329 tr("The ROM format is not supported."));
358 break; 330 break;
359 case Loader::ResultStatus::Error:
360 331
361 default: 332 default:
362 QMessageBox::critical(this, tr("Error while loading ROM!"), tr("Unknown error!")); 333 QMessageBox::critical(this, tr("Error while loading ROM!"), tr("Unknown error!"));
diff --git a/src/core/system.cpp b/src/core/system.cpp
index a5f763805..fa8bd0317 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -13,33 +13,30 @@
13#include "core/system.h" 13#include "core/system.h"
14#include "video_core/video_core.h" 14#include "video_core/video_core.h"
15 15
16namespace System { 16namespace Core {
17 17
18static bool is_powered_on{false}; 18/*static*/ System System::s_instance;
19 19
20Result Init(EmuWindow* emu_window, u32 system_mode) { 20System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
21 Core::Init(); 21 Core::Init();
22 CoreTiming::Init(); 22 CoreTiming::Init();
23 Memory::Init(); 23 Memory::Init();
24 HW::Init(); 24 HW::Init();
25 Kernel::Init(system_mode); 25 Kernel::Init(system_mode);
26 HLE::Init(); 26 HLE::Init();
27 if (!VideoCore::Init(emu_window)) {
28 return Result::ErrorInitVideoCore;
29 }
30 AudioCore::Init(); 27 AudioCore::Init();
31 GDBStub::Init(); 28 GDBStub::Init();
32 29
33 is_powered_on = true; 30 if (!VideoCore::Init(emu_window)) {
31 return ResultStatus::ErrorVideoCore;
32 }
34 33
35 return Result::Success; 34 is_powered_on = true;
36}
37 35
38bool IsPoweredOn() { 36 return ResultStatus::Success;
39 return is_powered_on;
40} 37}
41 38
42void Shutdown() { 39void System::Shutdown() {
43 GDBStub::Shutdown(); 40 GDBStub::Shutdown();
44 AudioCore::Shutdown(); 41 AudioCore::Shutdown();
45 VideoCore::Shutdown(); 42 VideoCore::Shutdown();
@@ -52,4 +49,42 @@ void Shutdown() {
52 is_powered_on = false; 49 is_powered_on = false;
53} 50}
54 51
55} // namespace 52System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& filepath) {
53 state.app_loader = Loader::GetLoader(filepath);
54
55 if (!state.app_loader) {
56 LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filepath.c_str());
57 return ResultStatus::ErrorGetLoader;
58 }
59
60 boost::optional<u32> system_mode{ state.app_loader->LoadKernelSystemMode() };
61 if (!system_mode) {
62 LOG_CRITICAL(Frontend, "Failed to determine system mode!");
63 return ResultStatus::ErrorSystemMode;
64 }
65
66 ResultStatus init_result{ Init(emu_window, system_mode.get()) };
67 if (init_result != ResultStatus::Success) {
68 LOG_CRITICAL(Frontend, "Failed to initialize system (Error %i)!", init_result);
69 System::Shutdown();
70 return init_result;
71 }
72
73 const Loader::ResultStatus load_result{ state.app_loader->Load() };
74 if (Loader::ResultStatus::Success != load_result) {
75 LOG_CRITICAL(Frontend, "Failed to load ROM (Error %i)!", load_result);
76 System::Shutdown();
77
78 switch (load_result) {
79 case Loader::ResultStatus::ErrorEncrypted:
80 return ResultStatus::ErrorLoader_ErrorEncrypted;
81 case Loader::ResultStatus::ErrorInvalidFormat:
82 return ResultStatus::ErrorLoader_ErrorInvalidFormat;
83 default:
84 return ResultStatus::ErrorLoader;
85 }
86 }
87 return ResultStatus::Success;
88}
89
90} // namespace Core
diff --git a/src/core/system.h b/src/core/system.h
index b41fc088a..192a9c447 100644
--- a/src/core/system.h
+++ b/src/core/system.h
@@ -4,18 +4,80 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <string>
8
9#include "core/loader/loader.h"
10
7class EmuWindow; 11class EmuWindow;
8 12
9namespace System { 13namespace Core {
14
15class System {
16public:
17 struct State {
18 std::unique_ptr<Loader::AppLoader> app_loader;
19 };
20
21 /**
22 * Gets the instance of the System singleton class.
23 * @returns Reference to the instance of the System singleton class.
24 */
25 static System& GetInstance() {
26 return s_instance;
27 }
28
29 /// Enumeration representing the return values of the System Initialize and Load process.
30 enum class ResultStatus : u32 {
31 Success, ///< Succeeded
32 ErrorGetLoader, ///< Error finding the correct application loader
33 ErrorSystemMode, ///< Error determining the system mode
34 ErrorLoader, ///< Error loading the specified application
35 ErrorLoader_ErrorEncrypted, ///< Error loading the specified application due to encryption
36 ErrorLoader_ErrorInvalidFormat, ///< Error loading the specified application due to an invalid format
37 ErrorVideoCore, ///< Error in the video core
38 };
39
40 /**
41 * Initialize the emulated system.
42 * @param emu_window Pointer to the host-system window used for video output and keyboard input.
43 * @param system_mode The system mode.
44 * @returns ResultStatus code, indicating if the operation succeeded.
45 */
46 ResultStatus Init(EmuWindow* emu_window, u32 system_mode);
47
48 /// Shutdown the emulated system.
49 void Shutdown();
50
51 /**
52 * Load an executable application.
53 * @param emu_window Pointer to the host-system window used for video output and keyboard input.
54 * @param filepath String path to the executable application to load on the host file system.
55 * @returns ResultStatus code, indicating if the operation succeeded.
56 */
57 ResultStatus Load(EmuWindow* emu_window, const std::string& filepath);
58
59 /**
60 * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
61 * application).
62 * @returns True if the emulated system is powered on, otherwise false.
63 */
64 bool IsPoweredOn() const {
65 return is_powered_on;
66 }
67
68 /**
69 * Gets the internal state of the emulated system.
70 * @returns The internal state of the emulated system
71 */
72 State& GetState() {
73 return state;
74 }
75
76private:
77 bool is_powered_on{};
78 State state;
10 79
11enum class Result { 80 static System s_instance;
12 Success, ///< Everything is fine
13 Error, ///< Something went wrong (no module specified)
14 ErrorInitCore, ///< Something went wrong during core init
15 ErrorInitVideoCore, ///< Something went wrong during video core init
16}; 81};
17 82
18Result Init(EmuWindow* emu_window, u32 system_mode); 83} // namespace Core
19bool IsPoweredOn();
20void Shutdown();
21}