core.cpp 6.0 KB

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