core.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <utility>
  6. #include "common/logging/log.h"
  7. #ifdef ARCHITECTURE_x86_64
  8. #include "core/arm/dynarmic/arm_dynarmic.h"
  9. #endif
  10. #include "core/arm/unicorn/arm_unicorn.h"
  11. #include "core/core.h"
  12. #include "core/core_timing.h"
  13. #include "core/gdbstub/gdbstub.h"
  14. #include "core/hle/kernel/client_port.h"
  15. #include "core/hle/kernel/kernel.h"
  16. #include "core/hle/kernel/process.h"
  17. #include "core/hle/kernel/thread.h"
  18. #include "core/hle/service/service.h"
  19. #include "core/hle/service/sm/controller.h"
  20. #include "core/hle/service/sm/sm.h"
  21. #include "core/hw/hw.h"
  22. #include "core/loader/loader.h"
  23. #include "core/memory_setup.h"
  24. #include "core/settings.h"
  25. #include "video_core/video_core.h"
  26. namespace Core {
  27. /*static*/ System System::s_instance;
  28. System::~System() = default;
  29. System::ResultStatus System::RunLoop(bool tight_loop) {
  30. status = ResultStatus::Success;
  31. if (!cpu_core) {
  32. return ResultStatus::ErrorNotInitialized;
  33. }
  34. if (GDBStub::IsServerEnabled()) {
  35. GDBStub::HandlePacket();
  36. // If the loop is halted and we want to step, use a tiny (1) number of instructions to
  37. // execute. Otherwise, get out of the loop function.
  38. if (GDBStub::GetCpuHaltFlag()) {
  39. if (GDBStub::GetCpuStepFlag()) {
  40. GDBStub::SetCpuStepFlag(false);
  41. tight_loop = false;
  42. } else {
  43. return ResultStatus::Success;
  44. }
  45. }
  46. }
  47. // If we don't have a currently active thread then don't execute instructions,
  48. // instead advance to the next event and try to yield to the next thread
  49. if (Kernel::GetCurrentThread() == nullptr) {
  50. NGLOG_TRACE(Core_ARM, "Idling");
  51. CoreTiming::Idle();
  52. CoreTiming::Advance();
  53. PrepareReschedule();
  54. } else {
  55. CoreTiming::Advance();
  56. if (tight_loop) {
  57. cpu_core->Run();
  58. } else {
  59. cpu_core->Step();
  60. }
  61. }
  62. HW::Update();
  63. Reschedule();
  64. return status;
  65. }
  66. System::ResultStatus System::SingleStep() {
  67. return RunLoop(false);
  68. }
  69. System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& filepath) {
  70. app_loader = Loader::GetLoader(filepath);
  71. if (!app_loader) {
  72. NGLOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath);
  73. return ResultStatus::ErrorGetLoader;
  74. }
  75. std::pair<boost::optional<u32>, Loader::ResultStatus> system_mode =
  76. app_loader->LoadKernelSystemMode();
  77. if (system_mode.second != Loader::ResultStatus::Success) {
  78. NGLOG_CRITICAL(Core, "Failed to determine system mode (Error {})!",
  79. static_cast<int>(system_mode.second));
  80. switch (system_mode.second) {
  81. case Loader::ResultStatus::ErrorEncrypted:
  82. return ResultStatus::ErrorLoader_ErrorEncrypted;
  83. case Loader::ResultStatus::ErrorInvalidFormat:
  84. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  85. case Loader::ResultStatus::ErrorUnsupportedArch:
  86. return ResultStatus::ErrorUnsupportedArch;
  87. default:
  88. return ResultStatus::ErrorSystemMode;
  89. }
  90. }
  91. ResultStatus init_result{Init(emu_window, system_mode.first.get())};
  92. if (init_result != ResultStatus::Success) {
  93. NGLOG_CRITICAL(Core, "Failed to initialize system (Error {})!",
  94. static_cast<int>(init_result));
  95. System::Shutdown();
  96. return init_result;
  97. }
  98. const Loader::ResultStatus load_result{app_loader->Load(current_process)};
  99. if (Loader::ResultStatus::Success != load_result) {
  100. NGLOG_CRITICAL(Core, "Failed to load ROM (Error {})!", static_cast<int>(load_result));
  101. System::Shutdown();
  102. switch (load_result) {
  103. case Loader::ResultStatus::ErrorEncrypted:
  104. return ResultStatus::ErrorLoader_ErrorEncrypted;
  105. case Loader::ResultStatus::ErrorInvalidFormat:
  106. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  107. case Loader::ResultStatus::ErrorUnsupportedArch:
  108. return ResultStatus::ErrorUnsupportedArch;
  109. default:
  110. return ResultStatus::ErrorLoader;
  111. }
  112. }
  113. status = ResultStatus::Success;
  114. return status;
  115. }
  116. void System::PrepareReschedule() {
  117. cpu_core->PrepareReschedule();
  118. reschedule_pending = true;
  119. }
  120. PerfStats::Results System::GetAndResetPerfStats() {
  121. return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());
  122. }
  123. void System::Reschedule() {
  124. if (!reschedule_pending) {
  125. return;
  126. }
  127. reschedule_pending = false;
  128. Core::System::GetInstance().Scheduler().Reschedule();
  129. }
  130. System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
  131. NGLOG_DEBUG(HW_Memory, "initialized OK");
  132. CoreTiming::Init();
  133. current_process = Kernel::Process::Create("main");
  134. if (Settings::values.use_cpu_jit) {
  135. #ifdef ARCHITECTURE_x86_64
  136. cpu_core = std::make_shared<ARM_Dynarmic>();
  137. #else
  138. cpu_core = std::make_shared<ARM_Unicorn>();
  139. NGLOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available");
  140. #endif
  141. } else {
  142. cpu_core = std::make_shared<ARM_Unicorn>();
  143. }
  144. gpu_core = std::make_unique<Tegra::GPU>();
  145. telemetry_session = std::make_unique<Core::TelemetrySession>();
  146. service_manager = std::make_shared<Service::SM::ServiceManager>();
  147. HW::Init();
  148. Kernel::Init(system_mode);
  149. scheduler = std::make_unique<Kernel::Scheduler>(cpu_core.get());
  150. Service::Init(service_manager);
  151. GDBStub::Init();
  152. if (!VideoCore::Init(emu_window)) {
  153. return ResultStatus::ErrorVideoCore;
  154. }
  155. NGLOG_DEBUG(Core, "Initialized OK");
  156. // Reset counters and set time origin to current frame
  157. GetAndResetPerfStats();
  158. perf_stats.BeginSystemFrame();
  159. return ResultStatus::Success;
  160. }
  161. void System::Shutdown() {
  162. // Log last frame performance stats
  163. auto perf_results = GetAndResetPerfStats();
  164. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_EmulationSpeed",
  165. perf_results.emulation_speed * 100.0);
  166. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Framerate",
  167. perf_results.game_fps);
  168. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Frametime",
  169. perf_results.frametime * 1000.0);
  170. // Shutdown emulation session
  171. VideoCore::Shutdown();
  172. GDBStub::Shutdown();
  173. Service::Shutdown();
  174. scheduler.reset();
  175. Kernel::Shutdown();
  176. HW::Shutdown();
  177. service_manager.reset();
  178. telemetry_session.reset();
  179. gpu_core.reset();
  180. cpu_core.reset();
  181. CoreTiming::Shutdown();
  182. app_loader.reset();
  183. NGLOG_DEBUG(Core, "Shutdown OK");
  184. }
  185. Service::SM::ServiceManager& System::ServiceManager() {
  186. return *service_manager;
  187. }
  188. const Service::SM::ServiceManager& System::ServiceManager() const {
  189. return *service_manager;
  190. }
  191. } // namespace Core