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