Skip to content

Commit

Permalink
n64: optimize DD RTC checks (#1300)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuigiBlood authored Nov 22, 2023
1 parent 67c6bfa commit 3392783
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions ares/n64/dd/dd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct DD : Memory::PI<DD> {
auto tickClock() -> void;
auto tickSecond() -> void;
auto valid() -> bool;
auto daysInMonth(u8 month, u8 year) -> u8;
} rtc;

auto title() const -> string { return information.title; }
Expand Down
23 changes: 14 additions & 9 deletions ares/n64/dd/rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ auto DD::RTC::tickSecond() -> void {
ram.write<Byte>(3, 0);

//day
u32 daysInMonth[12] = {0x31, 0x28, 0x31, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x31};
if(ram.read<Byte>(0) && !(BCD::decode(ram.read<Byte>(0)) % 4)) daysInMonth[1]++;

tick(2);
if(ram.read<Byte>(2) <= daysInMonth[BCD::decode(ram.read<Byte>(1))-1]) return;
if(ram.read<Byte>(2) <= daysInMonth(ram.read<Byte>(1), ram.read<Byte>(0))) return;
ram.write<Byte>(2, 1);

//month
Expand All @@ -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<Byte>(n) & 0x0f) >= 0x0a) return false;
if (ram.read<Byte>(n) >= 0xa0) return false;
if((ram.read<Byte>(n) & 0x0f) >= 0x0a) return false;
}

//check for valid values of each byte
//year
if(ram.read<Byte>(0) >= 0xa0) return false;
//second
if(ram.read<Byte>(5) >= 0x60) return false;
//minute
Expand All @@ -109,10 +107,17 @@ auto DD::RTC::valid() -> bool {
if(ram.read<Byte>(1) < 1) return false;
//day
if(ram.read<Byte>(2) < 1) return false;
u32 daysInMonth[12] = {0x31, 0x28, 0x31, 0x30, 0x31, 0x30, 0x31, 0x31, 0x30, 0x31, 0x30, 0x31};
if(ram.read<Byte>(0) && !(BCD::decode(ram.read<Byte>(0)) % 4)) daysInMonth[1]++;
if(ram.read<Byte>(2) > daysInMonth[BCD::decode(ram.read<Byte>(1))-1]) return false;
if(ram.read<Byte>(2) > daysInMonth(ram.read<Byte>(1), ram.read<Byte>(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);
}

0 comments on commit 3392783

Please sign in to comment.