2019-09-14 10:28:47 +00:00
|
|
|
#include "host_interface.h"
|
|
|
|
#include "YBaseLib/ByteStream.h"
|
2019-09-14 11:54:46 +00:00
|
|
|
#include "YBaseLib/Log.h"
|
2019-11-16 10:12:03 +00:00
|
|
|
#include "YBaseLib/Timer.h"
|
2019-11-16 05:27:57 +00:00
|
|
|
#include "bios.h"
|
2019-10-10 16:20:21 +00:00
|
|
|
#include "common/audio_stream.h"
|
2019-11-07 15:07:39 +00:00
|
|
|
#include "host_display.h"
|
2019-09-14 10:28:47 +00:00
|
|
|
#include "system.h"
|
2019-11-16 05:27:57 +00:00
|
|
|
#include <filesystem>
|
2019-09-14 11:54:46 +00:00
|
|
|
Log_SetChannel(HostInterface);
|
2019-09-14 10:28:47 +00:00
|
|
|
|
2019-11-16 10:12:03 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "YBaseLib/Windows/WindowsHeaders.h"
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2019-11-27 15:55:33 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
static std::string GetRelativePath(const std::string& path, const char* new_filename)
|
|
|
|
{
|
|
|
|
const char* last = std::strrchr(path.c_str(), '/');
|
|
|
|
if (!last)
|
|
|
|
return new_filename;
|
|
|
|
|
|
|
|
std::string new_path(path.c_str(), last - path.c_str() + 1);
|
|
|
|
new_path += new_filename;
|
|
|
|
return new_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
static std::string GetRelativePath(const std::string& path, const char* new_filename)
|
|
|
|
{
|
|
|
|
return std::filesystem::path(path).replace_filename(new_filename).string();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-11-03 14:39:48 +00:00
|
|
|
HostInterface::HostInterface()
|
|
|
|
{
|
2019-11-07 15:07:39 +00:00
|
|
|
m_settings.SetDefaults();
|
2019-11-16 10:12:03 +00:00
|
|
|
m_last_throttle_time = Y_TimerGetValue();
|
2019-11-03 14:39:48 +00:00
|
|
|
}
|
2019-09-14 10:28:47 +00:00
|
|
|
|
|
|
|
HostInterface::~HostInterface() = default;
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
bool HostInterface::CreateSystem()
|
2019-09-14 11:54:46 +00:00
|
|
|
{
|
2019-11-16 05:27:57 +00:00
|
|
|
m_system = System::Create(this);
|
2019-09-14 11:54:46 +00:00
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
// Pull in any invalid settings which have been reset.
|
|
|
|
m_settings = m_system->GetSettings();
|
|
|
|
m_paused = true;
|
2019-11-16 10:12:03 +00:00
|
|
|
UpdateSpeedLimiterState();
|
2019-11-16 05:27:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-14 11:54:46 +00:00
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
bool HostInterface::BootSystem(const char* filename, const char* state_filename)
|
|
|
|
{
|
|
|
|
if (!m_system->Boot(filename))
|
|
|
|
return false;
|
2019-09-22 15:28:00 +00:00
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
m_paused = m_settings.start_paused;
|
|
|
|
ConnectControllers();
|
2019-11-16 10:12:03 +00:00
|
|
|
UpdateSpeedLimiterState();
|
2019-11-17 09:41:21 +00:00
|
|
|
|
|
|
|
if (state_filename && !LoadState(state_filename))
|
|
|
|
return false;
|
|
|
|
|
2019-09-14 11:54:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
void HostInterface::DestroySystem()
|
2019-11-07 15:07:39 +00:00
|
|
|
{
|
|
|
|
m_system.reset();
|
|
|
|
m_paused = false;
|
2019-11-16 10:12:03 +00:00
|
|
|
UpdateSpeedLimiterState();
|
2019-11-07 15:07:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
void HostInterface::ReportError(const char* message)
|
|
|
|
{
|
|
|
|
Log_ErrorPrint(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HostInterface::ReportMessage(const char* message)
|
|
|
|
{
|
|
|
|
Log_InfoPrintf(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::vector<u8>> HostInterface::GetBIOSImage(ConsoleRegion region)
|
|
|
|
{
|
|
|
|
// Try the other default filenames in the directory of the configured BIOS.
|
|
|
|
#define TRY_FILENAME(filename) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
std::string try_filename = filename; \
|
|
|
|
std::optional<BIOS::Image> found_image = BIOS::LoadImageFromFile(try_filename); \
|
|
|
|
BIOS::Hash found_hash = BIOS::GetHash(*found_image); \
|
|
|
|
Log_DevPrintf("Hash for BIOS '%s': %s", try_filename.c_str(), found_hash.ToString().c_str()); \
|
|
|
|
if (BIOS::IsValidHashForRegion(region, found_hash)) \
|
|
|
|
{ \
|
|
|
|
Log_InfoPrintf("Using BIOS from '%s'", try_filename.c_str()); \
|
|
|
|
return found_image; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
// Try the configured image.
|
|
|
|
TRY_FILENAME(m_settings.bios_path);
|
|
|
|
|
|
|
|
// Try searching in the same folder for other region's images.
|
|
|
|
switch (region)
|
|
|
|
{
|
|
|
|
case ConsoleRegion::NTSC_J:
|
2019-11-27 15:55:33 +00:00
|
|
|
TRY_FILENAME(GetRelativePath(m_settings.bios_path, "scph1000.bin"));
|
|
|
|
TRY_FILENAME(GetRelativePath(m_settings.bios_path, "scph5500.bin"));
|
2019-11-16 05:27:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ConsoleRegion::NTSC_U:
|
2019-11-27 15:55:33 +00:00
|
|
|
TRY_FILENAME(GetRelativePath(m_settings.bios_path, "scph1001.bin"));
|
|
|
|
TRY_FILENAME(GetRelativePath(m_settings.bios_path, "scph5501.bin"));
|
2019-11-16 05:27:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ConsoleRegion::PAL:
|
2019-11-27 15:55:33 +00:00
|
|
|
TRY_FILENAME(GetRelativePath(m_settings.bios_path, "scph1002.bin"));
|
|
|
|
TRY_FILENAME(GetRelativePath(m_settings.bios_path, "scph5502.bin"));
|
2019-11-16 05:27:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef RELATIVE_PATH
|
|
|
|
#undef TRY_FILENAME
|
|
|
|
|
|
|
|
// Fall back to the default image.
|
|
|
|
Log_WarningPrintf("No suitable BIOS image for region %s could be located, using configured image '%s'. This may "
|
|
|
|
"result in instability.",
|
|
|
|
Settings::GetConsoleRegionName(region), m_settings.bios_path.c_str());
|
|
|
|
return BIOS::LoadImageFromFile(m_settings.bios_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HostInterface::ConnectControllers() {}
|
|
|
|
|
2019-11-16 10:12:03 +00:00
|
|
|
void HostInterface::Throttle()
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
const u64 time = static_cast<u64>(m_throttle_timer.GetTimeNanoseconds());
|
|
|
|
const s64 sleep_time = static_cast<s64>(m_last_throttle_time - time);
|
|
|
|
if (std::abs(sleep_time) >= MAX_VARIANCE_TIME)
|
|
|
|
{
|
|
|
|
#ifdef Y_BUILD_CONFIG_RELEASE
|
|
|
|
// Don't display the slow messages in debug, it'll always be slow...
|
|
|
|
// Limit how often the messages are displayed.
|
|
|
|
if (m_speed_lost_time_timestamp.GetTimeSeconds() >= 1.0f)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("System too %s, lost %.2f ms", sleep_time < 0 ? "slow" : "fast",
|
|
|
|
static_cast<double>(std::abs(sleep_time) - MAX_VARIANCE_TIME) / 1000000.0);
|
|
|
|
m_speed_lost_time_timestamp.Reset();
|
|
|
|
}
|
|
|
|
#endif
|
2019-11-16 15:47:50 +00:00
|
|
|
m_last_throttle_time = 0;
|
|
|
|
m_throttle_timer.Reset();
|
2019-11-16 10:12:03 +00:00
|
|
|
}
|
|
|
|
else if (sleep_time >= MINIMUM_SLEEP_TIME && sleep_time <= m_throttle_period)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
Sleep(static_cast<u32>(sleep_time / 1000000));
|
|
|
|
#else
|
|
|
|
const struct timespec ts = {0, static_cast<long>(sleep_time)};
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
m_last_throttle_time += m_throttle_period;
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:28:47 +00:00
|
|
|
bool HostInterface::LoadState(const char* filename)
|
|
|
|
{
|
|
|
|
ByteStream* stream;
|
|
|
|
if (!ByteStream_OpenFileStream(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED, &stream))
|
|
|
|
return false;
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
AddOSDMessage(SmallString::FromFormat("Loading state from %s...", filename));
|
2019-09-14 10:28:47 +00:00
|
|
|
|
|
|
|
const bool result = m_system->LoadState(stream);
|
|
|
|
if (!result)
|
|
|
|
{
|
2019-11-16 05:27:57 +00:00
|
|
|
ReportError(SmallString::FromFormat("Loading state from %s failed. Resetting.", filename));
|
2019-09-14 10:28:47 +00:00
|
|
|
m_system->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->Release();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HostInterface::SaveState(const char* filename)
|
|
|
|
{
|
|
|
|
ByteStream* stream;
|
|
|
|
if (!ByteStream_OpenFileStream(filename,
|
|
|
|
BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_WRITE | BYTESTREAM_OPEN_TRUNCATE |
|
|
|
|
BYTESTREAM_OPEN_ATOMIC_UPDATE | BYTESTREAM_OPEN_STREAMED,
|
|
|
|
&stream))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool result = m_system->SaveState(stream);
|
|
|
|
if (!result)
|
|
|
|
{
|
2019-11-16 05:27:57 +00:00
|
|
|
ReportError(SmallString::FromFormat("Saving state to %s failed.", filename));
|
2019-09-14 10:28:47 +00:00
|
|
|
stream->Discard();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-16 05:27:57 +00:00
|
|
|
AddOSDMessage(SmallString::FromFormat("State saved to %s.", filename));
|
2019-09-14 10:28:47 +00:00
|
|
|
stream->Commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->Release();
|
|
|
|
return result;
|
|
|
|
}
|
2019-11-07 15:07:39 +00:00
|
|
|
|
2019-11-16 10:12:03 +00:00
|
|
|
void HostInterface::UpdateSpeedLimiterState()
|
2019-11-07 15:07:39 +00:00
|
|
|
{
|
2019-11-16 10:12:03 +00:00
|
|
|
m_speed_limiter_enabled = m_settings.speed_limiter_enabled && !m_speed_limiter_temp_disabled;
|
|
|
|
|
2019-11-16 10:50:11 +00:00
|
|
|
const bool audio_sync_enabled = !m_system || m_paused || (m_speed_limiter_enabled && m_settings.audio_sync_enabled);
|
|
|
|
const bool video_sync_enabled = !m_system || m_paused || (m_speed_limiter_enabled && m_settings.video_sync_enabled);
|
2019-11-07 15:07:39 +00:00
|
|
|
Log_InfoPrintf("Syncing to %s%s", audio_sync_enabled ? "audio" : "",
|
2019-11-16 10:50:11 +00:00
|
|
|
(audio_sync_enabled && video_sync_enabled) ? " and video" : (video_sync_enabled ? "video" : ""));
|
2019-11-07 15:07:39 +00:00
|
|
|
|
2019-11-16 10:41:35 +00:00
|
|
|
m_audio_stream->SetSync(audio_sync_enabled);
|
2019-11-16 10:50:11 +00:00
|
|
|
m_display->SetVSync(video_sync_enabled);
|
2019-11-16 15:47:50 +00:00
|
|
|
m_throttle_timer.Reset();
|
|
|
|
m_last_throttle_time = 0;
|
2019-11-07 15:07:39 +00:00
|
|
|
}
|