core.h 10 KB

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