From e8ef801a3eb85e4f028218de89653caff7f2695b Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Wed, 12 Apr 2023 03:33:39 -0700 Subject: [PATCH] Vita: Work around broken mktime implementation in Vita SDK (fixes #2876) --- CHANGES | 1 + src/gba/savedata.c | 41 +++++++++++++++++++++++++++++++- src/platform/psp2/CMakeLists.txt | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 03d763dcc..3601b0fd9 100644 --- a/CHANGES +++ b/CHANGES @@ -33,6 +33,7 @@ Other fixes: - Scripting: Fix receiving packets for client sockets - Scripting: Fix empty receive calls returning unknown error on Windows - Scripting: Return proper callback ID from socket.add + - Vita: Work around broken mktime implementation in Vita SDK (fixes mgba.io/i/2876) Misc: - Core: Handle relative paths for saves, screenshots, etc consistently (fixes mgba.io/i/2826) - GB Serialize: Add missing savestate support for MBC6 and NT (newer) diff --git a/src/gba/savedata.c b/src/gba/savedata.c index 2d377a875..512af02c9 100644 --- a/src/gba/savedata.c +++ b/src/gba/savedata.c @@ -13,6 +13,10 @@ #include #include +#ifdef PSP2 +#include +#endif + #include #include @@ -637,6 +641,9 @@ void GBASavedataRTCRead(struct GBASavedata* savedata) { } LOAD_64LE(savedata->gpio->rtc.lastLatch, 0, &buffer.lastLatch); + time_t rtcTime; + +#ifndef PSP2 struct tm date; date.tm_year = _unBCD(savedata->gpio->rtc.time[0]) + 100; date.tm_mon = _unBCD(savedata->gpio->rtc.time[1]) - 1; @@ -645,8 +652,40 @@ void GBASavedataRTCRead(struct GBASavedata* savedata) { date.tm_min = _unBCD(savedata->gpio->rtc.time[5]); date.tm_sec = _unBCD(savedata->gpio->rtc.time[6]); date.tm_isdst = -1; + rtcTime = mktime(&date); +#else + struct SceDateTime date; + date.year = _unBCD(savedata->gpio->rtc.time[0]) + 2000; + date.month = _unBCD(savedata->gpio->rtc.time[1]); + date.day = _unBCD(savedata->gpio->rtc.time[2]); + date.hour = _unBCD(savedata->gpio->rtc.time[4]); + date.minute = _unBCD(savedata->gpio->rtc.time[5]); + date.second = _unBCD(savedata->gpio->rtc.time[6]); + date.microsecond = 0; - savedata->gpio->rtc.offset = savedata->gpio->rtc.lastLatch - mktime(&date); + struct SceRtcTick tick; + int res; + res = sceRtcConvertDateTimeToTick(&date, &tick); + if (res < 0) { + mLOG(GBA_SAVE, ERROR, "sceRtcConvertDateTimeToTick %lx", res); + } + res = sceRtcConvertLocalTimeToUtc(&tick, &tick); + if (res < 0) { + mLOG(GBA_SAVE, ERROR, "sceRtcConvertUtcToLocalTime %lx", res); + } + res = sceRtcConvertTickToDateTime(&tick, &date); + if (res < 0) { + mLOG(GBA_SAVE, ERROR, "sceRtcConvertTickToDateTime %lx", res); + } + res = sceRtcConvertDateTimeToTime_t(&date, &rtcTime); + if (res < 0) { + mLOG(GBA_SAVE, ERROR, "sceRtcConvertDateTimeToTime_t %lx", res); + } +#endif + + savedata->gpio->rtc.offset = savedata->gpio->rtc.lastLatch - rtcTime; + + mLOG(GBA_SAVE, ERROR, "Savegame time offset set to %li", savedata->gpio->rtc.offset); } void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state) { diff --git a/src/platform/psp2/CMakeLists.txt b/src/platform/psp2/CMakeLists.txt index fa9847f14..3e638aa64 100644 --- a/src/platform/psp2/CMakeLists.txt +++ b/src/platform/psp2/CMakeLists.txt @@ -28,6 +28,7 @@ set(OS_LIB -lvita2d -l${M_LIBRARY} -lScePgf_stub -lScePhotoExport_stub -lScePower_stub + -lSceRtc_stub -lSceSysmodule_stub -lSceTouch_stub) set(OS_LIB ${OS_LIB} PARENT_SCOPE)