core.h 9.5 KB

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