core.h 10 KB

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