core.h 11 KB

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