2012-05-23 11:27:45 +00:00
|
|
|
uint4 SharpRTC::rtc_read(uint4 addr) {
|
|
|
|
switch(addr) {
|
|
|
|
case 0: return second % 10;
|
|
|
|
case 1: return second / 10;
|
|
|
|
case 2: return minute % 10;
|
|
|
|
case 3: return minute / 10;
|
|
|
|
case 4: return hour % 10;
|
|
|
|
case 5: return hour / 10;
|
|
|
|
case 6: return day % 10;
|
|
|
|
case 7: return day / 10;
|
|
|
|
case 8: return month;
|
|
|
|
case 9: return year % 10;
|
|
|
|
case 10: return year / 10 % 10;
|
|
|
|
case 11: return year / 100;
|
|
|
|
case 12: return weekday;
|
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
|
|
|
default: return 0;
|
2012-05-23 11:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharpRTC::rtc_write(uint4 addr, uint4 data) {
|
|
|
|
switch(addr) {
|
|
|
|
case 0: second = second / 10 * 10 + data; break;
|
|
|
|
case 1: second = data * 10 + second % 10; break;
|
|
|
|
case 2: minute = minute / 10 * 10 + data; break;
|
|
|
|
case 3: minute = data * 10 + minute % 10; break;
|
|
|
|
case 4: hour = hour / 10 * 10 + data; break;
|
|
|
|
case 5: hour = data * 10 + hour % 10; break;
|
|
|
|
case 6: day = day / 10 * 10 + data; break;
|
|
|
|
case 7: day = data * 10 + day % 10; break;
|
|
|
|
case 8: month = data; break;
|
|
|
|
case 9: year = year / 10 * 10 + data; break;
|
|
|
|
case 10: year = year / 100 * 100 + data * 10 + year % 10; break;
|
|
|
|
case 11: year = data * 100 + year % 100; break;
|
|
|
|
case 12: weekday = data; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-05 09:21:30 +00:00
|
|
|
void SharpRTC::load(const uint8* data) {
|
2012-05-23 11:27:45 +00:00
|
|
|
for(unsigned byte = 0; byte < 8; byte++) {
|
|
|
|
rtc_write(byte * 2 + 0, data[byte] >> 0);
|
|
|
|
rtc_write(byte * 2 + 1, data[byte] >> 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 timestamp = 0;
|
|
|
|
for(unsigned byte = 0; byte < 8; byte++) {
|
|
|
|
timestamp |= data[8 + byte] << (byte * 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 diff = (uint64)time(0) - timestamp;
|
|
|
|
while(diff >= 60 * 60 * 24) { tick_day(); diff -= 60 * 60 * 24; }
|
|
|
|
while(diff >= 60 * 60) { tick_hour(); diff -= 60 * 60; }
|
|
|
|
while(diff >= 60) { tick_minute(); diff -= 60; }
|
|
|
|
while(diff--) tick_second();
|
|
|
|
}
|
|
|
|
|
2013-05-05 09:21:30 +00:00
|
|
|
void SharpRTC::save(uint8* data) {
|
2012-05-23 11:27:45 +00:00
|
|
|
for(unsigned byte = 0; byte < 8; byte++) {
|
|
|
|
data[byte] = rtc_read(byte * 2 + 0) << 0;
|
|
|
|
data[byte] |= rtc_read(byte * 2 + 1) << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 timestamp = (uint64)time(0);
|
|
|
|
for(unsigned byte = 0; byte < 8; byte++) {
|
|
|
|
data[8 + byte] = timestamp;
|
|
|
|
timestamp >>= 8;
|
|
|
|
}
|
|
|
|
}
|