core.h 8.9 KB

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