core.h 12 KB

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