core.h 11 KB

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