core.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <memory>
  6. #include <string>
  7. #include "common/common_types.h"
  8. #include "core/loader/loader.h"
  9. #include "core/memory.h"
  10. #include "core/perf_stats.h"
  11. #include "core/telemetry_session.h"
  12. #include "video_core/gpu.h"
  13. class EmuWindow;
  14. class ARM_Interface;
  15. namespace Core {
  16. class System {
  17. public:
  18. /**
  19. * Gets the instance of the System singleton class.
  20. * @returns Reference to the instance of the System singleton class.
  21. */
  22. static System& GetInstance() {
  23. return s_instance;
  24. }
  25. /// Enumeration representing the return values of the System Initialize and Load process.
  26. enum class ResultStatus : u32 {
  27. Success, ///< Succeeded
  28. ErrorNotInitialized, ///< Error trying to use core prior to initialization
  29. ErrorGetLoader, ///< Error finding the correct application loader
  30. ErrorSystemMode, ///< Error determining the system mode
  31. ErrorLoader, ///< Error loading the specified application
  32. ErrorLoader_ErrorEncrypted, ///< Error loading the specified application due to encryption
  33. ErrorLoader_ErrorInvalidFormat, ///< Error loading the specified application due to an
  34. /// invalid format
  35. ErrorSystemFiles, ///< Error in finding system files
  36. ErrorSharedFont, ///< Error in finding shared font
  37. ErrorVideoCore, ///< Error in the video core
  38. ErrorUnknown ///< Any other error
  39. };
  40. /**
  41. * Run the core CPU loop
  42. * This function runs the core for the specified number of CPU instructions before trying to
  43. * update hardware. This is much faster than SingleStep (and should be equivalent), as the CPU
  44. * is not required to do a full dispatch with each instruction. NOTE: the number of instructions
  45. * requested is not guaranteed to run, as this will be interrupted preemptively if a hardware
  46. * update is requested (e.g. on a thread switch).
  47. * @param tight_loop Number of instructions to execute.
  48. * @return Result status, indicating whether or not the operation succeeded.
  49. */
  50. ResultStatus RunLoop(int tight_loop = 100000);
  51. /**
  52. * Step the CPU one instruction
  53. * @return Result status, indicating whether or not the operation succeeded.
  54. */
  55. ResultStatus SingleStep();
  56. /// Shutdown the emulated system.
  57. void Shutdown();
  58. /**
  59. * Load an executable application.
  60. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  61. * @param filepath String path to the executable application to load on the host file system.
  62. * @returns ResultStatus code, indicating if the operation succeeded.
  63. */
  64. ResultStatus Load(EmuWindow* emu_window, const std::string& filepath);
  65. /**
  66. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  67. * application).
  68. * @returns True if the emulated system is powered on, otherwise false.
  69. */
  70. bool IsPoweredOn() const {
  71. return cpu_core != nullptr;
  72. }
  73. /**
  74. * Returns a reference to the telemetry session for this emulation session.
  75. * @returns Reference to the telemetry session.
  76. */
  77. Core::TelemetrySession& TelemetrySession() const {
  78. return *telemetry_session;
  79. }
  80. /// Prepare the core emulation for a reschedule
  81. void PrepareReschedule();
  82. PerfStats::Results GetAndResetPerfStats();
  83. /**
  84. * Gets a reference to the emulated CPU.
  85. * @returns A reference to the emulated CPU.
  86. */
  87. ARM_Interface& CPU() {
  88. return *cpu_core;
  89. }
  90. Tegra::GPU& GPU() {
  91. return *gpu_core;
  92. }
  93. PerfStats perf_stats;
  94. FrameLimiter frame_limiter;
  95. void SetStatus(ResultStatus new_status, const char* details = nullptr) {
  96. status = new_status;
  97. if (details) {
  98. status_details = details;
  99. }
  100. }
  101. const std::string& GetStatusDetails() const {
  102. return status_details;
  103. }
  104. Loader::AppLoader& GetAppLoader() const {
  105. return *app_loader;
  106. }
  107. private:
  108. /**
  109. * Initialize the emulated system.
  110. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  111. * @param system_mode The system mode.
  112. * @return ResultStatus code, indicating if the operation succeeded.
  113. */
  114. ResultStatus Init(EmuWindow* emu_window, u32 system_mode);
  115. /// Reschedule the core emulation
  116. void Reschedule();
  117. /// AppLoader used to load the current executing application
  118. std::unique_ptr<Loader::AppLoader> app_loader;
  119. std::shared_ptr<ARM_Interface> cpu_core;
  120. std::unique_ptr<Tegra::GPU> gpu_core;
  121. /// When true, signals that a reschedule should happen
  122. bool reschedule_pending{};
  123. /// Telemetry session for this emulation session
  124. std::unique_ptr<Core::TelemetrySession> telemetry_session;
  125. static System s_instance;
  126. ResultStatus status = ResultStatus::Success;
  127. std::string status_details = "";
  128. };
  129. inline ARM_Interface& CPU() {
  130. return System::GetInstance().CPU();
  131. }
  132. inline TelemetrySession& Telemetry() {
  133. return System::GetInstance().TelemetrySession();
  134. }
  135. } // namespace Core