bsnes/higan/sfc/coprocessor/sharprtc/time.cpp

82 lines
1.8 KiB
C++
Raw Normal View History

const uint SharpRTC::daysinmonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
auto SharpRTC::tick_second() -> void {
if(++second < 60) return;
second = 0;
tick_minute();
}
auto SharpRTC::tick_minute() -> void {
if(++minute < 60) return;
minute = 0;
tick_hour();
}
auto SharpRTC::tick_hour() -> void {
if(++hour < 24) return;
hour = 0;
tick_day();
}
auto SharpRTC::tick_day() -> void {
uint days = daysinmonth[month % 12];
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
//add one day for leap years
if(year % 400 == 0) days++;
else if(year % 100 == 0);
else if(year % 4 == 0) days++;
if(day++ < days) return;
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
day = 1;
tick_month();
}
auto SharpRTC::tick_month() -> void {
if(month++ < 12) return;
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
month = 1;
tick_year();
}
auto SharpRTC::tick_year() -> void {
year++;
year = (uint12)year;
}
//returns day of week for specified date
//eg 0 = Sunday, 1 = Monday, ... 6 = Saturday
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
//usage: calculate_weekday(2008, 1, 1) returns weekday of January 1st, 2008
auto SharpRTC::calculate_weekday(uint year, uint month, uint day) -> uint {
uint y = 1000, m = 1; //SharpRTC epoch is 1000-01-01
uint sum = 0; //number of days passed since epoch
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
year = max(1000, year);
month = max(1, min(12, month));
day = max(1, min(31, day));
while(y < year) {
bool leapyear = false;
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
if(y % 4 == 0) {
leapyear = true;
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
if(y % 100 == 0 && y % 400 != 0) leapyear = false;
}
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
sum += 365 + leapyear;
y++;
}
while(m < month) {
uint days = daysinmonth[m - 1];
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
bool leapyearmonth = false;
if(days == 28) {
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
if(y % 4 == 0) {
leapyearmonth = true;
if(y % 100 == 0 && y % 400 != 0) leapyearmonth = false;
}
}
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
sum += days + leapyearmonth;
m++;
}
sum += day - 1;
Update to v089r06 release. [Yes, the release number is re-used. -Ed.] byuu says: I had some bugs in r07 that I couldn't track down, DKJM2's clock was getting all out of sync. So I just reverted to r05, blew away both RTCs entirely, and wrote them cleanly from scratch (obviously looking off the old code.) A bit extreme, but it worked. I believe I found the error in the process, day and month were resetting the counter to 0 instead of 1, it wasn't handling leap year, etc. While I was at it, I fixed the day-of-week calculation. The SharpRTC epoch is actually 1000-01-01, and not 1900-01-01. I'm sure you guys will be really happy that if it ever becomes 1000AD again and you're playing a ROM hack that uses the SharpRTC and relies on its weekday value that it will show the correct day now ... Kind of a pain to compute, but nothing compared to the seventh circle of hell that was my IBM dBase III Julian<>Gregorian conversion functions :/ Also found a few bugs in the Epson code this way. And I moved the round seconds actions and flag clear to +125us after flag set. So, if you had the old r06 or r07, please delete those. Unfortunately, this took all of my energy today, so the file names inside manifest changes will have to be in the next WIP. EDIT: ran a diff against old r07 and new r06. - added if(days == 31) case twice in EpsonRTC::tick_day() - forgot weekday = 0; in SharpRTC::load() - need to move the cartridge+cheat objects up in sfc/Makefile again - System::init() needs assert(interface != nullptr /* not 0 */)
2012-05-24 23:26:06 +00:00
return (sum + 3) % 7; //1000-01-01 was a Wednesday
}