core.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string>
  7. #include "common/common_types.h"
  8. #include "core/hle/kernel/kernel.h"
  9. #include "core/hle/kernel/scheduler.h"
  10. #include "core/loader/loader.h"
  11. #include "core/memory.h"
  12. #include "core/perf_stats.h"
  13. #include "core/telemetry_session.h"
  14. #include "video_core/debug_utils/debug_utils.h"
  15. #include "video_core/gpu.h"
  16. class EmuWindow;
  17. class ARM_Interface;
  18. namespace Service::SM {
  19. class ServiceManager;
  20. }
  21. namespace Core {
  22. class System {
  23. public:
  24. ~System();
  25. /**
  26. * Gets the instance of the System singleton class.
  27. * @returns Reference to the instance of the System singleton class.
  28. */
  29. static System& GetInstance() {
  30. return s_instance;
  31. }
  32. /// Enumeration representing the return values of the System Initialize and Load process.
  33. enum class ResultStatus : u32 {
  34. Success, ///< Succeeded
  35. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  36. ErrorGetLoader, ///< Error finding the correct application loader
  37. ErrorSystemMode, ///< Error determining the system mode
  38. ErrorLoader, ///< Error loading the specified application
  39. ErrorLoader_ErrorEncrypted, ///< Error loading the specified application due to encryption
  40. ErrorLoader_ErrorInvalidFormat, ///< Error loading the specified application due to an
  41. /// invalid format
  42. ErrorSystemFiles, ///< Error in finding system files
  43. ErrorSharedFont, ///< Error in finding shared font
  44. ErrorVideoCore, ///< Error in the video core
  45. ErrorUnsupportedArch, ///< Unsupported Architecture (32-Bit ROMs)
  46. ErrorUnknown ///< Any other error
  47. };
  48. /**
  49. * Run the core CPU loop
  50. * This function runs the core for the specified number of CPU instructions before trying to
  51. * update hardware. This is much faster than SingleStep (and should be equivalent), as the CPU
  52. * is not required to do a full dispatch with each instruction. NOTE: the number of instructions
  53. * requested is not guaranteed to run, as this will be interrupted preemptively if a hardware
  54. * update is requested (e.g. on a thread switch).
  55. * @param tight_loop If false, the CPU single-steps.
  56. * @return Result status, indicating whether or not the operation succeeded.
  57. */
  58. ResultStatus RunLoop(bool tight_loop = true);
  59. /**
  60. * Step the CPU one instruction
  61. * @return Result status, indicating whether or not the operation succeeded.
  62. */
  63. ResultStatus SingleStep();
  64. /// Shutdown the emulated system.
  65. void Shutdown();
  66. /**
  67. * Load an executable application.
  68. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  69. * @param filepath String path to the executable application to load on the host file system.
  70. * @returns ResultStatus code, indicating if the operation succeeded.
  71. */
  72. ResultStatus Load(EmuWindow* emu_window, const std::string& filepath);
  73. /**
  74. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  75. * application).
  76. * @returns True if the emulated system is powered on, otherwise false.
  77. */
  78. bool IsPoweredOn() const {
  79. return cpu_core != nullptr;
  80. }
  81. /**
  82. * Returns a reference to the telemetry session for this emulation session.
  83. * @returns Reference to the telemetry session.
  84. */
  85. Core::TelemetrySession& TelemetrySession() const {
  86. return *telemetry_session;
  87. }
  88. /// Prepare the core emulation for a reschedule
  89. void PrepareReschedule();
  90. PerfStats::Results GetAndResetPerfStats();
  91. /**
  92. * Gets a reference to the emulated CPU.
  93. * @returns A reference to the emulated CPU.
  94. */
  95. ARM_Interface& CPU() {
  96. return *cpu_core;
  97. }
  98. Tegra::GPU& GPU() {
  99. return *gpu_core;
  100. }
  101. Kernel::Scheduler& Scheduler() {
  102. return *scheduler;
  103. }
  104. Kernel::SharedPtr<Kernel::Process>& CurrentProcess() {
  105. return current_process;
  106. }
  107. PerfStats perf_stats;
  108. FrameLimiter frame_limiter;
  109. void SetStatus(ResultStatus new_status, const char* details = nullptr) {
  110. status = new_status;
  111. if (details) {
  112. status_details = details;
  113. }
  114. }
  115. const std::string& GetStatusDetails() const {
  116. return status_details;
  117. }
  118. Loader::AppLoader& GetAppLoader() const {
  119. return *app_loader;
  120. }
  121. Service::SM::ServiceManager& ServiceManager();
  122. const Service::SM::ServiceManager& ServiceManager() const;
  123. void SetGPUDebugContext(std::shared_ptr<Tegra::DebugContext> context) {
  124. debug_context = std::move(context);
  125. }
  126. std::shared_ptr<Tegra::DebugContext> GetGPUDebugContext() const {
  127. return debug_context;
  128. }
  129. private:
  130. /**
  131. * Initialize the emulated system.
  132. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  133. * @param system_mode The system mode.
  134. * @return ResultStatus code, indicating if the operation succeeded.
  135. */
  136. ResultStatus Init(EmuWindow* emu_window, u32 system_mode);
  137. /// Reschedule the core emulation
  138. void Reschedule();
  139. /// AppLoader used to load the current executing application
  140. std::unique_ptr<Loader::AppLoader> app_loader;
  141. std::shared_ptr<ARM_Interface> cpu_core;
  142. std::unique_ptr<Kernel::Scheduler> scheduler;
  143. std::unique_ptr<Tegra::GPU> gpu_core;
  144. std::shared_ptr<Tegra::DebugContext> debug_context;
  145. Kernel::SharedPtr<Kernel::Process> current_process;
  146. /// When true, signals that a reschedule should happen
  147. bool reschedule_pending{};
  148. /// Service manager
  149. std::shared_ptr<Service::SM::ServiceManager> service_manager;
  150. /// Telemetry session for this emulation session
  151. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  152. static System s_instance;
  153. ResultStatus status = ResultStatus::Success;
  154. std::string status_details = "";
  155. };
  156. inline ARM_Interface& CPU() {
  157. return System::GetInstance().CPU();
  158. }
  159. inline TelemetrySession& Telemetry() {
  160. return System::GetInstance().TelemetrySession();
  161. }
  162. inline Kernel::SharedPtr<Kernel::Process>& CurrentProcess() {
  163. return System::GetInstance().CurrentProcess();
  164. }
  165. } // namespace Core