diff --git a/libn3ds/include/util.h b/libn3ds/include/util.h index 2814ce7..fe976ff 100644 --- a/libn3ds/include/util.h +++ b/libn3ds/include/util.h @@ -95,3 +95,9 @@ static inline u32 nextPow2(u32 val) // Everything else is undefined behavior. return 1u<<(32u - __builtin_clzl(val - 1)); } + +// https://stackoverflow.com/a/42340213 +static inline u8 bcd2dec(const u8 bcd) +{ + return bcd - 6u * (bcd>>4); +} diff --git a/libn3ds/source/arm11/drivers/lgy.c b/libn3ds/source/arm11/drivers/lgy.c index dd4f51c..8e84f4f 100644 --- a/libn3ds/source/arm11/drivers/lgy.c +++ b/libn3ds/source/arm11/drivers/lgy.c @@ -22,6 +22,7 @@ #include "drivers/lgy.h" #include "drivers/pxi.h" #include "ipc_handler.h" +#include "util.h" #include "arm11/drivers/hid.h" #include "arm11/drivers/interrupt.h" #include "drivers/cache.h" @@ -94,12 +95,6 @@ static void powerDownFcramForLegacy(u8 mode) while(pdn->fcram_cnt & PDN_FCRAM_CNT_CLK_EN_ACK); // Wait until clock is disabled. } -// https://stackoverflow.com/a/42340213 -static inline u8 bcd2dec(u8 bcd) -{ - return bcd - 6 * (bcd>>4); -} - // https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto's_methods static void calcDayOfWeek(GbaRtc *const rtc) { @@ -112,6 +107,17 @@ static void calcDayOfWeek(GbaRtc *const rtc) rtc->dow = (y + (y / 4u) - (y / 100u) + (y / 400u) + t[m - 1] + d) % 7u; } +// Extra safety check just in case. +static_assert(sizeof(GbaRtc) >= sizeof(RtcTimeDate), "GbaRtc is not bigger or same size as RtcTimeDate!"); +static void mcuTimeDateToGbaRtc(GbaRtc *const rtc) +{ + // Lazy conversion. + MCU_getRtcTimeDate((RtcTimeDate*)rtc); + rtc->time = __builtin_bswap32(rtc->time)>>8; + rtc->date = __builtin_bswap32(rtc->date)>>8; + calcDayOfWeek(rtc); +} + Result LGY_prepareGbaMode(bool directBoot, u16 saveType, const char *const savePath) { u32 cmdBuf[4]; @@ -124,10 +130,7 @@ Result LGY_prepareGbaMode(bool directBoot, u16 saveType, const char *const saveP // Setup GBA Real-Time Clock. GbaRtc rtc; - MCU_getRtcTimeDate((u8*)&rtc); - rtc.time = __builtin_bswap32(rtc.time)>>8; - rtc.date = __builtin_bswap32(rtc.date)>>8; - calcDayOfWeek(&rtc); + mcuTimeDateToGbaRtc(&rtc); LGY_setGbaRtc(rtc); // Setup FCRAM for GBA mode.