core.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "audio_core/audio_core.h"
  7. #include "common/logging/log.h"
  8. #include "core/arm/arm_interface.h"
  9. #include "core/arm/dynarmic/arm_dynarmic.h"
  10. #include "core/arm/dyncom/arm_dyncom.h"
  11. #include "core/arm/unicorn/arm_unicorn.h"
  12. #include "core/core.h"
  13. #include "core/core_timing.h"
  14. #include "core/gdbstub/gdbstub.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/hw/hw.h"
  20. #include "core/loader/loader.h"
  21. #include "core/memory_setup.h"
  22. #include "core/settings.h"
  23. #include "network/network.h"
  24. #include "video_core/video_core.h"
  25. namespace Core {
  26. /*static*/ System System::s_instance;
  27. System::ResultStatus System::RunLoop(int tight_loop) {
  28. status = ResultStatus::Success;
  29. if (!cpu_core) {
  30. return ResultStatus::ErrorNotInitialized;
  31. }
  32. if (GDBStub::IsServerEnabled()) {
  33. GDBStub::HandlePacket();
  34. // If the loop is halted and we want to step, use a tiny (1) number of instructions to
  35. // execute. Otherwise, get out of the loop function.
  36. if (GDBStub::GetCpuHaltFlag()) {
  37. if (GDBStub::GetCpuStepFlag()) {
  38. GDBStub::SetCpuStepFlag(false);
  39. tight_loop = 1;
  40. } else {
  41. return ResultStatus::Success;
  42. }
  43. }
  44. }
  45. // If we don't have a currently active thread then don't execute instructions,
  46. // instead advance to the next event and try to yield to the next thread
  47. if (Kernel::GetCurrentThread() == nullptr) {
  48. LOG_TRACE(Core_ARM, "Idling");
  49. CoreTiming::Idle();
  50. CoreTiming::Advance();
  51. PrepareReschedule();
  52. } else {
  53. cpu_core->Run(tight_loop);
  54. }
  55. HW::Update();
  56. Reschedule();
  57. return status;
  58. }
  59. System::ResultStatus System::SingleStep() {
  60. return RunLoop(1);
  61. }
  62. System::ResultStatus System::Load(EmuWindow* emu_window, const std::string& filepath) {
  63. app_loader = Loader::GetLoader(filepath);
  64. if (!app_loader) {
  65. LOG_CRITICAL(Core, "Failed to obtain loader for %s!", filepath.c_str());
  66. return ResultStatus::ErrorGetLoader;
  67. }
  68. std::pair<boost::optional<u32>, Loader::ResultStatus> system_mode =
  69. app_loader->LoadKernelSystemMode();
  70. if (system_mode.second != Loader::ResultStatus::Success) {
  71. LOG_CRITICAL(Core, "Failed to determine system mode (Error %i)!",
  72. static_cast<int>(system_mode.second));
  73. System::Shutdown();
  74. switch (system_mode.second) {
  75. case Loader::ResultStatus::ErrorEncrypted:
  76. return ResultStatus::ErrorLoader_ErrorEncrypted;
  77. case Loader::ResultStatus::ErrorInvalidFormat:
  78. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  79. default:
  80. return ResultStatus::ErrorSystemMode;
  81. }
  82. }
  83. ResultStatus init_result{Init(emu_window, system_mode.first.get())};
  84. if (init_result != ResultStatus::Success) {
  85. LOG_CRITICAL(Core, "Failed to initialize system (Error %i)!", init_result);
  86. System::Shutdown();
  87. return init_result;
  88. }
  89. const Loader::ResultStatus load_result{app_loader->Load(Kernel::g_current_process)};
  90. if (Loader::ResultStatus::Success != load_result) {
  91. LOG_CRITICAL(Core, "Failed to load ROM (Error %i)!", load_result);
  92. System::Shutdown();
  93. switch (load_result) {
  94. case Loader::ResultStatus::ErrorEncrypted:
  95. return ResultStatus::ErrorLoader_ErrorEncrypted;
  96. case Loader::ResultStatus::ErrorInvalidFormat:
  97. return ResultStatus::ErrorLoader_ErrorInvalidFormat;
  98. default:
  99. return ResultStatus::ErrorLoader;
  100. }
  101. }
  102. status = ResultStatus::Success;
  103. return status;
  104. }
  105. void System::PrepareReschedule() {
  106. cpu_core->PrepareReschedule();
  107. reschedule_pending = true;
  108. }
  109. PerfStats::Results System::GetAndResetPerfStats() {
  110. return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());
  111. }
  112. void System::Reschedule() {
  113. if (!reschedule_pending) {
  114. return;
  115. }
  116. reschedule_pending = false;
  117. Kernel::Reschedule();
  118. }
  119. System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
  120. LOG_DEBUG(HW_Memory, "initialized OK");
  121. if (Settings::values.use_cpu_jit) {
  122. cpu_core = std::make_unique<ARM_Dynarmic>(USER32MODE);
  123. } else {
  124. cpu_core = std::make_unique<ARM_DynCom>(USER32MODE);
  125. }
  126. telemetry_session = std::make_unique<Core::TelemetrySession>();
  127. CoreTiming::Init();
  128. HW::Init();
  129. Kernel::Init(system_mode);
  130. Service::Init();
  131. AudioCore::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. AudioCore::Shutdown();
  154. VideoCore::Shutdown();
  155. Service::Shutdown();
  156. Kernel::Shutdown();
  157. HW::Shutdown();
  158. CoreTiming::Shutdown();
  159. cpu_core = nullptr;
  160. app_loader = nullptr;
  161. telemetry_session = nullptr;
  162. if (auto room_member = Network::GetRoomMember().lock()) {
  163. Network::GameInfo game_info{};
  164. room_member->SendGameInfo(game_info);
  165. }
  166. LOG_DEBUG(Core, "Shutdown OK");
  167. }
  168. } // namespace Core