2019-09-09 07:01:26 +00:00
|
|
|
#include "system.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"
|
2020-03-23 14:21:11 +00:00
|
|
|
#include "common/audio_stream.h"
|
2020-08-01 03:34:07 +00:00
|
|
|
#include "common/file_system.h"
|
2020-09-01 02:29:22 +00:00
|
|
|
#include "common/iso_reader.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "common/log.h"
|
2019-09-14 10:28:47 +00:00
|
|
|
#include "common/state_wrapper.h"
|
2020-04-20 16:50:45 +00:00
|
|
|
#include "common/string_util.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"
|
|
|
|
#include "gpu.h"
|
2020-07-31 07:09:18 +00:00
|
|
|
#include "gte.h"
|
2019-12-06 15:37:27 +00:00
|
|
|
#include "host_display.h"
|
2019-11-03 14:39:48 +00:00
|
|
|
#include "host_interface.h"
|
2020-07-21 14:03:22 +00:00
|
|
|
#include "host_interface_progress_callback.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"
|
2019-09-20 06:47:41 +00:00
|
|
|
#include "pad.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"
|
2019-09-20 13:40:19 +00:00
|
|
|
#include "timers.h"
|
2020-09-03 02:25:59 +00:00
|
|
|
#include <cctype>
|
2020-09-09 12:11:28 +00:00
|
|
|
#include <cstdio>
|
2020-09-01 02:29:22 +00:00
|
|
|
#include <fstream>
|
2020-07-22 16:36:05 +00:00
|
|
|
#include <limits>
|
2019-09-22 15:28:00 +00:00
|
|
|
Log_SetChannel(System);
|
2019-09-09 07:01:26 +00:00
|
|
|
|
2020-02-09 13:16:25 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "common/windows_headers.h"
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2020-03-02 01:08:16 +00:00
|
|
|
SystemBootParameters::SystemBootParameters() = default;
|
|
|
|
|
2020-09-12 20:14:50 +00:00
|
|
|
SystemBootParameters::SystemBootParameters(SystemBootParameters&& other) = default;
|
2020-03-02 01:08:16 +00:00
|
|
|
|
2020-09-12 20:14:50 +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;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
namespace System {
|
|
|
|
|
2020-10-29 12:05:09 +00:00
|
|
|
static bool LoadEXE(const char* filename);
|
|
|
|
static bool LoadEXEFromBuffer(const void* buffer, u32 buffer_size);
|
|
|
|
static bool LoadPSF(const char* filename);
|
2020-07-31 07:09:18 +00:00
|
|
|
static bool SetExpansionROM(const char* filename);
|
|
|
|
|
|
|
|
/// Opens CD image, preloading if needed.
|
|
|
|
static std::unique_ptr<CDImage> OpenCDImage(const char* path, bool force_preload);
|
|
|
|
|
2020-11-25 13:06:39 +00:00
|
|
|
static bool DoLoadState(ByteStream* stream, bool force_software_renderer, bool update_display);
|
|
|
|
static bool DoState(StateWrapper& sw, bool update_display);
|
2020-07-31 07:09:18 +00:00
|
|
|
static bool CreateGPU(GPURenderer renderer);
|
|
|
|
|
|
|
|
static bool Initialize(bool force_software_renderer);
|
|
|
|
|
|
|
|
static void UpdateRunningGame(const char* path, CDImage* image);
|
|
|
|
|
|
|
|
static State s_state = State::Shutdown;
|
|
|
|
|
|
|
|
static ConsoleRegion s_region = ConsoleRegion::NTSC_U;
|
2020-09-29 13:29:28 +00:00
|
|
|
TickCount g_ticks_per_second = MASTER_CLOCK;
|
|
|
|
static TickCount s_max_slice_ticks = MASTER_CLOCK / 10;
|
2020-07-31 07:09:18 +00:00
|
|
|
static u32 s_frame_number = 1;
|
|
|
|
static u32 s_internal_frame_number = 1;
|
|
|
|
|
|
|
|
static std::string s_running_game_path;
|
|
|
|
static std::string s_running_game_code;
|
|
|
|
static std::string s_running_game_title;
|
|
|
|
|
|
|
|
static float s_throttle_frequency = 60.0f;
|
2020-11-03 11:21:11 +00:00
|
|
|
static float s_target_speed = 1.0f;
|
2020-07-31 07:09:18 +00:00
|
|
|
static s32 s_throttle_period = 0;
|
|
|
|
static u64 s_last_throttle_time = 0;
|
|
|
|
static Common::Timer s_throttle_timer;
|
|
|
|
static Common::Timer s_speed_lost_time_timestamp;
|
|
|
|
|
|
|
|
static float s_average_frame_time_accumulator = 0.0f;
|
|
|
|
static float s_worst_frame_time_accumulator = 0.0f;
|
|
|
|
|
|
|
|
static float s_vps = 0.0f;
|
|
|
|
static float s_fps = 0.0f;
|
|
|
|
static float s_speed = 0.0f;
|
|
|
|
static float s_worst_frame_time = 0.0f;
|
|
|
|
static float s_average_frame_time = 0.0f;
|
|
|
|
static u32 s_last_frame_number = 0;
|
|
|
|
static u32 s_last_internal_frame_number = 0;
|
|
|
|
static u32 s_last_global_tick_counter = 0;
|
|
|
|
static Common::Timer s_fps_timer;
|
|
|
|
static Common::Timer s_frame_timer;
|
|
|
|
|
|
|
|
// Playlist of disc images.
|
2020-08-15 10:38:17 +00:00
|
|
|
static std::vector<std::string> s_media_playlist;
|
|
|
|
static std::string s_media_playlist_filename;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2020-09-09 12:11:28 +00:00
|
|
|
static std::unique_ptr<CheatList> s_cheat_list;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
State GetState()
|
|
|
|
{
|
|
|
|
return s_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetState(State new_state)
|
|
|
|
{
|
|
|
|
Assert(s_state == State::Paused || s_state == State::Running);
|
|
|
|
Assert(new_state == State::Paused || new_state == State::Running);
|
|
|
|
s_state = new_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsRunning()
|
|
|
|
{
|
|
|
|
return s_state == State::Running;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPaused()
|
|
|
|
{
|
|
|
|
return s_state == State::Paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsShutdown()
|
|
|
|
{
|
|
|
|
return s_state == State::Shutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid()
|
|
|
|
{
|
2020-08-20 11:30:11 +00:00
|
|
|
return s_state != State::Shutdown && s_state != State::Starting;
|
2020-07-31 07:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleRegion GetRegion()
|
|
|
|
{
|
|
|
|
return s_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPALRegion()
|
|
|
|
{
|
|
|
|
return s_region == ConsoleRegion::PAL;
|
|
|
|
}
|
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
TickCount GetMaxSliceTicks()
|
|
|
|
{
|
|
|
|
return s_max_slice_ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateOverclock()
|
|
|
|
{
|
|
|
|
g_ticks_per_second = ScaleTicksToOverclock(MASTER_CLOCK);
|
|
|
|
s_max_slice_ticks = ScaleTicksToOverclock(MASTER_CLOCK / 10);
|
|
|
|
g_spu.CPUClockChanged();
|
|
|
|
g_cdrom.CPUClockChanged();
|
|
|
|
g_gpu->CPUClockChanged();
|
|
|
|
g_timers.CPUClocksChanged();
|
|
|
|
UpdateThrottlePeriod();
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
u32 GetFrameNumber()
|
|
|
|
{
|
|
|
|
return s_frame_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetInternalFrameNumber()
|
|
|
|
{
|
|
|
|
return s_internal_frame_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameDone()
|
|
|
|
{
|
|
|
|
s_frame_number++;
|
|
|
|
CPU::g_state.frame_done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementInternalFrameNumber()
|
|
|
|
{
|
|
|
|
s_internal_frame_number++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& GetRunningPath()
|
|
|
|
{
|
|
|
|
return s_running_game_path;
|
|
|
|
}
|
|
|
|
const std::string& GetRunningCode()
|
|
|
|
{
|
|
|
|
return s_running_game_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& GetRunningTitle()
|
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
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
float GetFPS()
|
|
|
|
{
|
|
|
|
return s_fps;
|
|
|
|
}
|
|
|
|
float GetVPS()
|
|
|
|
{
|
|
|
|
return s_vps;
|
|
|
|
}
|
|
|
|
float GetEmulationSpeed()
|
|
|
|
{
|
|
|
|
return s_speed;
|
|
|
|
}
|
|
|
|
float GetAverageFrameTime()
|
|
|
|
{
|
|
|
|
return s_average_frame_time;
|
|
|
|
}
|
|
|
|
float GetWorstFrameTime()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
return s_worst_frame_time;
|
|
|
|
}
|
|
|
|
float GetThrottleFrequency()
|
|
|
|
{
|
|
|
|
return s_throttle_frequency;
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
2019-09-09 07:01:26 +00:00
|
|
|
|
2020-09-01 02:29:22 +00:00
|
|
|
bool IsExeFileName(const char* path)
|
|
|
|
{
|
|
|
|
const char* extension = std::strrchr(path, '.');
|
|
|
|
return (extension &&
|
|
|
|
(StringUtil::Strcasecmp(extension, ".exe") == 0 || StringUtil::Strcasecmp(extension, ".psexe") == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPsfFileName(const char* path)
|
|
|
|
{
|
|
|
|
const char* extension = std::strrchr(path, '.');
|
|
|
|
return (extension && StringUtil::Strcasecmp(extension, ".psf") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsM3UFileName(const char* path)
|
|
|
|
{
|
|
|
|
const char* extension = std::strrchr(path, '.');
|
|
|
|
return (extension && StringUtil::Strcasecmp(extension, ".m3u") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ParseM3UFile(const char* path)
|
|
|
|
{
|
|
|
|
std::ifstream ifs(path);
|
|
|
|
if (!ifs.is_open())
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to open %s", path);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> entries;
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(ifs, line))
|
|
|
|
{
|
|
|
|
u32 start_offset = 0;
|
|
|
|
while (start_offset < line.size() && std::isspace(line[start_offset]))
|
|
|
|
start_offset++;
|
|
|
|
|
|
|
|
// skip comments
|
|
|
|
if (start_offset == line.size() || line[start_offset] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// strip ending whitespace
|
|
|
|
u32 end_offset = static_cast<u32>(line.size()) - 1;
|
|
|
|
while (std::isspace(line[end_offset]) && end_offset > start_offset)
|
|
|
|
end_offset--;
|
|
|
|
|
|
|
|
// anything?
|
|
|
|
if (start_offset == end_offset)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string entry_path(line.begin() + start_offset, line.begin() + end_offset + 1);
|
|
|
|
if (!FileSystem::IsAbsolutePath(entry_path))
|
|
|
|
{
|
|
|
|
SmallString absolute_path;
|
|
|
|
FileSystem::BuildPathRelativeToFile(absolute_path, path, entry_path.c_str());
|
|
|
|
entry_path = absolute_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_DevPrintf("Read path from m3u: '%s'", entry_path.c_str());
|
|
|
|
entries.push_back(std::move(entry_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_InfoPrintf("Loaded %zu paths from m3u '%s'", entries.size(), path);
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
ConsoleRegion 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:
|
|
|
|
default:
|
|
|
|
return ConsoleRegion::NTSC_U;
|
|
|
|
|
|
|
|
case DiscRegion::PAL:
|
|
|
|
return ConsoleRegion::PAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:29:22 +00:00
|
|
|
std::string_view GetTitleForPath(const char* path)
|
|
|
|
{
|
|
|
|
const char* extension = std::strrchr(path, '.');
|
|
|
|
if (path == extension)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
const char* path_end = path + std::strlen(path);
|
|
|
|
const char* title_end = extension ? (extension - 1) : (path_end);
|
|
|
|
const char* title_start = std::max(std::strrchr(path, '/'), std::strrchr(path, '\\'));
|
|
|
|
if (!title_start || title_start == path)
|
|
|
|
return std::string_view(path, title_end - title_start);
|
|
|
|
else
|
|
|
|
return std::string_view(title_start + 1, title_end - title_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetGameCodeForPath(const char* image_path)
|
|
|
|
{
|
|
|
|
std::unique_ptr<CDImage> cdi = CDImage::Open(image_path);
|
|
|
|
if (!cdi)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return GetGameCodeForImage(cdi.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetGameCodeForImage(CDImage* cdi)
|
|
|
|
{
|
|
|
|
ISOReader iso;
|
|
|
|
if (!iso.Open(cdi, 1))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// Read SYSTEM.CNF
|
|
|
|
std::vector<u8> system_cnf_data;
|
|
|
|
if (!iso.ReadFile("SYSTEM.CNF", &system_cnf_data))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// cdrom:\SCES_123.45;1
|
|
|
|
std::string code = iter->second;
|
|
|
|
std::string::size_type 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = code.find(';');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
code.erase(pos);
|
|
|
|
|
|
|
|
// SCES_123.45 -> SCES-12345
|
|
|
|
for (pos = 0; pos < code.size();)
|
|
|
|
{
|
|
|
|
if (code[pos] == '.')
|
|
|
|
{
|
|
|
|
code.erase(pos, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code[pos] == '_')
|
|
|
|
code[pos] = '-';
|
|
|
|
else
|
|
|
|
code[pos] = static_cast<char>(std::toupper(code[pos]));
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscRegion GetRegionForCode(std::string_view code)
|
|
|
|
{
|
|
|
|
std::string prefix;
|
|
|
|
for (size_t pos = 0; pos < code.length(); pos++)
|
|
|
|
{
|
|
|
|
const int ch = std::tolower(code[pos]);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscRegion GetRegionFromSystemArea(CDImage* cdi)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscRegion GetRegionForImage(CDImage* cdi)
|
|
|
|
{
|
|
|
|
DiscRegion system_area_region = GetRegionFromSystemArea(cdi);
|
|
|
|
if (system_area_region != DiscRegion::Other)
|
|
|
|
return system_area_region;
|
|
|
|
|
|
|
|
std::string code = GetGameCodeForImage(cdi);
|
|
|
|
if (code.empty())
|
|
|
|
return DiscRegion::Other;
|
|
|
|
|
|
|
|
return GetRegionForCode(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<DiscRegion> GetRegionForPath(const char* image_path)
|
|
|
|
{
|
|
|
|
std::unique_ptr<CDImage> cdi = CDImage::Open(image_path);
|
|
|
|
if (!cdi)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return GetRegionForImage(cdi.get());
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:06:39 +00:00
|
|
|
bool RecreateGPU(GPURenderer renderer, bool update_display /* = true*/)
|
2019-10-04 05:00:32 +00:00
|
|
|
{
|
2020-08-06 13:19:52 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
2019-10-26 02:55:56 +00:00
|
|
|
// save current state
|
2020-01-10 03:31:12 +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);
|
2020-11-25 13:06:39 +00:00
|
|
|
const bool state_valid = g_gpu->DoState(sw, 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");
|
|
|
|
|
2020-08-06 13:19:52 +00:00
|
|
|
g_gpu->ResetGraphicsAPIState();
|
|
|
|
|
2019-10-26 02:55:56 +00:00
|
|
|
// create new renderer
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu.reset();
|
2020-01-24 04:51:53 +00:00
|
|
|
if (!CreateGPU(renderer))
|
2019-10-26 02:55:56 +00:00
|
|
|
{
|
|
|
|
Panic("Failed to recreate GPU");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-18 04:43:55 +00:00
|
|
|
// reinitialize the code cache because the address space could change
|
|
|
|
CPU::CodeCache::Reinitialize();
|
|
|
|
|
2019-10-26 02:55:56 +00:00
|
|
|
if (state_valid)
|
|
|
|
{
|
|
|
|
state_stream->SeekAbsolute(0);
|
|
|
|
sw.SetMode(StateWrapper::Mode::Read);
|
2020-08-06 13:19:52 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
2020-11-25 13:06:39 +00:00
|
|
|
g_gpu->DoState(sw, update_display);
|
2020-08-15 04:56:20 +00:00
|
|
|
TimingEvents::DoState(sw);
|
2020-08-06 13:19:52 +00:00
|
|
|
g_gpu->ResetGraphicsAPIState();
|
2019-10-26 02:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-10-04 05:00:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
std::unique_ptr<CDImage> OpenCDImage(const char* path, bool force_preload)
|
2020-07-22 16:35:23 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<CDImage> media = CDImage::Open(path);
|
|
|
|
if (!media)
|
|
|
|
return {};
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (force_preload || g_settings.cdrom_load_image_to_ram)
|
2020-07-22 16:35:23 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
HostInterfaceProgressCallback callback;
|
2020-07-22 16:35:23 +00:00
|
|
|
std::unique_ptr<CDImage> memory_image = CDImage::CreateMemoryImage(media.get(), &callback);
|
|
|
|
if (memory_image)
|
|
|
|
media = std::move(memory_image);
|
|
|
|
else
|
|
|
|
Log_WarningPrintf("Failed to preload image '%s' to RAM", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return media;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool Boot(const SystemBootParameters& params)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
Assert(s_state == State::Shutdown);
|
2020-08-15 10:38:17 +00:00
|
|
|
Assert(s_media_playlist.empty());
|
2020-07-31 07:09:18 +00:00
|
|
|
s_state = State::Starting;
|
|
|
|
s_region = g_settings.region;
|
|
|
|
|
2020-05-26 16:01:09 +00:00
|
|
|
if (params.state_stream)
|
2020-08-15 10:38:17 +00:00
|
|
|
{
|
2020-11-25 13:06:39 +00:00
|
|
|
if (!DoLoadState(params.state_stream.get(), params.force_software_renderer, true))
|
2020-08-15 10:38:17 +00:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-26 16:01:09 +00:00
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
// Load CD image up and detect region.
|
|
|
|
std::unique_ptr<CDImage> media;
|
|
|
|
bool exe_boot = false;
|
2020-04-16 14:29:56 +00:00
|
|
|
bool psf_boot = false;
|
2020-03-02 01:08:16 +00:00
|
|
|
if (!params.filename.empty())
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-09-01 02:29:22 +00:00
|
|
|
exe_boot = IsExeFileName(params.filename.c_str());
|
|
|
|
psf_boot = (!exe_boot && IsPsfFileName(params.filename.c_str()));
|
2020-04-16 14:29:56 +00:00
|
|
|
if (exe_boot || psf_boot)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-04-16 14:29:56 +00:00
|
|
|
// TODO: Pull region from PSF
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_region == ConsoleRegion::Auto)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-09-09 06:54:07 +00:00
|
|
|
Log_InfoPrintf("Defaulting to NTSC-U/C region for executable.");
|
2020-07-31 07:09:18 +00:00
|
|
|
s_region = ConsoleRegion::NTSC_U;
|
2019-11-16 05:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-22 16:36:05 +00:00
|
|
|
u32 playlist_index;
|
2020-09-01 02:29:22 +00:00
|
|
|
if (IsM3UFileName(params.filename.c_str()))
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-09-01 02:29:22 +00:00
|
|
|
s_media_playlist = ParseM3UFile(params.filename.c_str());
|
2020-08-15 10:38:17 +00:00
|
|
|
s_media_playlist_filename = params.filename;
|
|
|
|
if (s_media_playlist.empty())
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->ReportFormattedError("Failed to parse playlist '%s'", params.filename.c_str());
|
|
|
|
Shutdown();
|
2020-07-22 16:36:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
if (params.media_playlist_index >= s_media_playlist.size())
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
|
|
|
Log_WarningPrintf("Media playlist index %u out of range, using first", params.media_playlist_index);
|
|
|
|
playlist_index = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
playlist_index = params.media_playlist_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddMediaPathToPlaylist(params.filename);
|
|
|
|
playlist_index = 0;
|
|
|
|
}
|
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
const std::string& media_path = s_media_playlist[playlist_index];
|
2020-07-22 16:36:05 +00:00
|
|
|
Log_InfoPrintf("Loading CD image '%s' from playlist index %u...", media_path.c_str(), playlist_index);
|
|
|
|
media = OpenCDImage(media_path.c_str(), params.load_image_to_ram);
|
2019-11-16 05:27:57 +00:00
|
|
|
if (!media)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->ReportFormattedError("Failed to load CD image '%s'", params.filename.c_str());
|
|
|
|
Shutdown();
|
2019-11-16 05:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_region == ConsoleRegion::Auto)
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-09-01 02:29:22 +00:00
|
|
|
const DiscRegion disc_region = GetRegionForImage(media.get());
|
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)",
|
2020-07-31 07:09:18 +00:00
|
|
|
Settings::GetConsoleRegionName(s_region), params.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
|
|
|
|
|
|
|
// Load BIOS image.
|
2020-07-31 07:09:18 +00:00
|
|
|
std::optional<BIOS::Image> bios_image = g_host_interface->GetBIOSImage(s_region);
|
2019-11-16 05:27:57 +00:00
|
|
|
if (!bios_image)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->ReportFormattedError("Failed to load %s BIOS", Settings::GetConsoleRegionName(s_region));
|
|
|
|
Shutdown();
|
2019-11-16 05:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-20 11:30:11 +00:00
|
|
|
// Notify change of disc.
|
|
|
|
UpdateRunningGame(media ? media->GetFileName().c_str() : params.filename.c_str(), media.get());
|
|
|
|
|
2020-06-28 15:51:01 +00:00
|
|
|
// Component setup.
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!Initialize(params.force_software_renderer))
|
|
|
|
{
|
|
|
|
Shutdown();
|
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
|
|
|
|
2020-10-29 12:05:09 +00:00
|
|
|
Bus::SetBIOS(*bios_image);
|
2019-12-14 13:20:24 +00:00
|
|
|
UpdateControllers();
|
2019-11-16 05:27:57 +00:00
|
|
|
UpdateMemoryCards();
|
2019-11-17 14:32:54 +00:00
|
|
|
Reset();
|
2019-11-16 05:27:57 +00:00
|
|
|
|
|
|
|
// Enable tty by patching bios.
|
|
|
|
const BIOS::Hash bios_hash = BIOS::GetHash(*bios_image);
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_settings.bios_patch_tty_enable)
|
2020-10-29 12:05:09 +00:00
|
|
|
BIOS::PatchBIOSEnableTTY(Bus::g_bios, Bus::BIOS_SIZE, bios_hash);
|
2019-11-16 05:27:57 +00:00
|
|
|
|
|
|
|
// Load EXE late after BIOS.
|
2020-10-29 12:05:09 +00:00
|
|
|
if (exe_boot && !LoadEXE(params.filename.c_str()))
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->ReportFormattedError("Failed to load EXE file '%s'", params.filename.c_str());
|
|
|
|
Shutdown();
|
2019-11-16 05:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-29 12:05:09 +00:00
|
|
|
else if (psf_boot && !LoadPSF(params.filename.c_str()))
|
2020-04-16 14:29:56 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->ReportFormattedError("Failed to load PSF file '%s'", params.filename.c_str());
|
|
|
|
Shutdown();
|
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.
|
2020-03-12 03:51:53 +00:00
|
|
|
if (media)
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.InsertMedia(std::move(media));
|
|
|
|
if (g_cdrom.HasMedia() &&
|
|
|
|
(params.override_fast_boot.has_value() ? params.override_fast_boot.value() : g_settings.bios_patch_fast_boot))
|
2020-03-02 01:08:16 +00:00
|
|
|
{
|
2020-10-29 12:05:09 +00:00
|
|
|
BIOS::PatchBIOSFastBoot(Bus::g_bios, Bus::BIOS_SIZE, bios_hash);
|
2020-03-02 01:08:16 +00:00
|
|
|
}
|
2019-11-16 05:27:57 +00:00
|
|
|
|
|
|
|
// Good to go.
|
2020-07-31 07:09:18 +00:00
|
|
|
s_state = State::Running;
|
2019-11-16 05:27:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool 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;
|
|
|
|
|
|
|
|
s_throttle_frequency = 60.0f;
|
|
|
|
s_throttle_period = 0;
|
|
|
|
s_last_throttle_time = 0;
|
|
|
|
s_throttle_timer.Reset();
|
|
|
|
s_speed_lost_time_timestamp.Reset();
|
|
|
|
|
|
|
|
s_average_frame_time_accumulator = 0.0f;
|
|
|
|
s_worst_frame_time_accumulator = 0.0f;
|
|
|
|
|
|
|
|
s_vps = 0.0f;
|
|
|
|
s_fps = 0.0f;
|
|
|
|
s_speed = 0.0f;
|
|
|
|
s_worst_frame_time = 0.0f;
|
|
|
|
s_average_frame_time = 0.0f;
|
|
|
|
s_last_frame_number = 0;
|
|
|
|
s_last_internal_frame_number = 0;
|
|
|
|
s_last_global_tick_counter = 0;
|
|
|
|
s_fps_timer.Reset();
|
|
|
|
s_frame_timer.Reset();
|
|
|
|
|
|
|
|
TimingEvents::Initialize();
|
|
|
|
|
|
|
|
CPU::Initialize();
|
2020-10-18 04:43:55 +00:00
|
|
|
|
|
|
|
if (!Bus::Initialize())
|
|
|
|
return false;
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
if (!CreateGPU(force_software_renderer ? GPURenderer::Software : g_settings.gpu_renderer))
|
2020-06-28 15:51:01 +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
|
|
|
|
2020-10-18 04:43:55 +00:00
|
|
|
g_dma.Initialize();
|
2020-07-31 07:09:18 +00:00
|
|
|
g_interrupt_controller.Initialize();
|
2019-09-11 04:01:19 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.Initialize();
|
|
|
|
g_pad.Initialize();
|
|
|
|
g_timers.Initialize();
|
|
|
|
g_spu.Initialize();
|
|
|
|
g_mdec.Initialize();
|
|
|
|
g_sio.Initialize();
|
2020-07-17 14:25:08 +00:00
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
if (g_settings.cpu_overclock_active)
|
|
|
|
{
|
|
|
|
g_host_interface->AddFormattedOSDMessage(
|
|
|
|
10.0f,
|
|
|
|
g_host_interface->TranslateString("OSDMessage",
|
|
|
|
"CPU clock speed is set to %u%% (%u / %u). This may result in instability."),
|
|
|
|
g_settings.GetCPUOverclockPercent(), g_settings.cpu_overclock_numerator, g_settings.cpu_overclock_denominator);
|
|
|
|
}
|
|
|
|
|
2020-02-11 03:02:42 +00:00
|
|
|
UpdateThrottlePeriod();
|
2020-06-28 15:51:01 +00:00
|
|
|
return true;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void Shutdown()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_state == State::Shutdown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_sio.Shutdown();
|
|
|
|
g_mdec.Shutdown();
|
|
|
|
g_spu.Shutdown();
|
|
|
|
g_timers.Shutdown();
|
|
|
|
g_pad.Shutdown();
|
|
|
|
g_cdrom.Shutdown();
|
|
|
|
g_gpu.reset();
|
|
|
|
g_interrupt_controller.Shutdown();
|
|
|
|
g_dma.Shutdown();
|
|
|
|
CPU::CodeCache::Shutdown();
|
|
|
|
Bus::Shutdown();
|
|
|
|
CPU::Shutdown();
|
|
|
|
TimingEvents::Shutdown();
|
|
|
|
s_running_game_code.clear();
|
|
|
|
s_running_game_path.clear();
|
|
|
|
s_running_game_title.clear();
|
2020-08-15 10:38:17 +00:00
|
|
|
s_media_playlist.clear();
|
|
|
|
s_media_playlist_filename.clear();
|
2020-09-09 13:44:02 +00:00
|
|
|
s_cheat_list.reset();
|
2020-07-31 07:09:18 +00:00
|
|
|
s_state = State::Shutdown;
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool CreateGPU(GPURenderer renderer)
|
2019-10-26 02:55:56 +00:00
|
|
|
{
|
2020-01-24 04:51:53 +00:00
|
|
|
switch (renderer)
|
2019-10-26 02:55:56 +00:00
|
|
|
{
|
2019-11-16 05:27:57 +00:00
|
|
|
case GPURenderer::HardwareOpenGL:
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu = GPU::CreateHardwareOpenGLRenderer();
|
2019-10-26 02:55:56 +00:00
|
|
|
break;
|
2019-10-26 02:57:35 +00:00
|
|
|
|
2020-06-18 14:18:17 +00:00
|
|
|
case GPURenderer::HardwareVulkan:
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu = GPU::CreateHardwareVulkanRenderer();
|
2020-06-18 14:18:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-11-03 15:57:08 +00:00
|
|
|
#ifdef WIN32
|
2019-11-16 05:27:57 +00:00
|
|
|
case GPURenderer::HardwareD3D11:
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu = GPU::CreateHardwareD3D11Renderer();
|
2019-11-03 14:39:48 +00:00
|
|
|
break;
|
2019-11-03 15:57:08 +00:00
|
|
|
#endif
|
2019-11-03 14:39:48 +00:00
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
case GPURenderer::Software:
|
2019-10-26 02:57:35 +00:00
|
|
|
default:
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu = GPU::CreateSoftwareRenderer();
|
2019-10-26 02:57:35 +00:00
|
|
|
break;
|
2019-10-26 02:55:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!g_gpu || !g_gpu->Initialize(g_host_interface->GetDisplay()))
|
2019-10-26 02:57:35 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Failed to initialize GPU, falling back to software");
|
2020-07-31 07:09:18 +00:00
|
|
|
g_gpu.reset();
|
|
|
|
g_gpu = GPU::CreateSoftwareRenderer();
|
|
|
|
if (!g_gpu->Initialize(g_host_interface->GetDisplay()))
|
2019-10-26 02:57:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-26 02:55:56 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
// we put this here rather than in Initialize() because of the virtual calls
|
|
|
|
g_gpu->Reset();
|
2019-10-26 02:55:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:06:39 +00:00
|
|
|
bool DoState(StateWrapper& sw, bool update_display)
|
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
|
|
|
|
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())
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::CodeCache::Flush();
|
2019-11-19 10:30:04 +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;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("DMA") || !g_dma.DoState(sw))
|
2019-09-14 10:28:47 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("InterruptController") || !g_interrupt_controller.DoState(sw))
|
2019-09-17 06:26:00 +00:00
|
|
|
return false;
|
|
|
|
|
2020-08-22 08:16:35 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
2020-11-25 13:06:39 +00:00
|
|
|
const bool gpu_result = sw.DoMarker("GPU") && g_gpu->DoState(sw, update_display);
|
2020-08-22 08:16:35 +00:00
|
|
|
g_gpu->ResetGraphicsAPIState();
|
|
|
|
if (!gpu_result)
|
2019-09-14 10:28:47 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("CDROM") || !g_cdrom.DoState(sw))
|
2019-09-17 09:22:39 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("Pad") || !g_pad.DoState(sw))
|
2019-09-20 06:47:41 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("Timers") || !g_timers.DoState(sw))
|
2019-09-20 13:40:19 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("SPU") || !g_spu.DoState(sw))
|
2019-09-24 13:44:38 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("MDEC") || !g_mdec.DoState(sw))
|
2019-09-29 02:51:34 +00:00
|
|
|
return false;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!sw.DoMarker("SIO") || !g_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))))
|
|
|
|
{
|
|
|
|
g_host_interface->AddFormattedOSDMessage(
|
|
|
|
10.0f,
|
|
|
|
g_host_interface->TranslateString("OSDMessage",
|
|
|
|
"WARNING: CPU overclock (%u%%) was different in save state (%u%%)."),
|
|
|
|
g_settings.cpu_overclock_enable ? g_settings.GetCPUOverclockPercent() : 100u,
|
|
|
|
cpu_overclock_active ?
|
|
|
|
Settings::CPUOverclockFractionToPercent(cpu_overclock_numerator, cpu_overclock_denominator) :
|
|
|
|
100u);
|
|
|
|
UpdateOverclock();
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:28:47 +00:00
|
|
|
return !sw.HasError();
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void Reset()
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2020-08-01 06:45:06 +00:00
|
|
|
if (IsShutdown())
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::Reset();
|
|
|
|
CPU::CodeCache::Flush();
|
|
|
|
Bus::Reset();
|
|
|
|
g_dma.Reset();
|
|
|
|
g_interrupt_controller.Reset();
|
|
|
|
g_gpu->Reset();
|
|
|
|
g_cdrom.Reset();
|
|
|
|
g_pad.Reset();
|
|
|
|
g_timers.Reset();
|
|
|
|
g_spu.Reset();
|
|
|
|
g_mdec.Reset();
|
|
|
|
g_sio.Reset();
|
|
|
|
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
|
|
|
|
|
|
|
g_gpu->ResetGraphicsAPIState();
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 13:06:39 +00:00
|
|
|
bool LoadState(ByteStream* state, bool update_display)
|
2020-05-26 16:01:09 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (IsShutdown())
|
|
|
|
return false;
|
|
|
|
|
2020-11-25 13:06:39 +00:00
|
|
|
return DoLoadState(state, false, update_display);
|
2020-05-26 16:01:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 13:06:39 +00:00
|
|
|
bool DoLoadState(ByteStream* state, bool force_software_renderer, bool update_display)
|
2019-09-14 10:28:47 +00:00
|
|
|
{
|
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-10-12 10:46:59 +00:00
|
|
|
if (header.version < SAVE_STATE_MINIMUM_VERSION)
|
2020-04-20 16:50:45 +00:00
|
|
|
{
|
2020-08-22 03:01:52 +00:00
|
|
|
g_host_interface->ReportFormattedError(
|
|
|
|
g_host_interface->TranslateString("System",
|
2020-10-12 10:46:59 +00:00
|
|
|
"Save state is incompatible: minimum version is %u but state is version %u."),
|
|
|
|
SAVE_STATE_MINIMUM_VERSION, header.version);
|
2020-04-20 16:50:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
media = g_cdrom.RemoveMedia();
|
2020-07-21 14:02:37 +00:00
|
|
|
if (!media || media->GetFileName() != media_filename)
|
2020-05-26 16:01:09 +00:00
|
|
|
{
|
2020-07-22 16:35:23 +00:00
|
|
|
media = OpenCDImage(media_filename.c_str(), false);
|
2020-07-21 14:02:37 +00:00
|
|
|
if (!media)
|
|
|
|
{
|
2020-08-22 03:01:52 +00:00
|
|
|
g_host_interface->ReportFormattedError(
|
|
|
|
g_host_interface->TranslateString("System", "Failed to open CD image from save state: '%s'."),
|
|
|
|
media_filename.c_str());
|
2020-07-21 14:02:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-26 16:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
std::string playlist_filename;
|
|
|
|
std::vector<std::string> playlist_entries;
|
|
|
|
if (header.playlist_filename_length > 0)
|
|
|
|
{
|
|
|
|
playlist_filename.resize(header.offset_to_playlist_filename);
|
|
|
|
if (!state->SeekAbsolute(header.offset_to_playlist_filename) ||
|
|
|
|
!state->Read2(playlist_filename.data(), header.playlist_filename_length))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:29:22 +00:00
|
|
|
playlist_entries = ParseM3UFile(playlist_filename.c_str());
|
2020-08-15 10:38:17 +00:00
|
|
|
if (playlist_entries.empty())
|
|
|
|
{
|
|
|
|
g_host_interface->ReportFormattedError("Failed to load save state playlist entries from '%s'",
|
|
|
|
playlist_filename.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:01:09 +00:00
|
|
|
UpdateRunningGame(media_filename.c_str(), media.get());
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_state == State::Starting)
|
2020-05-26 16:01:09 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!Initialize(force_software_renderer))
|
2020-06-28 15:51:01 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-26 16:01:09 +00:00
|
|
|
if (media)
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.InsertMedia(std::move(media));
|
2020-08-15 10:38:17 +00:00
|
|
|
|
|
|
|
s_media_playlist_filename = std::move(playlist_filename);
|
|
|
|
s_media_playlist = std::move(playlist_entries);
|
|
|
|
|
|
|
|
UpdateControllers();
|
|
|
|
UpdateMemoryCards();
|
2020-05-26 16:01:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.Reset();
|
2020-05-26 16:01:09 +00:00
|
|
|
if (media)
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.InsertMedia(std::move(media));
|
2020-05-26 16:01:09 +00:00
|
|
|
else
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.RemoveMedia();
|
2020-05-26 16:01:09 +00:00
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
s_media_playlist_filename = std::move(playlist_filename);
|
|
|
|
s_media_playlist = std::move(playlist_entries);
|
|
|
|
|
2020-05-26 16:01:09 +00:00
|
|
|
// ensure the correct card is loaded
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_settings.HasAnyPerGameMemoryCards())
|
2020-05-26 16:01:09 +00:00
|
|
|
UpdateMemoryCards();
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:50:45 +00:00
|
|
|
if (header.data_compression_type != 0)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->ReportFormattedError("Unknown save state compression type %u", header.data_compression_type);
|
2020-04-20 16:50:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state->SeekAbsolute(header.offset_to_data))
|
|
|
|
return false;
|
|
|
|
|
2020-10-12 10:46:59 +00:00
|
|
|
StateWrapper sw(state, StateWrapper::Mode::Read, header.version);
|
2020-11-25 13:06:39 +00:00
|
|
|
if (!DoState(sw, update_display))
|
2020-07-31 07:09:18 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (s_state == State::Starting)
|
|
|
|
s_state = State::Running;
|
|
|
|
|
|
|
|
return true;
|
2019-09-14 10:28:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool SaveState(ByteStream* state, u32 screenshot_size /* = 128 */)
|
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));
|
|
|
|
StringUtil::Strlcpy(header.game_code, s_running_game_code.c_str(), sizeof(header.game_code));
|
2020-05-26 16:01:09 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_cdrom.HasMedia())
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
const std::string& media_filename = g_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());
|
|
|
|
if (!media_filename.empty() && !state->Write2(media_filename.data(), header.media_filename_length))
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-20 16:50:45 +00:00
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
if (!s_media_playlist_filename.empty())
|
|
|
|
{
|
|
|
|
header.offset_to_playlist_filename = static_cast<u32>(state->GetPosition());
|
|
|
|
header.playlist_filename_length = static_cast<u32>(s_media_playlist_filename.length());
|
|
|
|
if (!state->Write2(s_media_playlist_filename.data(), header.playlist_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
|
|
|
{
|
|
|
|
std::vector<u32> screenshot_buffer;
|
2020-08-01 06:45:06 +00:00
|
|
|
if (g_host_interface->GetDisplay()->WriteDisplayTextureToBuffer(&screenshot_buffer, screenshot_size,
|
|
|
|
screenshot_size) &&
|
|
|
|
!screenshot_buffer.empty())
|
2020-04-20 16:50:45 +00:00
|
|
|
{
|
|
|
|
header.offset_to_screenshot = static_cast<u32>(state->GetPosition());
|
2020-06-22 07:05:02 +00:00
|
|
|
header.screenshot_width = screenshot_size;
|
|
|
|
header.screenshot_height = screenshot_size;
|
2020-04-20 16:50:45 +00:00
|
|
|
header.screenshot_size = static_cast<u32>(screenshot_buffer.size() * sizeof(u32));
|
|
|
|
if (!state->Write2(screenshot_buffer.data(), header.screenshot_size))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write data
|
|
|
|
{
|
|
|
|
header.offset_to_data = static_cast<u32>(state->GetPosition());
|
|
|
|
|
2020-08-01 06:45:06 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
2020-10-12 10:46:59 +00:00
|
|
|
StateWrapper sw(state, StateWrapper::Mode::Write, SAVE_STATE_VERSION);
|
2020-11-25 13:06:39 +00:00
|
|
|
const bool result = DoState(sw, false);
|
2020-08-01 06:45:06 +00:00
|
|
|
|
|
|
|
g_gpu->ResetGraphicsAPIState();
|
|
|
|
|
|
|
|
if (!result)
|
2020-04-20 16:50:45 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
header.data_compression_type = 0;
|
|
|
|
header.data_uncompressed_size = static_cast<u32>(state->GetPosition() - header.offset_to_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void RunFrame()
|
2019-09-09 07:01:26 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
s_frame_timer.Reset();
|
2020-02-09 13:16:25 +00:00
|
|
|
|
2020-08-01 06:45:06 +00:00
|
|
|
g_gpu->RestoreGraphicsAPIState();
|
|
|
|
|
2020-08-08 05:42:11 +00:00
|
|
|
switch (g_settings.cpu_execution_mode)
|
|
|
|
{
|
|
|
|
case CPUExecutionMode::Recompiler:
|
|
|
|
#ifdef WITH_RECOMPILER
|
|
|
|
CPU::CodeCache::ExecuteRecompiler();
|
|
|
|
#else
|
|
|
|
CPU::CodeCache::Execute();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPUExecutionMode::CachedInterpreter:
|
|
|
|
CPU::CodeCache::Execute();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CPUExecutionMode::Interpreter:
|
|
|
|
default:
|
|
|
|
CPU::Execute();
|
|
|
|
break;
|
|
|
|
}
|
2020-01-24 04:53:40 +00:00
|
|
|
|
|
|
|
// Generate any pending samples from the SPU before sleeping, this way we reduce the chances of underruns.
|
2020-07-31 07:09:18 +00:00
|
|
|
g_spu.GeneratePendingSamples();
|
2020-08-01 06:45:06 +00:00
|
|
|
|
2020-09-09 12:11:28 +00:00
|
|
|
if (s_cheat_list)
|
|
|
|
s_cheat_list->Apply();
|
|
|
|
|
2020-08-01 06:45:06 +00:00
|
|
|
g_gpu->ResetGraphicsAPIState();
|
2020-02-09 13:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:21:11 +00:00
|
|
|
void SetTargetSpeed(float speed)
|
|
|
|
{
|
|
|
|
s_target_speed = speed;
|
|
|
|
UpdateThrottlePeriod();
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void SetThrottleFrequency(float frequency)
|
2020-02-11 03:02:42 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
s_throttle_frequency = frequency;
|
2020-02-11 03:02:42 +00:00
|
|
|
UpdateThrottlePeriod();
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void UpdateThrottlePeriod()
|
2020-02-11 03:02:42 +00:00
|
|
|
{
|
2020-11-03 11:21:11 +00:00
|
|
|
s_throttle_period =
|
|
|
|
static_cast<s32>(1000000000.0 / static_cast<double>(s_throttle_frequency) / static_cast<double>(s_target_speed));
|
2020-02-11 03:02:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void Throttle()
|
2020-02-09 13:16:25 +00:00
|
|
|
{
|
|
|
|
// Allow variance of up to 40ms either way.
|
|
|
|
constexpr s64 MAX_VARIANCE_TIME = INT64_C(40000000);
|
|
|
|
|
|
|
|
// Don't sleep for <1ms or >=period.
|
|
|
|
constexpr s64 MINIMUM_SLEEP_TIME = INT64_C(1000000);
|
|
|
|
|
|
|
|
// Use unsigned for defined overflow/wrap-around.
|
2020-07-31 07:09:18 +00:00
|
|
|
const u64 time = static_cast<u64>(s_throttle_timer.GetTimeNanoseconds());
|
|
|
|
const s64 sleep_time = static_cast<s64>(s_last_throttle_time - time);
|
2020-02-11 03:02:42 +00:00
|
|
|
if (sleep_time < -MAX_VARIANCE_TIME)
|
2020-02-09 13:16:25 +00:00
|
|
|
{
|
|
|
|
#ifndef _DEBUG
|
|
|
|
// Don't display the slow messages in debug, it'll always be slow...
|
|
|
|
// Limit how often the messages are displayed.
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_speed_lost_time_timestamp.GetTimeSeconds() >= 1.0f)
|
2020-02-09 13:16:25 +00:00
|
|
|
{
|
2020-02-11 03:02:42 +00:00
|
|
|
Log_WarningPrintf("System too slow, lost %.2f ms",
|
|
|
|
static_cast<double>(-sleep_time - MAX_VARIANCE_TIME) / 1000000.0);
|
2020-07-31 07:09:18 +00:00
|
|
|
s_speed_lost_time_timestamp.Reset();
|
2020-02-09 13:16:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-07-31 07:09:18 +00:00
|
|
|
s_last_throttle_time = 0;
|
|
|
|
s_throttle_timer.Reset();
|
2020-02-09 13:16:25 +00:00
|
|
|
}
|
2020-11-02 08:47:17 +00:00
|
|
|
else if (sleep_time >= MINIMUM_SLEEP_TIME)
|
2020-02-09 13:16:25 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
Sleep(static_cast<u32>(sleep_time / 1000000));
|
|
|
|
#else
|
|
|
|
const struct timespec ts = {0, static_cast<long>(sleep_time)};
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
s_last_throttle_time += s_throttle_period;
|
2020-02-09 13:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void UpdatePerformanceCounters()
|
2020-02-09 13:16:25 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
const float frame_time = static_cast<float>(s_frame_timer.GetTimeMilliseconds());
|
|
|
|
s_average_frame_time_accumulator += frame_time;
|
|
|
|
s_worst_frame_time_accumulator = std::max(s_worst_frame_time_accumulator, frame_time);
|
2020-02-09 13:16:25 +00:00
|
|
|
|
|
|
|
// update fps counter
|
2020-07-31 07:09:18 +00:00
|
|
|
const float time = static_cast<float>(s_fps_timer.GetTimeSeconds());
|
2020-02-09 13:16:25 +00:00
|
|
|
if (time < 1.0f)
|
|
|
|
return;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
const float frames_presented = static_cast<float>(s_frame_number - s_last_frame_number);
|
|
|
|
const u32 global_tick_counter = TimingEvents::GetGlobalTickCounter();
|
|
|
|
|
|
|
|
s_worst_frame_time = s_worst_frame_time_accumulator;
|
|
|
|
s_worst_frame_time_accumulator = 0.0f;
|
|
|
|
s_average_frame_time = s_average_frame_time_accumulator / frames_presented;
|
|
|
|
s_average_frame_time_accumulator = 0.0f;
|
|
|
|
s_vps = static_cast<float>(frames_presented / time);
|
|
|
|
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;
|
|
|
|
s_fps_timer.Reset();
|
2020-02-09 13:16:25 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->OnSystemPerformanceCountersUpdated();
|
2020-02-09 13:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void 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();
|
|
|
|
s_average_frame_time_accumulator = 0.0f;
|
|
|
|
s_worst_frame_time_accumulator = 0.0f;
|
|
|
|
s_fps_timer.Reset();
|
|
|
|
s_throttle_timer.Reset();
|
|
|
|
s_last_throttle_time = 0;
|
2019-09-09 07:01:26 +00:00
|
|
|
}
|
2019-09-14 03:22:05 +00:00
|
|
|
|
2020-10-29 12:05:09 +00:00
|
|
|
bool LoadEXE(const char* filename)
|
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)
|
|
|
|
return false;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.file_size >= 4)
|
|
|
|
{
|
2019-12-04 11:12:50 +00:00
|
|
|
std::vector<u32> data_words((header.file_size + 3) / 4);
|
2019-09-14 03:22:05 +00:00
|
|
|
if (std::fread(data_words.data(), header.file_size, 1, fp) != 1)
|
|
|
|
{
|
|
|
|
std::fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 num_words = header.file_size / 4;
|
|
|
|
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
|
|
|
|
2020-10-29 12:05:09 +00:00
|
|
|
bool LoadEXEFromBuffer(const void* buffer, u32 buffer_size)
|
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);
|
|
|
|
|
|
|
|
if (!BIOS::IsValidPSExeHeader(header, static_cast<u32>(buffer_end - buffer_ptr)))
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.file_size >= 4)
|
|
|
|
{
|
|
|
|
std::vector<u32> data_words((header.file_size + 3) / 4);
|
|
|
|
if ((buffer_end - buffer_ptr) < header.file_size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::memcpy(data_words.data(), buffer_ptr, header.file_size);
|
|
|
|
|
|
|
|
const u32 num_words = header.file_size / 4;
|
|
|
|
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
|
|
|
|
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;
|
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);
|
2020-04-16 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 12:05:09 +00:00
|
|
|
bool LoadPSF(const char* filename)
|
2020-04-16 14:29:56 +00:00
|
|
|
{
|
|
|
|
Log_InfoPrintf("Loading PSF file from '%s'", filename);
|
|
|
|
|
|
|
|
PSFLoader::File psf;
|
|
|
|
if (!psf.Load(filename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::vector<u8>& exe_data = psf.GetProgramData();
|
2020-10-29 12:05:09 +00:00
|
|
|
return LoadEXEFromBuffer(exe_data.data(), static_cast<u32>(exe_data.size()));
|
2020-04-16 14:29:56 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void StallCPU(TickCount ticks)
|
2019-10-17 14:20:38 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
CPU::AddPendingTicks(ticks);
|
2020-04-29 06:52:15 +00:00
|
|
|
#if 0
|
2020-07-31 07:09:18 +00:00
|
|
|
if (CPU::GetPendingTicks() >= CPU::GetDowncount() && !m_running_events)
|
2020-01-24 04:53:40 +00:00
|
|
|
RunEvents();
|
2020-04-29 06:52:15 +00:00
|
|
|
#endif
|
2019-10-17 14:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
Controller* GetController(u32 slot)
|
2019-09-20 06:47:41 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
return g_pad.GetController(slot);
|
2019-12-14 13:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void UpdateControllers()
|
2019-12-14 13:20:24 +00:00
|
|
|
{
|
2020-01-10 03:31:12 +00:00
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_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
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
controller->LoadSettings(TinyString::FromFormat("Controller%u", i + 1u));
|
|
|
|
g_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
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void UpdateControllerSettings()
|
2020-06-30 14:33:45 +00:00
|
|
|
{
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
Controller* controller = g_pad.GetController(i);
|
2020-06-30 14:33:45 +00:00
|
|
|
if (controller)
|
2020-07-31 07:09:18 +00:00
|
|
|
controller->LoadSettings(TinyString::FromFormat("Controller%u", i + 1u));
|
2020-06-30 14:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void ResetControllers()
|
2020-07-04 14:05:14 +00:00
|
|
|
{
|
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
Controller* controller = g_pad.GetController(i);
|
2020-07-04 14:05:14 +00:00
|
|
|
if (controller)
|
|
|
|
controller->Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void UpdateMemoryCards()
|
2019-09-29 15:07:38 +00:00
|
|
|
{
|
2019-12-16 06:46:43 +00:00
|
|
|
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
|
2019-10-27 06:45:23 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_pad.SetMemoryCard(i, nullptr);
|
2020-03-29 14:14:15 +00:00
|
|
|
|
2020-04-27 06:15:38 +00:00
|
|
|
std::unique_ptr<MemoryCard> card;
|
2020-07-31 07:09:18 +00:00
|
|
|
const MemoryCardType type = g_settings.memory_card_types[i];
|
2020-07-09 03:29:58 +00:00
|
|
|
switch (type)
|
2020-04-27 06:15:38 +00:00
|
|
|
{
|
|
|
|
case MemoryCardType::None:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case MemoryCardType::PerGame:
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (s_running_game_code.empty())
|
2020-04-27 06:15:38 +00:00
|
|
|
{
|
2020-08-22 03:01:52 +00:00
|
|
|
g_host_interface->AddFormattedOSDMessage(
|
|
|
|
5.0f,
|
|
|
|
g_host_interface->TranslateString("System",
|
|
|
|
"Per-game memory card cannot be used for slot %u as the running "
|
|
|
|
"game has no code. Using shared card instead."),
|
|
|
|
i + 1u);
|
2020-07-31 07:09:18 +00:00
|
|
|
card = MemoryCard::Open(g_host_interface->GetSharedMemoryCardPath(i));
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
card = MemoryCard::Open(g_host_interface->GetGameMemoryCardPath(s_running_game_code.c_str(), i));
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-04-27 15:04:10 +00:00
|
|
|
case MemoryCardType::PerGameTitle:
|
|
|
|
{
|
2020-08-15 10:38:54 +00:00
|
|
|
if (!s_media_playlist_filename.empty() && g_settings.memory_card_use_playlist_title)
|
|
|
|
{
|
2020-09-01 02:29:22 +00:00
|
|
|
const std::string playlist_title(GetTitleForPath(s_media_playlist_filename.c_str()));
|
2020-08-15 10:38:54 +00:00
|
|
|
card = MemoryCard::Open(g_host_interface->GetGameMemoryCardPath(playlist_title.c_str(), i));
|
|
|
|
}
|
|
|
|
else if (s_running_game_title.empty())
|
2020-04-27 15:04:10 +00:00
|
|
|
{
|
2020-08-22 03:01:52 +00:00
|
|
|
g_host_interface->AddFormattedOSDMessage(
|
|
|
|
5.0f,
|
|
|
|
g_host_interface->TranslateString("System",
|
|
|
|
"Per-game memory card cannot be used for slot %u as the running "
|
|
|
|
"game has no title. Using shared card instead."),
|
|
|
|
i + 1u);
|
2020-07-31 07:09:18 +00:00
|
|
|
card = MemoryCard::Open(g_host_interface->GetSharedMemoryCardPath(i));
|
2020-04-27 15:04:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
card = MemoryCard::Open(g_host_interface->GetGameMemoryCardPath(s_running_game_title.c_str(), i));
|
2020-04-27 15:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-04-27 06:15:38 +00:00
|
|
|
case MemoryCardType::Shared:
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_settings.memory_card_paths[i].empty())
|
2020-04-27 06:15:38 +00:00
|
|
|
{
|
2020-08-22 03:01:52 +00:00
|
|
|
g_host_interface->AddFormattedOSDMessage(
|
|
|
|
10.0f,
|
|
|
|
g_host_interface->TranslateString("System", "Memory card path for slot %u is missing, using default."),
|
|
|
|
i + 1u);
|
2020-07-31 07:09:18 +00:00
|
|
|
card = MemoryCard::Open(g_host_interface->GetSharedMemoryCardPath(i));
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
card = MemoryCard::Open(g_settings.memory_card_paths[i]);
|
2020-04-27 06:15:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-27 06:45:23 +00:00
|
|
|
if (card)
|
2020-07-31 07:09:18 +00:00
|
|
|
g_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
|
|
|
|
2020-08-16 13:20:36 +00:00
|
|
|
bool DumpRAM(const char* filename)
|
|
|
|
{
|
|
|
|
auto fp = FileSystem::OpenManagedCFile(filename, "wb");
|
|
|
|
if (!fp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return std::fwrite(Bus::g_ram, Bus::RAM_SIZE, 1, fp.get()) == 1;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool HasMedia()
|
2019-09-20 10:14:00 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
return g_cdrom.HasMedia();
|
2019-09-20 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool InsertMedia(const char* path)
|
2019-09-20 10:14:00 +00:00
|
|
|
{
|
2020-07-22 16:35:23 +00:00
|
|
|
std::unique_ptr<CDImage> image = OpenCDImage(path, false);
|
2019-11-16 05:27:57 +00:00
|
|
|
if (!image)
|
|
|
|
return false;
|
|
|
|
|
2020-01-24 04:51:05 +00:00
|
|
|
UpdateRunningGame(path, image.get());
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.InsertMedia(std::move(image));
|
|
|
|
Log_InfoPrintf("Inserted media from %s (%s, %s)", s_running_game_path.c_str(), s_running_game_code.c_str(),
|
|
|
|
s_running_game_title.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
|
|
|
{
|
2020-08-22 03:01:52 +00:00
|
|
|
g_host_interface->AddOSDMessage(
|
|
|
|
g_host_interface->TranslateStdString("System", "Game changed, reloading memory cards."), 10.0f);
|
2020-04-27 06:15:38 +00:00
|
|
|
UpdateMemoryCards();
|
|
|
|
}
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
return true;
|
2019-09-20 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void RemoveMedia()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.RemoveMedia();
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void UpdateRunningGame(const char* path, CDImage* image)
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2020-08-20 11:30:11 +00:00
|
|
|
if (s_running_game_path == path)
|
|
|
|
return;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
s_running_game_path.clear();
|
|
|
|
s_running_game_code.clear();
|
|
|
|
s_running_game_title.clear();
|
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;
|
|
|
|
g_host_interface->GetGameInfo(path, image, &s_running_game_code, &s_running_game_title);
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
g_host_interface->OnRunningGameChanged();
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 10:38:54 +00:00
|
|
|
bool HasMediaPlaylist()
|
|
|
|
{
|
|
|
|
return !s_media_playlist_filename.empty();
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
u32 GetMediaPlaylistCount()
|
2020-01-24 04:53:40 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
return static_cast<u32>(s_media_playlist.size());
|
2020-01-24 04:53:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
const std::string& GetMediaPlaylistPath(u32 index)
|
2020-01-24 04:51:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
return s_media_playlist[index];
|
2020-01-24 04:51:05 +00:00
|
|
|
}
|
2020-07-22 16:36:05 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
u32 GetMediaPlaylistIndex()
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!g_cdrom.HasMedia())
|
2020-07-22 16:36:05 +00:00
|
|
|
return std::numeric_limits<u32>::max();
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
const std::string& media_path = g_cdrom.GetMediaFileName();
|
2020-08-15 10:38:17 +00:00
|
|
|
for (u32 i = 0; i < static_cast<u32>(s_media_playlist.size()); i++)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
if (s_media_playlist[i] == media_path)
|
2020-07-22 16:36:05 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::numeric_limits<u32>::max();
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool AddMediaPathToPlaylist(const std::string_view& path)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
if (std::any_of(s_media_playlist.begin(), s_media_playlist.end(),
|
2020-07-22 16:36:05 +00:00
|
|
|
[&path](const std::string& p) { return (path == p); }))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
s_media_playlist.emplace_back(path);
|
2020-07-22 16:36:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool RemoveMediaPathFromPlaylist(const std::string_view& path)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
for (u32 i = 0; i < static_cast<u32>(s_media_playlist.size()); i++)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
if (path == s_media_playlist[i])
|
2020-07-22 16:36:05 +00:00
|
|
|
return RemoveMediaPathFromPlaylist(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool RemoveMediaPathFromPlaylist(u32 index)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
if (index >= static_cast<u32>(s_media_playlist.size()))
|
2020-07-22 16:36:05 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (GetMediaPlaylistIndex() == index)
|
|
|
|
{
|
2020-10-22 09:07:48 +00:00
|
|
|
g_host_interface->AddFormattedOSDMessage(
|
|
|
|
10.0f,
|
|
|
|
g_host_interface->TranslateString("System", "Removing current media from playlist, removing media from CD-ROM."));
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.RemoveMedia();
|
2020-07-22 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
s_media_playlist.erase(s_media_playlist.begin() + index);
|
2020-07-22 16:36:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool ReplaceMediaPathFromPlaylist(u32 index, const std::string_view& path)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
if (index >= static_cast<u32>(s_media_playlist.size()))
|
2020-07-22 16:36:05 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (GetMediaPlaylistIndex() == index)
|
|
|
|
{
|
2020-10-22 09:07:48 +00:00
|
|
|
g_host_interface->AddFormattedOSDMessage(
|
|
|
|
10.0f,
|
|
|
|
g_host_interface->TranslateString("System", "Changing current media from playlist, replacing current media."));
|
2020-07-31 07:09:18 +00:00
|
|
|
g_cdrom.RemoveMedia();
|
2020-07-22 16:36:05 +00:00
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
s_media_playlist[index] = path;
|
|
|
|
InsertMedia(s_media_playlist[index].c_str());
|
2020-07-22 16:36:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
s_media_playlist[index] = path;
|
2020-07-22 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool SwitchMediaFromPlaylist(u32 index)
|
2020-07-22 16:36:05 +00:00
|
|
|
{
|
2020-08-15 10:38:17 +00:00
|
|
|
if (index >= s_media_playlist.size())
|
2020-07-22 16:36:05 +00:00
|
|
|
return false;
|
|
|
|
|
2020-08-15 10:38:17 +00:00
|
|
|
const std::string& path = s_media_playlist[index];
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_cdrom.HasMedia() && g_cdrom.GetMediaFileName() == path)
|
2020-07-22 16:36:05 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return InsertMedia(path.c_str());
|
|
|
|
}
|
2020-07-31 07:09:18 +00:00
|
|
|
|
2020-09-09 12:11:28 +00:00
|
|
|
bool HasCheatList()
|
|
|
|
{
|
|
|
|
return static_cast<bool>(s_cheat_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
CheatList* GetCheatList()
|
|
|
|
{
|
|
|
|
return s_cheat_list.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyCheatCode(const CheatCode& code)
|
|
|
|
{
|
|
|
|
Assert(!IsShutdown());
|
|
|
|
code.Apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCheatList(std::unique_ptr<CheatList> cheats)
|
|
|
|
{
|
|
|
|
Assert(!IsShutdown());
|
|
|
|
s_cheat_list = std::move(cheats);
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
} // namespace System
|