core.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/memory.h"
  9. #include "core/perf_stats.h"
  10. class EmuWindow;
  11. class ARM_Interface;
  12. namespace Loader {
  13. class AppLoader;
  14. }
  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. ErrorVideoCore, ///< Error in the video core
  36. };
  37. /**
  38. * Run the core CPU loop
  39. * This function runs the core for the specified number of CPU instructions before trying to
  40. * update hardware. This is much faster than SingleStep (and should be equivalent), as the CPU
  41. * is not required to do a full dispatch with each instruction. NOTE: the number of instructions
  42. * requested is not guaranteed to run, as this will be interrupted preemptively if a hardware
  43. * update is requested (e.g. on a thread switch).
  44. * @param tight_loop Number of instructions to execute.
  45. * @return Result status, indicating whethor or not the operation succeeded.
  46. */
  47. ResultStatus RunLoop(int tight_loop = 1000);
  48. /**
  49. * Step the CPU one instruction
  50. * @return Result status, indicating whethor or not the operation succeeded.
  51. */
  52. ResultStatus SingleStep();
  53. /// Shutdown the emulated system.
  54. void Shutdown();
  55. /**
  56. * Load an executable application.
  57. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  58. * @param filepath String path to the executable application to load on the host file system.
  59. * @returns ResultStatus code, indicating if the operation succeeded.
  60. */
  61. ResultStatus Load(EmuWindow* emu_window, const std::string& filepath);
  62. /**
  63. * Indicates if the emulated system is powered on (all subsystems initialized and able to run an
  64. * application).
  65. * @returns True if the emulated system is powered on, otherwise false.
  66. */
  67. bool IsPoweredOn() const {
  68. return cpu_core != nullptr;
  69. }
  70. /// Prepare the core emulation for a reschedule
  71. void PrepareReschedule();
  72. PerfStats::Results GetAndResetPerfStats();
  73. /**
  74. * Gets a reference to the emulated CPU.
  75. * @returns A reference to the emulated CPU.
  76. */
  77. ARM_Interface& CPU() {
  78. return *cpu_core;
  79. }
  80. PerfStats perf_stats;
  81. FrameLimiter frame_limiter;
  82. private:
  83. /**
  84. * Initialize the emulated system.
  85. * @param emu_window Pointer to the host-system window used for video output and keyboard input.
  86. * @param system_mode The system mode.
  87. * @return ResultStatus code, indicating if the operation succeeded.
  88. */
  89. ResultStatus Init(EmuWindow* emu_window, u32 system_mode);
  90. /// Reschedule the core emulation
  91. void Reschedule();
  92. /// AppLoader used to load the current executing application
  93. std::unique_ptr<Loader::AppLoader> app_loader;
  94. ///< ARM11 CPU core
  95. std::unique_ptr<ARM_Interface> cpu_core;
  96. /// When true, signals that a reschedule should happen
  97. bool reschedule_pending{};
  98. static System s_instance;
  99. };
  100. inline ARM_Interface& CPU() {
  101. return System::GetInstance().CPU();
  102. }
  103. } // namespace Core