core.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "core/arm/dynarmic/arm_dynarmic.h"
  8. #include "core/arm/unicorn/arm_unicorn.h"
  9. #include "core/core.h"
  10. #include "core/core_timing.h"
  11. #include "core/gdbstub/gdbstub.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/kernel/process.h"
  14. #include "core/hle/kernel/thread.h"
  15. #include "core/hle/service/service.h"
  16. #include "core/hw/hw.h"
  17. #include "core/loader/loader.h"
  18. #include "core/memory_setup.h"
  19. #include "core/settings.h"
  20. #include "video_core/video_core.h"
  21. namespace Core {
  22. /*static*/ System System::s_instance;
  23. System::ResultStatus System::RunLoop(int tight_loop) {
  24. status = ResultStatus::Success;
  25. if (!cpu_core) {
  26. return ResultStatus::ErrorNotInitialized;
  27. }
  28. if (GDBStub::IsServerEnabled()) {
  29. GDBStub::HandlePacket();
  30. // If the loop is halted and we want to step, use a tiny (1) number of instructions to
  31. // execute. Otherwise, get out of the loop function.
  32. if (GDBStub::GetCpuHaltFlag()) {
  33. if (GDBStub::GetCpuStepFlag()) {
  34. GDBStub::SetCpuStepFlag(false);
  35. tight_loop = 1;
  36. } else {
  37. return ResultStatus::Success;
  38. }
  39. }
  40. }
  41. // If we don't have a currently active thread then don't execute instructions,
  42. // instead advance to the next event and try to yield to the next thread
  43. if (Kernel::GetCurrentThread() == nullptr) {
  44. LOG_TRACE(Core_ARM, "Idling");
  45. CoreTiming::Idle();
  46. CoreTiming::Advance();
  47. PrepareReschedule();
  48. } else {
  49. CoreTiming::Advance();
  50. cpu_core->Run(tight_loop);
  51. }
  52. HW::Update();
  53. Reschedule();
  54. return status;
  55. }
  56. System::ResultStatus System::SingleStep() {
  57. return RunLoop(1);
  58. }
  59. System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& filepath) {
  60. app_loader = Loader::GetLoader(filepath);
  61. if (!app_loader) {
  62. LOG_CRITICAL(Core, "Failed to obtain loader for %s!", filepath.c_str());
  63. return ResultStatus::ErrorGetLoader;
  64. }
  65. std::pair<boost::optional<u32>, Loader::ResultStatus> system_mode =
  66. app_loader->LoadKernelSystemMode();
  67. if (system_mode.second != Loader::ResultStatus::Success) {
  68. LOG_CRITICAL(Core, "Failed to determine system mode (Error %i)!",
  69. static_cast<int>(system_mode.second));
  70. System::Shutdown();
  71. switch (system_mode.second) {
  72. case Loader::ResultStatus::ErrorEncrypted:
  73. return ResultStatus::ErrorLoader_ErrorEncrypted;
  74. case Loader::ResultStatus::ErrorInvalidFormat:
  75. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  76. default:
  77. return ResultStatus::ErrorSystemMode;
  78. }
  79. }
  80. ResultStatus init_result{Init(emu_window, system_mode.first.get())};
  81. if (init_result != ResultStatus::Success) {
  82. LOG_CRITICAL(Core, "Failed to initialize system (Error %i)!", init_result);
  83. System::Shutdown();
  84. return init_result;
  85. }
  86. const Loader::ResultStatus load_result{app_loader->Load(Kernel::g_current_process)};
  87. if (Loader::ResultStatus::Success != load_result) {
  88. LOG_CRITICAL(Core, "Failed to load ROM (Error %i)!", load_result);
  89. System::Shutdown();
  90. switch (load_result) {
  91. case Loader::ResultStatus::ErrorEncrypted:
  92. return ResultStatus::ErrorLoader_ErrorEncrypted;
  93. case Loader::ResultStatus::ErrorInvalidFormat:
  94. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  95. default:
  96. return ResultStatus::ErrorLoader;
  97. }
  98. }
  99. status = ResultStatus::Success;
  100. return status;
  101. }
  102. void System::PrepareReschedule() {
  103. cpu_core->PrepareReschedule();
  104. reschedule_pending = true;
  105. }
  106. PerfStats::Results System::GetAndResetPerfStats() {
  107. return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());
  108. }
  109. void System::Reschedule() {
  110. if (!reschedule_pending) {
  111. return;
  112. }
  113. reschedule_pending = false;
  114. Kernel::Reschedule();
  115. }
  116. System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
  117. LOG_DEBUG(HW_Memory, "initialized OK");
  118. switch (Settings::values.cpu_core) {
  119. case Settings::CpuCore::Unicorn:
  120. cpu_core = std::make_unique<ARM_Unicorn>();
  121. break;
  122. case Settings::CpuCore::Dynarmic:
  123. default:
  124. cpu_core = std::make_unique<ARM_Dynarmic>();
  125. break;
  126. }
  127. telemetry_session = std::make_unique<Core::TelemetrySession>();
  128. CoreTiming::Init();
  129. HW::Init();
  130. Kernel::Init(system_mode);
  131. Service::Init();
  132. GDBStub::Init();
  133. if (!VideoCore::Init(emu_window)) {
  134. return ResultStatus::ErrorVideoCore;
  135. }
  136. LOG_DEBUG(Core, "Initialized OK");
  137. // Reset counters and set time origin to current frame
  138. GetAndResetPerfStats();
  139. perf_stats.BeginSystemFrame();
  140. return ResultStatus::Success;
  141. }
  142. void System::Shutdown() {
  143. // Log last frame performance stats
  144. auto perf_results = GetAndResetPerfStats();
  145. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_EmulationSpeed",
  146. perf_results.emulation_speed * 100.0);
  147. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Framerate",
  148. perf_results.game_fps);
  149. Telemetry().AddField(Telemetry::FieldType::Performance, "Shutdown_Frametime",
  150. perf_results.frametime * 1000.0);
  151. // Shutdown emulation session
  152. GDBStub::Shutdown();
  153. VideoCore::Shutdown();
  154. Service::Shutdown();
  155. Kernel::Shutdown();
  156. HW::Shutdown();
  157. CoreTiming::Shutdown();
  158. cpu_core = nullptr;
  159. app_loader = nullptr;
  160. telemetry_session = nullptr;
  161. LOG_DEBUG(Core, "Shutdown OK");
  162. }
  163. } // namespace Core