core.h 7.4 KB

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