core.h 12 KB

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