core.h 10 KB

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