core.h 5.2 KB

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