From 3392783359723cc6d56dc47165122d63f3e93487 Mon Sep 17 00:00:00 2001 From: LuigiBlood Date: Wed, 22 Nov 2023 01:47:58 +0100 Subject: [PATCH] n64: optimize DD RTC checks (#1300) --- ares/n64/dd/dd.hpp | 1 + ares/n64/dd/rtc.cpp | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ares/n64/dd/dd.hpp b/ares/n64/dd/dd.hpp index 0ec49160d9..ff94407e74 100644 --- a/ares/n64/dd/dd.hpp +++ b/ares/n64/dd/dd.hpp @@ -38,6 +38,7 @@ struct DD : Memory::PI
{ auto tickClock() -> void; auto tickSecond() -> void; auto valid() -> bool; + auto daysInMonth(u8 month, u8 year) -> u8; } rtc; auto title() const -> string { return information.title; } diff --git a/ares/n64/dd/rtc.cpp b/ares/n64/dd/rtc.cpp index 06955d16a6..912a1bb86a 100644 --- a/ares/n64/dd/rtc.cpp +++ b/ares/n64/dd/rtc.cpp @@ -74,11 +74,8 @@ auto DD::RTC::tickSecond() -> void { ram.write(3, 0); //day - u32 daysInMonth[12] = {0x31, 0x28, 0x31, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x31}; - if(ram.read(0) && !(BCD::decode(ram.read(0)) % 4)) daysInMonth[1]++; - tick(2); - if(ram.read(2) <= daysInMonth[BCD::decode(ram.read(1))-1]) return; + if(ram.read(2) <= daysInMonth(ram.read(1), ram.read(0))) return; ram.write(2, 1); //month @@ -93,11 +90,12 @@ auto DD::RTC::tickSecond() -> void { auto DD::RTC::valid() -> bool { //check validity of ram rtc data (if it's BCD valid or not) for(auto n : range(6)) { - if ((ram.read(n) & 0x0f) >= 0x0a) return false; - if (ram.read(n) >= 0xa0) return false; + if((ram.read(n) & 0x0f) >= 0x0a) return false; } //check for valid values of each byte + //year + if(ram.read(0) >= 0xa0) return false; //second if(ram.read(5) >= 0x60) return false; //minute @@ -109,10 +107,17 @@ auto DD::RTC::valid() -> bool { if(ram.read(1) < 1) return false; //day if(ram.read(2) < 1) return false; - u32 daysInMonth[12] = {0x31, 0x28, 0x31, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x31}; - if(ram.read(0) && !(BCD::decode(ram.read(0)) % 4)) daysInMonth[1]++; - if(ram.read(2) > daysInMonth[BCD::decode(ram.read(1))-1]) return false; + if(ram.read(2) > daysInMonth(ram.read(1), ram.read(0))) return false; //everything is valid return true; } + +auto DD::RTC::daysInMonth(u8 month, u8 year) -> u8 { + year = BCD::decode(year); + month = BCD::decode(month); + u8 days = 30 + ((month + (month >> 3)) & 1); + if (month == 2) + days -= (year % 4 == 0) ? 1 : 2; + return BCD::encode(days); +}