2023-05-15 13:38:37 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
#include "system.h"
|
2022-08-05 07:07:40 +00:00
|
|
|
#include "IconsFontAwesome5.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "achievements.h"
|
2019-11-16 05:27:57 +00:00
|
|
|
#include "bios.h"
|
2019-09-12 02:53:04 +00:00
|
|
|
#include "bus.h"
|
2019-09-17 14:22:41 +00:00
|
|
|
#include "cdrom.h"
|
2020-09-09 12:11:28 +00:00
|
|
|
#include "cheats.h"
|
2019-12-08 14:51:52 +00:00
|
|
|
#include "controller.h"
|
2019-11-19 10:30:04 +00:00
|
|
|
#include "cpu_code_cache.h"
|
2019-09-12 02:53:04 +00:00
|
|
|
#include "cpu_core.h"
|
|
|
|
#include "dma.h"
|
2023-08-23 12:06:48 +00:00
|
|
|
#include "fullscreen_ui.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "game_database.h"
|
2023-08-23 12:06:48 +00:00
|
|
|
#include "game_list.h"
|
2019-09-12 02:53:04 +00:00
|
|
|
#include "gpu.h"
|
2020-07-31 07:09:18 +00:00
|
|
|
#include "gte.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "host.h"
|
2020-07-21 14:03:22 +00:00
|
|
|
#include "host_interface_progress_callback.h"
|
2023-08-23 12:06:48 +00:00
|
|
|
#include "imgui_overlays.h"
|
2019-09-17 06:26:00 +00:00
|
|
|
#include "interrupt_controller.h"
|
2019-09-29 02:51:34 +00:00
|
|
|
#include "mdec.h"
|
2019-10-27 06:45:23 +00:00
|
|
|
#include "memory_card.h"
|
2021-01-21 07:59:40 +00:00
|
|
|
#include "multitap.h"
|
2019-09-20 06:47:41 +00:00
|
|
|
#include "pad.h"
|
2023-04-29 10:45:39 +00:00
|
|
|
#include "pcdrv.h"
|
2021-04-27 16:50:46 +00:00
|
|
|
#include "pgxp.h"
|
2020-04-16 14:29:56 +00:00
|
|
|
#include "psf_loader.h"
|
2020-02-22 08:16:45 +00:00
|
|
|
#include "save_state_version.h"
|
2019-12-07 11:09:02 +00:00
|
|
|
#include "sio.h"
|
2019-09-24 13:44:38 +00:00
|
|
|
#include "spu.h"
|
2020-12-25 08:02:38 +00:00
|
|
|
#include "texture_replacements.h"
|
2019-09-20 13:40:19 +00:00
|
|
|
#include "timers.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/audio_stream.h"
|
2023-01-09 09:34:40 +00:00
|
|
|
#include "util/cd_image.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
#include "util/gpu_device.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "util/ini_settings_interface.h"
|
2023-08-23 12:06:48 +00:00
|
|
|
#include "util/input_manager.h"
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/iso_reader.h"
|
2023-08-23 12:06:48 +00:00
|
|
|
#include "util/platform_misc.h"
|
2023-08-27 12:48:40 +00:00
|
|
|
#include "util/postprocessing.h"
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/state_wrapper.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
|
|
|
#include "common/error.h"
|
|
|
|
#include "common/file_system.h"
|
|
|
|
#include "common/log.h"
|
|
|
|
#include "common/make_array.h"
|
|
|
|
#include "common/path.h"
|
|
|
|
#include "common/string_util.h"
|
|
|
|
#include "common/threading.h"
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
#include "fmt/chrono.h"
|
|
|
|
#include "fmt/format.h"
|
2021-03-06 16:44:09 +00:00
|
|
|
#include "xxhash.h"
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2020-09-03 02:25:59 +00:00
|
|
|
#include <cctype>
|
2021-01-23 09:00:54 +00:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cmath>
|
2020-09-09 12:11:28 +00:00
|
|
|
#include <cstdio>
|
2021-01-23 09:00:54 +00:00
|
|
|
#include <deque>
|
2020-09-01 02:29:22 +00:00
|
|
|
#include <fstream>
|
2020-07-22 16:36:05 +00:00
|
|
|
#include <limits>
|
2021-01-28 10:20:15 +00:00
|
|
|
#include <thread>
|
2023-08-13 03:42:02 +00:00
|
|
|
|
2019-09-22 15:28:00 +00:00
|
|
|
Log_SetChannel(System);
|
2019-09-09 07:01:26 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "common/windows_headers.h"
|
|
|
|
#include <mmsystem.h>
|
|
|
|
#endif
|
2022-03-26 14:52:11 +00:00
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
#include "discord_rpc.h"
|
|
|
|
#endif
|
|
|
|
|
2021-08-08 02:58:02 +00:00
|
|
|
// #define PROFILE_MEMORY_SAVE_STATES 1
|
|
|
|
|
2020-03-02 01:08:16 +00:00
|
|
|
SystemBootParameters::SystemBootParameters() = default;
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
SystemBootParameters::SystemBootParameters(const SystemBootParameters&) = default;
|
|
|
|
|
2020-09-12 20:14:50 +00:00
|
|
|
SystemBootParameters::SystemBootParameters(SystemBootParameters&& other) = default;
|
2020-03-02 01:08:16 +00:00
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
SystemBootParameters::SystemBootParameters(std::string filename_) : filename(std::move(filename_))
|
|
|
|
{
|
|
|
|
}
|
2020-05-26 16:01:09 +00:00
|
|
|
|
2020-03-02 01:08:16 +00:00
|
|
|
SystemBootParameters::~SystemBootParameters() = default;
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
struct MemorySaveState
|
|
|
|
{
|
2022-10-03 06:44:34 +00:00
|
|
|
std::unique_ptr<GPUTexture> vram_texture;
|
2021-01-23 09:00:54 +00:00
|
|
|
std::unique_ptr<GrowableMemoryByteStream> state_stream;
|
|
|
|
};
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
namespace System {
|
|
|
|
static std::optional<ExtendedSaveStateInfo> InternalGetExtendedSaveStateInfo(ByteStream* stream);
|
2022-08-18 11:21:22 +00:00
|
|
|
static bool InternalSaveState(ByteStream* state, u32 screenshot_size = 256,
|
|
|
|
u32 compression_method = SAVE_STATE_HEADER::COMPRESSION_TYPE_NONE);
|
2021-01-23 16:52:52 +00:00
|
|
|
static bool SaveMemoryState(MemorySaveState* mss);
|
|
|
|
static bool LoadMemoryState(const MemorySaveState& mss);
|
|
|
|
|
2020-10-29 12:05:09 +00:00
|
|
|
static bool LoadEXE(const char* filename);
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static std::string GetExecutableNameForImage(ISOReader& iso, bool strip_subdirectories);
|
2022-03-26 14:52:11 +00:00
|
|
|
static bool ReadExecutableFromImage(ISOReader& iso, std::string* out_executable_name,
|
|
|
|
std::vector<u8>* out_executable_data);
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static void StallCPU(TickCount ticks);
|
|
|
|
|
2023-03-16 09:22:54 +00:00
|
|
|
static bool LoadBIOS();
|
2022-07-11 13:03:29 +00:00
|
|
|
static void InternalReset();
|
|
|
|
static void ClearRunningGame();
|
|
|
|
static void DestroySystem();
|
|
|
|
static std::string GetMediaPathFromSaveState(const char* path);
|
2020-11-25 13:06:39 +00:00
|
|
|
static bool DoLoadState(ByteStream* stream, bool force_software_renderer, bool update_display);
|
2022-10-03 06:44:34 +00:00
|
|
|
static bool DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_display, bool is_memory_state);
|
2023-08-27 12:48:40 +00:00
|
|
|
static bool CreateGPU(GPURenderer renderer, bool is_switching);
|
2022-07-11 13:03:29 +00:00
|
|
|
static bool SaveUndoLoadState();
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
/// Throttles the system, i.e. sleeps until it's time to execute the next frame.
|
|
|
|
static void Throttle();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static void SetRewinding(bool enabled);
|
2021-01-23 16:52:52 +00:00
|
|
|
static bool SaveRewindState();
|
2021-01-23 09:00:54 +00:00
|
|
|
static void DoRewind();
|
2021-01-23 16:52:52 +00:00
|
|
|
|
|
|
|
static void SaveRunaheadState();
|
2023-08-15 13:12:21 +00:00
|
|
|
static bool DoRunahead();
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
static bool Initialize(bool force_software_renderer);
|
2023-08-15 13:12:21 +00:00
|
|
|
static bool FastForwardToFirstFrame();
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static bool UpdateGameSettingsLayer();
|
|
|
|
static void UpdateRunningGame(const char* path, CDImage* image, bool booting);
|
2021-02-10 16:04:47 +00:00
|
|
|
static bool CheckForSBIFile(CDImage* image);
|
2022-07-11 13:03:29 +00:00
|
|
|
static std::unique_ptr<MemoryCard> GetMemoryCardForSlot(u32 slot, MemoryCardType type);
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
static void UpdateSessionTime(const std::string& prev_serial);
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static void SetTimerResolutionIncreased(bool enabled);
|
2023-08-23 12:06:48 +00:00
|
|
|
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
static void InitializeDiscordPresence();
|
|
|
|
static void ShutdownDiscordPresence();
|
|
|
|
static void UpdateDiscordPresence(bool rich_presence_only);
|
|
|
|
static void PollDiscordPresence();
|
|
|
|
#endif
|
2022-07-11 13:03:29 +00:00
|
|
|
} // namespace System
|
|
|
|
|
2023-01-11 09:40:56 +00:00
|
|
|
static constexpr const float PERFORMANCE_COUNTER_UPDATE_INTERVAL = 1.0f;
|
2023-05-15 13:38:37 +00:00
|
|
|
static constexpr const char FALLBACK_EXE_NAME[] = "PSX.EXE";
|
2023-01-11 09:40:56 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static std::unique_ptr<INISettingsInterface> s_game_settings_interface;
|
|
|
|
static std::unique_ptr<INISettingsInterface> s_input_settings_interface;
|
|
|
|
static std::string s_input_profile_name;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static System::State s_state = System::State::Shutdown;
|
2021-02-18 15:48:44 +00:00
|
|
|
static std::atomic_bool s_startup_cancelled{false};
|
2023-08-13 03:42:02 +00:00
|
|
|
static bool s_keep_gpu_device_on_shutdown = false;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
static ConsoleRegion s_region = ConsoleRegion::NTSC_U;
|
2022-07-11 13:03:29 +00:00
|
|
|
TickCount System::g_ticks_per_second = System::MASTER_CLOCK;
|
|
|
|
static TickCount s_max_slice_ticks = System::MASTER_CLOCK / 10;
|
2020-07-31 07:09:18 +00:00
|
|
|
static u32 s_frame_number = 1;
|
|
|
|
static u32 s_internal_frame_number = 1;
|
2023-03-16 09:22:54 +00:00
|
|
|
static const BIOS::ImageInfo* s_bios_image_info = nullptr;
|
|
|
|
static BIOS::Hash s_bios_hash = {};
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
static std::string s_running_game_path;
|
2022-10-05 08:29:08 +00:00
|
|
|
static std::string s_running_game_serial;
|
2020-07-31 07:09:18 +00:00
|
|
|
static std::string s_running_game_title;
|
2023-08-19 12:00:20 +00:00
|
|
|
static const GameDatabase::Entry* s_running_game_entry = nullptr;
|
2023-05-15 13:38:37 +00:00
|
|
|
static System::GameHash s_running_game_hash;
|
2023-08-15 13:12:21 +00:00
|
|
|
static bool s_was_fast_booted;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
static float s_throttle_frequency = 60.0f;
|
2020-11-03 11:21:11 +00:00
|
|
|
static float s_target_speed = 1.0f;
|
2021-01-28 10:20:15 +00:00
|
|
|
static Common::Timer::Value s_frame_period = 0;
|
|
|
|
static Common::Timer::Value s_next_frame_time = 0;
|
2023-08-15 13:12:21 +00:00
|
|
|
static bool s_last_frame_skipped = false;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
static bool s_system_executing = false;
|
|
|
|
static bool s_system_interrupted = false;
|
2022-08-16 11:23:14 +00:00
|
|
|
static bool s_frame_step_request = false;
|
|
|
|
static bool s_fast_forward_enabled = false;
|
|
|
|
static bool s_turbo_enabled = false;
|
|
|
|
static bool s_throttler_enabled = true;
|
|
|
|
static bool s_display_all_frames = true;
|
|
|
|
static bool s_syncing_to_host = false;
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
static float s_average_frame_time_accumulator = 0.0f;
|
2023-01-07 03:09:20 +00:00
|
|
|
static float s_minimum_frame_time_accumulator = 0.0f;
|
|
|
|
static float s_maximum_frame_time_accumulator = 0.0f;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
static float s_vps = 0.0f;
|
|
|
|
static float s_fps = 0.0f;
|
|
|
|
static float s_speed = 0.0f;
|
2023-01-07 03:09:20 +00:00
|
|
|
static float s_minimum_frame_time = 0.0f;
|
|
|
|
static float s_maximum_frame_time = 0.0f;
|
2020-07-31 07:09:18 +00:00
|
|
|
static float s_average_frame_time = 0.0f;
|
2022-07-11 13:03:29 +00:00
|
|
|
static float s_cpu_thread_usage = 0.0f;
|
|
|
|
static float s_cpu_thread_time = 0.0f;
|
|
|
|
static float s_sw_thread_usage = 0.0f;
|
|
|
|
static float s_sw_thread_time = 0.0f;
|
2022-09-03 04:15:15 +00:00
|
|
|
static float s_average_gpu_time = 0.0f;
|
|
|
|
static float s_accumulated_gpu_time = 0.0f;
|
|
|
|
static float s_gpu_usage = 0.0f;
|
2023-01-07 03:09:20 +00:00
|
|
|
static System::FrameTimeHistory s_frame_time_history;
|
|
|
|
static u32 s_frame_time_history_pos = 0;
|
2020-07-31 07:09:18 +00:00
|
|
|
static u32 s_last_frame_number = 0;
|
|
|
|
static u32 s_last_internal_frame_number = 0;
|
|
|
|
static u32 s_last_global_tick_counter = 0;
|
2022-07-11 13:03:29 +00:00
|
|
|
static u64 s_last_cpu_time = 0;
|
|
|
|
static u64 s_last_sw_time = 0;
|
2022-09-03 04:15:15 +00:00
|
|
|
static u32 s_presents_since_last_update = 0;
|
2020-07-31 07:09:18 +00:00
|
|
|
static Common::Timer s_fps_timer;
|
|
|
|
static Common::Timer s_frame_timer;
|
2022-07-11 13:03:29 +00:00
|
|
|
static Threading::ThreadHandle s_cpu_thread_handle;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2020-09-09 12:11:28 +00:00
|
|
|
static std::unique_ptr<CheatList> s_cheat_list;
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
// temporary save state, created when loading, used to undo load state
|
|
|
|
static std::unique_ptr<ByteStream> m_undo_load_state;
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
static bool s_memory_saves_enabled = false;
|
|
|
|
|
|
|
|
static std::deque<MemorySaveState> s_rewind_states;
|
|
|
|
static s32 s_rewind_load_frequency = -1;
|
|
|
|
static s32 s_rewind_load_counter = -1;
|
|
|
|
static s32 s_rewind_save_frequency = -1;
|
|
|
|
static s32 s_rewind_save_counter = -1;
|
|
|
|
static bool s_rewinding_first_save = false;
|
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
static std::deque<MemorySaveState> s_runahead_states;
|
|
|
|
static bool s_runahead_replay_pending = false;
|
|
|
|
static u32 s_runahead_frames = 0;
|
2023-08-15 13:12:21 +00:00
|
|
|
static u32 s_runahead_replay_frames = 0;
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
// Used to track play time. We use a monotonic timer here, in case of clock changes.
|
|
|
|
static u64 s_session_start_time = 0;
|
|
|
|
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
// discord rich presence
|
|
|
|
static bool s_discord_presence_active = false;
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
static std::string s_discord_presence_cheevos_string;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static TinyString GetTimestampStringForFileName()
|
|
|
|
{
|
|
|
|
return TinyString::FromFmt("{:%Y-%m-%d_%H-%M-%S}", fmt::localtime(std::time(nullptr)));
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
void System::Internal::ProcessStartup()
|
|
|
|
{
|
|
|
|
// This will call back to Host::LoadSettings() -> ReloadSources().
|
|
|
|
LoadSettings(false);
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
#ifdef WITH_RAINTEGRATION
|
|
|
|
if (Host::GetBaseBoolSettingValue("Cheevos", "UseRAIntegration", false))
|
|
|
|
Achievements::SwitchToRAIntegration();
|
|
|
|
#endif
|
|
|
|
if (g_settings.achievements_enabled)
|
|
|
|
Achievements::Initialize();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::Internal::ProcessShutdown()
|
|
|
|
{
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
ShutdownDiscordPresence();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
Achievements::Shutdown();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
InputManager::CloseSources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::Internal::IdlePollUpdate()
|
|
|
|
{
|
|
|
|
InputManager::PollSources();
|
|
|
|
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
PollDiscordPresence();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
Achievements::ProcessPendingHTTPRequests();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
System::State System::GetState()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_state;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::SetState(State new_state)
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
2020-12-16 15:18:02 +00:00
|
|
|
if (s_state == new_state)
|
|
|
|
return;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
Assert(s_state == State::Paused || s_state == State::Running);
|
|
|
|
Assert(new_state == State::Paused || new_state == State::Running);
|
|
|
|
s_state = new_state;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsRunning()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_state == State::Running;
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
bool System::IsExecutionInterrupted()
|
|
|
|
{
|
|
|
|
return s_state != State::Running || s_system_interrupted;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsPaused()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_state == State::Paused;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsShutdown()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_state == State::Shutdown;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsValid()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
return s_state == State::Running || s_state == State::Paused;
|
2020-07-31 07:09:18 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 07:17:57 +00:00
|
|
|
bool System::IsExecuting()
|
|
|
|
{
|
2023-08-29 11:18:04 +00:00
|
|
|
DebugAssert(s_state != State::Shutdown);
|
2023-08-29 07:17:57 +00:00
|
|
|
return s_system_executing;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsStartupCancelled()
|
2021-02-18 15:48:44 +00:00
|
|
|
{
|
|
|
|
return s_startup_cancelled.load();
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::CancelPendingStartup()
|
2021-02-18 15:48:44 +00:00
|
|
|
{
|
|
|
|
if (s_state == State::Starting)
|
|
|
|
s_startup_cancelled.store(true);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
ConsoleRegion System::GetRegion()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_region;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
DiscRegion System::GetDiscRegion()
|
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
return CDROM::GetDiscRegion();
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool System::IsPALRegion()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_region == ConsoleRegion::PAL;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
TickCount System::GetMaxSliceTicks()
|
2020-09-29 13:29:28 +00:00
|
|
|
{
|
|
|
|
return s_max_slice_ticks;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateOverclock()
|
2020-09-29 13:29:28 +00:00
|
|
|
{
|
|
|
|
g_ticks_per_second = ScaleTicksToOverclock(MASTER_CLOCK);
|
|
|
|
s_max_slice_ticks = ScaleTicksToOverclock(MASTER_CLOCK / 10);
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::CPUClockChanged();
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::CPUClockChanged();
|
2020-09-29 13:29:28 +00:00
|
|
|
g_gpu->CPUClockChanged();
|
2023-01-11 08:58:25 +00:00
|
|
|
Timers::CPUClocksChanged();
|
2020-09-29 13:29:28 +00:00
|
|
|
UpdateThrottlePeriod();
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
u32 System::GetFrameNumber()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_frame_number;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
u32 System::GetInternalFrameNumber()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_internal_frame_number;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
const std::string& System::GetDiscPath()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_running_game_path;
|
|
|
|
}
|
2023-05-15 13:38:37 +00:00
|
|
|
const std::string& System::GetGameSerial()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
return s_running_game_serial;
|
2020-07-31 07:09:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
const std::string& System::GetGameTitle()
|
2019-09-12 02:53:04 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
return s_running_game_title;
|
2019-09-12 02:53:04 +00:00
|
|
|
}
|
2019-09-09 07:01:26 +00:00
|
|
|
|
2023-08-23 08:12:10 +00:00
|
|
|
const GameDatabase::Entry* System::GetGameDatabaseEntry()
|
|
|
|
{
|
|
|
|
return s_running_game_entry;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
System::GameHash System::GetGameHash()
|
|
|
|
{
|
|
|
|
return s_running_game_hash;
|
|
|
|
}
|
|
|
|
|
2023-05-08 03:07:19 +00:00
|
|
|
bool System::IsRunningUnknownGame()
|
2022-10-03 14:20:37 +00:00
|
|
|
{
|
2023-08-19 12:00:20 +00:00
|
|
|
return !s_running_game_entry;
|
2022-10-03 14:20:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
bool System::WasFastBooted()
|
|
|
|
{
|
|
|
|
return s_was_fast_booted;
|
|
|
|
}
|
|
|
|
|
2023-03-16 09:22:54 +00:00
|
|
|
const BIOS::ImageInfo* System::GetBIOSImageInfo()
|
|
|
|
{
|
|
|
|
return s_bios_image_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BIOS::Hash& System::GetBIOSHash()
|
|
|
|
{
|
|
|
|
return s_bios_hash;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float System::GetFPS()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_fps;
|
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
float System::GetVPS()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_vps;
|
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
float System::GetEmulationSpeed()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_speed;
|
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
float System::GetAverageFrameTime()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_average_frame_time;
|
|
|
|
}
|
2023-01-07 03:09:20 +00:00
|
|
|
float System::GetMinimumFrameTime()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2023-01-07 03:09:20 +00:00
|
|
|
return s_minimum_frame_time;
|
|
|
|
}
|
|
|
|
float System::GetMaximumFrameTime()
|
|
|
|
{
|
|
|
|
return s_maximum_frame_time;
|
2020-07-31 07:09:18 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
float System::GetThrottleFrequency()
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
|
|
|
return s_throttle_frequency;
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
float System::GetCPUThreadUsage()
|
|
|
|
{
|
|
|
|
return s_cpu_thread_usage;
|
|
|
|
}
|
|
|
|
float System::GetCPUThreadAverageTime()
|
|
|
|
{
|
|
|
|
return s_cpu_thread_time;
|
|
|
|
}
|
|
|
|
float System::GetSWThreadUsage()
|
|
|
|
{
|
|
|
|
return s_sw_thread_usage;
|
|
|
|
}
|
|
|
|
float System::GetSWThreadAverageTime()
|
|
|
|
{
|
|
|
|
return s_sw_thread_time;
|
|
|
|
}
|
2022-09-03 04:15:15 +00:00
|
|
|
float System::GetGPUUsage()
|
|
|
|
{
|
|
|
|
return s_gpu_usage;
|
|
|
|
}
|
|
|
|
float System::GetGPUAverageTime()
|
|
|
|
{
|
|
|
|
return s_average_gpu_time;
|
|
|
|
}
|
2023-01-07 03:09:20 +00:00
|
|
|
const System::FrameTimeHistory& System::GetFrameTimeHistory()
|
|
|
|
{
|
|
|
|
return s_frame_time_history;
|
|
|
|
}
|
|
|
|
u32 System::GetFrameTimeHistoryPos()
|
|
|
|
{
|
|
|
|
return s_frame_time_history_pos;
|
|
|
|
}
|
2019-09-09 07:01:26 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsExeFileName(const std::string_view& path)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
return (StringUtil::EndsWithNoCase(path, ".exe") || StringUtil::EndsWithNoCase(path, ".psexe") ||
|
|
|
|
StringUtil::EndsWithNoCase(path, ".ps-exe"));
|
2020-09-01 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsPsfFileName(const std::string_view& path)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
return (StringUtil::EndsWithNoCase(path, ".psf") || StringUtil::EndsWithNoCase(path, ".minipsf"));
|
2020-09-01 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsLoadableFilename(const std::string_view& path)
|
2021-02-18 08:07:09 +00:00
|
|
|
{
|
2021-03-07 06:26:12 +00:00
|
|
|
static constexpr auto extensions = make_array(".bin", ".cue", ".img", ".iso", ".chd", ".ecm", ".mds", // discs
|
2022-07-16 16:00:21 +00:00
|
|
|
".exe", ".psexe", ".ps-exe", // exes
|
2021-03-07 06:26:12 +00:00
|
|
|
".psf", ".minipsf", // psf
|
2021-03-12 07:24:12 +00:00
|
|
|
".m3u", // playlists
|
|
|
|
".pbp");
|
2021-02-18 08:07:09 +00:00
|
|
|
|
|
|
|
for (const char* test_extension : extensions)
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (StringUtil::EndsWithNoCase(path, test_extension))
|
2021-02-18 08:07:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsSaveStateFilename(const std::string_view& path)
|
|
|
|
{
|
|
|
|
return StringUtil::EndsWithNoCase(path, ".sav");
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleRegion System::GetConsoleRegionForDiscRegion(DiscRegion region)
|
2020-03-12 03:51:29 +00:00
|
|
|
{
|
|
|
|
switch (region)
|
|
|
|
{
|
|
|
|
case DiscRegion::NTSC_J:
|
|
|
|
return ConsoleRegion::NTSC_J;
|
|
|
|
|
|
|
|
case DiscRegion::NTSC_U:
|
|
|
|
case DiscRegion::Other:
|
2023-05-15 13:38:37 +00:00
|
|
|
case DiscRegion::NonPS1:
|
2020-03-12 03:51:29 +00:00
|
|
|
default:
|
|
|
|
return ConsoleRegion::NTSC_U;
|
|
|
|
|
|
|
|
case DiscRegion::PAL:
|
|
|
|
return ConsoleRegion::PAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
std::string System::GetGameHashId(GameHash hash)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
return StringUtil::StdStringFromFormat("HASH-%" PRIX64, hash);
|
2021-04-17 04:23:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
bool System::GetGameDetailsFromImage(CDImage* cdi, std::string* out_id, GameHash* out_hash)
|
2021-04-17 04:23:47 +00:00
|
|
|
{
|
2021-11-09 02:54:30 +00:00
|
|
|
ISOReader iso;
|
|
|
|
if (!iso.Open(cdi, 1))
|
2023-05-15 13:38:37 +00:00
|
|
|
{
|
|
|
|
if (out_id)
|
|
|
|
out_id->clear();
|
|
|
|
if (out_hash)
|
|
|
|
*out_hash = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-09 02:54:30 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
std::string id;
|
2021-03-06 16:44:09 +00:00
|
|
|
std::string exe_name;
|
|
|
|
std::vector<u8> exe_buffer;
|
2023-05-15 13:38:37 +00:00
|
|
|
if (!ReadExecutableFromImage(iso, &exe_name, &exe_buffer))
|
|
|
|
{
|
|
|
|
if (out_id)
|
|
|
|
out_id->clear();
|
|
|
|
if (out_hash)
|
|
|
|
*out_hash = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-06 16:44:09 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
// Always compute the hash.
|
2021-11-09 02:54:30 +00:00
|
|
|
const u32 track_1_length = cdi->GetTrackLength(1);
|
2021-03-06 16:44:09 +00:00
|
|
|
XXH64_state_t* state = XXH64_createState();
|
|
|
|
XXH64_reset(state, 0x4242D00C);
|
|
|
|
XXH64_update(state, exe_name.c_str(), exe_name.size());
|
|
|
|
XXH64_update(state, exe_buffer.data(), exe_buffer.size());
|
2021-11-09 02:54:30 +00:00
|
|
|
XXH64_update(state, &iso.GetPVD(), sizeof(ISOReader::ISOPrimaryVolumeDescriptor));
|
|
|
|
XXH64_update(state, &track_1_length, sizeof(track_1_length));
|
2023-05-15 13:38:37 +00:00
|
|
|
const GameHash hash = XXH64_digest(state);
|
2021-03-06 16:44:09 +00:00
|
|
|
XXH64_freeState(state);
|
2023-05-15 13:38:37 +00:00
|
|
|
Log_DevPrintf("Hash for '%s' - %" PRIX64, exe_name.c_str(), hash);
|
2021-03-06 16:44:09 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
if (exe_name != FALLBACK_EXE_NAME)
|
|
|
|
{
|
|
|
|
// Strip off any subdirectories.
|
|
|
|
const std::string::size_type slash = exe_name.rfind('\\');
|
|
|
|
if (slash != std::string::npos)
|
|
|
|
id = std::string_view(exe_name).substr(slash + 1);
|
|
|
|
else
|
|
|
|
id = exe_name;
|
|
|
|
|
|
|
|
// SCES_123.45 -> SCES-12345
|
|
|
|
for (std::string::size_type pos = 0; pos < id.size();)
|
|
|
|
{
|
|
|
|
if (id[pos] == '.')
|
|
|
|
{
|
|
|
|
id.erase(pos, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id[pos] == '_')
|
|
|
|
id[pos] = '-';
|
|
|
|
else
|
|
|
|
id[pos] = static_cast<char>(std::toupper(id[pos]));
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 13:12:21 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
if (out_id)
|
|
|
|
{
|
|
|
|
if (id.empty())
|
|
|
|
*out_id = GetGameHashId(hash);
|
|
|
|
else
|
|
|
|
*out_id = std::move(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_hash)
|
|
|
|
*out_hash = hash;
|
|
|
|
|
|
|
|
return true;
|
2021-02-21 06:53:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string System::GetExecutableNameForImage(ISOReader& iso, bool strip_subdirectories)
|
2021-02-21 06:53:29 +00:00
|
|
|
{
|
2020-09-01 02:29:22 +00:00
|
|
|
// Read SYSTEM.CNF
|
|
|
|
std::vector<u8> system_cnf_data;
|
|
|
|
if (!iso.ReadFile("SYSTEM.CNF", &system_cnf_data))
|
2023-05-15 13:38:37 +00:00
|
|
|
return FALLBACK_EXE_NAME;
|
2020-09-01 02:29:22 +00:00
|
|
|
|
|
|
|
// Parse lines
|
|
|
|
std::vector<std::pair<std::string, std::string>> lines;
|
|
|
|
std::pair<std::string, std::string> current_line;
|
|
|
|
bool reading_value = false;
|
|
|
|
for (size_t pos = 0; pos < system_cnf_data.size(); pos++)
|
|
|
|
{
|
|
|
|
const char ch = static_cast<char>(system_cnf_data[pos]);
|
|
|
|
if (ch == '\r' || ch == '\n')
|
|
|
|
{
|
|
|
|
if (!current_line.first.empty())
|
|
|
|
{
|
|
|
|
lines.push_back(std::move(current_line));
|
|
|
|
current_line = {};
|
|
|
|
reading_value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ch == ' ' || (ch >= 0x09 && ch <= 0x0D))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ch == '=' && !reading_value)
|
|
|
|
{
|
|
|
|
reading_value = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (reading_value)
|
|
|
|
current_line.second.push_back(ch);
|
|
|
|
else
|
|
|
|
current_line.first.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current_line.first.empty())
|
|
|
|
lines.push_back(std::move(current_line));
|
|
|
|
|
|
|
|
// Find the BOOT line
|
|
|
|
auto iter = std::find_if(lines.begin(), lines.end(),
|
|
|
|
[](const auto& it) { return StringUtil::Strcasecmp(it.first.c_str(), "boot") == 0; });
|
|
|
|
if (iter == lines.end())
|
2023-05-15 13:38:37 +00:00
|
|
|
{
|
|
|
|
// Fallback to PSX.EXE
|
|
|
|
return FALLBACK_EXE_NAME;
|
|
|
|
}
|
2020-09-01 02:29:22 +00:00
|
|
|
|
|
|
|
std::string code = iter->second;
|
2021-03-01 15:37:25 +00:00
|
|
|
std::string::size_type pos;
|
|
|
|
if (strip_subdirectories)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
2021-03-01 15:37:25 +00:00
|
|
|
// cdrom:\SCES_123.45;1
|
|
|
|
pos = code.rfind('\\');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
code.erase(0, pos + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// cdrom:SCES_123.45;1
|
|
|
|
pos = code.rfind(':');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
code.erase(0, pos + 1);
|
|
|
|
}
|
2020-09-01 02:29:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-01 15:37:25 +00:00
|
|
|
if (code.compare(0, 6, "cdrom:") == 0)
|
|
|
|
code.erase(0, 6);
|
|
|
|
else
|
|
|
|
Log_WarningPrintf("Unknown prefix in executable path: '%s'", code.c_str());
|
|
|
|
|
|
|
|
// remove leading slashes
|
|
|
|
while (code[0] == '/' || code[0] == '\\')
|
|
|
|
code.erase(0, 1);
|
2020-09-01 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 06:53:29 +00:00
|
|
|
// strip off ; or version number
|
|
|
|
pos = code.rfind(';');
|
2020-09-01 02:29:22 +00:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
code.erase(pos);
|
|
|
|
|
2021-02-21 06:53:29 +00:00
|
|
|
return code;
|
|
|
|
}
|
2020-09-01 02:29:22 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
std::string System::GetExecutableNameForImage(CDImage* cdi, bool strip_subdirectories)
|
2021-02-21 06:53:29 +00:00
|
|
|
{
|
|
|
|
ISOReader iso;
|
|
|
|
if (!iso.Open(cdi, 1))
|
|
|
|
return {};
|
2020-09-01 02:29:22 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
return GetExecutableNameForImage(iso, strip_subdirectories);
|
2021-02-21 06:53:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
bool System::ReadExecutableFromImage(CDImage* cdi, std::string* out_executable_name,
|
2023-08-15 13:12:21 +00:00
|
|
|
std::vector<u8>* out_executable_data)
|
2021-02-21 06:53:29 +00:00
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
ISOReader iso;
|
|
|
|
if (!iso.Open(cdi, 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ReadExecutableFromImage(iso, out_executable_name, out_executable_data);
|
|
|
|
}
|
2021-02-21 06:53:29 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
bool System::ReadExecutableFromImage(ISOReader& iso, std::string* out_executable_name,
|
|
|
|
std::vector<u8>* out_executable_data)
|
2023-05-15 13:38:37 +00:00
|
|
|
{
|
|
|
|
const std::string executable_path = GetExecutableNameForImage(iso, false);
|
2021-03-01 15:37:25 +00:00
|
|
|
Log_DevPrintf("Executable path: '%s'", executable_path.c_str());
|
2023-05-15 13:38:37 +00:00
|
|
|
if (!executable_path.empty() && out_executable_data)
|
2021-02-21 06:53:29 +00:00
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
if (!iso.ReadFile(executable_path.c_str(), out_executable_data))
|
|
|
|
{
|
2021-03-01 15:37:25 +00:00
|
|
|
Log_ErrorPrintf("Failed to read executable '%s' from disc", executable_path.c_str());
|
2023-05-15 13:38:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-01 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 06:53:29 +00:00
|
|
|
if (out_executable_name)
|
2021-03-01 15:37:25 +00:00
|
|
|
*out_executable_name = std::move(executable_path);
|
2021-02-21 06:53:29 +00:00
|
|
|
|
|
|
|
return true;
|
2020-09-01 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
DiscRegion System::GetRegionForSerial(std::string_view serial)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
|
|
|
std::string prefix;
|
2022-10-05 08:29:08 +00:00
|
|
|
for (size_t pos = 0; pos < serial.length(); pos++)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
const int ch = std::tolower(serial[pos]);
|
2020-09-01 02:29:22 +00:00
|
|
|
if (ch < 'a' || ch > 'z')
|
|
|
|
break;
|
|
|
|
|
|
|
|
prefix.push_back(static_cast<char>(ch));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix == "sces" || prefix == "sced" || prefix == "sles" || prefix == "sled")
|
|
|
|
return DiscRegion::PAL;
|
|
|
|
else if (prefix == "scps" || prefix == "slps" || prefix == "slpm" || prefix == "sczs" || prefix == "papx")
|
|
|
|
return DiscRegion::NTSC_J;
|
|
|
|
else if (prefix == "scus" || prefix == "slus")
|
|
|
|
return DiscRegion::NTSC_U;
|
|
|
|
else
|
|
|
|
return DiscRegion::Other;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
DiscRegion System::GetRegionFromSystemArea(CDImage* cdi)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
|
|
|
// The license code is on sector 4 of the disc.
|
|
|
|
u8 sector[CDImage::DATA_SECTOR_SIZE];
|
|
|
|
if (!cdi->Seek(1, 4) || cdi->Read(CDImage::ReadMode::DataOnly, 1, sector) != 1)
|
|
|
|
return DiscRegion::Other;
|
|
|
|
|
|
|
|
static constexpr char ntsc_u_string[] = " Licensed by Sony Computer Entertainment Amer ica ";
|
|
|
|
static constexpr char ntsc_j_string[] = " Licensed by Sony Computer Entertainment Inc.";
|
|
|
|
static constexpr char pal_string[] = " Licensed by Sony Computer Entertainment Euro pe";
|
|
|
|
|
|
|
|
// subtract one for the terminating null
|
|
|
|
if (std::equal(ntsc_u_string, ntsc_u_string + countof(ntsc_u_string) - 1, sector))
|
|
|
|
return DiscRegion::NTSC_U;
|
|
|
|
else if (std::equal(ntsc_j_string, ntsc_j_string + countof(ntsc_j_string) - 1, sector))
|
|
|
|
return DiscRegion::NTSC_J;
|
|
|
|
else if (std::equal(pal_string, pal_string + countof(pal_string) - 1, sector))
|
|
|
|
return DiscRegion::PAL;
|
|
|
|
else
|
|
|
|
return DiscRegion::Other;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
DiscRegion System::GetRegionForImage(CDImage* cdi)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
const DiscRegion system_area_region = GetRegionFromSystemArea(cdi);
|
2020-09-01 02:29:22 +00:00
|
|
|
if (system_area_region != DiscRegion::Other)
|
|
|
|
return system_area_region;
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
ISOReader iso;
|
|
|
|
if (!iso.Open(cdi, 1))
|
|
|
|
return DiscRegion::NonPS1;
|
|
|
|
|
|
|
|
// The executable must exist, because this just returns PSX.EXE if it doesn't.
|
|
|
|
const std::string exename = GetExecutableNameForImage(iso, false);
|
|
|
|
if (exename.empty() || !iso.FileExists(exename.c_str()))
|
|
|
|
return DiscRegion::NonPS1;
|
2020-09-01 02:29:22 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
// Strip off any subdirectories.
|
|
|
|
const std::string::size_type slash = exename.rfind('\\');
|
|
|
|
if (slash != std::string::npos)
|
|
|
|
return GetRegionForSerial(std::string_view(exename).substr(slash + 1));
|
|
|
|
else
|
|
|
|
return GetRegionForSerial(exename);
|
2020-09-01 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
DiscRegion System::GetRegionForExe(const char* path)
|
2021-01-24 04:05:45 +00:00
|
|
|
{
|
|
|
|
auto fp = FileSystem::OpenManagedCFile(path, "rb");
|
|
|
|
if (!fp)
|
|
|
|
return DiscRegion::Other;
|
|
|
|
|
|
|
|
BIOS::PSEXEHeader header;
|
|
|
|
if (std::fread(&header, sizeof(header), 1, fp.get()) != 1)
|
|
|
|
return DiscRegion::Other;
|
|
|
|
|
|
|
|
return BIOS::GetPSExeDiscRegion(header);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
DiscRegion System::GetRegionForPsf(const char* path)
|
2021-01-24 04:05:45 +00:00
|
|
|
{
|
|
|
|
PSFLoader::File psf;
|
|
|
|
if (!psf.Load(path))
|
|
|
|
return DiscRegion::Other;
|
|
|
|
|
|
|
|
return psf.GetRegion();
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::optional<DiscRegion> System::GetRegionForPath(const char* image_path)
|
2020-09-01 02:29:22 +00:00
|
|
|
{
|
2021-01-24 04:05:45 +00:00
|
|
|
if (IsExeFileName(image_path))
|
|
|
|
return GetRegionForExe(image_path);
|
|
|
|
else if (IsPsfFileName(image_path))
|
|
|
|
return GetRegionForPsf(image_path);
|
|
|
|
|
2022-08-27 06:52:24 +00:00
|
|
|
std::unique_ptr<CDImage> cdi = CDImage::Open(image_path, false, nullptr);
|
2020-09-01 02:29:22 +00:00
|
|
|
if (!cdi)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return GetRegionForImage(cdi.get());
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string System::GetGameSettingsPath(const std::string_view& game_serial)
|
|
|
|
{
|
2022-07-30 11:46:14 +00:00
|
|
|
const std::string sanitized_serial(Path::SanitizeFileName(game_serial));
|
2022-07-11 13:03:29 +00:00
|
|
|
return Path::Combine(EmuFolders::GameSettings, fmt::format("{}.ini", sanitized_serial));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string System::GetInputProfilePath(const std::string_view& name)
|
|
|
|
{
|
|
|
|
return Path::Combine(EmuFolders::InputProfiles, fmt::format("{}.ini", name));
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:48:40 +00:00
|
|
|
bool System::RecreateGPU(GPURenderer renderer, bool force_recreate_device, bool update_display /* = true*/)
|
2019-10-04 05:00:32 +00:00
|
|
|
{
|
2021-01-23 09:00:54 +00:00
|
|
|
ClearMemorySaveStates();
|
2020-08-06 13:19:52 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
2019-10-26 02:55:56 +00:00
|
|
|
// save current state
|
2022-07-08 10:11:31 +00:00
|
|
|
std::unique_ptr<ByteStream> state_stream = ByteStream::CreateGrowableMemoryStream();
|
2020-10-12 10:46:59 +00:00
|
|
|
StateWrapper sw(state_stream.get(), StateWrapper::Mode::Write, SAVE_STATE_VERSION);
|
2021-01-23 09:00:54 +00:00
|
|
|
const bool state_valid = g_gpu->DoState(sw, nullptr, false) && TimingEvents::DoState(sw);
|
2019-10-26 02:55:56 +00:00
|
|
|
if (!state_valid)
|
|
|
|
Log_ErrorPrintf("Failed to save old GPU state when switching renderers");
|
|
|
|
|
|
|
|
// create new renderer
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu.reset();
|
2023-08-27 12:48:40 +00:00
|
|
|
if (force_recreate_device)
|
2023-08-13 03:42:02 +00:00
|
|
|
Host::ReleaseGPUDevice();
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2023-08-27 12:48:40 +00:00
|
|
|
if (!CreateGPU(renderer, true))
|
2019-10-26 02:55:56 +00:00
|
|
|
{
|
2021-02-18 15:48:44 +00:00
|
|
|
if (!IsStartupCancelled())
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::ReportErrorAsync("Error", "Failed to recreate GPU.");
|
2021-02-18 15:48:44 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
DestroySystem();
|
2019-10-26 02:55:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state_valid)
|
|
|
|
{
|
|
|
|
state_stream->SeekAbsolute(0);
|
|
|
|
sw.SetMode(StateWrapper::Mode::Read);
|
2020-08-06 13:19:52 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
2021-01-23 09:00:54 +00:00
|
|
|
g_gpu->DoState(sw, nullptr, update_display);
|
2020-08-15 04:56:20 +00:00
|
|
|
TimingEvents::DoState(sw);
|
2019-10-26 02:55:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 03:54:20 +00:00
|
|
|
// fix up vsync etc
|
|
|
|
UpdateSpeedLimiterState();
|
2019-10-26 02:55:56 +00:00
|
|
|
return true;
|
2019-10-04 05:00:32 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::LoadSettings(bool display_osd_messages)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
std::unique_lock<std::mutex> lock = Host::GetSettingsLock();
|
|
|
|
SettingsInterface& si = *Host::GetSettingsInterface();
|
|
|
|
g_settings.Load(si);
|
2023-08-23 12:06:48 +00:00
|
|
|
g_settings.UpdateLogSettings();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::LoadSettings(si, lock);
|
2023-08-23 12:06:48 +00:00
|
|
|
InputManager::ReloadSources(si, lock);
|
|
|
|
InputManager::ReloadBindings(si, *Host::GetSettingsInterfaceForBindings());
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
// apply compatibility settings
|
2022-10-05 08:29:08 +00:00
|
|
|
if (g_settings.apply_compatibility_settings && !s_running_game_serial.empty())
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
const GameDatabase::Entry* entry = GameDatabase::GetEntryForSerial(s_running_game_serial);
|
2022-07-11 13:03:29 +00:00
|
|
|
if (entry)
|
|
|
|
entry->ApplySettings(g_settings, display_osd_messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_settings.FixIncompatibleSettings(display_osd_messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::SetDefaultSettings(SettingsInterface& si)
|
|
|
|
{
|
|
|
|
Settings temp;
|
2022-10-08 13:36:39 +00:00
|
|
|
|
|
|
|
// we don't want to reset some things (e.g. OSD)
|
|
|
|
temp.display_show_osd_messages = g_settings.display_show_osd_messages;
|
|
|
|
temp.display_show_fps = g_settings.display_show_fps;
|
|
|
|
temp.display_show_speed = g_settings.display_show_speed;
|
|
|
|
temp.display_show_resolution = g_settings.display_show_resolution;
|
|
|
|
temp.display_show_cpu = g_settings.display_show_cpu;
|
|
|
|
temp.display_show_gpu = g_settings.display_show_gpu;
|
2023-01-07 03:09:20 +00:00
|
|
|
temp.display_show_frame_times = g_settings.display_show_frame_times;
|
2022-10-08 13:36:39 +00:00
|
|
|
|
2022-10-20 14:29:42 +00:00
|
|
|
// keep controller, we reset it elsewhere
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
temp.controller_types[i] = g_settings.controller_types[i];
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
temp.Save(si);
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ApplySettings(bool display_osd_messages)
|
|
|
|
{
|
|
|
|
Log_DevPrint("Applying settings...");
|
|
|
|
|
|
|
|
const Settings old_config(std::move(g_settings));
|
|
|
|
g_settings = Settings();
|
|
|
|
LoadSettings(display_osd_messages);
|
2022-07-22 14:13:55 +00:00
|
|
|
|
|
|
|
// If we've disabled/enabled game settings, we need to reload without it.
|
|
|
|
if (g_settings.apply_game_settings != old_config.apply_game_settings)
|
|
|
|
{
|
|
|
|
UpdateGameSettingsLayer();
|
|
|
|
LoadSettings(display_osd_messages);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
CheckForSettingsChanges(old_config);
|
|
|
|
Host::CheckForSettingsChanges(old_config);
|
|
|
|
|
|
|
|
if (IsValid())
|
2023-08-15 13:12:21 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
ResetPerformanceCounters();
|
2023-08-15 13:12:21 +00:00
|
|
|
if (s_system_executing)
|
|
|
|
s_system_interrupted = true;
|
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool System::ReloadGameSettings(bool display_osd_messages)
|
|
|
|
{
|
|
|
|
if (!IsValid() || !UpdateGameSettingsLayer())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ApplySettings(display_osd_messages);
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::UpdateGameSettingsLayer()
|
|
|
|
{
|
|
|
|
std::unique_ptr<INISettingsInterface> new_interface;
|
2022-10-05 08:29:08 +00:00
|
|
|
if (g_settings.apply_game_settings && !s_running_game_serial.empty())
|
2020-08-15 10:38:17 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
std::string filename(GetGameSettingsPath(s_running_game_serial));
|
2022-07-11 13:03:29 +00:00
|
|
|
if (FileSystem::FileExists(filename.c_str()))
|
2020-08-15 10:38:17 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Log_InfoPrintf("Loading game settings from '%s'...", filename.c_str());
|
|
|
|
new_interface = std::make_unique<INISettingsInterface>(std::move(filename));
|
|
|
|
if (!new_interface->Load())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to parse game settings ini '%s'", new_interface->GetFileName().c_str());
|
|
|
|
new_interface.reset();
|
|
|
|
}
|
2020-08-15 10:38:17 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("No game settings found (tried '%s')", filename.c_str());
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 10:38:17 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string input_profile_name;
|
2022-08-10 03:09:48 +00:00
|
|
|
bool use_game_settings_for_controller = false;
|
2022-07-11 13:03:29 +00:00
|
|
|
if (new_interface)
|
2022-08-10 03:09:48 +00:00
|
|
|
{
|
|
|
|
new_interface->GetBoolValue("ControllerPorts", "UseGameSettingsForController", &use_game_settings_for_controller);
|
|
|
|
if (!use_game_settings_for_controller)
|
|
|
|
new_interface->GetStringValue("ControllerPorts", "InputProfileName", &input_profile_name);
|
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (!s_game_settings_interface && !new_interface && s_input_profile_name == input_profile_name)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Host::Internal::SetGameSettingsLayer(new_interface.get());
|
|
|
|
s_game_settings_interface = std::move(new_interface);
|
|
|
|
|
|
|
|
std::unique_ptr<INISettingsInterface> input_interface;
|
2022-08-10 03:09:48 +00:00
|
|
|
if (!use_game_settings_for_controller)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-08-10 03:09:48 +00:00
|
|
|
if (!input_profile_name.empty())
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-08-10 03:09:48 +00:00
|
|
|
const std::string filename(GetInputProfilePath(input_profile_name));
|
|
|
|
if (FileSystem::FileExists(filename.c_str()))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-08-10 03:09:48 +00:00
|
|
|
Log_InfoPrintf("Loading input profile from '%s'...", filename.c_str());
|
|
|
|
input_interface = std::make_unique<INISettingsInterface>(std::move(filename));
|
|
|
|
if (!input_interface->Load())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to parse input profile ini '%s'", input_interface->GetFileName().c_str());
|
|
|
|
input_interface.reset();
|
|
|
|
input_profile_name = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("No input profile found (tried '%s')", filename.c_str());
|
2022-07-11 13:03:29 +00:00
|
|
|
input_profile_name = {};
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 03:09:48 +00:00
|
|
|
|
2022-10-03 06:44:34 +00:00
|
|
|
Host::Internal::SetInputSettingsLayer(input_interface ? input_interface.get() :
|
|
|
|
Host::Internal::GetBaseSettingsLayer());
|
2022-08-10 03:09:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// using game settings for bindings too
|
|
|
|
Host::Internal::SetInputSettingsLayer(s_game_settings_interface.get());
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
2020-12-26 14:15:12 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
s_input_settings_interface = std::move(input_interface);
|
|
|
|
s_input_profile_name = std::move(input_profile_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ResetSystem()
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
2022-09-21 12:02:54 +00:00
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
if (!Achievements::ConfirmSystemReset())
|
|
|
|
return;
|
2022-10-08 10:59:18 +00:00
|
|
|
|
|
|
|
if (Achievements::ResetChallengeMode())
|
|
|
|
ApplySettings(false);
|
2022-09-21 12:02:54 +00:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
InternalReset();
|
|
|
|
ResetPerformanceCounters();
|
|
|
|
ResetThrottler();
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(TRANSLATE_STR("OSDMessage", "System reset."));
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void System::PauseSystem(bool paused)
|
|
|
|
{
|
|
|
|
if (paused == IsPaused() || !IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SetState(paused ? State::Paused : State::Running);
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::GetOutputStream()->SetPaused(paused);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (paused)
|
|
|
|
{
|
2023-08-23 12:06:48 +00:00
|
|
|
FullscreenUI::OnSystemPaused();
|
|
|
|
|
|
|
|
InputManager::PauseVibration();
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
Achievements::OnSystemPaused(true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (g_settings.inhibit_screensaver)
|
|
|
|
PlatformMisc::ResumeScreensaver();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::OnSystemPaused();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-23 12:06:48 +00:00
|
|
|
FullscreenUI::OnSystemResumed();
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
Achievements::OnSystemPaused(false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (g_settings.inhibit_screensaver)
|
|
|
|
PlatformMisc::SuspendScreensaver();
|
|
|
|
|
2022-08-10 03:09:32 +00:00
|
|
|
UpdateDisplaySync();
|
2023-08-23 12:06:48 +00:00
|
|
|
|
|
|
|
Host::OnSystemResumed();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
ResetPerformanceCounters();
|
|
|
|
ResetThrottler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::LoadState(const char* filename)
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
if (Achievements::ChallengeModeActive() &&
|
2023-08-19 14:08:03 +00:00
|
|
|
!Achievements::ConfirmChallengeModeDisable(TRANSLATE("Achievements", "Loading state")))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 11:21:22 +00:00
|
|
|
Common::Timer load_timer;
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::unique_ptr<ByteStream> stream = ByteStream::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
|
|
|
if (!stream)
|
|
|
|
return false;
|
|
|
|
|
2022-07-31 07:33:03 +00:00
|
|
|
Log_InfoPrintf("Loading state from '%s'...", filename);
|
2022-08-05 07:07:40 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const std::string display_name(FileSystem::GetDisplayNameFromPath(filename));
|
|
|
|
Host::AddIconOSDMessage(
|
|
|
|
"load_state", ICON_FA_FOLDER_OPEN,
|
2023-08-19 14:08:03 +00:00
|
|
|
fmt::format(TRANSLATE_FS("OSDMessage", "Loading state from '{}'..."), Path::GetFileName(display_name)), 5.0f);
|
2022-08-05 07:07:40 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
SaveUndoLoadState();
|
|
|
|
|
|
|
|
if (!DoLoadState(stream.get(), false, true))
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::ReportFormattedErrorAsync("Load State Error",
|
|
|
|
TRANSLATE("OSDMessage", "Loading state from '%s' failed. Resetting."), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (m_undo_load_state)
|
|
|
|
UndoLoadState();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetPerformanceCounters();
|
|
|
|
ResetThrottler();
|
2022-08-18 13:51:43 +00:00
|
|
|
Host::RenderDisplay(false);
|
2022-08-18 11:21:22 +00:00
|
|
|
Log_VerbosePrintf("Loading state took %.2f msec", load_timer.GetTimeMilliseconds());
|
2022-07-11 13:03:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::SaveState(const char* filename, bool backup_existing_save)
|
|
|
|
{
|
|
|
|
if (backup_existing_save && FileSystem::FileExists(filename))
|
|
|
|
{
|
|
|
|
const std::string backup_filename(Path::ReplaceExtension(filename, "bak"));
|
|
|
|
if (!FileSystem::RenamePath(filename, backup_filename.c_str()))
|
|
|
|
Log_ErrorPrintf("Failed to rename save state backup '%s'", backup_filename.c_str());
|
|
|
|
}
|
|
|
|
|
2022-08-18 11:21:22 +00:00
|
|
|
Common::Timer save_timer;
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::unique_ptr<ByteStream> stream =
|
|
|
|
ByteStream::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE | BYTESTREAM_OPEN_TRUNCATE |
|
|
|
|
BYTESTREAM_OPEN_ATOMIC_UPDATE | BYTESTREAM_OPEN_STREAMED);
|
|
|
|
if (!stream)
|
|
|
|
return false;
|
|
|
|
|
2022-07-31 07:33:03 +00:00
|
|
|
Log_InfoPrintf("Saving state to '%s'...", filename);
|
|
|
|
|
2022-08-18 11:21:22 +00:00
|
|
|
const u32 screenshot_size = 256;
|
|
|
|
const bool result = InternalSaveState(stream.get(), screenshot_size,
|
|
|
|
g_settings.compress_save_states ? SAVE_STATE_HEADER::COMPRESSION_TYPE_ZSTD :
|
|
|
|
SAVE_STATE_HEADER::COMPRESSION_TYPE_NONE);
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!result)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::ReportFormattedErrorAsync(TRANSLATE("OSDMessage", "Save State"),
|
|
|
|
TRANSLATE("OSDMessage", "Saving state to '%s' failed."), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
stream->Discard();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-05 07:07:07 +00:00
|
|
|
const std::string display_name(FileSystem::GetDisplayNameFromPath(filename));
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddIconOSDMessage(
|
|
|
|
"save_state", ICON_FA_SAVE,
|
|
|
|
fmt::format(TRANSLATE_FS("OSDMessage", "State saved to '{}'."), Path::GetFileName(display_name)), 5.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
stream->Commit();
|
|
|
|
}
|
|
|
|
|
2022-08-18 11:21:22 +00:00
|
|
|
Log_VerbosePrintf("Saving state took %.2f msec", save_timer.GetTimeMilliseconds());
|
2022-07-11 13:03:29 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:07:07 +00:00
|
|
|
bool System::SaveResumeState()
|
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
if (s_running_game_serial.empty())
|
2022-08-05 07:07:07 +00:00
|
|
|
return false;
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
const std::string path(GetGameSaveStateFileName(s_running_game_serial, -1));
|
2022-08-05 07:07:07 +00:00
|
|
|
return SaveState(path.c_str(), false);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::BootSystem(SystemBootParameters parameters)
|
|
|
|
{
|
2022-07-23 03:23:54 +00:00
|
|
|
if (!parameters.save_state.empty())
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-07-23 03:23:54 +00:00
|
|
|
// loading a state, so pull the media path from the save state to avoid a double change
|
2022-08-13 03:59:58 +00:00
|
|
|
std::string state_media(GetMediaPathFromSaveState(parameters.save_state.c_str()));
|
|
|
|
if (FileSystem::FileExists(state_media.c_str()))
|
|
|
|
parameters.filename = std::move(state_media);
|
2020-08-15 10:38:17 +00:00
|
|
|
}
|
2020-05-26 16:01:09 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
if (parameters.filename.empty())
|
|
|
|
Log_InfoPrintf("Boot Filename: <BIOS/Shell>");
|
|
|
|
else
|
|
|
|
Log_InfoPrintf("Boot Filename: %s", parameters.filename.c_str());
|
|
|
|
|
|
|
|
Assert(s_state == State::Shutdown);
|
|
|
|
s_state = State::Starting;
|
|
|
|
s_startup_cancelled.store(false);
|
2023-08-13 03:42:02 +00:00
|
|
|
s_keep_gpu_device_on_shutdown = static_cast<bool>(g_gpu_device);
|
2022-07-11 13:03:29 +00:00
|
|
|
s_region = g_settings.region;
|
|
|
|
Host::OnSystemStarting();
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
// Load CD image up and detect region.
|
2023-08-19 13:40:36 +00:00
|
|
|
Error error;
|
2023-05-15 13:38:37 +00:00
|
|
|
std::unique_ptr<CDImage> disc;
|
|
|
|
DiscRegion disc_region = DiscRegion::NonPS1;
|
2023-01-11 09:40:56 +00:00
|
|
|
std::string exe_boot;
|
|
|
|
std::string psf_boot;
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!parameters.filename.empty())
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2023-01-11 09:40:56 +00:00
|
|
|
const bool do_exe_boot = IsExeFileName(parameters.filename);
|
|
|
|
const bool do_psf_boot = (!do_exe_boot && IsPsfFileName(parameters.filename));
|
|
|
|
if (do_exe_boot || do_psf_boot)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_region == ConsoleRegion::Auto)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2021-01-24 04:05:45 +00:00
|
|
|
const DiscRegion file_region =
|
2023-01-11 09:40:56 +00:00
|
|
|
(do_exe_boot ? GetRegionForExe(parameters.filename.c_str()) : GetRegionForPsf(parameters.filename.c_str()));
|
2021-01-24 04:05:45 +00:00
|
|
|
Log_InfoPrintf("EXE/PSF Region: %s", Settings::GetDiscRegionDisplayName(file_region));
|
|
|
|
s_region = GetConsoleRegionForDiscRegion(file_region);
|
2023-01-11 09:40:56 +00:00
|
|
|
if (do_psf_boot)
|
|
|
|
psf_boot = std::move(parameters.filename);
|
|
|
|
else
|
|
|
|
exe_boot = std::move(parameters.filename);
|
2019-11-16 05:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Log_InfoPrintf("Loading CD image '%s'...", parameters.filename.c_str());
|
2023-05-15 13:38:37 +00:00
|
|
|
disc = CDImage::Open(parameters.filename.c_str(), g_settings.cdrom_load_image_patches, &error);
|
|
|
|
if (!disc)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::ReportErrorAsync("Error", fmt::format("Failed to load CD image '{}': {}",
|
2023-08-19 13:40:36 +00:00
|
|
|
Path::GetFileName(parameters.filename), error.GetDescription()));
|
2022-07-11 13:03:29 +00:00
|
|
|
s_state = State::Shutdown;
|
|
|
|
Host::OnSystemDestroyed();
|
2019-11-16 05:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-24 10:33:24 +00:00
|
|
|
disc_region = GetRegionForImage(disc.get());
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_region == ConsoleRegion::Auto)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-03-12 03:51:29 +00:00
|
|
|
if (disc_region != DiscRegion::Other)
|
2019-12-05 07:09:10 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
s_region = GetConsoleRegionForDiscRegion(disc_region);
|
2020-03-12 03:51:29 +00:00
|
|
|
Log_InfoPrintf("Auto-detected console %s region for '%s' (region %s)",
|
2022-07-11 13:03:29 +00:00
|
|
|
Settings::GetConsoleRegionName(s_region), parameters.filename.c_str(),
|
2020-03-12 03:51:29 +00:00
|
|
|
Settings::GetDiscRegionName(disc_region));
|
2019-12-05 07:09:10 +00:00
|
|
|
}
|
2019-11-16 05:27:57 +00:00
|
|
|
else
|
2019-12-05 07:09:10 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
s_region = ConsoleRegion::NTSC_U;
|
2020-03-12 03:51:29 +00:00
|
|
|
Log_WarningPrintf("Could not determine console region for disc region %s. Defaulting to %s.",
|
2020-07-31 07:09:18 +00:00
|
|
|
Settings::GetDiscRegionName(disc_region), Settings::GetConsoleRegionName(s_region));
|
2019-12-05 07:09:10 +00:00
|
|
|
}
|
2019-11-16 05:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 10:13:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Default to NTSC for BIOS boot.
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_region == ConsoleRegion::Auto)
|
|
|
|
s_region = ConsoleRegion::NTSC_U;
|
2019-11-16 10:13:33 +00:00
|
|
|
}
|
2019-11-16 05:27:57 +00:00
|
|
|
|
2021-01-24 04:05:45 +00:00
|
|
|
Log_InfoPrintf("Console Region: %s", Settings::GetConsoleRegionDisplayName(s_region));
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
// Switch subimage.
|
2023-05-15 13:38:37 +00:00
|
|
|
if (disc && parameters.media_playlist_index != 0 && !disc->SwitchSubImage(parameters.media_playlist_index, &error))
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2023-08-19 13:40:36 +00:00
|
|
|
Host::ReportErrorAsync("Error",
|
|
|
|
fmt::format("Failed to switch to subimage {] in '{}': {}", parameters.media_playlist_index,
|
|
|
|
parameters.filename, error.GetDescription()));
|
2022-07-11 13:03:29 +00:00
|
|
|
s_state = State::Shutdown;
|
|
|
|
Host::OnSystemDestroyed();
|
2019-11-16 05:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-06 08:09:40 +00:00
|
|
|
// Update running game, this will apply settings as well.
|
2023-05-15 13:38:37 +00:00
|
|
|
UpdateRunningGame(disc ? disc->GetFileName().c_str() : parameters.filename.c_str(), disc.get(), true);
|
2022-12-06 08:09:40 +00:00
|
|
|
|
2023-01-11 09:40:56 +00:00
|
|
|
if (!parameters.override_exe.empty())
|
|
|
|
{
|
|
|
|
if (!FileSystem::FileExists(parameters.override_exe.c_str()) || !IsExeFileName(parameters.override_exe))
|
|
|
|
{
|
|
|
|
Host::ReportFormattedErrorAsync("Error", "File '%s' is not a valid executable to boot.",
|
|
|
|
parameters.override_exe.c_str());
|
|
|
|
s_state = State::Shutdown;
|
|
|
|
Host::OnSystemDestroyed();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_InfoPrintf("Overriding boot executable: '%s'", parameters.override_exe.c_str());
|
|
|
|
exe_boot = std::move(parameters.override_exe);
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:04:47 +00:00
|
|
|
// Check for SBI.
|
2023-05-15 13:38:37 +00:00
|
|
|
if (!CheckForSBIFile(disc.get()))
|
2021-02-10 16:04:47 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
s_state = State::Shutdown;
|
2022-12-06 08:09:40 +00:00
|
|
|
ClearRunningGame();
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::OnSystemDestroyed();
|
2021-02-10 16:04:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
// Check for resuming with hardcore mode.
|
|
|
|
if (!parameters.save_state.empty() && Achievements::ChallengeModeActive() &&
|
2023-08-19 14:08:03 +00:00
|
|
|
!Achievements::ConfirmChallengeModeDisable(TRANSLATE("Achievements", "Resuming state")))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
s_state = State::Shutdown;
|
|
|
|
ClearRunningGame();
|
|
|
|
Host::OnSystemDestroyed();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Load BIOS image.
|
2023-03-16 09:22:54 +00:00
|
|
|
if (!LoadBIOS())
|
2021-03-26 16:19:23 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
s_state = State::Shutdown;
|
|
|
|
ClearRunningGame();
|
|
|
|
Host::OnSystemDestroyed();
|
2021-03-26 16:19:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-28 15:51:01 +00:00
|
|
|
// Component setup.
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!Initialize(parameters.force_software_renderer))
|
2020-07-31 07:09:18 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
s_state = State::Shutdown;
|
|
|
|
ClearRunningGame();
|
|
|
|
Host::OnSystemDestroyed();
|
2020-06-28 15:51:01 +00:00
|
|
|
return false;
|
2020-07-31 07:09:18 +00:00
|
|
|
}
|
2020-06-28 15:51:01 +00:00
|
|
|
|
2019-12-14 13:20:24 +00:00
|
|
|
UpdateControllers();
|
2021-05-24 09:55:07 +00:00
|
|
|
UpdateMemoryCardTypes();
|
2021-01-21 07:59:40 +00:00
|
|
|
UpdateMultitaps();
|
2022-07-11 13:03:29 +00:00
|
|
|
InternalReset();
|
2019-11-16 05:27:57 +00:00
|
|
|
|
|
|
|
// Load EXE late after BIOS.
|
2023-01-11 09:40:56 +00:00
|
|
|
if (!exe_boot.empty() && !LoadEXE(exe_boot.c_str()))
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2023-01-11 09:40:56 +00:00
|
|
|
Host::ReportFormattedErrorAsync("Error", "Failed to load EXE file '%s'", exe_boot.c_str());
|
2022-07-11 13:03:29 +00:00
|
|
|
DestroySystem();
|
2019-11-16 05:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-01-11 09:40:56 +00:00
|
|
|
else if (!psf_boot.empty() && !PSFLoader::Load(psf_boot.c_str()))
|
2020-04-16 14:29:56 +00:00
|
|
|
{
|
2023-01-11 09:40:56 +00:00
|
|
|
Host::ReportFormattedErrorAsync("Error", "Failed to load PSF file '%s'", psf_boot.c_str());
|
2022-07-11 13:03:29 +00:00
|
|
|
DestroySystem();
|
2020-04-16 14:29:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-16 05:27:57 +00:00
|
|
|
|
|
|
|
// Insert CD, and apply fastboot patch if enabled.
|
2023-05-15 13:38:37 +00:00
|
|
|
if (disc)
|
|
|
|
CDROM::InsertMedia(std::move(disc), disc_region);
|
2023-01-09 09:34:40 +00:00
|
|
|
if (CDROM::HasMedia() && (parameters.override_fast_boot.has_value() ? parameters.override_fast_boot.value() :
|
|
|
|
g_settings.bios_patch_fast_boot))
|
2020-03-02 01:08:16 +00:00
|
|
|
{
|
2023-03-16 09:22:54 +00:00
|
|
|
if (s_bios_image_info && s_bios_image_info->patch_compatible)
|
2023-08-15 13:12:21 +00:00
|
|
|
{
|
|
|
|
// TODO: Fast boot without patches...
|
2022-11-15 11:22:42 +00:00
|
|
|
BIOS::PatchBIOSFastBoot(Bus::g_bios, Bus::BIOS_SIZE);
|
2023-08-15 13:12:21 +00:00
|
|
|
s_was_fast_booted = true;
|
|
|
|
}
|
2022-11-15 11:22:42 +00:00
|
|
|
else
|
2023-08-15 13:12:21 +00:00
|
|
|
{
|
2022-11-15 11:22:42 +00:00
|
|
|
Log_ErrorPrintf("Not patching fast boot, as BIOS is not patch compatible.");
|
2023-08-15 13:12:21 +00:00
|
|
|
}
|
2020-03-02 01:08:16 +00:00
|
|
|
}
|
2019-11-16 05:27:57 +00:00
|
|
|
|
|
|
|
// Good to go.
|
2023-08-23 12:06:48 +00:00
|
|
|
s_state = State::Running;
|
2022-07-11 13:03:29 +00:00
|
|
|
UpdateSoftwareCursor();
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::GetOutputStream()->SetPaused(false);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
FullscreenUI::OnSystemStarted();
|
|
|
|
|
|
|
|
if (g_settings.inhibit_screensaver)
|
|
|
|
PlatformMisc::SuspendScreensaver();
|
|
|
|
|
|
|
|
Host::OnSystemStarted();
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
// try to load the state, if it fails, bail out
|
|
|
|
if (!parameters.save_state.empty())
|
|
|
|
{
|
|
|
|
std::unique_ptr<ByteStream> stream =
|
|
|
|
ByteStream::OpenFile(parameters.save_state.c_str(), BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
|
|
|
|
if (!stream)
|
|
|
|
{
|
|
|
|
Host::ReportErrorAsync(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE("System", "Error"),
|
|
|
|
fmt::format(TRANSLATE_FS("System", "Failed to load save state file '{}' for booting."), parameters.save_state));
|
2022-07-11 13:03:29 +00:00
|
|
|
DestroySystem();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoLoadState(stream.get(), false, true))
|
|
|
|
{
|
|
|
|
DestroySystem();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 03:23:54 +00:00
|
|
|
if (parameters.load_image_to_ram || g_settings.cdrom_load_image_to_ram)
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::PrecacheMedia();
|
2022-07-23 03:23:54 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
if (parameters.fast_forward_to_first_frame)
|
|
|
|
FastForwardToFirstFrame();
|
|
|
|
|
2022-11-21 02:55:24 +00:00
|
|
|
if (g_settings.audio_dump_on_boot)
|
|
|
|
StartDumpingAudio();
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
if (g_settings.start_paused || parameters.override_start_paused.value_or(false))
|
|
|
|
PauseSystem(true);
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
ResetPerformanceCounters();
|
2022-07-27 15:39:01 +00:00
|
|
|
if (IsRunning())
|
|
|
|
UpdateSpeedLimiterState();
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::Initialize(bool force_software_renderer)
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2020-09-29 13:29:28 +00:00
|
|
|
g_ticks_per_second = ScaleTicksToOverclock(MASTER_CLOCK);
|
|
|
|
s_max_slice_ticks = ScaleTicksToOverclock(MASTER_CLOCK / 10);
|
2020-07-31 07:09:18 +00:00
|
|
|
s_frame_number = 1;
|
|
|
|
s_internal_frame_number = 1;
|
|
|
|
|
2022-08-10 03:09:32 +00:00
|
|
|
s_target_speed = g_settings.emulation_speed;
|
2020-07-31 07:09:18 +00:00
|
|
|
s_throttle_frequency = 60.0f;
|
2021-01-28 10:20:15 +00:00
|
|
|
s_frame_period = 0;
|
|
|
|
s_next_frame_time = 0;
|
2022-08-16 11:23:14 +00:00
|
|
|
s_turbo_enabled = false;
|
|
|
|
s_fast_forward_enabled = false;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
s_rewind_load_frequency = -1;
|
|
|
|
s_rewind_load_counter = -1;
|
|
|
|
s_rewinding_first_save = true;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
s_average_frame_time_accumulator = 0.0f;
|
2023-01-07 03:09:20 +00:00
|
|
|
s_minimum_frame_time_accumulator = 0.0f;
|
|
|
|
s_maximum_frame_time_accumulator = 0.0f;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
s_vps = 0.0f;
|
|
|
|
s_fps = 0.0f;
|
|
|
|
s_speed = 0.0f;
|
2023-01-07 03:09:20 +00:00
|
|
|
s_minimum_frame_time = 0.0f;
|
|
|
|
s_maximum_frame_time = 0.0f;
|
2020-07-31 07:09:18 +00:00
|
|
|
s_average_frame_time = 0.0f;
|
2022-07-11 13:03:29 +00:00
|
|
|
s_cpu_thread_usage = 0.0f;
|
|
|
|
s_cpu_thread_time = 0.0f;
|
|
|
|
s_sw_thread_usage = 0.0f;
|
|
|
|
s_sw_thread_time = 0.0f;
|
2022-09-03 04:15:15 +00:00
|
|
|
s_average_gpu_time = 0.0f;
|
|
|
|
s_accumulated_gpu_time = 0.0f;
|
|
|
|
s_gpu_usage = 0.0f;
|
2020-07-31 07:09:18 +00:00
|
|
|
s_last_frame_number = 0;
|
|
|
|
s_last_internal_frame_number = 0;
|
|
|
|
s_last_global_tick_counter = 0;
|
2022-09-03 04:15:15 +00:00
|
|
|
s_presents_since_last_update = 0;
|
2022-08-05 07:06:50 +00:00
|
|
|
s_last_cpu_time = 0;
|
2020-07-31 07:09:18 +00:00
|
|
|
s_fps_timer.Reset();
|
|
|
|
s_frame_timer.Reset();
|
2023-01-07 03:09:20 +00:00
|
|
|
s_frame_time_history.fill(0.0f);
|
|
|
|
s_frame_time_history_pos = 0;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
TimingEvents::Initialize();
|
|
|
|
|
|
|
|
CPU::Initialize();
|
2020-10-18 04:43:55 +00:00
|
|
|
|
|
|
|
if (!Bus::Initialize())
|
2021-04-27 16:50:46 +00:00
|
|
|
{
|
|
|
|
CPU::Shutdown();
|
2020-10-18 04:43:55 +00:00
|
|
|
return false;
|
2021-04-27 16:50:46 +00:00
|
|
|
}
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2023-08-27 12:48:40 +00:00
|
|
|
if (!CreateGPU(force_software_renderer ? GPURenderer::Software : g_settings.gpu_renderer, false))
|
2021-04-27 16:50:46 +00:00
|
|
|
{
|
|
|
|
Bus::Shutdown();
|
|
|
|
CPU::Shutdown();
|
2020-06-28 15:51:01 +00:00
|
|
|
return false;
|
2021-04-27 16:50:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 07:06:02 +00:00
|
|
|
GTE::UpdateAspectRatio();
|
|
|
|
|
2021-04-27 16:50:46 +00:00
|
|
|
if (g_settings.gpu_pgxp_enable)
|
|
|
|
PGXP::Initialize();
|
2020-06-28 15:51:01 +00:00
|
|
|
|
2021-02-18 15:48:44 +00:00
|
|
|
// Was startup cancelled? (e.g. shading compilers took too long and the user closed the application)
|
|
|
|
if (IsStartupCancelled())
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
g_gpu.reset();
|
2023-08-13 03:42:02 +00:00
|
|
|
if (!s_keep_gpu_device_on_shutdown)
|
|
|
|
{
|
|
|
|
Host::ReleaseGPUDevice();
|
|
|
|
Host::ReleaseRenderWindow();
|
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
if (g_settings.gpu_pgxp_enable)
|
|
|
|
PGXP::Shutdown();
|
|
|
|
CPU::Shutdown();
|
|
|
|
Bus::Shutdown();
|
2021-02-18 15:48:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-18 04:43:55 +00:00
|
|
|
// CPU code cache must happen after GPU, because it might steal our address space.
|
|
|
|
CPU::CodeCache::Initialize();
|
2019-09-09 07:01:26 +00:00
|
|
|
|
2023-01-11 08:51:38 +00:00
|
|
|
DMA::Initialize();
|
2023-01-11 09:01:20 +00:00
|
|
|
InterruptController::Initialize();
|
2019-09-11 04:01:19 +00:00
|
|
|
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::Initialize();
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::Initialize();
|
2023-01-11 08:58:25 +00:00
|
|
|
Timers::Initialize();
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::Initialize();
|
2022-12-20 10:45:42 +00:00
|
|
|
MDEC::Initialize();
|
2023-01-11 09:03:07 +00:00
|
|
|
SIO::Initialize();
|
2023-04-29 10:45:39 +00:00
|
|
|
PCDrv::Initialize();
|
2023-08-27 12:48:40 +00:00
|
|
|
PostProcessing::Initialize();
|
2020-07-17 14:25:08 +00:00
|
|
|
|
2021-05-23 03:35:10 +00:00
|
|
|
static constexpr float WARNING_DURATION = 15.0f;
|
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
if (g_settings.cpu_overclock_active)
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::AddFormattedOSDMessage(
|
2021-05-23 03:35:10 +00:00
|
|
|
WARNING_DURATION,
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE("OSDMessage", "CPU clock speed is set to %u%% (%u / %u). This may result in instability."),
|
2020-09-29 13:29:28 +00:00
|
|
|
g_settings.GetCPUOverclockPercent(), g_settings.cpu_overclock_numerator, g_settings.cpu_overclock_denominator);
|
|
|
|
}
|
2021-03-16 14:58:09 +00:00
|
|
|
if (g_settings.cdrom_read_speedup > 1)
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::AddFormattedOSDMessage(
|
2021-05-23 03:35:10 +00:00
|
|
|
WARNING_DURATION,
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE("OSDMessage", "CD-ROM read speedup set to %ux (effective speed %ux). This may result in instability."),
|
2021-03-16 14:58:09 +00:00
|
|
|
g_settings.cdrom_read_speedup, g_settings.cdrom_read_speedup * 2);
|
|
|
|
}
|
2021-05-23 03:35:10 +00:00
|
|
|
if (g_settings.cdrom_seek_speedup != 1)
|
|
|
|
{
|
|
|
|
if (g_settings.cdrom_seek_speedup == 0)
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::AddOSDMessage(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE_STR("OSDMessage", "CD-ROM seek speedup set to instant. This may result in instability."),
|
2021-05-23 03:35:10 +00:00
|
|
|
WARNING_DURATION);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::AddFormattedOSDMessage(
|
2023-08-19 14:08:03 +00:00
|
|
|
WARNING_DURATION, TRANSLATE("OSDMessage", "CD-ROM seek speedup set to %ux. This may result in instability."),
|
2021-05-23 03:35:10 +00:00
|
|
|
g_settings.cdrom_seek_speedup);
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 13:29:28 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
s_cpu_thread_handle = Threading::ThreadHandle::GetForCallingThread();
|
|
|
|
|
2020-02-11 03:02:42 +00:00
|
|
|
UpdateThrottlePeriod();
|
2021-01-23 09:00:54 +00:00
|
|
|
UpdateMemorySaveStateSettings();
|
2020-06-28 15:51:01 +00:00
|
|
|
return true;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::DestroySystem()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2023-08-15 13:12:21 +00:00
|
|
|
DebugAssert(!s_system_executing);
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_state == State::Shutdown)
|
|
|
|
return;
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
Host::ClearOSDMessages();
|
|
|
|
|
2023-08-27 12:48:40 +00:00
|
|
|
PostProcessing::Shutdown();
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
SaveStateSelectorUI::Close(true);
|
|
|
|
FullscreenUI::OnSystemDestroyed();
|
|
|
|
|
|
|
|
InputManager::PauseVibration();
|
|
|
|
|
|
|
|
if (g_settings.inhibit_screensaver)
|
|
|
|
PlatformMisc::ResumeScreensaver();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
SetTimerResolutionIncreased(false);
|
|
|
|
|
|
|
|
s_cpu_thread_usage = {};
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
ClearMemorySaveStates();
|
|
|
|
|
2020-12-25 08:02:38 +00:00
|
|
|
g_texture_replacements.Shutdown();
|
|
|
|
|
2023-04-29 10:45:39 +00:00
|
|
|
PCDrv::Shutdown();
|
2023-01-11 09:03:07 +00:00
|
|
|
SIO::Shutdown();
|
2022-12-20 10:45:42 +00:00
|
|
|
MDEC::Shutdown();
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::Shutdown();
|
2023-01-11 08:58:25 +00:00
|
|
|
Timers::Shutdown();
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::Shutdown();
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::Shutdown();
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu.reset();
|
2023-01-11 09:01:20 +00:00
|
|
|
InterruptController::Shutdown();
|
2023-01-11 08:51:38 +00:00
|
|
|
DMA::Shutdown();
|
2021-04-27 16:50:46 +00:00
|
|
|
PGXP::Shutdown();
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::CodeCache::Shutdown();
|
|
|
|
Bus::Shutdown();
|
|
|
|
CPU::Shutdown();
|
|
|
|
TimingEvents::Shutdown();
|
2022-07-11 13:03:29 +00:00
|
|
|
ClearRunningGame();
|
|
|
|
|
|
|
|
// Restore present-all-frames behavior.
|
2023-08-13 03:42:02 +00:00
|
|
|
if (s_keep_gpu_device_on_shutdown && g_gpu_device)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2023-08-13 03:42:02 +00:00
|
|
|
g_gpu_device->SetDisplayMaxFPS(0.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
UpdateSoftwareCursor();
|
2023-08-13 03:42:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Host::ReleaseGPUDevice();
|
|
|
|
Host::ReleaseRenderWindow();
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 09:22:54 +00:00
|
|
|
s_bios_hash = {};
|
|
|
|
s_bios_image_info = nullptr;
|
2023-08-15 13:12:21 +00:00
|
|
|
s_was_fast_booted = false;
|
|
|
|
s_cheat_list.reset();
|
|
|
|
|
|
|
|
s_state = State::Shutdown;
|
2023-03-16 09:22:54 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::OnSystemDestroyed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ClearRunningGame()
|
|
|
|
{
|
2023-08-23 12:06:48 +00:00
|
|
|
UpdateSessionTime(s_running_game_serial);
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
s_running_game_serial.clear();
|
2020-07-31 07:09:18 +00:00
|
|
|
s_running_game_path.clear();
|
|
|
|
s_running_game_title.clear();
|
2023-08-19 12:00:20 +00:00
|
|
|
s_running_game_entry = nullptr;
|
2023-05-15 13:38:37 +00:00
|
|
|
s_running_game_hash = 0;
|
2021-02-21 06:53:14 +00:00
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
Host::OnGameChanged(s_running_game_path, s_running_game_serial, s_running_game_title);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
Achievements::GameChanged(s_running_game_path, nullptr);
|
|
|
|
#endif
|
2023-08-23 12:06:48 +00:00
|
|
|
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
UpdateDiscordPresence(false);
|
|
|
|
#endif
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
bool System::FastForwardToFirstFrame()
|
|
|
|
{
|
|
|
|
// If we're taking more than 60 seconds to load the game, oof..
|
|
|
|
static constexpr u32 MAX_FRAMES_TO_SKIP = 30 * 60;
|
|
|
|
const u32 current_frame_number = s_frame_number;
|
|
|
|
const u32 current_internal_frame_number = s_internal_frame_number;
|
|
|
|
|
|
|
|
SPU::SetAudioOutputMuted(true);
|
|
|
|
while (s_internal_frame_number == current_internal_frame_number &&
|
|
|
|
(s_frame_number - current_frame_number) <= MAX_FRAMES_TO_SKIP)
|
|
|
|
{
|
|
|
|
Panic("Fixme");
|
|
|
|
// System::RunFrame();
|
|
|
|
}
|
|
|
|
SPU::SetAudioOutputMuted(false);
|
|
|
|
|
|
|
|
return (s_internal_frame_number != current_internal_frame_number);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::Execute()
|
|
|
|
{
|
2023-08-15 13:12:21 +00:00
|
|
|
for (;;)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2023-08-15 13:12:21 +00:00
|
|
|
switch (s_state)
|
|
|
|
{
|
|
|
|
case State::Running:
|
|
|
|
{
|
|
|
|
s_system_executing = true;
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
// TODO: Purge reset/restore
|
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
|
|
|
if (s_rewind_load_counter >= 0)
|
|
|
|
DoRewind();
|
|
|
|
else
|
|
|
|
CPU::Execute();
|
|
|
|
|
|
|
|
s_system_executing = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case State::Stopping:
|
|
|
|
{
|
|
|
|
DestroySystem();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case State::Paused:
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::FrameDone()
|
|
|
|
{
|
|
|
|
s_frame_number++;
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
// Vertex buffer is shared, need to flush what we have.
|
|
|
|
g_gpu->FlushRender();
|
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
// Generate any pending samples from the SPU before sleeping, this way we reduce the chances of underruns.
|
2023-08-23 12:06:48 +00:00
|
|
|
// TODO: when running ahead, we can skip this (and the flush above)
|
2023-08-15 13:12:21 +00:00
|
|
|
SPU::GeneratePendingSamples();
|
|
|
|
|
|
|
|
if (s_cheat_list)
|
|
|
|
s_cheat_list->Apply();
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
if (s_frame_step_request)
|
|
|
|
{
|
|
|
|
s_frame_step_request = false;
|
|
|
|
PauseSystem(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save states for rewind and runahead.
|
|
|
|
if (s_rewind_save_counter >= 0)
|
|
|
|
{
|
|
|
|
if (s_rewind_save_counter == 0)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2023-08-15 13:12:21 +00:00
|
|
|
SaveRewindState();
|
|
|
|
s_rewind_save_counter = s_rewind_save_frequency;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
2023-08-15 13:12:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
s_rewind_save_counter--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (s_runahead_frames > 0)
|
|
|
|
{
|
|
|
|
// We don't want to poll during replay, because otherwise we'll lose frames.
|
|
|
|
if (s_runahead_replay_frames == 0)
|
|
|
|
{
|
|
|
|
// For runahead, poll input early, that way we can use the remainder of this frame to replay.
|
|
|
|
// *technically* this means higher input latency (by less than a frame), but runahead itself
|
|
|
|
// counter-acts that.
|
|
|
|
Host::PumpMessagesOnCPUThread();
|
2023-08-23 12:06:48 +00:00
|
|
|
InputManager::PollSources();
|
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
if (IsExecutionInterrupted())
|
|
|
|
{
|
|
|
|
s_system_interrupted = false;
|
|
|
|
CPU::ExitExecution();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DoRunahead())
|
|
|
|
{
|
|
|
|
// running ahead, get it done as soon as possible
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveRunaheadState();
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
if (Achievements::IsActive())
|
|
|
|
Achievements::FrameUpdate();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
PollDiscordPresence();
|
|
|
|
#endif
|
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
const Common::Timer::Value current_time = Common::Timer::GetCurrentValue();
|
|
|
|
if (current_time < s_next_frame_time || s_display_all_frames || s_last_frame_skipped)
|
|
|
|
{
|
|
|
|
s_last_frame_skipped = false;
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
const bool skip_present = g_gpu_device->ShouldSkipDisplayingFrame();
|
2022-09-03 04:15:15 +00:00
|
|
|
Host::RenderDisplay(skip_present);
|
2023-08-13 03:42:02 +00:00
|
|
|
if (!skip_present && g_gpu_device->IsGPUTimingEnabled())
|
2022-09-03 04:15:15 +00:00
|
|
|
{
|
2023-08-13 03:42:02 +00:00
|
|
|
s_accumulated_gpu_time += g_gpu_device->GetAndResetAccumulatedGPUTime();
|
2022-09-03 04:15:15 +00:00
|
|
|
s_presents_since_last_update++;
|
|
|
|
}
|
2023-08-15 13:12:21 +00:00
|
|
|
}
|
|
|
|
else if (current_time >= s_next_frame_time)
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Skipping displaying frame");
|
|
|
|
s_last_frame_skipped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_throttler_enabled && !IsExecutionInterrupted())
|
|
|
|
Throttle();
|
|
|
|
|
|
|
|
// Input poll already done above
|
|
|
|
if (s_runahead_frames == 0)
|
|
|
|
{
|
|
|
|
Host::PumpMessagesOnCPUThread();
|
2023-08-23 12:06:48 +00:00
|
|
|
InputManager::PollSources();
|
2023-08-15 13:12:21 +00:00
|
|
|
|
|
|
|
if (IsExecutionInterrupted())
|
|
|
|
{
|
|
|
|
s_system_interrupted = false;
|
|
|
|
CPU::ExitExecution();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update perf counters *after* throttling, we want to measure from start-of-frame
|
|
|
|
// to start-of-frame, not end-of-frame to end-of-frame (will be noisy due to different
|
|
|
|
// amounts of computation happening in each frame).
|
|
|
|
System::UpdatePerformanceCounters();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::SetThrottleFrequency(float frequency)
|
|
|
|
{
|
|
|
|
if (s_throttle_frequency == frequency)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_throttle_frequency = frequency;
|
|
|
|
UpdateThrottlePeriod();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::UpdateThrottlePeriod()
|
|
|
|
{
|
|
|
|
if (s_target_speed > std::numeric_limits<double>::epsilon())
|
|
|
|
{
|
|
|
|
const double target_speed = std::max(static_cast<double>(s_target_speed), std::numeric_limits<double>::epsilon());
|
|
|
|
s_frame_period =
|
|
|
|
Common::Timer::ConvertSecondsToValue(1.0 / (static_cast<double>(s_throttle_frequency) * target_speed));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_frame_period = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetThrottler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ResetThrottler()
|
|
|
|
{
|
|
|
|
s_next_frame_time = Common::Timer::GetCurrentValue() + s_frame_period;
|
|
|
|
}
|
2023-01-12 07:44:32 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
void System::Throttle()
|
|
|
|
{
|
|
|
|
// If we're running too slow, advance the next frame time based on the time we lost. Effectively skips
|
|
|
|
// running those frames at the intended time, because otherwise if we pause in the debugger, we'll run
|
|
|
|
// hundreds of frames when we resume.
|
|
|
|
Common::Timer::Value current_time = Common::Timer::GetCurrentValue();
|
|
|
|
if (current_time > s_next_frame_time)
|
|
|
|
{
|
|
|
|
const Common::Timer::Value diff = static_cast<s64>(current_time) - static_cast<s64>(s_next_frame_time);
|
|
|
|
s_next_frame_time += (diff / s_frame_period) * s_frame_period + s_frame_period;
|
|
|
|
return;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
2023-08-15 13:12:21 +00:00
|
|
|
|
|
|
|
// Use a spinwait if we undersleep for all platforms except android.. don't want to burn battery.
|
|
|
|
// Linux also seems to do a much better job of waking up at the requested time.
|
|
|
|
#if !defined(__linux__) && !defined(__ANDROID__)
|
|
|
|
Common::Timer::SleepUntil(s_next_frame_time, g_settings.display_all_frames);
|
|
|
|
#else
|
|
|
|
Common::Timer::SleepUntil(s_next_frame_time, false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s_next_frame_time += s_frame_period;
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::SingleStepCPU()
|
|
|
|
{
|
|
|
|
s_frame_timer.Reset();
|
|
|
|
s_system_executing = true;
|
|
|
|
|
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
|
|
|
CPU::SingleStep();
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
g_gpu->FlushRender();
|
2023-08-15 13:12:21 +00:00
|
|
|
SPU::GeneratePendingSamples();
|
|
|
|
|
|
|
|
s_system_executing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::IncrementInternalFrameNumber()
|
|
|
|
{
|
|
|
|
s_internal_frame_number++;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void System::RecreateSystem()
|
|
|
|
{
|
|
|
|
Assert(!IsShutdown());
|
|
|
|
|
|
|
|
const bool was_paused = System::IsPaused();
|
|
|
|
std::unique_ptr<ByteStream> stream = ByteStream::CreateGrowableMemoryStream(nullptr, 8 * 1024);
|
2022-08-18 11:21:22 +00:00
|
|
|
if (!System::InternalSaveState(stream.get(), 0, SAVE_STATE_HEADER::COMPRESSION_TYPE_NONE) || !stream->SeekAbsolute(0))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
Host::ReportErrorAsync("Error", "Failed to save state before system recreation. Shutting down.");
|
|
|
|
DestroySystem();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DestroySystem();
|
|
|
|
|
|
|
|
SystemBootParameters boot_params;
|
|
|
|
if (!BootSystem(std::move(boot_params)))
|
|
|
|
{
|
|
|
|
Host::ReportErrorAsync("Error", "Failed to boot system after recreation.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoLoadState(stream.get(), false, false))
|
|
|
|
{
|
|
|
|
DestroySystem();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetPerformanceCounters();
|
|
|
|
ResetThrottler();
|
2022-08-18 13:51:43 +00:00
|
|
|
Host::RenderDisplay(false);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (was_paused)
|
|
|
|
PauseSystem(true);
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 12:48:40 +00:00
|
|
|
bool System::CreateGPU(GPURenderer renderer, bool is_switching)
|
2019-10-26 02:55:56 +00:00
|
|
|
{
|
2023-08-13 03:42:02 +00:00
|
|
|
const RenderAPI api = Settings::GetRenderAPIForRenderer(renderer);
|
2020-06-18 14:18:17 +00:00
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
if (!g_gpu_device || (renderer != GPURenderer::Software && g_gpu_device->GetRenderAPI() != api))
|
|
|
|
{
|
|
|
|
if (g_gpu_device)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Recreating GPU device, expecting %s got %s", GPUDevice::RenderAPIToString(api),
|
|
|
|
GPUDevice::RenderAPIToString(g_gpu_device->GetRenderAPI()));
|
2023-08-27 12:48:40 +00:00
|
|
|
PostProcessing::Shutdown();
|
2023-08-13 03:42:02 +00:00
|
|
|
}
|
2019-11-03 14:39:48 +00:00
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
Host::ReleaseGPUDevice();
|
|
|
|
if (!Host::CreateGPUDevice(api))
|
|
|
|
{
|
|
|
|
Host::ReleaseRenderWindow();
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-27 12:48:40 +00:00
|
|
|
|
|
|
|
if (is_switching)
|
|
|
|
PostProcessing::Initialize();
|
2019-10-26 02:55:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
if (renderer == GPURenderer::Software)
|
|
|
|
g_gpu = GPU::CreateSoftwareRenderer();
|
|
|
|
else
|
|
|
|
g_gpu = GPU::CreateHardwareRenderer();
|
|
|
|
|
2023-01-28 11:38:03 +00:00
|
|
|
if (!g_gpu)
|
2019-10-26 02:57:35 +00:00
|
|
|
{
|
2021-03-04 15:05:13 +00:00
|
|
|
Log_ErrorPrintf("Failed to initialize %s renderer, falling back to software renderer",
|
|
|
|
Settings::GetRendererName(renderer));
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::AddFormattedOSDMessage(
|
2023-08-19 14:08:03 +00:00
|
|
|
30.0f, TRANSLATE("OSDMessage", "Failed to initialize %s renderer, falling back to software renderer."),
|
2021-03-04 15:05:13 +00:00
|
|
|
Settings::GetRendererName(renderer));
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu.reset();
|
|
|
|
g_gpu = GPU::CreateSoftwareRenderer();
|
2023-01-28 11:38:03 +00:00
|
|
|
if (!g_gpu)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to create fallback software renderer.");
|
2023-08-13 03:42:02 +00:00
|
|
|
if (!s_keep_gpu_device_on_shutdown)
|
|
|
|
{
|
|
|
|
Host::ReleaseGPUDevice();
|
|
|
|
Host::ReleaseRenderWindow();
|
|
|
|
}
|
2019-10-26 02:57:35 +00:00
|
|
|
return false;
|
2023-01-28 11:38:03 +00:00
|
|
|
}
|
2019-10-26 02:57:35 +00:00
|
|
|
}
|
2019-10-26 02:55:56 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-03 06:44:34 +00:00
|
|
|
bool System::DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_display, bool is_memory_state)
|
2019-09-14 10:28:47 +00:00
|
|
|
{
|
2019-10-04 10:48:29 +00:00
|
|
|
if (!sw.DoMarker("System"))
|
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
sw.Do(&s_region);
|
|
|
|
sw.Do(&s_frame_number);
|
|
|
|
sw.Do(&s_internal_frame_number);
|
2019-10-04 10:48:29 +00:00
|
|
|
|
2023-03-16 09:33:59 +00:00
|
|
|
// Don't bother checking this at all for memory states, since they won't have a different BIOS...
|
|
|
|
if (!is_memory_state)
|
|
|
|
{
|
|
|
|
BIOS::Hash bios_hash = s_bios_hash;
|
|
|
|
sw.DoBytesEx(bios_hash.bytes, sizeof(bios_hash.bytes), 58, s_bios_hash.bytes);
|
|
|
|
if (bios_hash != s_bios_hash)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("BIOS hash mismatch: System: %s | State: %s", s_bios_hash.ToString().c_str(),
|
|
|
|
bios_hash.ToString().c_str());
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddKeyedOSDMessage("StateBIOSMismatch",
|
|
|
|
TRANSLATE_STR("OSDMessage",
|
|
|
|
"This save state was created with a different BIOS version or patch "
|
|
|
|
"options. This may cause stability issues."),
|
|
|
|
10.0f);
|
2023-03-16 09:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("CPU") || !CPU::DoState(sw))
|
2019-09-14 10:28:47 +00:00
|
|
|
return false;
|
|
|
|
|
2019-11-19 10:30:04 +00:00
|
|
|
if (sw.IsReading())
|
2021-12-02 14:03:18 +00:00
|
|
|
{
|
|
|
|
if (is_memory_state)
|
|
|
|
CPU::CodeCache::InvalidateAll();
|
|
|
|
else
|
|
|
|
CPU::CodeCache::Flush();
|
|
|
|
}
|
2019-11-19 10:30:04 +00:00
|
|
|
|
2021-04-27 16:50:46 +00:00
|
|
|
// only reset pgxp if we're not runahead-rollbacking. the value checks will save us from broken rendering, and it
|
|
|
|
// saves using imprecise values for a frame in 30fps games.
|
|
|
|
if (sw.IsReading() && g_settings.gpu_pgxp_enable && !is_memory_state)
|
2021-06-12 12:02:28 +00:00
|
|
|
PGXP::Reset();
|
2021-04-27 16:50:46 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("Bus") || !Bus::DoState(sw))
|
2019-09-14 10:28:47 +00:00
|
|
|
return false;
|
|
|
|
|
2023-01-11 08:51:38 +00:00
|
|
|
if (!sw.DoMarker("DMA") || !DMA::DoState(sw))
|
2019-09-14 10:28:47 +00:00
|
|
|
return false;
|
|
|
|
|
2023-01-11 09:01:20 +00:00
|
|
|
if (!sw.DoMarker("InterruptController") || !InterruptController::DoState(sw))
|
2019-09-17 06:26:00 +00:00
|
|
|
return false;
|
|
|
|
|
2020-08-22 08:16:35 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
2023-08-13 03:42:02 +00:00
|
|
|
if (!sw.DoMarker("GPU") || !g_gpu->DoState(sw, host_texture, update_display))
|
2019-09-14 10:28:47 +00:00
|
|
|
return false;
|
|
|
|
|
2023-01-09 09:34:40 +00:00
|
|
|
if (!sw.DoMarker("CDROM") || !CDROM::DoState(sw))
|
2019-09-17 09:22:39 +00:00
|
|
|
return false;
|
|
|
|
|
2023-03-16 11:22:08 +00:00
|
|
|
if (!sw.DoMarker("Pad") || !Pad::DoState(sw, is_memory_state))
|
2019-09-20 06:47:41 +00:00
|
|
|
return false;
|
|
|
|
|
2023-01-11 08:58:25 +00:00
|
|
|
if (!sw.DoMarker("Timers") || !Timers::DoState(sw))
|
2019-09-20 13:40:19 +00:00
|
|
|
return false;
|
|
|
|
|
2022-08-01 13:39:41 +00:00
|
|
|
if (!sw.DoMarker("SPU") || !SPU::DoState(sw))
|
2019-09-24 13:44:38 +00:00
|
|
|
return false;
|
|
|
|
|
2022-12-20 10:45:42 +00:00
|
|
|
if (!sw.DoMarker("MDEC") || !MDEC::DoState(sw))
|
2019-09-29 02:51:34 +00:00
|
|
|
return false;
|
|
|
|
|
2023-01-11 09:03:07 +00:00
|
|
|
if (!sw.DoMarker("SIO") || !SIO::DoState(sw))
|
2019-12-09 14:28:58 +00:00
|
|
|
return false;
|
|
|
|
|
2020-08-15 04:56:20 +00:00
|
|
|
if (!sw.DoMarker("Events") || !TimingEvents::DoState(sw))
|
2020-01-24 04:53:40 +00:00
|
|
|
return false;
|
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
if (!sw.DoMarker("Overclock"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool cpu_overclock_active = g_settings.cpu_overclock_active;
|
|
|
|
u32 cpu_overclock_numerator = g_settings.cpu_overclock_numerator;
|
|
|
|
u32 cpu_overclock_denominator = g_settings.cpu_overclock_denominator;
|
|
|
|
sw.Do(&cpu_overclock_active);
|
|
|
|
sw.Do(&cpu_overclock_numerator);
|
|
|
|
sw.Do(&cpu_overclock_denominator);
|
|
|
|
|
|
|
|
if (sw.IsReading() && (cpu_overclock_active != g_settings.cpu_overclock_active ||
|
|
|
|
(cpu_overclock_active && (g_settings.cpu_overclock_numerator != cpu_overclock_numerator ||
|
|
|
|
g_settings.cpu_overclock_denominator != cpu_overclock_denominator))))
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::AddFormattedOSDMessage(
|
2023-08-19 14:08:03 +00:00
|
|
|
10.0f, TRANSLATE("OSDMessage", "WARNING: CPU overclock (%u%%) was different in save state (%u%%)."),
|
2020-09-29 13:29:28 +00:00
|
|
|
g_settings.cpu_overclock_enable ? g_settings.GetCPUOverclockPercent() : 100u,
|
|
|
|
cpu_overclock_active ?
|
|
|
|
Settings::CPUOverclockFractionToPercent(cpu_overclock_numerator, cpu_overclock_denominator) :
|
|
|
|
100u);
|
|
|
|
UpdateOverclock();
|
|
|
|
}
|
|
|
|
|
2022-03-26 14:52:11 +00:00
|
|
|
if (!is_memory_state)
|
|
|
|
{
|
|
|
|
if (sw.GetVersion() >= 56)
|
|
|
|
{
|
|
|
|
if (!sw.DoMarker("Cheevos"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!Achievements::DoState(sw))
|
2022-03-26 14:52:11 +00:00
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
// if we compiled without cheevos, we need to toss out the data from states which were
|
|
|
|
u32 data_size = 0;
|
|
|
|
sw.Do(&data_size);
|
|
|
|
if (data_size > 0)
|
|
|
|
sw.SkipBytes(data_size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
// loading an old state without cheevos, so reset the runtime
|
2022-09-21 12:02:54 +00:00
|
|
|
Achievements::ResetRuntime();
|
2022-03-26 14:52:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:28:47 +00:00
|
|
|
return !sw.HasError();
|
|
|
|
}
|
|
|
|
|
2023-03-16 09:22:54 +00:00
|
|
|
bool System::LoadBIOS()
|
|
|
|
{
|
|
|
|
std::optional<BIOS::Image> bios_image(BIOS::GetBIOSImage(s_region));
|
|
|
|
if (!bios_image.has_value())
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::ReportFormattedErrorAsync("Error", TRANSLATE("System", "Failed to load %s BIOS."),
|
2023-03-16 09:22:54 +00:00
|
|
|
Settings::GetConsoleRegionName(s_region));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bios_image->size() != static_cast<u32>(Bus::BIOS_SIZE))
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::ReportFormattedErrorAsync("Error", TRANSLATE("System", "Incorrect BIOS image size"));
|
2023-03-16 09:22:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_bios_hash = BIOS::GetImageHash(bios_image.value());
|
|
|
|
s_bios_image_info = BIOS::GetInfoForImage(bios_image.value(), s_bios_hash);
|
|
|
|
if (s_bios_image_info)
|
|
|
|
Log_InfoPrintf("Using BIOS: %s", s_bios_image_info->description);
|
|
|
|
else
|
2023-04-14 08:14:39 +00:00
|
|
|
Log_WarningPrintf("Using an unknown BIOS: %s", s_bios_hash.ToString().c_str());
|
2023-03-16 09:22:54 +00:00
|
|
|
|
|
|
|
std::memcpy(Bus::g_bios, bios_image->data(), Bus::BIOS_SIZE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::InternalReset()
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2020-08-01 06:45:06 +00:00
|
|
|
if (IsShutdown())
|
|
|
|
return;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::Reset();
|
|
|
|
CPU::CodeCache::Flush();
|
2021-04-27 16:50:46 +00:00
|
|
|
if (g_settings.gpu_pgxp_enable)
|
|
|
|
PGXP::Initialize();
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
Bus::Reset();
|
2023-01-11 08:51:38 +00:00
|
|
|
DMA::Reset();
|
2023-01-11 09:01:20 +00:00
|
|
|
InterruptController::Reset();
|
2021-01-23 09:00:54 +00:00
|
|
|
g_gpu->Reset(true);
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::Reset();
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::Reset();
|
2023-01-11 08:58:25 +00:00
|
|
|
Timers::Reset();
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::Reset();
|
2022-12-20 10:45:42 +00:00
|
|
|
MDEC::Reset();
|
2023-01-11 09:03:07 +00:00
|
|
|
SIO::Reset();
|
2023-04-29 10:45:39 +00:00
|
|
|
PCDrv::Reset();
|
2020-07-31 07:09:18 +00:00
|
|
|
s_frame_number = 1;
|
|
|
|
s_internal_frame_number = 0;
|
|
|
|
TimingEvents::Reset();
|
2020-02-09 13:16:25 +00:00
|
|
|
ResetPerformanceCounters();
|
2020-08-01 06:45:06 +00:00
|
|
|
|
2022-03-26 14:52:11 +00:00
|
|
|
#ifdef WITH_CHEEVOS
|
2022-09-21 12:02:54 +00:00
|
|
|
Achievements::ResetRuntime();
|
2022-03-26 14:52:11 +00:00
|
|
|
#endif
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string System::GetMediaPathFromSaveState(const char* path)
|
2020-05-26 16:01:09 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
std::unique_ptr<ByteStream> stream(ByteStream::OpenFile(path, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_SEEKABLE));
|
|
|
|
if (stream)
|
|
|
|
{
|
|
|
|
SAVE_STATE_HEADER header;
|
|
|
|
if (stream->Read2(&header, sizeof(header)) && header.magic == SAVE_STATE_MAGIC &&
|
|
|
|
header.version >= SAVE_STATE_MINIMUM_VERSION && header.version <= SAVE_STATE_VERSION)
|
|
|
|
{
|
|
|
|
if (header.media_filename_length > 0)
|
|
|
|
{
|
|
|
|
ret.resize(header.media_filename_length);
|
|
|
|
if (!stream->SeekAbsolute(header.offset_to_media_filename) ||
|
|
|
|
!stream->Read2(ret.data(), header.media_filename_length))
|
|
|
|
{
|
|
|
|
ret = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
return ret;
|
2020-05-26 16:01:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::DoLoadState(ByteStream* state, bool force_software_renderer, bool update_display)
|
2019-09-14 10:28:47 +00:00
|
|
|
{
|
2022-07-23 03:23:54 +00:00
|
|
|
Assert(IsValid());
|
|
|
|
|
2020-04-20 16:50:45 +00:00
|
|
|
SAVE_STATE_HEADER header;
|
|
|
|
if (!state->Read2(&header, sizeof(header)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (header.magic != SAVE_STATE_MAGIC)
|
|
|
|
return false;
|
|
|
|
|
2020-11-27 14:35:03 +00:00
|
|
|
if (header.version < SAVE_STATE_MINIMUM_VERSION)
|
2020-04-20 16:50:45 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::ReportFormattedErrorAsync(
|
2023-08-19 14:08:03 +00:00
|
|
|
"Error", TRANSLATE("System", "Save state is incompatible: minimum version is %u but state is version %u."),
|
2020-11-27 14:35:03 +00:00
|
|
|
SAVE_STATE_MINIMUM_VERSION, header.version);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.version > SAVE_STATE_VERSION)
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::ReportFormattedErrorAsync(
|
2023-08-19 14:08:03 +00:00
|
|
|
"Error", TRANSLATE("System", "Save state is incompatible: maximum version is %u but state is version %u."),
|
2020-11-27 14:35:03 +00:00
|
|
|
SAVE_STATE_VERSION, header.version);
|
2020-04-20 16:50:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-19 13:40:36 +00:00
|
|
|
Error error;
|
2020-05-26 16:01:09 +00:00
|
|
|
std::string media_filename;
|
|
|
|
std::unique_ptr<CDImage> media;
|
|
|
|
if (header.media_filename_length > 0)
|
|
|
|
{
|
|
|
|
media_filename.resize(header.media_filename_length);
|
|
|
|
if (!state->SeekAbsolute(header.offset_to_media_filename) ||
|
|
|
|
!state->Read2(media_filename.data(), header.media_filename_length))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-09 09:34:40 +00:00
|
|
|
std::unique_ptr<CDImage> old_media = CDROM::RemoveMedia(false);
|
2020-11-29 13:00:24 +00:00
|
|
|
if (old_media && old_media->GetFileName() == media_filename)
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("Re-using same media '%s'", media_filename.c_str());
|
|
|
|
media = std::move(old_media);
|
|
|
|
}
|
|
|
|
else
|
2020-05-26 16:01:09 +00:00
|
|
|
{
|
2022-08-27 06:52:24 +00:00
|
|
|
media = CDImage::Open(media_filename.c_str(), g_settings.cdrom_load_image_patches, &error);
|
2020-07-21 14:02:37 +00:00
|
|
|
if (!media)
|
|
|
|
{
|
2020-11-29 13:00:24 +00:00
|
|
|
if (old_media)
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::AddFormattedOSDMessage(
|
2020-11-29 13:00:24 +00:00
|
|
|
30.0f,
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE("OSDMessage", "Failed to open CD image from save state '%s': %s. Using "
|
|
|
|
"existing image '%s', this may result in instability."),
|
2023-08-19 13:40:36 +00:00
|
|
|
media_filename.c_str(), error.GetDescription().c_str(), old_media->GetFileName().c_str());
|
2020-11-29 13:00:24 +00:00
|
|
|
media = std::move(old_media);
|
2022-08-13 03:59:58 +00:00
|
|
|
header.media_subimage_index = media->GetCurrentSubImage();
|
2020-11-29 13:00:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::ReportFormattedErrorAsync("Error",
|
|
|
|
TRANSLATE("System", "Failed to open CD image '%s' used by save state: %s."),
|
|
|
|
media_filename.c_str(), error.GetDescription().c_str());
|
2020-11-29 13:00:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-21 14:02:37 +00:00
|
|
|
}
|
2020-05-26 16:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
UpdateRunningGame(media_filename.c_str(), media.get(), false);
|
2021-03-26 16:19:23 +00:00
|
|
|
|
|
|
|
if (media && header.version >= 51)
|
2020-08-15 10:38:17 +00:00
|
|
|
{
|
2021-03-27 05:43:39 +00:00
|
|
|
const u32 num_subimages = media->HasSubImages() ? media->GetSubImageCount() : 1;
|
2021-03-26 16:19:23 +00:00
|
|
|
if (header.media_subimage_index >= num_subimages ||
|
|
|
|
(media->HasSubImages() && media->GetCurrentSubImage() != header.media_subimage_index &&
|
|
|
|
!media->SwitchSubImage(header.media_subimage_index, &error)))
|
2020-08-15 10:38:17 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::ReportFormattedErrorAsync(
|
2023-08-19 14:08:03 +00:00
|
|
|
"Error", TRANSLATE("System", "Failed to switch to subimage %u in CD image '%s' used by save state: %s."),
|
2023-08-19 13:40:36 +00:00
|
|
|
header.media_subimage_index + 1u, media_filename.c_str(), error.GetDescription().c_str());
|
2020-08-15 10:38:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-26 16:19:23 +00:00
|
|
|
else
|
2020-08-15 10:38:17 +00:00
|
|
|
{
|
2021-03-26 16:19:23 +00:00
|
|
|
Log_InfoPrintf("Switched to subimage %u in '%s'", header.media_subimage_index, media_filename.c_str());
|
2020-08-15 10:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
ClearMemorySaveStates();
|
2020-05-26 16:01:09 +00:00
|
|
|
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::Reset();
|
2022-07-23 03:23:54 +00:00
|
|
|
if (media)
|
2020-05-26 16:01:09 +00:00
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
const DiscRegion region = GetRegionForImage(media.get());
|
|
|
|
CDROM::InsertMedia(std::move(media), region);
|
2022-07-23 03:23:54 +00:00
|
|
|
if (g_settings.cdrom_load_image_to_ram)
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::PrecacheMedia();
|
2020-05-26 16:01:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::RemoveMedia(false);
|
2020-05-26 16:01:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 03:23:54 +00:00
|
|
|
// ensure the correct card is loaded
|
|
|
|
if (g_settings.HasAnyPerGameMemoryCards())
|
|
|
|
UpdatePerGameMemoryCards();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
// Updating game/loading settings can turn on hardcore mode. Catch this.
|
|
|
|
if (Achievements::ChallengeModeActive())
|
|
|
|
{
|
|
|
|
Host::AddKeyedOSDMessage("challenge_mode_reset",
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE_STR("Achievements", "Hardcore mode disabled by state switch."), 10.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
Achievements::DisableChallengeMode();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-20 16:50:45 +00:00
|
|
|
if (!state->SeekAbsolute(header.offset_to_data))
|
|
|
|
return false;
|
|
|
|
|
2022-08-18 11:21:22 +00:00
|
|
|
if (header.data_compression_type == SAVE_STATE_HEADER::COMPRESSION_TYPE_NONE)
|
|
|
|
{
|
|
|
|
StateWrapper sw(state, StateWrapper::Mode::Read, header.version);
|
|
|
|
if (!DoState(sw, nullptr, update_display, false))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (header.data_compression_type == SAVE_STATE_HEADER::COMPRESSION_TYPE_ZSTD)
|
|
|
|
{
|
2022-08-18 12:43:02 +00:00
|
|
|
std::unique_ptr<ByteStream> dstream(ByteStream::CreateZstdDecompressStream(state, header.data_compressed_size));
|
|
|
|
StateWrapper sw(dstream.get(), StateWrapper::Mode::Read, header.version);
|
2022-08-18 11:21:22 +00:00
|
|
|
if (!DoState(sw, nullptr, update_display, false))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Host::ReportFormattedErrorAsync("Error", "Unknown save state compression type %u", header.data_compression_type);
|
2020-07-31 07:09:18 +00:00
|
|
|
return false;
|
2022-08-18 11:21:22 +00:00
|
|
|
}
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
if (s_state == State::Starting)
|
|
|
|
s_state = State::Running;
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
ResetPerformanceCounters();
|
|
|
|
ResetThrottler();
|
2020-07-31 07:09:18 +00:00
|
|
|
return true;
|
2019-09-14 10:28:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 11:21:22 +00:00
|
|
|
bool System::InternalSaveState(ByteStream* state, u32 screenshot_size /* = 256 */,
|
|
|
|
u32 compression_method /* = SAVE_STATE_HEADER::COMPRESSION_TYPE_NONE*/)
|
2019-09-14 10:28:47 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (IsShutdown())
|
|
|
|
return false;
|
|
|
|
|
2020-04-20 16:50:45 +00:00
|
|
|
SAVE_STATE_HEADER header = {};
|
|
|
|
|
|
|
|
const u64 header_position = state->GetPosition();
|
|
|
|
if (!state->Write2(&header, sizeof(header)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// fill in header
|
|
|
|
header.magic = SAVE_STATE_MAGIC;
|
|
|
|
header.version = SAVE_STATE_VERSION;
|
2020-07-31 07:09:18 +00:00
|
|
|
StringUtil::Strlcpy(header.title, s_running_game_title.c_str(), sizeof(header.title));
|
2022-10-05 08:29:08 +00:00
|
|
|
StringUtil::Strlcpy(header.serial, s_running_game_serial.c_str(), sizeof(header.serial));
|
2020-05-26 16:01:09 +00:00
|
|
|
|
2023-01-09 09:34:40 +00:00
|
|
|
if (CDROM::HasMedia())
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
const std::string& media_filename = CDROM::GetMediaFileName();
|
2020-07-22 16:36:05 +00:00
|
|
|
header.offset_to_media_filename = static_cast<u32>(state->GetPosition());
|
|
|
|
header.media_filename_length = static_cast<u32>(media_filename.length());
|
2023-01-09 09:34:40 +00:00
|
|
|
header.media_subimage_index = CDROM::GetMedia()->HasSubImages() ? CDROM::GetMedia()->GetCurrentSubImage() : 0;
|
2020-07-22 16:36:05 +00:00
|
|
|
if (!media_filename.empty() && !state->Write2(media_filename.data(), header.media_filename_length))
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-20 16:50:45 +00:00
|
|
|
|
|
|
|
// save screenshot
|
2020-06-22 07:05:02 +00:00
|
|
|
if (screenshot_size > 0)
|
2020-04-20 16:50:45 +00:00
|
|
|
{
|
2021-04-27 16:37:52 +00:00
|
|
|
// assume this size is the width
|
2023-08-27 08:13:50 +00:00
|
|
|
const float display_aspect_ratio = g_gpu->GetDisplayAspectRatio();
|
2021-04-27 16:37:52 +00:00
|
|
|
const u32 screenshot_width = screenshot_size;
|
|
|
|
const u32 screenshot_height =
|
|
|
|
std::max(1u, static_cast<u32>(static_cast<float>(screenshot_width) /
|
|
|
|
((display_aspect_ratio > 0.0f) ? display_aspect_ratio : 1.0f)));
|
|
|
|
Log_VerbosePrintf("Saving %ux%u screenshot for state", screenshot_width, screenshot_height);
|
|
|
|
|
2020-04-20 16:50:45 +00:00
|
|
|
std::vector<u32> screenshot_buffer;
|
2021-04-27 16:37:52 +00:00
|
|
|
u32 screenshot_stride;
|
2022-10-03 06:44:34 +00:00
|
|
|
GPUTexture::Format screenshot_format;
|
2023-08-27 08:13:50 +00:00
|
|
|
if (g_gpu->RenderScreenshotToBuffer(screenshot_width, screenshot_height,
|
|
|
|
Common::Rectangle<s32>::FromExtents(0, 0, screenshot_width, screenshot_height),
|
|
|
|
false, &screenshot_buffer, &screenshot_stride, &screenshot_format) &&
|
2022-10-03 06:44:34 +00:00
|
|
|
GPUTexture::ConvertTextureDataToRGBA8(screenshot_width, screenshot_height, screenshot_buffer, screenshot_stride,
|
|
|
|
screenshot_format))
|
2020-04-20 16:50:45 +00:00
|
|
|
{
|
2021-04-27 16:37:52 +00:00
|
|
|
if (screenshot_stride != (screenshot_width * sizeof(u32)))
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Failed to save %ux%u screenshot for save state due to incorrect stride(%u)",
|
|
|
|
screenshot_width, screenshot_height, screenshot_stride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-13 03:42:02 +00:00
|
|
|
if (g_gpu_device->UsesLowerLeftOrigin())
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-10-03 06:44:34 +00:00
|
|
|
GPUTexture::FlipTextureDataRGBA8(screenshot_width, screenshot_height, screenshot_buffer, screenshot_stride);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
2021-04-27 16:37:52 +00:00
|
|
|
|
|
|
|
header.offset_to_screenshot = static_cast<u32>(state->GetPosition());
|
|
|
|
header.screenshot_width = screenshot_width;
|
|
|
|
header.screenshot_height = screenshot_height;
|
|
|
|
header.screenshot_size = static_cast<u32>(screenshot_buffer.size() * sizeof(u32));
|
|
|
|
if (!state->Write2(screenshot_buffer.data(), header.screenshot_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Failed to save %ux%u screenshot for save state due to render/conversion failure",
|
|
|
|
screenshot_width, screenshot_height);
|
2020-04-20 16:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write data
|
|
|
|
{
|
|
|
|
header.offset_to_data = static_cast<u32>(state->GetPosition());
|
|
|
|
|
2020-08-01 06:45:06 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
2022-08-18 11:21:22 +00:00
|
|
|
header.data_compression_type = compression_method;
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
if (compression_method == SAVE_STATE_HEADER::COMPRESSION_TYPE_NONE)
|
|
|
|
{
|
|
|
|
StateWrapper sw(state, StateWrapper::Mode::Write, SAVE_STATE_VERSION);
|
|
|
|
result = DoState(sw, nullptr, false, false);
|
|
|
|
header.data_uncompressed_size = static_cast<u32>(state->GetPosition() - header.offset_to_data);
|
|
|
|
}
|
|
|
|
else if (compression_method == SAVE_STATE_HEADER::COMPRESSION_TYPE_ZSTD)
|
|
|
|
{
|
2022-08-18 12:43:02 +00:00
|
|
|
std::unique_ptr<ByteStream> cstream(ByteStream::CreateZstdCompressStream(state, 0));
|
|
|
|
StateWrapper sw(cstream.get(), StateWrapper::Mode::Write, SAVE_STATE_VERSION);
|
|
|
|
result = DoState(sw, nullptr, false, false) && cstream->Commit();
|
|
|
|
header.data_uncompressed_size = static_cast<u32>(cstream->GetPosition());
|
|
|
|
header.data_compressed_size = static_cast<u32>(state->GetPosition() - header.offset_to_data);
|
2022-08-18 11:21:22 +00:00
|
|
|
}
|
2020-08-01 06:45:06 +00:00
|
|
|
|
|
|
|
if (!result)
|
2020-04-20 16:50:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-write header
|
|
|
|
const u64 end_position = state->GetPosition();
|
|
|
|
if (!state->SeekAbsolute(header_position) || !state->Write2(&header, sizeof(header)) ||
|
|
|
|
!state->SeekAbsolute(end_position))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-09-14 10:28:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float System::GetTargetSpeed()
|
2021-01-12 16:36:11 +00:00
|
|
|
{
|
|
|
|
return s_target_speed;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdatePerformanceCounters()
|
2020-02-09 13:16:25 +00:00
|
|
|
{
|
2023-01-07 03:09:20 +00:00
|
|
|
const float frame_time = static_cast<float>(s_frame_timer.GetTimeMillisecondsAndReset());
|
2023-01-09 09:34:40 +00:00
|
|
|
s_minimum_frame_time_accumulator =
|
|
|
|
(s_minimum_frame_time_accumulator == 0.0f) ? frame_time : std::min(s_minimum_frame_time_accumulator, frame_time);
|
2020-07-31 07:09:18 +00:00
|
|
|
s_average_frame_time_accumulator += frame_time;
|
2023-01-07 03:09:20 +00:00
|
|
|
s_maximum_frame_time_accumulator = std::max(s_maximum_frame_time_accumulator, frame_time);
|
|
|
|
s_frame_time_history[s_frame_time_history_pos] = frame_time;
|
|
|
|
s_frame_time_history_pos = (s_frame_time_history_pos + 1) % NUM_FRAME_TIME_SAMPLES;
|
2020-02-09 13:16:25 +00:00
|
|
|
|
|
|
|
// update fps counter
|
2022-07-11 13:03:29 +00:00
|
|
|
const Common::Timer::Value now_ticks = Common::Timer::GetCurrentValue();
|
|
|
|
const Common::Timer::Value ticks_diff = now_ticks - s_fps_timer.GetStartValue();
|
|
|
|
const float time = static_cast<float>(Common::Timer::ConvertValueToSeconds(ticks_diff));
|
2023-01-11 09:40:56 +00:00
|
|
|
if (time < PERFORMANCE_COUNTER_UPDATE_INTERVAL)
|
2020-02-09 13:16:25 +00:00
|
|
|
return;
|
|
|
|
|
2022-09-03 04:15:15 +00:00
|
|
|
const float frames_run = static_cast<float>(s_frame_number - s_last_frame_number);
|
2020-07-31 07:09:18 +00:00
|
|
|
const u32 global_tick_counter = TimingEvents::GetGlobalTickCounter();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
// TODO: Make the math here less rubbish
|
|
|
|
const double pct_divider =
|
|
|
|
100.0 * (1.0 / ((static_cast<double>(ticks_diff) * static_cast<double>(Threading::GetThreadTicksPerSecond())) /
|
|
|
|
Common::Timer::GetFrequency() / 1000000000.0));
|
|
|
|
const double time_divider = 1000.0 * (1.0 / static_cast<double>(Threading::GetThreadTicksPerSecond())) *
|
2022-09-03 04:15:15 +00:00
|
|
|
(1.0 / static_cast<double>(frames_run));
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2023-01-07 03:09:20 +00:00
|
|
|
s_minimum_frame_time = std::exchange(s_minimum_frame_time_accumulator, 0.0f);
|
|
|
|
s_average_frame_time = std::exchange(s_average_frame_time_accumulator, 0.0f) / frames_run;
|
|
|
|
s_maximum_frame_time = std::exchange(s_maximum_frame_time_accumulator, 0.0f);
|
|
|
|
|
2022-09-03 04:15:15 +00:00
|
|
|
s_vps = static_cast<float>(frames_run / time);
|
2020-07-31 07:09:18 +00:00
|
|
|
s_last_frame_number = s_frame_number;
|
|
|
|
s_fps = static_cast<float>(s_internal_frame_number - s_last_internal_frame_number) / time;
|
|
|
|
s_last_internal_frame_number = s_internal_frame_number;
|
|
|
|
s_speed = static_cast<float>(static_cast<double>(global_tick_counter - s_last_global_tick_counter) /
|
2020-09-29 13:29:28 +00:00
|
|
|
(static_cast<double>(g_ticks_per_second) * time)) *
|
2020-02-09 13:16:25 +00:00
|
|
|
100.0f;
|
2020-07-31 07:09:18 +00:00
|
|
|
s_last_global_tick_counter = global_tick_counter;
|
2020-02-09 13:16:25 +00:00
|
|
|
|
2022-08-05 07:17:29 +00:00
|
|
|
const Threading::Thread* sw_thread = g_gpu->GetSWThread();
|
|
|
|
const u64 cpu_time = s_cpu_thread_handle ? s_cpu_thread_handle.GetCPUTime() : 0;
|
2022-07-11 13:03:29 +00:00
|
|
|
const u64 sw_time = sw_thread ? sw_thread->GetCPUTime() : 0;
|
|
|
|
const u64 cpu_delta = cpu_time - s_last_cpu_time;
|
|
|
|
const u64 sw_delta = sw_time - s_last_sw_time;
|
|
|
|
s_last_cpu_time = cpu_time;
|
|
|
|
s_last_sw_time = sw_time;
|
|
|
|
|
|
|
|
s_cpu_thread_usage = static_cast<float>(static_cast<double>(cpu_delta) * pct_divider);
|
|
|
|
s_cpu_thread_time = static_cast<float>(static_cast<double>(cpu_delta) * time_divider);
|
|
|
|
s_sw_thread_usage = static_cast<float>(static_cast<double>(sw_delta) * pct_divider);
|
|
|
|
s_sw_thread_time = static_cast<float>(static_cast<double>(sw_delta) * time_divider);
|
|
|
|
|
|
|
|
s_fps_timer.ResetTo(now_ticks);
|
2020-12-07 15:47:28 +00:00
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
if (g_gpu_device->IsGPUTimingEnabled())
|
2022-09-03 04:15:15 +00:00
|
|
|
{
|
|
|
|
s_average_gpu_time = s_accumulated_gpu_time / static_cast<float>(std::max(s_presents_since_last_update, 1u));
|
|
|
|
s_gpu_usage = s_accumulated_gpu_time / (time * 10.0f);
|
|
|
|
}
|
|
|
|
s_accumulated_gpu_time = 0.0f;
|
|
|
|
s_presents_since_last_update = 0;
|
|
|
|
|
2023-01-07 03:09:20 +00:00
|
|
|
Log_VerbosePrintf("FPS: %.2f VPS: %.2f CPU: %.2f GPU: %.2f Average: %.2fms Min: %.2fms Max: %.2f ms", s_fps, s_vps,
|
|
|
|
s_cpu_thread_usage, s_gpu_usage, s_average_frame_time, s_minimum_frame_time, s_maximum_frame_time);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
Host::OnPerformanceCountersUpdated();
|
2020-02-09 13:16:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::ResetPerformanceCounters()
|
2020-02-09 13:16:25 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
s_last_frame_number = s_frame_number;
|
|
|
|
s_last_internal_frame_number = s_internal_frame_number;
|
|
|
|
s_last_global_tick_counter = TimingEvents::GetGlobalTickCounter();
|
2022-08-05 07:17:29 +00:00
|
|
|
s_last_cpu_time = s_cpu_thread_handle ? s_cpu_thread_handle.GetCPUTime() : 0;
|
|
|
|
if (const Threading::Thread* sw_thread = g_gpu->GetSWThread(); sw_thread)
|
|
|
|
s_last_sw_time = sw_thread->GetCPUTime();
|
|
|
|
else
|
|
|
|
s_last_sw_time = 0;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
s_average_frame_time_accumulator = 0.0f;
|
2023-01-07 03:09:20 +00:00
|
|
|
s_minimum_frame_time_accumulator = 0.0f;
|
|
|
|
s_maximum_frame_time_accumulator = 0.0f;
|
|
|
|
s_frame_timer.Reset();
|
2020-07-31 07:09:18 +00:00
|
|
|
s_fps_timer.Reset();
|
2020-12-06 16:43:46 +00:00
|
|
|
ResetThrottler();
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
2019-09-14 03:22:05 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateSpeedLimiterState()
|
|
|
|
{
|
2022-08-15 07:21:19 +00:00
|
|
|
const float old_target_speed = s_target_speed;
|
2022-08-16 11:23:14 +00:00
|
|
|
s_target_speed = s_turbo_enabled ?
|
2022-08-15 07:21:19 +00:00
|
|
|
g_settings.turbo_speed :
|
2022-08-16 11:23:14 +00:00
|
|
|
(s_fast_forward_enabled ? g_settings.fast_forward_speed : g_settings.emulation_speed);
|
|
|
|
s_throttler_enabled = (s_target_speed != 0.0f);
|
|
|
|
s_display_all_frames = !s_throttler_enabled || g_settings.display_all_frames;
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2022-08-16 11:23:14 +00:00
|
|
|
s_syncing_to_host = false;
|
2022-07-27 14:42:41 +00:00
|
|
|
if (g_settings.sync_to_host_refresh_rate && (g_settings.audio_stretch_mode != AudioStretchMode::Off) &&
|
2022-08-15 07:21:19 +00:00
|
|
|
s_target_speed == 1.0f && IsValid())
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
float host_refresh_rate;
|
2023-08-13 03:42:02 +00:00
|
|
|
if (g_gpu_device->GetHostRefreshRate(&host_refresh_rate))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
const float ratio = host_refresh_rate / System::GetThrottleFrequency();
|
2022-08-16 11:23:14 +00:00
|
|
|
s_syncing_to_host = (ratio >= 0.95f && ratio <= 1.05f);
|
2022-07-11 13:03:29 +00:00
|
|
|
Log_InfoPrintf("Refresh rate: Host=%fhz Guest=%fhz Ratio=%f - %s", host_refresh_rate,
|
2022-08-16 11:23:14 +00:00
|
|
|
System::GetThrottleFrequency(), ratio, s_syncing_to_host ? "can sync" : "can't sync");
|
|
|
|
if (s_syncing_to_host)
|
2022-08-15 07:21:19 +00:00
|
|
|
s_target_speed *= ratio;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:23:14 +00:00
|
|
|
// When syncing to host and using vsync, we don't need to sleep.
|
|
|
|
if (s_syncing_to_host && ShouldUseVSync() && s_display_all_frames)
|
|
|
|
{
|
|
|
|
Log_InfoPrintf("Using host vsync for throttling.");
|
|
|
|
s_throttler_enabled = false;
|
|
|
|
}
|
|
|
|
|
2022-08-10 03:09:32 +00:00
|
|
|
Log_VerbosePrintf("Target speed: %f%%", s_target_speed * 100.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (IsValid())
|
|
|
|
{
|
2022-08-15 07:21:19 +00:00
|
|
|
// Update audio output.
|
2022-08-01 13:39:41 +00:00
|
|
|
AudioStream* stream = SPU::GetOutputStream();
|
2022-08-15 07:21:19 +00:00
|
|
|
stream->SetOutputVolume(GetAudioOutputVolume());
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2022-07-27 14:42:41 +00:00
|
|
|
// Adjust nominal rate when resampling, or syncing to host.
|
|
|
|
const bool rate_adjust =
|
2022-08-16 11:23:14 +00:00
|
|
|
(s_syncing_to_host || g_settings.audio_stretch_mode == AudioStretchMode::Resample) && s_target_speed > 0.0f;
|
2022-08-15 07:21:19 +00:00
|
|
|
stream->SetNominalRate(rate_adjust ? s_target_speed : 1.0f);
|
|
|
|
|
|
|
|
if (old_target_speed < s_target_speed)
|
|
|
|
stream->UpdateTargetTempo(s_target_speed);
|
2022-07-27 14:42:41 +00:00
|
|
|
|
2022-07-29 11:16:02 +00:00
|
|
|
UpdateThrottlePeriod();
|
|
|
|
ResetThrottler();
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 03:09:32 +00:00
|
|
|
// Defer vsync update until we unpause, in case of fullscreen UI.
|
|
|
|
if (IsRunning())
|
|
|
|
UpdateDisplaySync();
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (g_settings.increase_timer_resolution)
|
2022-08-16 11:23:14 +00:00
|
|
|
SetTimerResolutionIncreased(s_throttler_enabled);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 03:09:32 +00:00
|
|
|
void System::UpdateDisplaySync()
|
|
|
|
{
|
|
|
|
const bool video_sync_enabled = ShouldUseVSync();
|
2022-08-16 11:23:14 +00:00
|
|
|
const bool syncing_to_host_vsync = (s_syncing_to_host && video_sync_enabled && s_display_all_frames);
|
|
|
|
const float max_display_fps = (s_throttler_enabled || s_syncing_to_host) ? 0.0f : g_settings.display_max_fps;
|
2022-10-03 13:20:03 +00:00
|
|
|
Log_VerbosePrintf("Using vsync: %s%s", video_sync_enabled ? "YES" : "NO",
|
2022-08-18 11:21:22 +00:00
|
|
|
syncing_to_host_vsync ? " (for throttling)" : "");
|
2022-08-10 03:09:32 +00:00
|
|
|
Log_VerbosePrintf("Max display fps: %f (%s)", max_display_fps,
|
2022-08-16 11:23:14 +00:00
|
|
|
s_display_all_frames ? "displaying all frames" : "skipping displaying frames when needed");
|
2022-08-10 03:09:32 +00:00
|
|
|
|
2023-08-13 03:42:02 +00:00
|
|
|
g_gpu_device->SetDisplayMaxFPS(max_display_fps);
|
|
|
|
g_gpu_device->SetVSync(video_sync_enabled);
|
2022-08-10 03:09:32 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 11:46:18 +00:00
|
|
|
bool System::ShouldUseVSync()
|
|
|
|
{
|
2022-08-10 03:09:32 +00:00
|
|
|
return g_settings.video_sync_enabled && !IsRunningAtNonStandardSpeed();
|
2022-07-26 11:46:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsFastForwardEnabled()
|
|
|
|
{
|
2022-08-16 11:23:14 +00:00
|
|
|
return s_fast_forward_enabled;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void System::SetFastForwardEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
2022-08-16 11:23:14 +00:00
|
|
|
s_fast_forward_enabled = enabled;
|
2022-07-11 13:03:29 +00:00
|
|
|
UpdateSpeedLimiterState();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::IsTurboEnabled()
|
|
|
|
{
|
2022-08-16 11:23:14 +00:00
|
|
|
return s_turbo_enabled;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void System::SetTurboEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
2022-08-16 11:23:14 +00:00
|
|
|
s_turbo_enabled = enabled;
|
2022-07-11 13:03:29 +00:00
|
|
|
UpdateSpeedLimiterState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::SetRewindState(bool enabled)
|
|
|
|
{
|
|
|
|
if (!System::IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!g_settings.rewind_enable)
|
|
|
|
{
|
|
|
|
if (enabled)
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddKeyedOSDMessage("SetRewindState", TRANSLATE_STR("OSDMessage", "Rewinding is not enabled."), 5.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
if (Achievements::ChallengeModeActive() && !Achievements::ConfirmChallengeModeDisable("Rewinding"))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
System::SetRewinding(enabled);
|
|
|
|
UpdateSpeedLimiterState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::DoFrameStep()
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
if (Achievements::ChallengeModeActive() && !Achievements::ConfirmChallengeModeDisable("Frame stepping"))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2022-08-16 11:23:14 +00:00
|
|
|
s_frame_step_request = true;
|
2022-07-11 13:03:29 +00:00
|
|
|
PauseSystem(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::DoToggleCheats()
|
|
|
|
{
|
|
|
|
if (!System::IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
if (Achievements::ChallengeModeActive() && !Achievements::ConfirmChallengeModeDisable("Toggling cheats"))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CheatList* cl = GetCheatList();
|
|
|
|
if (!cl)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddKeyedOSDMessage("ToggleCheats", TRANSLATE_STR("OSDMessage", "No cheats are loaded."), 10.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cl->SetMasterEnable(!cl->GetMasterEnable());
|
|
|
|
Host::AddKeyedOSDMessage(
|
|
|
|
"ToggleCheats",
|
|
|
|
cl->GetMasterEnable() ?
|
2023-08-19 14:08:03 +00:00
|
|
|
fmt::format(TRANSLATE_FS("OSDMessage", "{} cheats are now active."), cl->GetEnabledCodeCount()) :
|
|
|
|
fmt::format(TRANSLATE_FS("OSDMessage", "{} cheats are now inactive."), cl->GetEnabledCodeCount()),
|
2022-07-11 13:03:29 +00:00
|
|
|
10.0f);
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:51:43 +00:00
|
|
|
static bool LoadEXEToRAM(const char* filename, bool patch_bios)
|
2019-09-14 03:22:05 +00:00
|
|
|
{
|
2020-08-01 03:34:07 +00:00
|
|
|
std::FILE* fp = FileSystem::OpenCFile(filename, "rb");
|
2019-09-14 03:22:05 +00:00
|
|
|
if (!fp)
|
2021-06-11 00:51:43 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to open exe file '%s'", filename);
|
2019-09-14 03:22:05 +00:00
|
|
|
return false;
|
2021-06-11 00:51:43 +00:00
|
|
|
}
|
2019-09-14 03:22:05 +00:00
|
|
|
|
2019-12-04 11:12:50 +00:00
|
|
|
std::fseek(fp, 0, SEEK_END);
|
|
|
|
const u32 file_size = static_cast<u32>(std::ftell(fp));
|
|
|
|
std::fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
BIOS::PSEXEHeader header;
|
|
|
|
if (std::fread(&header, sizeof(header), 1, fp) != 1 || !BIOS::IsValidPSExeHeader(header, file_size))
|
2019-09-14 03:22:05 +00:00
|
|
|
{
|
2021-06-11 00:51:43 +00:00
|
|
|
Log_ErrorPrintf("'%s' is not a valid PS-EXE", filename);
|
2019-09-14 03:22:05 +00:00
|
|
|
std::fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.memfill_size > 0)
|
|
|
|
{
|
|
|
|
const u32 words_to_write = header.memfill_size / 4;
|
|
|
|
u32 address = header.memfill_start & ~UINT32_C(3);
|
|
|
|
for (u32 i = 0; i < words_to_write; i++)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::SafeWriteMemoryWord(address, 0);
|
2019-09-14 03:22:05 +00:00
|
|
|
address += sizeof(u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:48:23 +00:00
|
|
|
const u32 file_data_size = std::min<u32>(file_size - sizeof(BIOS::PSEXEHeader), header.file_size);
|
|
|
|
if (file_data_size >= 4)
|
2019-09-14 03:22:05 +00:00
|
|
|
{
|
2021-11-06 00:48:23 +00:00
|
|
|
std::vector<u32> data_words((file_data_size + 3) / 4);
|
|
|
|
if (std::fread(data_words.data(), file_data_size, 1, fp) != 1)
|
2019-09-14 03:22:05 +00:00
|
|
|
{
|
|
|
|
std::fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:48:23 +00:00
|
|
|
const u32 num_words = file_data_size / 4;
|
2019-09-14 03:22:05 +00:00
|
|
|
u32 address = header.load_address;
|
|
|
|
for (u32 i = 0; i < num_words; i++)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::SafeWriteMemoryWord(address, data_words[i]);
|
2019-09-14 03:22:05 +00:00
|
|
|
address += sizeof(u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fclose(fp);
|
|
|
|
|
|
|
|
// patch the BIOS to jump to the executable directly
|
2019-12-11 06:34:56 +00:00
|
|
|
const u32 r_pc = header.initial_pc;
|
2019-11-16 05:27:57 +00:00
|
|
|
const u32 r_gp = header.initial_gp;
|
2019-12-11 06:34:56 +00:00
|
|
|
const u32 r_sp = header.initial_sp_base + header.initial_sp_offset;
|
2019-11-16 05:27:57 +00:00
|
|
|
const u32 r_fp = header.initial_sp_base + header.initial_sp_offset;
|
2020-10-29 12:05:09 +00:00
|
|
|
return BIOS::PatchBIOSForEXE(Bus::g_bios, Bus::BIOS_SIZE, r_pc, r_gp, r_sp, r_fp);
|
2019-09-14 03:22:05 +00:00
|
|
|
}
|
2019-09-14 10:28:47 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::LoadEXE(const char* filename)
|
2021-06-11 00:51:43 +00:00
|
|
|
{
|
2022-07-08 11:57:06 +00:00
|
|
|
const std::string libps_path(Path::BuildRelativePath(filename, "libps.exe"));
|
2021-06-11 00:51:43 +00:00
|
|
|
if (!libps_path.empty() && FileSystem::FileExists(libps_path.c_str()) && !LoadEXEToRAM(libps_path.c_str(), false))
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to load libps.exe from '%s'", libps_path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoadEXEToRAM(filename, true);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::InjectEXEFromBuffer(const void* buffer, u32 buffer_size, bool patch_bios)
|
2020-04-16 14:29:56 +00:00
|
|
|
{
|
|
|
|
const u8* buffer_ptr = static_cast<const u8*>(buffer);
|
|
|
|
const u8* buffer_end = static_cast<const u8*>(buffer) + buffer_size;
|
|
|
|
|
|
|
|
BIOS::PSEXEHeader header;
|
|
|
|
if (buffer_size < sizeof(header))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::memcpy(&header, buffer_ptr, sizeof(header));
|
|
|
|
buffer_ptr += sizeof(header);
|
|
|
|
|
2021-11-06 00:48:23 +00:00
|
|
|
const u32 file_size = static_cast<u32>(static_cast<u32>(buffer_end - buffer_ptr));
|
|
|
|
if (!BIOS::IsValidPSExeHeader(header, file_size))
|
2020-04-16 14:29:56 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (header.memfill_size > 0)
|
|
|
|
{
|
|
|
|
const u32 words_to_write = header.memfill_size / 4;
|
|
|
|
u32 address = header.memfill_start & ~UINT32_C(3);
|
|
|
|
for (u32 i = 0; i < words_to_write; i++)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::SafeWriteMemoryWord(address, 0);
|
2020-04-16 14:29:56 +00:00
|
|
|
address += sizeof(u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:48:23 +00:00
|
|
|
const u32 file_data_size = std::min<u32>(file_size - sizeof(BIOS::PSEXEHeader), header.file_size);
|
|
|
|
if (file_data_size >= 4)
|
2020-04-16 14:29:56 +00:00
|
|
|
{
|
2021-11-06 00:48:23 +00:00
|
|
|
std::vector<u32> data_words((file_data_size + 3) / 4);
|
|
|
|
if ((buffer_end - buffer_ptr) < file_data_size)
|
2020-04-16 14:29:56 +00:00
|
|
|
return false;
|
|
|
|
|
2021-11-06 00:48:23 +00:00
|
|
|
std::memcpy(data_words.data(), buffer_ptr, file_data_size);
|
2020-04-16 14:29:56 +00:00
|
|
|
|
2021-11-06 00:48:23 +00:00
|
|
|
const u32 num_words = file_data_size / 4;
|
2020-04-16 14:29:56 +00:00
|
|
|
u32 address = header.load_address;
|
|
|
|
for (u32 i = 0; i < num_words; i++)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::SafeWriteMemoryWord(address, data_words[i]);
|
2020-04-16 14:29:56 +00:00
|
|
|
address += sizeof(u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// patch the BIOS to jump to the executable directly
|
2021-01-24 03:17:22 +00:00
|
|
|
if (patch_bios)
|
|
|
|
{
|
|
|
|
const u32 r_pc = header.initial_pc;
|
|
|
|
const u32 r_gp = header.initial_gp;
|
|
|
|
const u32 r_sp = header.initial_sp_base + header.initial_sp_offset;
|
|
|
|
const u32 r_fp = header.initial_sp_base + header.initial_sp_offset;
|
|
|
|
if (!BIOS::PatchBIOSForEXE(Bus::g_bios, Bus::BIOS_SIZE, r_pc, r_gp, r_sp, r_fp))
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-16 14:29:56 +00:00
|
|
|
|
2021-01-24 03:17:22 +00:00
|
|
|
return true;
|
2020-04-16 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 09:27:51 +00:00
|
|
|
#if 0
|
|
|
|
// currently not used until EXP1 is implemented
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool SetExpansionROM(const char* filename)
|
2019-09-22 15:28:00 +00:00
|
|
|
{
|
2020-08-01 03:34:07 +00:00
|
|
|
std::FILE* fp = FileSystem::OpenCFile(filename, "rb");
|
2019-09-22 15:28:00 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to open '%s'", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fseek(fp, 0, SEEK_END);
|
|
|
|
const u32 size = static_cast<u32>(std::ftell(fp));
|
|
|
|
std::fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
std::vector<u8> data(size);
|
|
|
|
if (std::fread(data.data(), size, 1, fp) != 1)
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to read ROM data from '%s'", filename);
|
|
|
|
std::fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fclose(fp);
|
|
|
|
|
|
|
|
Log_InfoPrintf("Loaded expansion ROM from '%s': %u bytes", filename, size);
|
2020-07-31 07:09:18 +00:00
|
|
|
Bus::SetExpansionROM(std::move(data));
|
2019-09-22 15:28:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::StallCPU(TickCount ticks)
|
2019-10-17 14:20:38 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::AddPendingTicks(ticks);
|
2019-10-17 14:20:38 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
#endif
|
2019-10-17 14:20:38 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
Controller* System::GetController(u32 slot)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
return Pad::GetController(slot);
|
2019-12-14 13:20:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateControllers()
|
2019-12-14 13:20:24 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
auto lock = Host::GetSettingsLock();
|
|
|
|
|
2020-01-10 03:31:12 +00:00
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::SetController(i, nullptr);
|
2019-12-16 06:46:43 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
const ControllerType type = g_settings.controller_types[i];
|
2020-01-10 03:31:12 +00:00
|
|
|
if (type != ControllerType::None)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
std::unique_ptr<Controller> controller = Controller::Create(type, i);
|
2019-12-16 06:46:43 +00:00
|
|
|
if (controller)
|
2020-06-30 14:33:45 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
controller->LoadSettings(*Host::GetSettingsInterfaceForBindings(), Controller::GetSettingsSection(i).c_str());
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::SetController(i, std::move(controller));
|
2020-06-30 14:33:45 +00:00
|
|
|
}
|
2019-12-16 06:46:43 +00:00
|
|
|
}
|
2019-12-14 13:29:26 +00:00
|
|
|
}
|
2019-09-29 15:07:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateControllerSettings()
|
2020-06-30 14:33:45 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
auto lock = Host::GetSettingsLock();
|
|
|
|
|
2020-06-30 14:33:45 +00:00
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Controller* controller = Pad::GetController(i);
|
2020-06-30 14:33:45 +00:00
|
|
|
if (controller)
|
2022-07-11 13:03:29 +00:00
|
|
|
controller->LoadSettings(*Host::GetSettingsInterfaceForBindings(), Controller::GetSettingsSection(i).c_str());
|
2020-06-30 14:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::ResetControllers()
|
2020-07-04 14:05:14 +00:00
|
|
|
{
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Controller* controller = Pad::GetController(i);
|
2020-07-04 14:05:14 +00:00
|
|
|
if (controller)
|
|
|
|
controller->Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::unique_ptr<MemoryCard> System::GetMemoryCardForSlot(u32 slot, MemoryCardType type)
|
2019-09-29 15:07:38 +00:00
|
|
|
{
|
2021-04-08 01:34:15 +00:00
|
|
|
// Disable memory cards when running PSFs.
|
|
|
|
const bool is_running_psf = !s_running_game_path.empty() && IsPsfFileName(s_running_game_path.c_str());
|
2021-05-24 09:55:07 +00:00
|
|
|
if (is_running_psf)
|
|
|
|
return nullptr;
|
2021-04-08 01:34:15 +00:00
|
|
|
|
2021-05-24 09:55:07 +00:00
|
|
|
switch (type)
|
2019-10-27 06:45:23 +00:00
|
|
|
{
|
2021-05-24 09:55:07 +00:00
|
|
|
case MemoryCardType::PerGame:
|
2020-04-27 06:15:38 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
if (s_running_game_serial.empty())
|
2020-04-27 06:15:38 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f,
|
|
|
|
TRANSLATE("System",
|
|
|
|
"Per-game memory card cannot be used for slot %u as the running "
|
|
|
|
"game has no code. Using shared card instead."),
|
|
|
|
slot + 1u);
|
2022-07-11 13:03:29 +00:00
|
|
|
return MemoryCard::Open(g_settings.GetSharedMemoryCardPath(slot));
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
2021-05-24 09:55:07 +00:00
|
|
|
else
|
2020-04-27 15:04:10 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
return MemoryCard::Open(g_settings.GetGameMemoryCardPath(s_running_game_serial.c_str(), slot));
|
2020-04-27 15:04:10 +00:00
|
|
|
}
|
2021-05-24 09:55:07 +00:00
|
|
|
}
|
2020-04-27 15:04:10 +00:00
|
|
|
|
2021-05-24 09:55:07 +00:00
|
|
|
case MemoryCardType::PerGameTitle:
|
|
|
|
{
|
|
|
|
if (s_running_game_title.empty())
|
2021-05-21 03:18:32 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f,
|
|
|
|
TRANSLATE("System",
|
|
|
|
"Per-game memory card cannot be used for slot %u as the running "
|
|
|
|
"game has no title. Using shared card instead."),
|
|
|
|
slot + 1u);
|
2022-07-11 13:03:29 +00:00
|
|
|
return MemoryCard::Open(g_settings.GetSharedMemoryCardPath(slot));
|
2021-05-21 03:18:32 +00:00
|
|
|
}
|
2021-05-24 09:55:07 +00:00
|
|
|
else
|
2020-04-27 06:15:38 +00:00
|
|
|
{
|
2023-08-23 08:40:59 +00:00
|
|
|
std::string card_path;
|
|
|
|
|
|
|
|
// Playlist - use title if different.
|
|
|
|
if (HasMediaSubImages() && s_running_game_entry && s_running_game_title != s_running_game_entry->title)
|
|
|
|
{
|
|
|
|
card_path = g_settings.GetGameMemoryCardPath(
|
|
|
|
MemoryCard::SanitizeGameTitleForFileName(s_running_game_entry->title), slot);
|
|
|
|
}
|
|
|
|
// Multi-disc game - use disc set name.
|
|
|
|
else if (s_running_game_entry && !s_running_game_entry->disc_set_name.empty())
|
|
|
|
{
|
|
|
|
card_path = g_settings.GetGameMemoryCardPath(
|
|
|
|
MemoryCard::SanitizeGameTitleForFileName(s_running_game_entry->disc_set_name), slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
// But prefer a disc-specific card if one already exists.
|
|
|
|
std::string disc_card_path =
|
2023-08-23 09:39:22 +00:00
|
|
|
g_settings.GetGameMemoryCardPath(MemoryCard::SanitizeGameTitleForFileName(
|
|
|
|
s_running_game_entry ? s_running_game_entry->title : s_running_game_title),
|
|
|
|
slot);
|
2023-08-23 08:40:59 +00:00
|
|
|
if (disc_card_path != card_path)
|
|
|
|
{
|
|
|
|
if (card_path.empty() || !g_settings.memory_card_use_playlist_title ||
|
|
|
|
FileSystem::FileExists(disc_card_path.c_str()))
|
|
|
|
{
|
|
|
|
if (g_settings.memory_card_use_playlist_title && !card_path.empty())
|
|
|
|
{
|
|
|
|
Host::AddIconOSDMessage(
|
|
|
|
fmt::format("DiscSpecificMC{}", slot), ICON_FA_SD_CARD,
|
|
|
|
fmt::format(TRANSLATE_FS("System", "Using disc-specific memory card '{}' instead of per-game card."),
|
|
|
|
Path::GetFileName(disc_card_path)),
|
|
|
|
Host::OSD_INFO_DURATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
card_path = std::move(disc_card_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MemoryCard::Open(card_path.c_str());
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
2021-05-24 09:55:07 +00:00
|
|
|
}
|
2021-05-23 07:11:59 +00:00
|
|
|
|
2021-05-24 09:55:07 +00:00
|
|
|
case MemoryCardType::PerGameFileTitle:
|
|
|
|
{
|
|
|
|
const std::string display_name(FileSystem::GetDisplayNameFromPath(s_running_game_path));
|
2022-07-08 11:57:06 +00:00
|
|
|
const std::string_view file_title(Path::GetFileTitle(display_name));
|
2021-05-24 09:55:07 +00:00
|
|
|
if (file_title.empty())
|
2021-05-23 07:11:59 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f,
|
|
|
|
TRANSLATE("System",
|
|
|
|
"Per-game memory card cannot be used for slot %u as the running "
|
|
|
|
"game has no path. Using shared card instead."),
|
|
|
|
slot + 1u);
|
2022-07-11 13:03:29 +00:00
|
|
|
return MemoryCard::Open(g_settings.GetSharedMemoryCardPath(slot));
|
2021-05-23 07:11:59 +00:00
|
|
|
}
|
2021-05-24 09:55:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return MemoryCard::Open(
|
2022-07-11 13:03:29 +00:00
|
|
|
g_settings.GetGameMemoryCardPath(MemoryCard::SanitizeGameTitleForFileName(file_title).c_str(), slot));
|
2021-05-24 09:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemoryCardType::Shared:
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
return MemoryCard::Open(g_settings.GetSharedMemoryCardPath(slot));
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 09:55:07 +00:00
|
|
|
case MemoryCardType::NonPersistent:
|
|
|
|
return MemoryCard::Create();
|
|
|
|
|
|
|
|
case MemoryCardType::None:
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateMemoryCardTypes()
|
2021-05-24 09:55:07 +00:00
|
|
|
{
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::SetMemoryCard(i, nullptr);
|
2021-05-24 09:55:07 +00:00
|
|
|
|
|
|
|
const MemoryCardType type = g_settings.memory_card_types[i];
|
|
|
|
std::unique_ptr<MemoryCard> card = GetMemoryCardForSlot(i, type);
|
|
|
|
if (card)
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::SetMemoryCard(i, std::move(card));
|
2021-05-24 09:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdatePerGameMemoryCards()
|
2021-05-24 09:55:07 +00:00
|
|
|
{
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
|
|
|
const MemoryCardType type = g_settings.memory_card_types[i];
|
|
|
|
if (!Settings::IsPerGameMemoryCardType(type))
|
|
|
|
continue;
|
|
|
|
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::SetMemoryCard(i, nullptr);
|
2021-05-24 09:55:07 +00:00
|
|
|
|
|
|
|
std::unique_ptr<MemoryCard> card = GetMemoryCardForSlot(i, type);
|
2019-10-27 06:45:23 +00:00
|
|
|
if (card)
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::SetMemoryCard(i, std::move(card));
|
2019-10-27 06:45:23 +00:00
|
|
|
}
|
2019-09-20 06:47:41 +00:00
|
|
|
}
|
2019-09-20 10:14:00 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::HasMemoryCard(u32 slot)
|
2021-05-18 04:36:55 +00:00
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
return (Pad::GetMemoryCard(slot) != nullptr);
|
2021-05-18 04:36:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::SwapMemoryCards()
|
2021-05-18 04:36:55 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
2023-01-11 09:10:21 +00:00
|
|
|
std::unique_ptr<MemoryCard> first = Pad::RemoveMemoryCard(0);
|
|
|
|
std::unique_ptr<MemoryCard> second = Pad::RemoveMemoryCard(1);
|
|
|
|
Pad::SetMemoryCard(0, std::move(second));
|
|
|
|
Pad::SetMemoryCard(1, std::move(first));
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (HasMemoryCard(0) && HasMemoryCard(1))
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(TRANSLATE_STR("OSDMessage", "Swapped memory card ports. Both ports have a memory card."),
|
|
|
|
10.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
else if (HasMemoryCard(1))
|
|
|
|
{
|
|
|
|
Host::AddOSDMessage(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE_STR("OSDMessage", "Swapped memory card ports. Port 2 has a memory card, Port 1 is empty."), 10.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
else if (HasMemoryCard(0))
|
|
|
|
{
|
|
|
|
Host::AddOSDMessage(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE_STR("OSDMessage", "Swapped memory card ports. Port 1 has a memory card, Port 2 is empty."), 10.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(TRANSLATE_STR("OSDMessage", "Swapped memory card ports. Neither port has a memory card."),
|
|
|
|
10.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
2021-05-18 04:36:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateMultitaps()
|
2021-01-21 07:59:40 +00:00
|
|
|
{
|
|
|
|
switch (g_settings.multitap_mode)
|
|
|
|
{
|
|
|
|
case MultitapMode::Disabled:
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::GetMultitap(0)->SetEnable(false, 0);
|
|
|
|
Pad::GetMultitap(1)->SetEnable(false, 0);
|
2021-01-21 07:59:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultitapMode::Port1Only:
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::GetMultitap(0)->SetEnable(true, 0);
|
|
|
|
Pad::GetMultitap(1)->SetEnable(false, 0);
|
2021-03-01 22:59:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultitapMode::Port2Only:
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::GetMultitap(0)->SetEnable(false, 0);
|
|
|
|
Pad::GetMultitap(1)->SetEnable(true, 1);
|
2021-01-21 07:59:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MultitapMode::BothPorts:
|
|
|
|
{
|
2023-01-11 09:10:21 +00:00
|
|
|
Pad::GetMultitap(0)->SetEnable(true, 0);
|
|
|
|
Pad::GetMultitap(1)->SetEnable(true, 4);
|
2021-01-21 07:59:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::DumpRAM(const char* filename)
|
2020-08-16 13:20:36 +00:00
|
|
|
{
|
2021-01-13 09:24:41 +00:00
|
|
|
if (!IsValid())
|
|
|
|
return false;
|
|
|
|
|
2021-05-02 10:46:48 +00:00
|
|
|
return FileSystem::WriteBinaryFile(filename, Bus::g_ram, Bus::g_ram_size);
|
2021-01-13 09:24:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::DumpVRAM(const char* filename)
|
2021-01-13 09:24:41 +00:00
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
2023-08-13 03:42:02 +00:00
|
|
|
return g_gpu->DumpVRAMToFile(filename);
|
2021-01-13 09:24:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::DumpSPURAM(const char* filename)
|
2021-01-13 09:24:41 +00:00
|
|
|
{
|
|
|
|
if (!IsValid())
|
2020-08-16 13:20:36 +00:00
|
|
|
return false;
|
|
|
|
|
2022-08-01 13:39:41 +00:00
|
|
|
return FileSystem::WriteBinaryFile(filename, SPU::GetRAM().data(), SPU::RAM_SIZE);
|
2020-08-16 13:20:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::HasMedia()
|
2019-09-20 10:14:00 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
return CDROM::HasMedia();
|
2019-09-20 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string System::GetMediaFileName()
|
2021-01-30 05:30:52 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
if (!CDROM::HasMedia())
|
2021-02-16 16:16:23 +00:00
|
|
|
return {};
|
|
|
|
|
2023-01-09 09:34:40 +00:00
|
|
|
return CDROM::GetMediaFileName();
|
2021-01-30 05:30:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::InsertMedia(const char* path)
|
2019-09-20 10:14:00 +00:00
|
|
|
{
|
2023-08-19 13:40:36 +00:00
|
|
|
Error error;
|
2022-08-27 06:52:24 +00:00
|
|
|
std::unique_ptr<CDImage> image = CDImage::Open(path, g_settings.cdrom_load_image_patches, &error);
|
2019-11-16 05:27:57 +00:00
|
|
|
if (!image)
|
2021-01-30 05:30:52 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(10.0f, TRANSLATE("OSDMessage", "Failed to open disc image '%s': %s."), path,
|
|
|
|
error.GetDescription().c_str());
|
2019-11-16 05:27:57 +00:00
|
|
|
return false;
|
2021-01-30 05:30:52 +00:00
|
|
|
}
|
2019-11-16 05:27:57 +00:00
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
const DiscRegion region = GetRegionForImage(image.get());
|
2022-07-11 13:03:29 +00:00
|
|
|
UpdateRunningGame(path, image.get(), false);
|
2023-05-15 13:38:37 +00:00
|
|
|
CDROM::InsertMedia(std::move(image), region);
|
2022-10-05 08:29:08 +00:00
|
|
|
Log_InfoPrintf("Inserted media from %s (%s, %s)", s_running_game_path.c_str(), s_running_game_serial.c_str(),
|
2020-07-31 07:09:18 +00:00
|
|
|
s_running_game_title.c_str());
|
2022-07-23 03:23:54 +00:00
|
|
|
if (g_settings.cdrom_load_image_to_ram)
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::PrecacheMedia();
|
2022-07-23 03:23:54 +00:00
|
|
|
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(10.0f, TRANSLATE("OSDMessage", "Inserted disc '%s' (%s)."), s_running_game_title.c_str(),
|
|
|
|
s_running_game_serial.c_str());
|
2020-04-27 06:15:38 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_settings.HasAnyPerGameMemoryCards())
|
2020-04-27 06:15:38 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(TRANSLATE_STR("System", "Game changed, reloading memory cards."), 10.0f);
|
2021-05-24 09:55:07 +00:00
|
|
|
UpdatePerGameMemoryCards();
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
ClearMemorySaveStates();
|
2019-11-16 05:27:57 +00:00
|
|
|
return true;
|
2019-09-20 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::RemoveMedia()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::RemoveMedia(false);
|
2021-01-23 09:00:54 +00:00
|
|
|
ClearMemorySaveStates();
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateRunningGame(const char* path, CDImage* image, bool booting)
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!booting && s_running_game_path == path)
|
2020-08-20 11:30:11 +00:00
|
|
|
return;
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
const std::string prev_serial = std::move(s_running_game_serial);
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
s_running_game_path.clear();
|
2023-08-23 12:06:48 +00:00
|
|
|
s_running_game_serial = {};
|
2020-07-31 07:09:18 +00:00
|
|
|
s_running_game_title.clear();
|
2023-08-19 12:00:20 +00:00
|
|
|
s_running_game_entry = nullptr;
|
2023-05-15 13:38:37 +00:00
|
|
|
s_running_game_hash = 0;
|
2020-01-24 04:53:40 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (path && std::strlen(path) > 0)
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
s_running_game_path = path;
|
2021-03-26 16:19:23 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
if (IsExeFileName(path) || IsPsfFileName(path))
|
2021-03-26 16:19:23 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
// TODO: We could pull the title from the PSF.
|
|
|
|
s_running_game_title = Path::GetFileTitle(path);
|
|
|
|
}
|
|
|
|
else if (image)
|
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
std::string id;
|
|
|
|
GetGameDetailsFromImage(image, &id, &s_running_game_hash);
|
|
|
|
|
2023-08-19 12:00:20 +00:00
|
|
|
s_running_game_entry = GameDatabase::GetEntryForId(id);
|
|
|
|
if (s_running_game_entry)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2023-08-19 12:00:20 +00:00
|
|
|
s_running_game_serial = s_running_game_entry->serial;
|
|
|
|
s_running_game_title = s_running_game_entry->title;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
2022-07-23 03:36:53 +00:00
|
|
|
else
|
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
s_running_game_serial = std::move(id);
|
|
|
|
s_running_game_title = Path::GetFileTitle(FileSystem::GetDisplayNameFromPath(path));
|
2022-07-23 03:36:53 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2023-08-23 08:40:59 +00:00
|
|
|
if (image->HasSubImages())
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2023-08-23 08:40:59 +00:00
|
|
|
std::string image_title = image->GetMetadata("title");
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!image_title.empty())
|
|
|
|
s_running_game_title = std::move(image_title);
|
|
|
|
}
|
2021-03-26 16:19:23 +00:00
|
|
|
}
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
g_texture_replacements.SetGameID(s_running_game_serial);
|
2020-12-17 17:02:11 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
if (booting)
|
|
|
|
Achievements::ResetChallengeMode();
|
|
|
|
|
|
|
|
Achievements::GameChanged(s_running_game_path, image);
|
|
|
|
#endif
|
2022-10-08 10:59:18 +00:00
|
|
|
|
|
|
|
UpdateGameSettingsLayer();
|
|
|
|
ApplySettings(true);
|
|
|
|
|
|
|
|
s_cheat_list.reset();
|
|
|
|
if (g_settings.auto_load_cheats && !Achievements::ChallengeModeActive())
|
|
|
|
LoadCheatListFromGameTitle();
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
if (s_running_game_serial != prev_serial)
|
|
|
|
UpdateSessionTime(prev_serial);
|
|
|
|
|
|
|
|
SaveStateSelectorUI::RefreshList();
|
|
|
|
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
UpdateDiscordPresence(false);
|
|
|
|
#endif
|
|
|
|
|
2022-10-08 10:59:18 +00:00
|
|
|
Host::OnGameChanged(s_running_game_path, s_running_game_serial, s_running_game_title);
|
2021-02-10 16:04:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::CheckForSBIFile(CDImage* image)
|
2021-02-10 16:04:47 +00:00
|
|
|
{
|
2023-08-19 12:00:20 +00:00
|
|
|
if (!s_running_game_entry || !s_running_game_entry->HasTrait(GameDatabase::Trait::IsLibCryptProtected) || !image ||
|
2021-02-10 16:04:47 +00:00
|
|
|
image->HasNonStandardSubchannel())
|
|
|
|
{
|
|
|
|
return true;
|
2020-12-17 17:02:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
Log_WarningPrintf("SBI file missing but required for %s (%s)", s_running_game_serial.c_str(),
|
2021-02-10 16:04:47 +00:00
|
|
|
s_running_game_title.c_str());
|
2020-12-25 08:02:38 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
if (Host::GetBoolSettingValue("CDROM", "AllowBootingWithoutSBIFile", false))
|
2021-04-22 03:39:38 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
return Host::ConfirmMessage(
|
|
|
|
"Confirm Unsupported Configuration",
|
2021-04-22 03:39:38 +00:00
|
|
|
StringUtil::StdStringFromFormat(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE(
|
2021-04-22 03:39:38 +00:00
|
|
|
"System",
|
|
|
|
"You are attempting to run a libcrypt protected game without an SBI file:\n\n%s: %s\n\nThe game will "
|
|
|
|
"likely not run properly.\n\nPlease check the README for instructions on how to add an SBI file.\n\nDo "
|
|
|
|
"you wish to continue?"),
|
2022-10-05 08:29:08 +00:00
|
|
|
s_running_game_serial.c_str(), s_running_game_title.c_str())
|
2021-04-22 03:39:38 +00:00
|
|
|
.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::ReportErrorAsync(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE("System", "Error"),
|
2022-07-11 13:03:29 +00:00
|
|
|
SmallString::FromFormat(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE("System",
|
|
|
|
"You are attempting to run a libcrypt protected game without an SBI file:\n\n%s: %s\n\nYour dump is "
|
|
|
|
"incomplete, you must add the SBI file to run this game. \n\n"
|
|
|
|
"The name of the SBI file must match the name of the disc image."),
|
2022-10-05 08:29:08 +00:00
|
|
|
s_running_game_serial.c_str(), s_running_game_title.c_str()));
|
2021-04-22 03:39:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::HasMediaSubImages()
|
2020-08-15 10:38:54 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
const CDImage* cdi = CDROM::GetMedia();
|
2021-03-26 16:19:23 +00:00
|
|
|
return cdi ? cdi->HasSubImages() : false;
|
2020-08-15 10:38:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
u32 System::GetMediaSubImageCount()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
const CDImage* cdi = CDROM::GetMedia();
|
2021-03-26 16:19:23 +00:00
|
|
|
return cdi ? cdi->GetSubImageCount() : 0;
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
u32 System::GetMediaSubImageIndex()
|
2020-01-24 04:51:05 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
const CDImage* cdi = CDROM::GetMedia();
|
2021-03-26 16:19:23 +00:00
|
|
|
return cdi ? cdi->GetCurrentSubImage() : 0;
|
2020-01-24 04:51:05 +00:00
|
|
|
}
|
2020-07-22 16:36:05 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
u32 System::GetMediaSubImageIndexForTitle(const std::string_view& title)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
const CDImage* cdi = CDROM::GetMedia();
|
2021-03-26 16:19:23 +00:00
|
|
|
if (!cdi)
|
|
|
|
return 0;
|
2021-02-26 14:44:53 +00:00
|
|
|
|
2021-03-26 16:19:23 +00:00
|
|
|
const u32 count = cdi->GetSubImageCount();
|
|
|
|
for (u32 i = 0; i < count; i++)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2021-03-26 16:19:23 +00:00
|
|
|
if (title == cdi->GetSubImageMetadata(i, "title"))
|
2020-07-22 16:36:05 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::numeric_limits<u32>::max();
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
std::string System::GetMediaSubImageTitle(u32 index)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
const CDImage* cdi = CDROM::GetMedia();
|
2021-03-26 16:19:23 +00:00
|
|
|
if (!cdi)
|
|
|
|
return {};
|
2020-07-22 16:36:05 +00:00
|
|
|
|
2021-03-26 16:19:23 +00:00
|
|
|
return cdi->GetSubImageMetadata(index, "title");
|
2020-07-22 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::SwitchMediaSubImage(u32 index)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2023-01-09 09:34:40 +00:00
|
|
|
if (!CDROM::HasMedia())
|
2020-07-22 16:36:05 +00:00
|
|
|
return false;
|
|
|
|
|
2023-01-09 09:34:40 +00:00
|
|
|
std::unique_ptr<CDImage> image = CDROM::RemoveMedia(true);
|
2021-03-26 16:19:23 +00:00
|
|
|
Assert(image);
|
2020-07-22 16:36:05 +00:00
|
|
|
|
2023-08-19 13:40:36 +00:00
|
|
|
Error error;
|
2021-03-26 16:19:23 +00:00
|
|
|
if (!image->SwitchSubImage(index, &error))
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(10.0f, TRANSLATE("OSDMessage", "Failed to switch to subimage %u in '%s': %s."),
|
2023-08-19 13:40:36 +00:00
|
|
|
index + 1u, image->GetFileName().c_str(), error.GetDescription().c_str());
|
2023-05-15 13:38:37 +00:00
|
|
|
|
|
|
|
const DiscRegion region = GetRegionForImage(image.get());
|
|
|
|
CDROM::InsertMedia(std::move(image), region);
|
2021-03-26 16:19:23 +00:00
|
|
|
return false;
|
2020-07-22 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(20.0f, TRANSLATE("OSDMessage", "Switched to sub-image %s (%u) in '%s'."),
|
2022-07-11 13:03:29 +00:00
|
|
|
image->GetSubImageMetadata(index, "title").c_str(), index + 1u,
|
|
|
|
image->GetMetadata("title").c_str());
|
2023-05-15 13:38:37 +00:00
|
|
|
const DiscRegion region = GetRegionForImage(image.get());
|
|
|
|
CDROM::InsertMedia(std::move(image), region);
|
2020-07-22 16:36:05 +00:00
|
|
|
|
2021-03-26 16:19:23 +00:00
|
|
|
ClearMemorySaveStates();
|
|
|
|
return true;
|
2020-07-22 16:36:05 +00:00
|
|
|
}
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::HasCheatList()
|
2020-09-09 12:11:28 +00:00
|
|
|
{
|
|
|
|
return static_cast<bool>(s_cheat_list);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
CheatList* System::GetCheatList()
|
2020-09-09 12:11:28 +00:00
|
|
|
{
|
|
|
|
return s_cheat_list.get();
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::ApplyCheatCode(const CheatCode& code)
|
2020-09-09 12:11:28 +00:00
|
|
|
{
|
|
|
|
Assert(!IsShutdown());
|
|
|
|
code.Apply();
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::SetCheatList(std::unique_ptr<CheatList> cheats)
|
2020-09-09 12:11:28 +00:00
|
|
|
{
|
|
|
|
Assert(!IsShutdown());
|
|
|
|
s_cheat_list = std::move(cheats);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::CheckForSettingsChanges(const Settings& old_settings)
|
|
|
|
{
|
|
|
|
if (IsValid() && (g_settings.gpu_renderer != old_settings.gpu_renderer ||
|
|
|
|
g_settings.gpu_use_debug_device != old_settings.gpu_use_debug_device ||
|
|
|
|
g_settings.gpu_threaded_presentation != old_settings.gpu_threaded_presentation))
|
|
|
|
{
|
|
|
|
// if debug device/threaded presentation change, we need to recreate the whole display
|
2023-08-27 12:48:40 +00:00
|
|
|
const bool recreate_device = (g_settings.gpu_use_debug_device != old_settings.gpu_use_debug_device ||
|
2022-07-11 13:03:29 +00:00
|
|
|
g_settings.gpu_threaded_presentation != old_settings.gpu_threaded_presentation);
|
|
|
|
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Switching to %s%s GPU renderer."),
|
2022-07-11 13:03:29 +00:00
|
|
|
Settings::GetRendererName(g_settings.gpu_renderer),
|
|
|
|
g_settings.gpu_use_debug_device ? " (debug)" : "");
|
2023-08-27 12:48:40 +00:00
|
|
|
RecreateGPU(g_settings.gpu_renderer, recreate_device);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsValid())
|
|
|
|
{
|
|
|
|
ClearMemorySaveStates();
|
|
|
|
|
|
|
|
if (g_settings.cpu_overclock_active != old_settings.cpu_overclock_active ||
|
|
|
|
(g_settings.cpu_overclock_active &&
|
|
|
|
(g_settings.cpu_overclock_numerator != old_settings.cpu_overclock_numerator ||
|
|
|
|
g_settings.cpu_overclock_denominator != old_settings.cpu_overclock_denominator)))
|
|
|
|
{
|
|
|
|
UpdateOverclock();
|
|
|
|
}
|
|
|
|
|
2022-12-14 07:58:14 +00:00
|
|
|
if (g_settings.audio_backend != old_settings.audio_backend ||
|
|
|
|
g_settings.audio_driver != old_settings.audio_driver ||
|
|
|
|
g_settings.audio_output_device != old_settings.audio_output_device)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
if (g_settings.audio_backend != old_settings.audio_backend)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Switching to %s audio backend."),
|
2022-07-11 13:03:29 +00:00
|
|
|
Settings::GetAudioBackendName(g_settings.audio_backend));
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::RecreateOutputStream();
|
2022-07-27 14:42:41 +00:00
|
|
|
}
|
|
|
|
if (g_settings.audio_stretch_mode != old_settings.audio_stretch_mode)
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::GetOutputStream()->SetStretchMode(g_settings.audio_stretch_mode);
|
2022-07-27 14:42:41 +00:00
|
|
|
if (g_settings.audio_buffer_ms != old_settings.audio_buffer_ms ||
|
|
|
|
g_settings.audio_output_latency_ms != old_settings.audio_output_latency_ms ||
|
|
|
|
g_settings.audio_stretch_mode != old_settings.audio_stretch_mode)
|
|
|
|
{
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::RecreateOutputStream();
|
2022-07-27 14:42:41 +00:00
|
|
|
UpdateSpeedLimiterState();
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.emulation_speed != old_settings.emulation_speed)
|
|
|
|
UpdateThrottlePeriod();
|
|
|
|
|
|
|
|
if (g_settings.cpu_execution_mode != old_settings.cpu_execution_mode ||
|
|
|
|
g_settings.cpu_fastmem_mode != old_settings.cpu_fastmem_mode)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(fmt::format(TRANSLATE_FS("OSDMessage", "Switching to {} CPU execution mode."),
|
|
|
|
TRANSLATE_SV("CPUExecutionMode", Settings::GetCPUExecutionModeDisplayName(
|
|
|
|
g_settings.cpu_execution_mode))),
|
|
|
|
5.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
CPU::CodeCache::Reinitialize();
|
|
|
|
CPU::ClearICache();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.cpu_execution_mode == CPUExecutionMode::Recompiler &&
|
|
|
|
(g_settings.cpu_recompiler_memory_exceptions != old_settings.cpu_recompiler_memory_exceptions ||
|
|
|
|
g_settings.cpu_recompiler_block_linking != old_settings.cpu_recompiler_block_linking ||
|
2023-08-29 11:18:04 +00:00
|
|
|
g_settings.cpu_recompiler_icache != old_settings.cpu_recompiler_icache ||
|
|
|
|
g_settings.bios_tty_logging != old_settings.bios_tty_logging))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(TRANSLATE_STR("OSDMessage", "Recompiler options changed, flushing all blocks."), 5.0f);
|
2022-09-17 04:26:49 +00:00
|
|
|
|
|
|
|
// changing memory exceptions can re-enable fastmem
|
|
|
|
if (g_settings.cpu_recompiler_memory_exceptions != old_settings.cpu_recompiler_memory_exceptions)
|
|
|
|
CPU::CodeCache::Reinitialize();
|
|
|
|
else
|
|
|
|
CPU::CodeCache::Flush();
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (g_settings.cpu_recompiler_icache != old_settings.cpu_recompiler_icache)
|
|
|
|
CPU::ClearICache();
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::GetOutputStream()->SetOutputVolume(GetAudioOutputVolume());
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (g_settings.gpu_resolution_scale != old_settings.gpu_resolution_scale ||
|
|
|
|
g_settings.gpu_multisamples != old_settings.gpu_multisamples ||
|
|
|
|
g_settings.gpu_per_sample_shading != old_settings.gpu_per_sample_shading ||
|
|
|
|
g_settings.gpu_use_thread != old_settings.gpu_use_thread ||
|
|
|
|
g_settings.gpu_use_software_renderer_for_readbacks != old_settings.gpu_use_software_renderer_for_readbacks ||
|
|
|
|
g_settings.gpu_fifo_size != old_settings.gpu_fifo_size ||
|
|
|
|
g_settings.gpu_max_run_ahead != old_settings.gpu_max_run_ahead ||
|
|
|
|
g_settings.gpu_true_color != old_settings.gpu_true_color ||
|
|
|
|
g_settings.gpu_scaled_dithering != old_settings.gpu_scaled_dithering ||
|
|
|
|
g_settings.gpu_texture_filter != old_settings.gpu_texture_filter ||
|
|
|
|
g_settings.gpu_disable_interlacing != old_settings.gpu_disable_interlacing ||
|
|
|
|
g_settings.gpu_force_ntsc_timings != old_settings.gpu_force_ntsc_timings ||
|
|
|
|
g_settings.gpu_24bit_chroma_smoothing != old_settings.gpu_24bit_chroma_smoothing ||
|
|
|
|
g_settings.gpu_downsample_mode != old_settings.gpu_downsample_mode ||
|
|
|
|
g_settings.display_crop_mode != old_settings.display_crop_mode ||
|
|
|
|
g_settings.display_aspect_ratio != old_settings.display_aspect_ratio ||
|
2022-10-09 04:55:56 +00:00
|
|
|
g_settings.display_alignment != old_settings.display_alignment ||
|
2022-12-20 11:32:51 +00:00
|
|
|
g_settings.display_show_gpu != old_settings.display_show_gpu ||
|
2022-07-11 13:03:29 +00:00
|
|
|
g_settings.gpu_pgxp_enable != old_settings.gpu_pgxp_enable ||
|
2022-10-03 10:03:30 +00:00
|
|
|
g_settings.gpu_pgxp_texture_correction != old_settings.gpu_pgxp_texture_correction ||
|
|
|
|
g_settings.gpu_pgxp_color_correction != old_settings.gpu_pgxp_color_correction ||
|
2022-07-11 13:03:29 +00:00
|
|
|
g_settings.gpu_pgxp_depth_buffer != old_settings.gpu_pgxp_depth_buffer ||
|
|
|
|
g_settings.display_active_start_offset != old_settings.display_active_start_offset ||
|
|
|
|
g_settings.display_active_end_offset != old_settings.display_active_end_offset ||
|
|
|
|
g_settings.display_line_start_offset != old_settings.display_line_start_offset ||
|
|
|
|
g_settings.display_line_end_offset != old_settings.display_line_end_offset ||
|
|
|
|
g_settings.rewind_enable != old_settings.rewind_enable ||
|
2022-09-03 04:15:15 +00:00
|
|
|
g_settings.runahead_frames != old_settings.runahead_frames)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
g_gpu->UpdateSettings();
|
|
|
|
Host::InvalidateDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.gpu_widescreen_hack != old_settings.gpu_widescreen_hack ||
|
|
|
|
g_settings.display_aspect_ratio != old_settings.display_aspect_ratio ||
|
|
|
|
(g_settings.display_aspect_ratio == DisplayAspectRatio::Custom &&
|
|
|
|
(g_settings.display_aspect_ratio_custom_numerator != old_settings.display_aspect_ratio_custom_numerator ||
|
|
|
|
g_settings.display_aspect_ratio_custom_denominator != old_settings.display_aspect_ratio_custom_denominator)))
|
|
|
|
{
|
|
|
|
GTE::UpdateAspectRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.gpu_pgxp_enable != old_settings.gpu_pgxp_enable ||
|
|
|
|
(g_settings.gpu_pgxp_enable && (g_settings.gpu_pgxp_culling != old_settings.gpu_pgxp_culling ||
|
|
|
|
g_settings.gpu_pgxp_vertex_cache != old_settings.gpu_pgxp_vertex_cache ||
|
|
|
|
g_settings.gpu_pgxp_cpu != old_settings.gpu_pgxp_cpu)))
|
|
|
|
{
|
|
|
|
if (g_settings.IsUsingCodeCache())
|
|
|
|
{
|
|
|
|
Host::AddOSDMessage(g_settings.gpu_pgxp_enable ?
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE_STR("OSDMessage", "PGXP enabled, recompiling all blocks.") :
|
|
|
|
TRANSLATE_STR("OSDMessage", "PGXP disabled, recompiling all blocks."),
|
2022-07-11 13:03:29 +00:00
|
|
|
5.0f);
|
|
|
|
CPU::CodeCache::Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_settings.gpu_pgxp_enable)
|
|
|
|
PGXP::Shutdown();
|
|
|
|
|
|
|
|
if (g_settings.gpu_pgxp_enable)
|
|
|
|
PGXP::Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.cdrom_readahead_sectors != old_settings.cdrom_readahead_sectors)
|
2023-01-09 09:34:40 +00:00
|
|
|
CDROM::SetReadaheadSectors(g_settings.cdrom_readahead_sectors);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (g_settings.memory_card_types != old_settings.memory_card_types ||
|
2022-12-20 11:32:51 +00:00
|
|
|
g_settings.memory_card_paths != old_settings.memory_card_paths ||
|
2023-08-23 08:40:59 +00:00
|
|
|
(g_settings.memory_card_use_playlist_title != old_settings.memory_card_use_playlist_title))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
UpdateMemoryCardTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.rewind_enable != old_settings.rewind_enable ||
|
|
|
|
g_settings.rewind_save_frequency != old_settings.rewind_save_frequency ||
|
|
|
|
g_settings.rewind_save_slots != old_settings.rewind_save_slots ||
|
|
|
|
g_settings.runahead_frames != old_settings.runahead_frames)
|
|
|
|
{
|
|
|
|
UpdateMemorySaveStateSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.texture_replacements.enable_vram_write_replacements !=
|
|
|
|
old_settings.texture_replacements.enable_vram_write_replacements ||
|
|
|
|
g_settings.texture_replacements.preload_textures != old_settings.texture_replacements.preload_textures)
|
|
|
|
{
|
|
|
|
g_texture_replacements.Reload();
|
|
|
|
}
|
|
|
|
|
2023-01-11 08:51:38 +00:00
|
|
|
DMA::SetMaxSliceTicks(g_settings.dma_max_slice_ticks);
|
|
|
|
DMA::SetHaltTicks(g_settings.dma_halt_ticks);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (g_settings.audio_backend != old_settings.audio_backend ||
|
|
|
|
g_settings.video_sync_enabled != old_settings.video_sync_enabled ||
|
|
|
|
g_settings.increase_timer_resolution != old_settings.increase_timer_resolution ||
|
|
|
|
g_settings.emulation_speed != old_settings.emulation_speed ||
|
|
|
|
g_settings.fast_forward_speed != old_settings.fast_forward_speed ||
|
|
|
|
g_settings.display_max_fps != old_settings.display_max_fps ||
|
|
|
|
g_settings.display_all_frames != old_settings.display_all_frames ||
|
|
|
|
g_settings.sync_to_host_refresh_rate != old_settings.sync_to_host_refresh_rate)
|
|
|
|
{
|
|
|
|
UpdateSpeedLimiterState();
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
if (g_settings.inhibit_screensaver != old_settings.inhibit_screensaver)
|
|
|
|
{
|
|
|
|
if (g_settings.inhibit_screensaver)
|
|
|
|
PlatformMisc::SuspendScreensaver();
|
|
|
|
else
|
|
|
|
PlatformMisc::ResumeScreensaver();
|
|
|
|
}
|
2023-08-27 12:48:40 +00:00
|
|
|
|
|
|
|
PostProcessing::UpdateSettings();
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 06:00:06 +00:00
|
|
|
if (g_gpu_device && g_settings.display_osd_scale != old_settings.display_osd_scale)
|
|
|
|
ImGuiManager::SetGlobalScale(g_settings.display_osd_scale / 100.0f);
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool controllers_updated = false;
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
|
|
|
if (g_settings.controller_types[i] != old_settings.controller_types[i])
|
|
|
|
{
|
|
|
|
if (IsValid() && !controllers_updated)
|
|
|
|
{
|
|
|
|
UpdateControllers();
|
|
|
|
ResetControllers();
|
|
|
|
UpdateSoftwareCursor();
|
|
|
|
controllers_updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsValid() && !controllers_updated)
|
|
|
|
{
|
|
|
|
UpdateControllerSettings();
|
|
|
|
UpdateSoftwareCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.multitap_mode != old_settings.multitap_mode)
|
|
|
|
UpdateMultitaps();
|
2023-08-23 12:06:48 +00:00
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
Achievements::UpdateSettings(old_settings);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FullscreenUI::CheckForConfigChanges(old_settings);
|
|
|
|
|
|
|
|
if (g_settings.enable_discord_presence != old_settings.enable_discord_presence)
|
|
|
|
{
|
|
|
|
if (g_settings.enable_discord_presence)
|
|
|
|
InitializeDiscordPresence();
|
|
|
|
else
|
|
|
|
ShutdownDiscordPresence();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.log_level != old_settings.log_level || g_settings.log_filter != old_settings.log_filter ||
|
|
|
|
g_settings.log_to_console != old_settings.log_to_console ||
|
|
|
|
g_settings.log_to_debug != old_settings.log_to_debug || g_settings.log_to_window != old_settings.log_to_window ||
|
|
|
|
g_settings.log_to_file != old_settings.log_to_file)
|
|
|
|
{
|
|
|
|
g_settings.UpdateLogSettings();
|
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void System::CalculateRewindMemoryUsage(u32 num_saves, u64* ram_usage, u64* vram_usage)
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
|
|
|
*ram_usage = MAX_SAVE_STATE_SIZE * static_cast<u64>(num_saves);
|
|
|
|
*vram_usage = (VRAM_WIDTH * VRAM_HEIGHT * 4) * static_cast<u64>(std::max(g_settings.gpu_resolution_scale, 1u)) *
|
2021-01-25 08:48:55 +00:00
|
|
|
static_cast<u64>(g_settings.gpu_multisamples) * static_cast<u64>(num_saves);
|
2021-01-23 09:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::ClearMemorySaveStates()
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
|
|
|
s_rewind_states.clear();
|
2021-01-23 16:52:52 +00:00
|
|
|
s_runahead_states.clear();
|
2021-01-23 09:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::UpdateMemorySaveStateSettings()
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
|
|
|
ClearMemorySaveStates();
|
|
|
|
|
|
|
|
s_memory_saves_enabled = g_settings.rewind_enable;
|
|
|
|
|
|
|
|
if (g_settings.rewind_enable)
|
|
|
|
{
|
|
|
|
s_rewind_save_frequency = static_cast<s32>(std::ceil(g_settings.rewind_save_frequency * s_throttle_frequency));
|
|
|
|
s_rewind_save_counter = 0;
|
|
|
|
|
|
|
|
u64 ram_usage, vram_usage;
|
|
|
|
CalculateRewindMemoryUsage(g_settings.rewind_save_slots, &ram_usage, &vram_usage);
|
|
|
|
Log_InfoPrintf(
|
|
|
|
"Rewind is enabled, saving every %d frames, with %u slots and %" PRIu64 "MB RAM and %" PRIu64 "MB VRAM usage",
|
|
|
|
std::max(s_rewind_save_frequency, 1), g_settings.rewind_save_slots, ram_usage / 1048576, vram_usage / 1048576);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_rewind_save_frequency = -1;
|
|
|
|
s_rewind_save_counter = -1;
|
|
|
|
}
|
2021-01-23 16:52:52 +00:00
|
|
|
|
|
|
|
s_rewind_load_frequency = -1;
|
|
|
|
s_rewind_load_counter = -1;
|
|
|
|
|
2021-01-25 16:48:40 +00:00
|
|
|
s_runahead_frames = g_settings.runahead_frames;
|
2021-01-23 16:52:52 +00:00
|
|
|
s_runahead_replay_pending = false;
|
|
|
|
if (s_runahead_frames > 0)
|
|
|
|
Log_InfoPrintf("Runahead is active with %u frames", s_runahead_frames);
|
2021-01-23 09:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::LoadMemoryState(const MemorySaveState& mss)
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
2021-01-23 16:52:52 +00:00
|
|
|
mss.state_stream->SeekAbsolute(0);
|
2021-01-23 09:00:54 +00:00
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
StateWrapper sw(mss.state_stream.get(), StateWrapper::Mode::Read, SAVE_STATE_VERSION);
|
2022-10-03 06:44:34 +00:00
|
|
|
GPUTexture* host_texture = mss.vram_texture.get();
|
2021-12-02 14:03:18 +00:00
|
|
|
if (!DoState(sw, &host_texture, true, true))
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Host::ReportErrorAsync("Error", "Failed to load memory save state, resetting.");
|
|
|
|
InternalReset();
|
2021-01-23 16:52:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-23 09:00:54 +00:00
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::SaveMemoryState(MemorySaveState* mss)
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
2021-01-24 11:12:28 +00:00
|
|
|
if (!mss->state_stream)
|
|
|
|
mss->state_stream = std::make_unique<GrowableMemoryByteStream>(nullptr, MAX_SAVE_STATE_SIZE);
|
|
|
|
else
|
|
|
|
mss->state_stream->SeekAbsolute(0);
|
2021-01-23 09:00:54 +00:00
|
|
|
|
2022-10-03 06:44:34 +00:00
|
|
|
GPUTexture* host_texture = mss->vram_texture.release();
|
2021-01-23 16:52:52 +00:00
|
|
|
StateWrapper sw(mss->state_stream.get(), StateWrapper::Mode::Write, SAVE_STATE_VERSION);
|
2021-12-02 14:03:18 +00:00
|
|
|
if (!DoState(sw, &host_texture, false, true))
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrint("Failed to create rewind state.");
|
2021-01-24 11:12:28 +00:00
|
|
|
delete host_texture;
|
2021-01-23 09:00:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
mss->vram_texture.reset(host_texture);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::SaveRewindState()
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2021-01-23 16:52:52 +00:00
|
|
|
Common::Timer save_timer;
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2021-01-24 11:12:28 +00:00
|
|
|
// try to reuse the frontmost slot
|
2021-01-23 16:52:52 +00:00
|
|
|
const u32 save_slots = g_settings.rewind_save_slots;
|
2021-01-24 11:12:28 +00:00
|
|
|
MemorySaveState mss;
|
2021-01-23 16:52:52 +00:00
|
|
|
while (s_rewind_states.size() >= save_slots)
|
2021-01-24 11:12:28 +00:00
|
|
|
{
|
|
|
|
mss = std::move(s_rewind_states.front());
|
2021-01-23 16:52:52 +00:00
|
|
|
s_rewind_states.pop_front();
|
2021-01-24 11:12:28 +00:00
|
|
|
}
|
2021-01-23 16:52:52 +00:00
|
|
|
|
|
|
|
if (!SaveMemoryState(&mss))
|
|
|
|
return false;
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
s_rewind_states.push_back(std::move(mss));
|
|
|
|
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2021-02-06 09:19:55 +00:00
|
|
|
Log_DevPrintf("Saved rewind state (%" PRIu64 " bytes, took %.4f ms)", s_rewind_states.back().state_stream->GetSize(),
|
2021-01-23 09:00:54 +00:00
|
|
|
save_timer.GetTimeMilliseconds());
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
2021-01-23 09:00:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::LoadRewindState(u32 skip_saves /*= 0*/, bool consume_state /*=true */)
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
|
|
|
while (skip_saves > 0 && !s_rewind_states.empty())
|
|
|
|
{
|
|
|
|
s_rewind_states.pop_back();
|
|
|
|
skip_saves--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_rewind_states.empty())
|
|
|
|
return false;
|
|
|
|
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2021-01-23 09:00:54 +00:00
|
|
|
Common::Timer load_timer;
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
2021-01-23 09:00:54 +00:00
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
if (!LoadMemoryState(s_rewind_states.back()))
|
2021-01-23 09:00:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (consume_state)
|
|
|
|
s_rewind_states.pop_back();
|
|
|
|
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2021-01-23 09:00:54 +00:00
|
|
|
Log_DevPrintf("Rewind load took %.4f ms", load_timer.GetTimeMilliseconds());
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
bool System::IsRewinding()
|
2021-08-07 02:07:04 +00:00
|
|
|
{
|
|
|
|
return (s_rewind_load_frequency >= 0);
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::SetRewinding(bool enabled)
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
2023-08-15 13:12:21 +00:00
|
|
|
const bool was_enabled = IsRewinding();
|
|
|
|
|
2021-01-23 09:00:54 +00:00
|
|
|
// Try to rewind at the replay speed, or one per second maximum.
|
|
|
|
const float load_frequency = std::min(g_settings.rewind_save_frequency, 1.0f);
|
|
|
|
s_rewind_load_frequency = static_cast<s32>(std::ceil(load_frequency * s_throttle_frequency));
|
|
|
|
s_rewind_load_counter = 0;
|
2023-08-15 13:12:21 +00:00
|
|
|
|
|
|
|
if (!was_enabled && s_system_executing)
|
|
|
|
s_system_interrupted = true;
|
2021-01-23 09:00:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_rewind_load_frequency = -1;
|
|
|
|
s_rewind_load_counter = -1;
|
2023-08-15 13:12:21 +00:00
|
|
|
s_rewinding_first_save = true;
|
2021-01-23 09:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::DoRewind()
|
2021-01-23 09:00:54 +00:00
|
|
|
{
|
|
|
|
if (s_rewind_load_counter == 0)
|
|
|
|
{
|
|
|
|
const u32 skip_saves = BoolToUInt32(!s_rewinding_first_save);
|
|
|
|
s_rewinding_first_save = false;
|
|
|
|
LoadRewindState(skip_saves, false);
|
|
|
|
ResetPerformanceCounters();
|
|
|
|
s_rewind_load_counter = s_rewind_load_frequency;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_rewind_load_counter--;
|
|
|
|
}
|
2021-07-17 10:09:29 +00:00
|
|
|
|
|
|
|
s_next_frame_time += s_frame_period;
|
2023-08-15 13:12:21 +00:00
|
|
|
|
|
|
|
Host::RenderDisplay(false);
|
|
|
|
Host::PumpMessagesOnCPUThread();
|
|
|
|
|
|
|
|
Throttle();
|
2021-01-23 09:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::SaveRunaheadState()
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
2021-01-24 11:12:28 +00:00
|
|
|
// try to reuse the frontmost slot
|
|
|
|
MemorySaveState mss;
|
|
|
|
while (s_runahead_states.size() >= s_runahead_frames)
|
|
|
|
{
|
|
|
|
mss = std::move(s_runahead_states.front());
|
2021-01-23 16:52:52 +00:00
|
|
|
s_runahead_states.pop_front();
|
2021-01-24 11:12:28 +00:00
|
|
|
}
|
2021-01-23 16:52:52 +00:00
|
|
|
|
|
|
|
if (!SaveMemoryState(&mss))
|
|
|
|
{
|
|
|
|
Log_ErrorPrint("Failed to save runahead state.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_runahead_states.push_back(std::move(mss));
|
|
|
|
}
|
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
bool System::DoRunahead()
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2023-08-15 13:12:21 +00:00
|
|
|
static Common::Timer replay_timer;
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
2021-01-23 16:52:52 +00:00
|
|
|
|
|
|
|
if (s_runahead_replay_pending)
|
|
|
|
{
|
2023-08-15 13:12:21 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
|
|
|
Log_DevPrintf("runahead starting at frame %u", s_frame_number);
|
|
|
|
replay_timer.Reset();
|
|
|
|
#endif
|
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
// we need to replay and catch up - load the state,
|
|
|
|
s_runahead_replay_pending = false;
|
2021-05-14 06:43:11 +00:00
|
|
|
if (s_runahead_states.empty() || !LoadMemoryState(s_runahead_states.front()))
|
|
|
|
{
|
|
|
|
s_runahead_states.clear();
|
2023-08-15 13:12:21 +00:00
|
|
|
return false;
|
2021-05-14 06:43:11 +00:00
|
|
|
}
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
// figure out how many frames we need to run to catch up
|
|
|
|
s_runahead_replay_frames = static_cast<u32>(s_runahead_states.size());
|
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
// and throw away all the states, forcing us to catch up below
|
|
|
|
s_runahead_states.clear();
|
2021-08-08 02:58:02 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
// run the frames with no audio
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::SetAudioOutputMuted(true);
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2023-08-15 13:12:21 +00:00
|
|
|
Log_VerbosePrintf("Rewound to frame %u, took %.2f ms", s_frame_number, replay_timer.GetTimeMilliseconds());
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
2023-08-15 13:12:21 +00:00
|
|
|
|
|
|
|
// we don't want to save the frame we just loaded. but we are "one frame ahead", because the frame we just tossed
|
|
|
|
// was never saved, so return but don't decrement the counter
|
|
|
|
return true;
|
2021-01-23 16:52:52 +00:00
|
|
|
}
|
2023-08-15 13:12:21 +00:00
|
|
|
else if (s_runahead_replay_frames == 0)
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
2023-08-15 13:12:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_runahead_replay_frames--;
|
|
|
|
if (s_runahead_replay_frames > 0)
|
|
|
|
{
|
|
|
|
// keep running ahead
|
2021-01-23 16:52:52 +00:00
|
|
|
SaveRunaheadState();
|
2023-08-15 13:12:21 +00:00
|
|
|
return true;
|
2021-01-23 16:52:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2023-08-15 13:12:21 +00:00
|
|
|
Log_VerbosePrintf("Running %d frames to catch up took %.2f ms", s_runahead_frames,
|
|
|
|
replay_timer.GetTimeMilliseconds());
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
// we're all caught up. this frame gets saved in DoMemoryStates().
|
|
|
|
SPU::SetAudioOutputMuted(false);
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
|
|
|
Log_DevPrintf("runahead ending at frame %u, took %.2f ms", s_frame_number, replay_timer.GetTimeMilliseconds());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
2021-01-23 16:52:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::SetRunaheadReplayFlag()
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
2021-05-14 06:43:11 +00:00
|
|
|
if (s_runahead_frames == 0 || s_runahead_states.empty())
|
2021-01-28 15:27:27 +00:00
|
|
|
return;
|
|
|
|
|
2021-08-08 02:58:02 +00:00
|
|
|
#ifdef PROFILE_MEMORY_SAVE_STATES
|
2021-01-23 16:52:52 +00:00
|
|
|
Log_DevPrintf("Runahead rewind pending...");
|
2021-08-08 02:58:02 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-23 16:52:52 +00:00
|
|
|
s_runahead_replay_pending = true;
|
2021-01-23 09:00:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void System::ShutdownSystem(bool save_resume_state)
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
2022-08-05 07:07:07 +00:00
|
|
|
if (save_resume_state)
|
|
|
|
SaveResumeState();
|
2022-07-11 13:03:29 +00:00
|
|
|
|
2023-08-15 13:12:21 +00:00
|
|
|
if (s_system_executing)
|
|
|
|
s_state = State::Stopping;
|
|
|
|
else
|
|
|
|
DestroySystem();
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool System::CanUndoLoadState()
|
|
|
|
{
|
|
|
|
return static_cast<bool>(m_undo_load_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ExtendedSaveStateInfo> System::GetUndoSaveStateInfo()
|
|
|
|
{
|
|
|
|
std::optional<ExtendedSaveStateInfo> ssi;
|
|
|
|
if (m_undo_load_state)
|
|
|
|
{
|
|
|
|
m_undo_load_state->SeekAbsolute(0);
|
|
|
|
ssi = InternalGetExtendedSaveStateInfo(m_undo_load_state.get());
|
|
|
|
m_undo_load_state->SeekAbsolute(0);
|
|
|
|
|
|
|
|
if (ssi)
|
|
|
|
ssi->timestamp = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssi;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::UndoLoadState()
|
|
|
|
{
|
|
|
|
if (!m_undo_load_state)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Assert(IsValid());
|
|
|
|
|
|
|
|
m_undo_load_state->SeekAbsolute(0);
|
|
|
|
if (!DoLoadState(m_undo_load_state.get(), false, true))
|
|
|
|
{
|
|
|
|
Host::ReportErrorAsync("Error", "Failed to load undo state, resetting system.");
|
|
|
|
m_undo_load_state.reset();
|
|
|
|
ResetSystem();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_InfoPrintf("Loaded undo save state.");
|
|
|
|
m_undo_load_state.reset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::SaveUndoLoadState()
|
|
|
|
{
|
|
|
|
if (m_undo_load_state)
|
|
|
|
m_undo_load_state.reset();
|
|
|
|
|
|
|
|
m_undo_load_state = ByteStream::CreateGrowableMemoryStream(nullptr, System::MAX_SAVE_STATE_SIZE);
|
|
|
|
if (!InternalSaveState(m_undo_load_state.get()))
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(TRANSLATE_STR("OSDMessage", "Failed to save undo load state."), 15.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
m_undo_load_state.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_InfoPrintf("Saved undo load state: %" PRIu64 " bytes", m_undo_load_state->GetSize());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::IsRunningAtNonStandardSpeed()
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const float target_speed = System::GetTargetSpeed();
|
|
|
|
return (target_speed <= 0.95f || target_speed >= 1.05f);
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 System::GetAudioOutputVolume()
|
|
|
|
{
|
|
|
|
return g_settings.GetAudioOutputVolume(IsRunningAtNonStandardSpeed());
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::UpdateVolume()
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
2022-08-01 13:39:41 +00:00
|
|
|
SPU::GetOutputStream()->SetOutputVolume(GetAudioOutputVolume());
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool System::IsDumpingAudio()
|
|
|
|
{
|
2022-08-01 13:39:41 +00:00
|
|
|
return SPU::IsDumpingAudio();
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool System::StartDumpingAudio(const char* filename)
|
|
|
|
{
|
|
|
|
if (System::IsShutdown())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string auto_filename;
|
|
|
|
if (!filename)
|
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
const auto& serial = System::GetGameSerial();
|
2022-10-05 08:29:08 +00:00
|
|
|
if (serial.empty())
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
auto_filename = Path::Combine(
|
|
|
|
EmuFolders::Dumps, fmt::format("audio" FS_OSPATH_SEPARATOR_STR "{}.wav", GetTimestampStringForFileName()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
auto_filename = Path::Combine(EmuFolders::Dumps, fmt::format("audio" FS_OSPATH_SEPARATOR_STR "{}_{}.wav", serial,
|
2022-07-11 13:03:29 +00:00
|
|
|
GetTimestampStringForFileName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = auto_filename.c_str();
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:39:41 +00:00
|
|
|
if (SPU::StartDumpingAudio(filename))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Started dumping audio to '%s'."), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(10.0f, TRANSLATE("OSDMessage", "Failed to start dumping audio to '%s'."), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::StopDumpingAudio()
|
|
|
|
{
|
2022-08-01 13:39:41 +00:00
|
|
|
if (System::IsShutdown() || !SPU::StopDumpingAudio())
|
2022-07-11 13:03:29 +00:00
|
|
|
return;
|
|
|
|
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(TRANSLATE_STR("OSDMessage", "Stopped dumping audio."), 5.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool System::SaveScreenshot(const char* filename /* = nullptr */, bool full_resolution /* = true */,
|
|
|
|
bool apply_aspect_ratio /* = true */, bool compress_on_thread /* = true */)
|
|
|
|
{
|
|
|
|
if (!System::IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string auto_filename;
|
|
|
|
if (!filename)
|
|
|
|
{
|
2023-05-15 13:38:37 +00:00
|
|
|
const auto& code = System::GetGameSerial();
|
2022-07-11 13:03:29 +00:00
|
|
|
const char* extension = "png";
|
|
|
|
if (code.empty())
|
|
|
|
{
|
|
|
|
auto_filename =
|
|
|
|
Path::Combine(EmuFolders::Screenshots, fmt::format("{}.{}", GetTimestampStringForFileName(), extension));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto_filename = Path::Combine(EmuFolders::Screenshots,
|
|
|
|
fmt::format("{}_{}.{}", code, GetTimestampStringForFileName(), extension));
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = auto_filename.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileSystem::FileExists(filename))
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(10.0f, TRANSLATE("OSDMessage", "Screenshot file '%s' already exists."), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-27 08:13:50 +00:00
|
|
|
const bool screenshot_saved =
|
|
|
|
g_gpu->RenderScreenshotToFile(filename, g_settings.display_internal_resolution_screenshots, compress_on_thread);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (!screenshot_saved)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(10.0f, TRANSLATE("OSDMessage", "Failed to save screenshot to '%s'"), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Screenshot saved to '%s'."), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
std::string System::GetGameSaveStateFileName(const std::string_view& serial, s32 slot)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
if (slot < 0)
|
2022-10-05 08:29:08 +00:00
|
|
|
return Path::Combine(EmuFolders::SaveStates, fmt::format("{}_resume.sav", serial));
|
2022-07-11 13:03:29 +00:00
|
|
|
else
|
2022-10-05 08:29:08 +00:00
|
|
|
return Path::Combine(EmuFolders::SaveStates, fmt::format("{}_{}.sav", serial, slot));
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string System::GetGlobalSaveStateFileName(s32 slot)
|
|
|
|
{
|
|
|
|
if (slot < 0)
|
|
|
|
return Path::Combine(EmuFolders::SaveStates, "resume.sav");
|
|
|
|
else
|
|
|
|
return Path::Combine(EmuFolders::SaveStates, fmt::format("savestate_{}.sav", slot));
|
|
|
|
}
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
std::vector<SaveStateInfo> System::GetAvailableSaveStates(const char* serial)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
std::vector<SaveStateInfo> si;
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
auto add_path = [&si](std::string path, s32 slot, bool global) {
|
|
|
|
FILESYSTEM_STAT_DATA sd;
|
|
|
|
if (!FileSystem::StatFile(path.c_str(), &sd))
|
|
|
|
return;
|
|
|
|
|
|
|
|
si.push_back(SaveStateInfo{std::move(path), sd.ModificationTime, static_cast<s32>(slot), global});
|
|
|
|
};
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
if (serial && std::strlen(serial) > 0)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
add_path(GetGameSaveStateFileName(serial, -1), -1, false);
|
2022-07-11 13:03:29 +00:00
|
|
|
for (s32 i = 1; i <= PER_GAME_SAVE_STATE_SLOTS; i++)
|
2022-10-05 08:29:08 +00:00
|
|
|
add_path(GetGameSaveStateFileName(serial, i), i, false);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (s32 i = 1; i <= GLOBAL_SAVE_STATE_SLOTS; i++)
|
|
|
|
add_path(GetGlobalSaveStateFileName(i), i, true);
|
|
|
|
|
|
|
|
return si;
|
|
|
|
}
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
std::optional<SaveStateInfo> System::GetSaveStateInfo(const char* serial, s32 slot)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
const bool global = (!serial || serial[0] == 0);
|
|
|
|
std::string path = global ? GetGlobalSaveStateFileName(slot) : GetGameSaveStateFileName(serial, slot);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
FILESYSTEM_STAT_DATA sd;
|
|
|
|
if (!FileSystem::StatFile(path.c_str(), &sd))
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
return SaveStateInfo{std::move(path), sd.ModificationTime, slot, global};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ExtendedSaveStateInfo> System::GetExtendedSaveStateInfo(const char* path)
|
|
|
|
{
|
|
|
|
FILESYSTEM_STAT_DATA sd;
|
|
|
|
if (!FileSystem::StatFile(path, &sd))
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
std::unique_ptr<ByteStream> stream = ByteStream::OpenFile(path, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_SEEKABLE);
|
|
|
|
if (!stream)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
std::optional<ExtendedSaveStateInfo> ssi(InternalGetExtendedSaveStateInfo(stream.get()));
|
|
|
|
if (ssi)
|
|
|
|
ssi->timestamp = sd.ModificationTime;
|
|
|
|
|
|
|
|
return ssi;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ExtendedSaveStateInfo> System::InternalGetExtendedSaveStateInfo(ByteStream* stream)
|
|
|
|
{
|
|
|
|
SAVE_STATE_HEADER header;
|
|
|
|
if (!stream->Read(&header, sizeof(header)) || header.magic != SAVE_STATE_MAGIC)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
ExtendedSaveStateInfo ssi;
|
|
|
|
if (header.version < SAVE_STATE_MINIMUM_VERSION || header.version > SAVE_STATE_VERSION)
|
|
|
|
{
|
|
|
|
ssi.title = StringUtil::StdStringFromFormat(
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE("CommonHostInterface", "Invalid version %u (%s version %u)"), header.version,
|
2022-07-11 13:03:29 +00:00
|
|
|
header.version > SAVE_STATE_VERSION ? "maximum" : "minimum",
|
|
|
|
header.version > SAVE_STATE_VERSION ? SAVE_STATE_VERSION : SAVE_STATE_MINIMUM_VERSION);
|
|
|
|
return ssi;
|
|
|
|
}
|
|
|
|
|
|
|
|
header.title[sizeof(header.title) - 1] = 0;
|
|
|
|
ssi.title = header.title;
|
2022-10-05 08:29:08 +00:00
|
|
|
header.serial[sizeof(header.serial) - 1] = 0;
|
|
|
|
ssi.serial = header.serial;
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
if (header.media_filename_length > 0 &&
|
|
|
|
(header.offset_to_media_filename + header.media_filename_length) <= stream->GetSize())
|
|
|
|
{
|
|
|
|
stream->SeekAbsolute(header.offset_to_media_filename);
|
|
|
|
ssi.media_path.resize(header.media_filename_length);
|
|
|
|
if (!stream->Read2(ssi.media_path.data(), header.media_filename_length))
|
|
|
|
std::string().swap(ssi.media_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.screenshot_width > 0 && header.screenshot_height > 0 && header.screenshot_size > 0 &&
|
|
|
|
(static_cast<u64>(header.offset_to_screenshot) + static_cast<u64>(header.screenshot_size)) <= stream->GetSize())
|
|
|
|
{
|
|
|
|
stream->SeekAbsolute(header.offset_to_screenshot);
|
|
|
|
ssi.screenshot_data.resize((header.screenshot_size + 3u) / 4u);
|
|
|
|
if (stream->Read2(ssi.screenshot_data.data(), header.screenshot_size))
|
|
|
|
{
|
|
|
|
ssi.screenshot_width = header.screenshot_width;
|
|
|
|
ssi.screenshot_height = header.screenshot_height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
decltype(ssi.screenshot_data)().swap(ssi.screenshot_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssi;
|
|
|
|
}
|
|
|
|
|
2022-10-05 08:29:08 +00:00
|
|
|
void System::DeleteSaveStates(const char* serial, bool resume)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
const std::vector<SaveStateInfo> states(GetAvailableSaveStates(serial));
|
2022-07-11 13:03:29 +00:00
|
|
|
for (const SaveStateInfo& si : states)
|
|
|
|
{
|
|
|
|
if (si.global || (!resume && si.slot < 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Log_InfoPrintf("Removing save state at '%s'", si.path.c_str());
|
|
|
|
if (!FileSystem::DeleteFile(si.path.c_str()))
|
|
|
|
Log_ErrorPrintf("Failed to delete save state file '%s'", si.path.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string System::GetMostRecentResumeSaveStatePath()
|
|
|
|
{
|
|
|
|
std::vector<FILESYSTEM_FIND_DATA> files;
|
|
|
|
if (!FileSystem::FindFiles(EmuFolders::SaveStates.c_str(), "*resume.sav", FILESYSTEM_FIND_FILES, &files) ||
|
|
|
|
files.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
FILESYSTEM_FIND_DATA* most_recent = &files[0];
|
|
|
|
for (FILESYSTEM_FIND_DATA& file : files)
|
|
|
|
{
|
|
|
|
if (file.ModificationTime > most_recent->ModificationTime)
|
|
|
|
most_recent = &file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(most_recent->FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string System::GetCheatFileName()
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
|
2023-05-15 13:38:37 +00:00
|
|
|
const std::string& title = System::GetGameTitle();
|
2022-07-11 13:03:29 +00:00
|
|
|
if (!title.empty())
|
|
|
|
ret = Path::Combine(EmuFolders::Cheats, fmt::format("{}.cht", title.c_str()));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::LoadCheatList(const char* filename)
|
|
|
|
{
|
|
|
|
if (System::IsShutdown())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::unique_ptr<CheatList> cl = std::make_unique<CheatList>();
|
|
|
|
if (!cl->LoadFromFile(filename, CheatList::Format::Autodetect))
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(15.0f, TRANSLATE("OSDMessage", "Failed to load cheats from '%s'."), filename);
|
2022-07-11 13:03:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cl->GetEnabledCodeCount() > 0)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(
|
|
|
|
fmt::format(TRANSLATE_FS("OSDMessage", "{} cheats are enabled. This may result in instability."),
|
|
|
|
cl->GetEnabledCodeCount()),
|
|
|
|
30.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
System::SetCheatList(std::move(cl));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::LoadCheatListFromGameTitle()
|
|
|
|
{
|
2022-07-22 12:51:29 +00:00
|
|
|
// Called when booting, needs to test for shutdown.
|
|
|
|
if (IsShutdown() || Achievements::ChallengeModeActive())
|
2022-07-11 13:03:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::string filename(GetCheatFileName());
|
|
|
|
if (filename.empty() || !FileSystem::FileExists(filename.c_str()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return LoadCheatList(filename.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::LoadCheatListFromDatabase()
|
|
|
|
{
|
2022-10-05 08:29:08 +00:00
|
|
|
if (IsShutdown() || s_running_game_serial.empty() || Achievements::ChallengeModeActive())
|
2022-07-11 13:03:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
std::unique_ptr<CheatList> cl = std::make_unique<CheatList>();
|
2022-10-05 08:29:08 +00:00
|
|
|
if (!cl->LoadFromPackage(s_running_game_serial))
|
2022-07-11 13:03:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Log_InfoPrintf("Loaded %u cheats from database.", cl->GetCodeCount());
|
2022-07-22 12:51:29 +00:00
|
|
|
SetCheatList(std::move(cl));
|
2022-07-11 13:03:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::SaveCheatList()
|
|
|
|
{
|
|
|
|
if (!System::IsValid() || !System::HasCheatList())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::string filename(GetCheatFileName());
|
|
|
|
if (filename.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!System::GetCheatList()->SaveToPCSXRFile(filename.c_str()))
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(15.0f, TRANSLATE("OSDMessage", "Failed to save cheat list to '%s'"), filename.c_str());
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::SaveCheatList(const char* filename)
|
|
|
|
{
|
|
|
|
if (!System::IsValid() || !System::HasCheatList())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!System::GetCheatList()->SaveToPCSXRFile(filename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This shouldn't be needed, but lupdate doesn't gather this string otherwise...
|
|
|
|
const u32 code_count = System::GetCheatList()->GetCodeCount();
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(fmt::format(TRANSLATE_FS("OSDMessage", "Saved {} cheats to '{}'."), code_count, filename), 5.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool System::DeleteCheatList()
|
|
|
|
{
|
|
|
|
if (!System::IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::string filename(GetCheatFileName());
|
|
|
|
if (!filename.empty())
|
|
|
|
{
|
|
|
|
if (!FileSystem::DeleteFile(filename.c_str()))
|
|
|
|
return false;
|
|
|
|
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddOSDMessage(fmt::format(TRANSLATE_FS("OSDMessage", "Deleted cheat list '{}'."), filename), 5.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
System::SetCheatList(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ClearCheatList(bool save_to_file)
|
|
|
|
{
|
|
|
|
if (!System::IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheatList* cl = System::GetCheatList();
|
|
|
|
if (!cl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (cl->GetCodeCount() > 0)
|
|
|
|
cl->RemoveCode(cl->GetCodeCount() - 1);
|
|
|
|
|
|
|
|
if (save_to_file)
|
|
|
|
SaveCheatList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::SetCheatCodeState(u32 index, bool enabled, bool save_to_file)
|
|
|
|
{
|
|
|
|
if (!System::IsValid() || !System::HasCheatList())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheatList* cl = System::GetCheatList();
|
|
|
|
if (index >= cl->GetCodeCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheatCode& cc = cl->GetCode(index);
|
|
|
|
if (cc.enabled == enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cc.enabled = enabled;
|
|
|
|
if (!enabled)
|
|
|
|
cc.ApplyOnDisable();
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Cheat '%s' enabled."), cc.description.c_str());
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Cheat '%s' disabled."), cc.description.c_str());
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (save_to_file)
|
|
|
|
SaveCheatList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ApplyCheatCode(u32 index)
|
|
|
|
{
|
|
|
|
if (!System::HasCheatList() || index >= System::GetCheatList()->GetCodeCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const CheatCode& cc = System::GetCheatList()->GetCode(index);
|
|
|
|
if (!cc.enabled)
|
|
|
|
{
|
|
|
|
cc.Apply();
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Applied cheat '%s'."), cc.description.c_str());
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddFormattedOSDMessage(5.0f, TRANSLATE("OSDMessage", "Cheat '%s' is already enabled."),
|
2022-07-11 13:03:29 +00:00
|
|
|
cc.description.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ToggleWidescreen()
|
|
|
|
{
|
|
|
|
g_settings.gpu_widescreen_hack = !g_settings.gpu_widescreen_hack;
|
|
|
|
|
|
|
|
const DisplayAspectRatio user_ratio =
|
|
|
|
Settings::ParseDisplayAspectRatio(
|
|
|
|
Host::GetStringSettingValue("Display", "AspectRatio",
|
|
|
|
Settings::GetDisplayAspectRatioName(Settings::DEFAULT_DISPLAY_ASPECT_RATIO))
|
|
|
|
.c_str())
|
|
|
|
.value_or(DisplayAspectRatio::Auto);
|
|
|
|
;
|
|
|
|
|
|
|
|
if (user_ratio == DisplayAspectRatio::Auto || user_ratio == DisplayAspectRatio::PAR1_1 ||
|
|
|
|
user_ratio == DisplayAspectRatio::R4_3)
|
|
|
|
{
|
|
|
|
g_settings.display_aspect_ratio = g_settings.gpu_widescreen_hack ? DisplayAspectRatio::R16_9 : user_ratio;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_settings.display_aspect_ratio = g_settings.gpu_widescreen_hack ? user_ratio : DisplayAspectRatio::Auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_settings.gpu_widescreen_hack)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddKeyedOSDMessage(
|
|
|
|
"WidescreenHack",
|
2023-08-20 03:25:43 +00:00
|
|
|
fmt::format(TRANSLATE_FS("OSDMessage", "Widescreen hack is now enabled, and aspect ratio is set to {}."),
|
|
|
|
Settings::GetDisplayAspectRatioDisplayName(g_settings.display_aspect_ratio)),
|
2023-08-19 14:08:03 +00:00
|
|
|
5.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddKeyedOSDMessage(
|
|
|
|
"WidescreenHack",
|
|
|
|
fmt::format(TRANSLATE_FS("OSDMessage", "Widescreen hack is now disabled, and aspect ratio is set to {}."),
|
2023-08-20 03:25:43 +00:00
|
|
|
Settings::GetDisplayAspectRatioDisplayName(g_settings.display_aspect_ratio), 5.0f));
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GTE::UpdateAspectRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ToggleSoftwareRendering()
|
|
|
|
{
|
|
|
|
if (IsShutdown() || g_settings.gpu_renderer == GPURenderer::Software)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const GPURenderer new_renderer = g_gpu->IsHardwareRenderer() ? GPURenderer::Software : g_settings.gpu_renderer;
|
|
|
|
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddKeyedFormattedOSDMessage("SoftwareRendering", 5.0f, TRANSLATE("OSDMessage", "Switching to %s renderer..."),
|
2022-07-11 13:03:29 +00:00
|
|
|
Settings::GetRendererDisplayName(new_renderer));
|
|
|
|
RecreateGPU(new_renderer);
|
|
|
|
Host::InvalidateDisplay();
|
|
|
|
ResetPerformanceCounters();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::UpdateSoftwareCursor()
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
{
|
|
|
|
Host::SetMouseMode(false, false);
|
2023-08-27 06:22:45 +00:00
|
|
|
ImGuiManager::ClearSoftwareCursor(0);
|
2022-07-11 13:03:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-27 06:22:45 +00:00
|
|
|
std::string image_path;
|
2022-07-11 13:03:29 +00:00
|
|
|
float image_scale = 1.0f;
|
|
|
|
bool relative_mode = false;
|
|
|
|
bool hide_cursor = false;
|
|
|
|
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
|
|
|
Controller* controller = System::GetController(i);
|
2023-08-27 06:22:45 +00:00
|
|
|
if (controller && controller->GetSoftwareCursor(&image_path, &image_scale, &relative_mode))
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
hide_cursor = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Host::SetMouseMode(relative_mode, hide_cursor);
|
|
|
|
|
2023-08-27 06:22:45 +00:00
|
|
|
if (!image_path.empty())
|
|
|
|
ImGuiManager::SetSoftwareCursor(0, std::move(image_path), image_scale);
|
2022-07-11 13:03:29 +00:00
|
|
|
else
|
2023-08-27 06:22:45 +00:00
|
|
|
ImGuiManager::ClearSoftwareCursor(0);
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void System::RequestDisplaySize(float scale /*= 0.0f*/)
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (scale == 0.0f)
|
|
|
|
scale = g_gpu->IsHardwareRenderer() ? static_cast<float>(g_settings.gpu_resolution_scale) : 1.0f;
|
|
|
|
|
2023-08-27 08:13:50 +00:00
|
|
|
const float y_scale = (static_cast<float>(g_gpu->GetDisplayWidth()) / static_cast<float>(g_gpu->GetDisplayHeight())) /
|
|
|
|
g_gpu->GetDisplayAspectRatio();
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
const u32 requested_width =
|
2023-08-27 08:13:50 +00:00
|
|
|
std::max<u32>(static_cast<u32>(std::ceil(static_cast<float>(g_gpu->GetDisplayWidth()) * scale)), 1);
|
|
|
|
const u32 requested_height =
|
|
|
|
std::max<u32>(static_cast<u32>(std::ceil(static_cast<float>(g_gpu->GetDisplayHeight()) * y_scale * scale)), 1);
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
Host::RequestResizeHostDisplay(static_cast<s32>(requested_width), static_cast<s32>(requested_height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::HostDisplayResized()
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_settings.gpu_widescreen_hack && g_settings.display_aspect_ratio == DisplayAspectRatio::MatchWindow)
|
|
|
|
GTE::UpdateAspectRatio();
|
|
|
|
|
|
|
|
g_gpu->UpdateResolutionScale();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::SetTimerResolutionIncreased(bool enabled)
|
|
|
|
{
|
2022-08-22 09:55:38 +00:00
|
|
|
#if defined(_WIN32)
|
2022-07-11 13:03:29 +00:00
|
|
|
static bool current_state = false;
|
|
|
|
if (current_state == enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
current_state = enabled;
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
else
|
|
|
|
timeEndPeriod(1);
|
|
|
|
#endif
|
2022-07-27 14:02:17 +00:00
|
|
|
}
|
2023-08-23 12:06:48 +00:00
|
|
|
|
|
|
|
void System::UpdateSessionTime(const std::string& prev_serial)
|
|
|
|
{
|
|
|
|
const u64 ctime = Common::Timer::GetCurrentValue();
|
|
|
|
if (!prev_serial.empty())
|
|
|
|
{
|
|
|
|
// round up to seconds
|
|
|
|
const std::time_t etime =
|
|
|
|
static_cast<std::time_t>(std::round(Common::Timer::ConvertValueToSeconds(ctime - s_session_start_time)));
|
|
|
|
const std::time_t wtime = std::time(nullptr);
|
|
|
|
GameList::AddPlayedTimeForSerial(prev_serial, wtime, etime);
|
|
|
|
}
|
|
|
|
|
|
|
|
s_session_start_time = ctime;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 System::GetSessionPlayedTime()
|
|
|
|
{
|
|
|
|
const u64 ctime = Common::Timer::GetCurrentValue();
|
|
|
|
return static_cast<u64>(std::round(Common::Timer::ConvertValueToSeconds(ctime - s_session_start_time)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_DISCORD_PRESENCE
|
|
|
|
|
|
|
|
void System::InitializeDiscordPresence()
|
|
|
|
{
|
|
|
|
if (s_discord_presence_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DiscordEventHandlers handlers = {};
|
|
|
|
Discord_Initialize("705325712680288296", &handlers, 0, nullptr);
|
|
|
|
s_discord_presence_active = true;
|
|
|
|
|
|
|
|
UpdateDiscordPresence(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::ShutdownDiscordPresence()
|
|
|
|
{
|
|
|
|
if (!s_discord_presence_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Discord_ClearPresence();
|
|
|
|
Discord_Shutdown();
|
|
|
|
s_discord_presence_active = false;
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
s_discord_presence_cheevos_string.clear();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::UpdateDiscordPresence(bool rich_presence_only)
|
|
|
|
{
|
|
|
|
if (!s_discord_presence_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
// Update only if RetroAchievements rich presence has changed
|
|
|
|
const std::string& new_rich_presence = Achievements::GetRichPresenceString();
|
|
|
|
if (new_rich_presence == s_discord_presence_cheevos_string && rich_presence_only)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s_discord_presence_cheevos_string = new_rich_presence;
|
|
|
|
#else
|
|
|
|
if (rich_presence_only)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// https://discord.com/developers/docs/rich-presence/how-to#updating-presence-update-presence-payload-fields
|
|
|
|
DiscordRichPresence rp = {};
|
|
|
|
rp.largeImageKey = "duckstation_logo";
|
|
|
|
rp.largeImageText = "DuckStation PS1/PSX Emulator";
|
|
|
|
rp.startTimestamp = std::time(nullptr);
|
|
|
|
|
|
|
|
SmallString details_string;
|
|
|
|
if (!System::IsShutdown())
|
|
|
|
{
|
|
|
|
details_string.AppendFormattedString("%s (%s)", System::GetGameTitle().c_str(), System::GetGameSerial().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
details_string.AppendString("No Game Running");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_CHEEVOS
|
|
|
|
SmallString state_string;
|
|
|
|
// Trim to 128 bytes as per Discord-RPC requirements
|
|
|
|
if (s_discord_presence_cheevos_string.length() >= 128)
|
|
|
|
{
|
|
|
|
// 124 characters + 3 dots + null terminator
|
|
|
|
state_string = s_discord_presence_cheevos_string.substr(0, 124);
|
|
|
|
state_string.AppendString("...");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state_string = s_discord_presence_cheevos_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.state = state_string;
|
|
|
|
#endif
|
|
|
|
rp.details = details_string;
|
|
|
|
|
|
|
|
Discord_UpdatePresence(&rp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::PollDiscordPresence()
|
|
|
|
{
|
|
|
|
if (!s_discord_presence_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UpdateDiscordPresence(true);
|
|
|
|
|
|
|
|
Discord_RunCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|