From 33f5d9cb9c687f4952a7090f0cab45f076308274 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Thu, 16 Mar 2023 19:33:59 +1000 Subject: [PATCH] System: Remove BIOS from save state Makes the states smaller, as well as not creating potential piracy issues when sending across the wire. --- src/core/bus.cpp | 8 +++++++- src/core/save_state_version.h | 2 +- src/core/system.cpp | 17 +++++++++++++++++ src/util/state_wrapper.cpp | 11 +++++++++++ src/util/state_wrapper.h | 3 ++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/core/bus.cpp b/src/core/bus.cpp index ec5e85d2b..13c591ef1 100644 --- a/src/core/bus.cpp +++ b/src/core/bus.cpp @@ -211,7 +211,13 @@ bool DoState(StateWrapper& sw) sw.Do(&m_cdrom_access_time); sw.Do(&m_spu_access_time); sw.DoBytes(g_ram, g_ram_size); - sw.DoBytes(g_bios, BIOS_SIZE); + + if (sw.GetVersion() < 58) + { + Log_WarningPrint("Overwriting loaded BIOS with old save state."); + sw.DoBytes(g_bios, BIOS_SIZE); + } + sw.DoArray(m_MEMCTRL.regs, countof(m_MEMCTRL.regs)); sw.Do(&m_ram_size_reg); sw.Do(&m_tty_line_buffer); diff --git a/src/core/save_state_version.h b/src/core/save_state_version.h index 248ecb303..29bbdc7b0 100644 --- a/src/core/save_state_version.h +++ b/src/core/save_state_version.h @@ -5,7 +5,7 @@ #include "types.h" static constexpr u32 SAVE_STATE_MAGIC = 0x43435544; -static constexpr u32 SAVE_STATE_VERSION = 57; +static constexpr u32 SAVE_STATE_VERSION = 58; static constexpr u32 SAVE_STATE_MINIMUM_VERSION = 42; static_assert(SAVE_STATE_VERSION >= SAVE_STATE_MINIMUM_VERSION); diff --git a/src/core/system.cpp b/src/core/system.cpp index c05ae33e6..2e01579d6 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -1661,6 +1661,23 @@ bool System::DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_di sw.Do(&s_frame_number); sw.Do(&s_internal_frame_number); + // 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()); + Host::AddKeyedOSDMessage( + "StateBIOSMismatch", + Host::TranslateStdString("OSDMessage", "This save state was created with a different BIOS version or patch " + "options. This may cause stability issues."), + 10.0f); + } + } + if (!sw.DoMarker("CPU") || !CPU::DoState(sw)) return false; diff --git a/src/util/state_wrapper.cpp b/src/util/state_wrapper.cpp index 6e9d353f7..9e4ec032c 100644 --- a/src/util/state_wrapper.cpp +++ b/src/util/state_wrapper.cpp @@ -29,6 +29,17 @@ void StateWrapper::DoBytes(void* data, size_t length) } } +void StateWrapper::DoBytesEx(void* data, size_t length, u32 version_introduced, const void* default_value) +{ + if (m_mode == Mode::Read && m_version < version_introduced) + { + std::memcpy(data, default_value, length); + return; + } + + DoBytes(data, length); +} + void StateWrapper::Do(bool* value_ptr) { if (m_mode == Mode::Read) diff --git a/src/util/state_wrapper.h b/src/util/state_wrapper.h index a4b71905f..d9ec78058 100644 --- a/src/util/state_wrapper.h +++ b/src/util/state_wrapper.h @@ -104,6 +104,7 @@ public: } void DoBytes(void* data, size_t length); + void DoBytesEx(void* data, size_t length, u32 version_introduced, const void* default_value); void Do(bool* value_ptr); void Do(std::string* value_ptr); @@ -182,7 +183,7 @@ public: template void DoEx(T* data, u32 version_introduced, T default_value) { - if (m_version < version_introduced) + if (m_mode == Mode::Read && m_version < version_introduced) { *data = std::move(default_value); return;