core.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <functional>
  7. #include <memory>
  8. #include <mutex>
  9. #include <string>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. #include "core/file_sys/vfs_types.h"
  13. namespace Core::Frontend {
  14. class EmuWindow;
  15. } // namespace Core::Frontend
  16. namespace FileSys {
  17. class ContentProvider;
  18. class ContentProviderUnion;
  19. enum class ContentProviderUnionSlot;
  20. class VfsFilesystem;
  21. } // namespace FileSys
  22. namespace Kernel {
  23. class GlobalSchedulerContext;
  24. class KernelCore;
  25. class PhysicalCore;
  26. class KProcess;
  27. class KScheduler;
  28. } // namespace Kernel
  29. namespace Loader {
  30. class AppLoader;
  31. enum class ResultStatus : u16;
  32. } // namespace Loader
  33. namespace Core::Memory {
  34. struct CheatEntry;
  35. class Memory;
  36. } // namespace Core::Memory
  37. namespace Service {
  38. namespace AM::Applets {
  39. struct AppletFrontendSet;
  40. class AppletManager;
  41. } // namespace AM::Applets
  42. namespace APM {
  43. class Controller;
  44. }
  45. namespace FileSystem {
  46. class FileSystemController;
  47. } // namespace FileSystem
  48. namespace Glue {
  49. class ARPManager;
  50. }
  51. namespace SM {
  52. class ServiceManager;
  53. } // namespace SM
  54. namespace Time {
  55. class TimeManager;
  56. } // namespace Time
  57. } // namespace Service
  58. namespace Tegra {
  59. class DebugContext;
  60. class GPU;
  61. } // namespace Tegra
  62. namespace VideoCore {
  63. class RendererBase;
  64. } // namespace VideoCore
  65. namespace AudioCore {
  66. class AudioCore;
  67. } // namespace AudioCore
  68. namespace Core::Timing {
  69. class CoreTiming;
  70. }
  71. namespace Core::Hardware {
  72. class InterruptManager;
  73. }
  74. namespace Core::HID {
  75. class HIDCore;
  76. }
  77. namespace Network {
  78. class RoomNetwork;
  79. }
  80. namespace Core {
  81. class ARM_Interface;
  82. class CpuManager;
  83. class Debugger;
  84. class DeviceMemory;
  85. class ExclusiveMonitor;
  86. class SpeedLimiter;
  87. class PerfStats;
  88. class Reporter;
  89. class TelemetrySession;
  90. struct PerfStatsResults;
  91. FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
  92. const std::string& path);
  93. /// Enumeration representing the return values of the System Initialize and Load process.
  94. enum class SystemResultStatus : u32 {
  95. Success, ///< Succeeded
  96. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  97. ErrorGetLoader, ///< Error finding the correct application loader
  98. ErrorSystemFiles, ///< Error in finding system files
  99. ErrorSharedFont, ///< Error in finding shared font
  100. ErrorVideoCore, ///< Error in the video core
  101. ErrorUnknown, ///< Any other error
  102. ErrorLoader, ///< The base for loader errors (too many to repeat)
  103. };
  104. class System {
  105. public:
  106. using CurrentBuildProcessID = std::array<u8, 0x20>;
  107. explicit System();
  108. ~System();
  109. System(const System&) = delete;
  110. System& operator=(const System&) = delete;
  111. System(System&&) = delete;
  112. System& operator=(System&&) = delete;
  113. /**
  114. * Run the OS and Application
  115. * This function will start emulation and run the relevant devices
  116. */
  117. [[nodiscard]] SystemResultStatus Run();
  118. /**
  119. * Pause the OS and Application
  120. * This function will pause emulation and stop the relevant devices
  121. */
  122. [[nodiscard]] SystemResultStatus Pause();
  123. /// Check if the core is currently paused.
  124. [[nodiscard]] bool IsPaused() const;
  125. /**
  126. * Invalidate the CPU instruction caches
  127. * This function should only be used by GDB Stub to support breakpoints, memory updates and
  128. * step/continue commands.
  129. */
  130. void InvalidateCpuInstructionCaches();
  131. void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
  132. /// Shutdown the emulated system.
  133. void Shutdown();
  134. /// Check if the core is shutting down.
  135. [[nodiscard]] bool IsShuttingDown() const;
  136. /// Set the shutting down state.
  137. void SetShuttingDown(bool shutting_down);
  138. /// Forcibly detach the debugger if it is running.
  139. void DetachDebugger();
  140. std::unique_lock<std::mutex> StallProcesses();
  141. void UnstallProcesses();
  142. /**
  143. * Initialize the debugger.
  144. */
  145. void InitializeDebugger();
  146. /**
  147. * Load an executable application.
  148. * @param emu_window Reference to the host-system window used for video output and keyboard
  149. * input.
  150. * @param filepath String path to the executable application to load on the host file system.
  151. * @param program_index Specifies the index within the container of the program to launch.
  152. * @returns SystemResultStatus code, indicating if the operation succeeded.
  153. */
  154. [[nodiscard]] SystemResultStatus Load(Frontend::EmuWindow& emu_window,
  155. const std::string& filepath, u64 program_id = 0,
  156. std::size_t program_index = 0);
  157. /**
  158. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  159. * application).
  160. * @returns True if the emulated system is powered on, otherwise false.
  161. */
  162. [[nodiscard]] bool IsPoweredOn() const;
  163. /// Gets a reference to the telemetry session for this emulation session.
  164. [[nodiscard]] Core::TelemetrySession& TelemetrySession();
  165. /// Gets a reference to the telemetry session for this emulation session.
  166. [[nodiscard]] const Core::TelemetrySession& TelemetrySession() const;
  167. /// Prepare the core emulation for a reschedule
  168. void PrepareReschedule(u32 core_index);
  169. /// Gets and resets core performance statistics
  170. [[nodiscard]] PerfStatsResults GetAndResetPerfStats();
  171. /// Gets an ARM interface to the CPU core that is currently running
  172. [[nodiscard]] ARM_Interface& CurrentArmInterface();
  173. /// Gets an ARM interface to the CPU core that is currently running
  174. [[nodiscard]] const ARM_Interface& CurrentArmInterface() const;
  175. /// Gets the physical core for the CPU core that is currently running
  176. [[nodiscard]] Kernel::PhysicalCore& CurrentPhysicalCore();
  177. /// Gets the physical core for the CPU core that is currently running
  178. [[nodiscard]] const Kernel::PhysicalCore& CurrentPhysicalCore() const;
  179. /// Gets a reference to an ARM interface for the CPU core with the specified index
  180. [[nodiscard]] ARM_Interface& ArmInterface(std::size_t core_index);
  181. /// Gets a const reference to an ARM interface from the CPU core with the specified index
  182. [[nodiscard]] const ARM_Interface& ArmInterface(std::size_t core_index) const;
  183. /// Gets a reference to the underlying CPU manager.
  184. [[nodiscard]] CpuManager& GetCpuManager();
  185. /// Gets a const reference to the underlying CPU manager
  186. [[nodiscard]] const CpuManager& GetCpuManager() const;
  187. /// Gets a reference to the exclusive monitor
  188. [[nodiscard]] ExclusiveMonitor& Monitor();
  189. /// Gets a constant reference to the exclusive monitor
  190. [[nodiscard]] const ExclusiveMonitor& Monitor() const;
  191. /// Gets a mutable reference to the system memory instance.
  192. [[nodiscard]] Core::Memory::Memory& Memory();
  193. /// Gets a constant reference to the system memory instance.
  194. [[nodiscard]] const Core::Memory::Memory& Memory() const;
  195. /// Gets a mutable reference to the GPU interface
  196. [[nodiscard]] Tegra::GPU& GPU();
  197. /// Gets an immutable reference to the GPU interface.
  198. [[nodiscard]] const Tegra::GPU& GPU() const;
  199. /// Gets a mutable reference to the renderer.
  200. [[nodiscard]] VideoCore::RendererBase& Renderer();
  201. /// Gets an immutable reference to the renderer.
  202. [[nodiscard]] const VideoCore::RendererBase& Renderer() const;
  203. /// Gets a mutable reference to the audio interface
  204. [[nodiscard]] AudioCore::AudioCore& AudioCore();
  205. /// Gets an immutable reference to the audio interface.
  206. [[nodiscard]] const AudioCore::AudioCore& AudioCore() const;
  207. /// Gets the global scheduler
  208. [[nodiscard]] Kernel::GlobalSchedulerContext& GlobalSchedulerContext();
  209. /// Gets the global scheduler
  210. [[nodiscard]] const Kernel::GlobalSchedulerContext& GlobalSchedulerContext() const;
  211. /// Gets the manager for the guest device memory
  212. [[nodiscard]] Core::DeviceMemory& DeviceMemory();
  213. /// Gets the manager for the guest device memory
  214. [[nodiscard]] const Core::DeviceMemory& DeviceMemory() const;
  215. /// Provides a pointer to the current process
  216. [[nodiscard]] Kernel::KProcess* CurrentProcess();
  217. /// Provides a constant pointer to the current process.
  218. [[nodiscard]] const Kernel::KProcess* CurrentProcess() const;
  219. /// Provides a reference to the core timing instance.
  220. [[nodiscard]] Timing::CoreTiming& CoreTiming();
  221. /// Provides a constant reference to the core timing instance.
  222. [[nodiscard]] const Timing::CoreTiming& CoreTiming() const;
  223. /// Provides a reference to the interrupt manager instance.
  224. [[nodiscard]] Core::Hardware::InterruptManager& InterruptManager();
  225. /// Provides a constant reference to the interrupt manager instance.
  226. [[nodiscard]] const Core::Hardware::InterruptManager& InterruptManager() const;
  227. /// Provides a reference to the kernel instance.
  228. [[nodiscard]] Kernel::KernelCore& Kernel();
  229. /// Provides a constant reference to the kernel instance.
  230. [[nodiscard]] const Kernel::KernelCore& Kernel() const;
  231. /// Gets a mutable reference to the HID interface.
  232. [[nodiscard]] HID::HIDCore& HIDCore();
  233. /// Gets an immutable reference to the HID interface.
  234. [[nodiscard]] const HID::HIDCore& HIDCore() const;
  235. /// Provides a reference to the internal PerfStats instance.
  236. [[nodiscard]] Core::PerfStats& GetPerfStats();
  237. /// Provides a constant reference to the internal PerfStats instance.
  238. [[nodiscard]] const Core::PerfStats& GetPerfStats() const;
  239. /// Provides a reference to the speed limiter;
  240. [[nodiscard]] Core::SpeedLimiter& SpeedLimiter();
  241. /// Provides a constant reference to the speed limiter
  242. [[nodiscard]] const Core::SpeedLimiter& SpeedLimiter() const;
  243. [[nodiscard]] u64 GetCurrentProcessProgramID() const;
  244. /// Gets the name of the current game
  245. [[nodiscard]] Loader::ResultStatus GetGameName(std::string& out) const;
  246. void SetStatus(SystemResultStatus new_status, const char* details);
  247. [[nodiscard]] const std::string& GetStatusDetails() const;
  248. [[nodiscard]] Loader::AppLoader& GetAppLoader();
  249. [[nodiscard]] const Loader::AppLoader& GetAppLoader() const;
  250. [[nodiscard]] Service::SM::ServiceManager& ServiceManager();
  251. [[nodiscard]] const Service::SM::ServiceManager& ServiceManager() const;
  252. void SetFilesystem(FileSys::VirtualFilesystem vfs);
  253. [[nodiscard]] FileSys::VirtualFilesystem GetFilesystem() const;
  254. void RegisterCheatList(const std::vector<Memory::CheatEntry>& list,
  255. const std::array<u8, 0x20>& build_id, VAddr main_region_begin,
  256. u64 main_region_size);
  257. void SetAppletFrontendSet(Service::AM::Applets::AppletFrontendSet&& set);
  258. void SetDefaultAppletFrontendSet();
  259. [[nodiscard]] Service::AM::Applets::AppletManager& GetAppletManager();
  260. [[nodiscard]] const Service::AM::Applets::AppletManager& GetAppletManager() const;
  261. void SetContentProvider(std::unique_ptr<FileSys::ContentProviderUnion> provider);
  262. [[nodiscard]] FileSys::ContentProvider& GetContentProvider();
  263. [[nodiscard]] const FileSys::ContentProvider& GetContentProvider() const;
  264. [[nodiscard]] Service::FileSystem::FileSystemController& GetFileSystemController();
  265. [[nodiscard]] const Service::FileSystem::FileSystemController& GetFileSystemController() const;
  266. void RegisterContentProvider(FileSys::ContentProviderUnionSlot slot,
  267. FileSys::ContentProvider* provider);
  268. void ClearContentProvider(FileSys::ContentProviderUnionSlot slot);
  269. [[nodiscard]] const Reporter& GetReporter() const;
  270. [[nodiscard]] Service::Glue::ARPManager& GetARPManager();
  271. [[nodiscard]] const Service::Glue::ARPManager& GetARPManager() const;
  272. [[nodiscard]] Service::APM::Controller& GetAPMController();
  273. [[nodiscard]] const Service::APM::Controller& GetAPMController() const;
  274. [[nodiscard]] Service::Time::TimeManager& GetTimeManager();
  275. [[nodiscard]] const Service::Time::TimeManager& GetTimeManager() const;
  276. [[nodiscard]] Core::Debugger& GetDebugger();
  277. [[nodiscard]] const Core::Debugger& GetDebugger() const;
  278. /// Gets a mutable reference to the Room Network.
  279. [[nodiscard]] Network::RoomNetwork& GetRoomNetwork();
  280. /// Gets an immutable reference to the Room Network.
  281. [[nodiscard]] const Network::RoomNetwork& GetRoomNetwork() const;
  282. void SetExitLock(bool locked);
  283. [[nodiscard]] bool GetExitLock() const;
  284. void SetCurrentProcessBuildID(const CurrentBuildProcessID& id);
  285. [[nodiscard]] const CurrentBuildProcessID& GetCurrentProcessBuildID() const;
  286. /// Register a host thread as an emulated CPU Core.
  287. void RegisterCoreThread(std::size_t id);
  288. /// Register a host thread as an auxiliary thread.
  289. void RegisterHostThread();
  290. /// Enter Dynarmic Microprofile
  291. void EnterDynarmicProfile();
  292. /// Exit Dynarmic Microprofile
  293. void ExitDynarmicProfile();
  294. /// Tells if system is running on multicore.
  295. [[nodiscard]] bool IsMulticore() const;
  296. /// Tells if the system debugger is enabled.
  297. [[nodiscard]] bool DebuggerEnabled() const;
  298. /// Type used for the frontend to designate a callback for System to re-launch the application
  299. /// using a specified program index.
  300. using ExecuteProgramCallback = std::function<void(std::size_t)>;
  301. /**
  302. * Registers a callback from the frontend for System to re-launch the application using a
  303. * specified program index.
  304. * @param callback Callback from the frontend to relaunch the application.
  305. */
  306. void RegisterExecuteProgramCallback(ExecuteProgramCallback&& callback);
  307. /**
  308. * Instructs the frontend to re-launch the application using the specified program_index.
  309. * @param program_index Specifies the index within the application of the program to launch.
  310. */
  311. void ExecuteProgram(std::size_t program_index);
  312. /// Type used for the frontend to designate a callback for System to exit the application.
  313. using ExitCallback = std::function<void()>;
  314. /**
  315. * Registers a callback from the frontend for System to exit the application.
  316. * @param callback Callback from the frontend to exit the application.
  317. */
  318. void RegisterExitCallback(ExitCallback&& callback);
  319. /// Instructs the frontend to exit the application.
  320. void Exit();
  321. /// Applies any changes to settings to this core instance.
  322. void ApplySettings();
  323. private:
  324. struct Impl;
  325. std::unique_ptr<Impl> impl;
  326. };
  327. } // namespace Core