core.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <cstddef>
  6. #include <memory>
  7. #include <string>
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/object.h"
  10. namespace Core::Frontend {
  11. class EmuWindow;
  12. class SoftwareKeyboardApplet;
  13. } // namespace Core::Frontend
  14. namespace FileSys {
  15. class VfsFilesystem;
  16. } // namespace FileSys
  17. namespace Kernel {
  18. class KernelCore;
  19. class Process;
  20. class Scheduler;
  21. } // namespace Kernel
  22. namespace Loader {
  23. class AppLoader;
  24. enum class ResultStatus : u16;
  25. } // namespace Loader
  26. namespace Service::SM {
  27. class ServiceManager;
  28. } // namespace Service::SM
  29. namespace Tegra {
  30. class DebugContext;
  31. class GPU;
  32. } // namespace Tegra
  33. namespace VideoCore {
  34. class RendererBase;
  35. } // namespace VideoCore
  36. namespace Core {
  37. class ARM_Interface;
  38. class Cpu;
  39. class ExclusiveMonitor;
  40. class FrameLimiter;
  41. class PerfStats;
  42. class TelemetrySession;
  43. struct PerfStatsResults;
  44. class System {
  45. public:
  46. System(const System&) = delete;
  47. System& operator=(const System&) = delete;
  48. System(System&&) = delete;
  49. System& operator=(System&&) = delete;
  50. ~System();
  51. /**
  52. * Gets the instance of the System singleton class.
  53. * @returns Reference to the instance of the System singleton class.
  54. */
  55. static System& GetInstance() {
  56. return s_instance;
  57. }
  58. /// Enumeration representing the return values of the System Initialize and Load process.
  59. enum class ResultStatus : u32 {
  60. Success, ///< Succeeded
  61. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  62. ErrorGetLoader, ///< Error finding the correct application loader
  63. ErrorSystemMode, ///< Error determining the system mode
  64. ErrorSystemFiles, ///< Error in finding system files
  65. ErrorSharedFont, ///< Error in finding shared font
  66. ErrorVideoCore, ///< Error in the video core
  67. ErrorUnknown, ///< Any other error
  68. ErrorLoader, ///< The base for loader errors (too many to repeat)
  69. };
  70. /**
  71. * Run the core CPU loop
  72. * This function runs the core for the specified number of CPU instructions before trying to
  73. * update hardware. This is much faster than SingleStep (and should be equivalent), as the CPU
  74. * is not required to do a full dispatch with each instruction. NOTE: the number of instructions
  75. * requested is not guaranteed to run, as this will be interrupted preemptively if a hardware
  76. * update is requested (e.g. on a thread switch).
  77. * @param tight_loop If false, the CPU single-steps.
  78. * @return Result status, indicating whether or not the operation succeeded.
  79. */
  80. ResultStatus RunLoop(bool tight_loop = true);
  81. /**
  82. * Step the CPU one instruction
  83. * @return Result status, indicating whether or not the operation succeeded.
  84. */
  85. ResultStatus SingleStep();
  86. /**
  87. * Invalidate the CPU instruction caches
  88. * This function should only be used by GDB Stub to support breakpoints, memory updates and
  89. * step/continue commands.
  90. */
  91. void InvalidateCpuInstructionCaches();
  92. /// Shutdown the emulated system.
  93. void Shutdown();
  94. /**
  95. * Load an executable application.
  96. * @param emu_window Reference to the host-system window used for video output and keyboard
  97. * input.
  98. * @param filepath String path to the executable application to load on the host file system.
  99. * @returns ResultStatus code, indicating if the operation succeeded.
  100. */
  101. ResultStatus Load(Frontend::EmuWindow& emu_window, const std::string& filepath);
  102. /**
  103. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  104. * application).
  105. * @returns True if the emulated system is powered on, otherwise false.
  106. */
  107. bool IsPoweredOn() const;
  108. /// Gets a reference to the telemetry session for this emulation session.
  109. Core::TelemetrySession& TelemetrySession();
  110. /// Gets a reference to the telemetry session for this emulation session.
  111. const Core::TelemetrySession& TelemetrySession() const;
  112. /// Prepare the core emulation for a reschedule
  113. void PrepareReschedule();
  114. /// Gets and resets core performance statistics
  115. PerfStatsResults GetAndResetPerfStats();
  116. /// Gets an ARM interface to the CPU core that is currently running
  117. ARM_Interface& CurrentArmInterface();
  118. /// Gets an ARM interface to the CPU core that is currently running
  119. const ARM_Interface& CurrentArmInterface() const;
  120. /// Gets the index of the currently running CPU core
  121. std::size_t CurrentCoreIndex() const;
  122. /// Gets the scheduler for the CPU core that is currently running
  123. Kernel::Scheduler& CurrentScheduler();
  124. /// Gets the scheduler for the CPU core that is currently running
  125. const Kernel::Scheduler& CurrentScheduler() const;
  126. /// Gets a reference to an ARM interface for the CPU core with the specified index
  127. ARM_Interface& ArmInterface(std::size_t core_index);
  128. /// Gets a const reference to an ARM interface from the CPU core with the specified index
  129. const ARM_Interface& ArmInterface(std::size_t core_index) const;
  130. /// Gets a CPU interface to the CPU core with the specified index
  131. Cpu& CpuCore(std::size_t core_index);
  132. /// Gets a CPU interface to the CPU core with the specified index
  133. const Cpu& CpuCore(std::size_t core_index) const;
  134. /// Gets a reference to the exclusive monitor
  135. ExclusiveMonitor& Monitor();
  136. /// Gets a constant reference to the exclusive monitor
  137. const ExclusiveMonitor& Monitor() const;
  138. /// Gets a mutable reference to the GPU interface
  139. Tegra::GPU& GPU();
  140. /// Gets an immutable reference to the GPU interface.
  141. const Tegra::GPU& GPU() const;
  142. /// Gets a mutable reference to the renderer.
  143. VideoCore::RendererBase& Renderer();
  144. /// Gets an immutable reference to the renderer.
  145. const VideoCore::RendererBase& Renderer() const;
  146. /// Gets the scheduler for the CPU core with the specified index
  147. Kernel::Scheduler& Scheduler(std::size_t core_index);
  148. /// Gets the scheduler for the CPU core with the specified index
  149. const Kernel::Scheduler& Scheduler(std::size_t core_index) const;
  150. /// Provides a pointer to the current process
  151. Kernel::Process* CurrentProcess();
  152. /// Provides a constant pointer to the current process.
  153. const Kernel::Process* CurrentProcess() const;
  154. /// Provides a reference to the kernel instance.
  155. Kernel::KernelCore& Kernel();
  156. /// Provides a constant reference to the kernel instance.
  157. const Kernel::KernelCore& Kernel() const;
  158. /// Provides a reference to the internal PerfStats instance.
  159. Core::PerfStats& GetPerfStats();
  160. /// Provides a constant reference to the internal PerfStats instance.
  161. const Core::PerfStats& GetPerfStats() const;
  162. /// Provides a reference to the frame limiter;
  163. Core::FrameLimiter& FrameLimiter();
  164. /// Provides a constant referent to the frame limiter
  165. const Core::FrameLimiter& FrameLimiter() const;
  166. /// Gets the name of the current game
  167. Loader::ResultStatus GetGameName(std::string& out) const;
  168. void SetStatus(ResultStatus new_status, const char* details);
  169. const std::string& GetStatusDetails() const;
  170. Loader::AppLoader& GetAppLoader() const;
  171. Service::SM::ServiceManager& ServiceManager();
  172. const Service::SM::ServiceManager& ServiceManager() const;
  173. void SetGPUDebugContext(std::shared_ptr<Tegra::DebugContext> context);
  174. Tegra::DebugContext* GetGPUDebugContext() const;
  175. void SetFilesystem(std::shared_ptr<FileSys::VfsFilesystem> vfs);
  176. std::shared_ptr<FileSys::VfsFilesystem> GetFilesystem() const;
  177. void SetSoftwareKeyboard(std::unique_ptr<Core::Frontend::SoftwareKeyboardApplet> applet);
  178. const Core::Frontend::SoftwareKeyboardApplet& GetSoftwareKeyboard() const;
  179. private:
  180. System();
  181. /// Returns the currently running CPU core
  182. Cpu& CurrentCpuCore();
  183. /// Returns the currently running CPU core
  184. const Cpu& CurrentCpuCore() const;
  185. /**
  186. * Initialize the emulated system.
  187. * @param emu_window Reference to the host-system window used for video output and keyboard
  188. * input.
  189. * @return ResultStatus code, indicating if the operation succeeded.
  190. */
  191. ResultStatus Init(Frontend::EmuWindow& emu_window);
  192. struct Impl;
  193. std::unique_ptr<Impl> impl;
  194. static System s_instance;
  195. };
  196. inline ARM_Interface& CurrentArmInterface() {
  197. return System::GetInstance().CurrentArmInterface();
  198. }
  199. inline TelemetrySession& Telemetry() {
  200. return System::GetInstance().TelemetrySession();
  201. }
  202. inline Kernel::Process* CurrentProcess() {
  203. return System::GetInstance().CurrentProcess();
  204. }
  205. } // namespace Core