The RTC rework is done

melonDS has got a fun little dialog for changing the date and time now.

Basically what it does is show you the current date and time of the emulated DS. You can change it to any date and time, or you can reset it back to your computer's date and time.

What you set is saved as an offset in seconds from your computer's time. Every time you launch a game, the computer's time is used as a reference to calculate the emulated DS's time, and then melonDS does its own time counting from this point.

This also means that, unlike the previous behavior, if melonDS runs too slow or too fast, it will affect the passage of time for the emulated game from your perspective. The point is to keep melonDS's time counting accurate from the perspective of the game.

This was also the occasion to rework the RTC emulation itself, because it was quite lacking.

For example, when returning the current time, it would simply return the current hour from 0 to 23, ignoring the AM/PM flag that is also encoded in the hour register. It gets a bit weird, because the way this register works depends on the 12/24-hour setting in status register 1. In 24-hour mode, the register goes from 0 to 23, and the AM/PM flag is automatically based on that. In 12-hour mode, the register goes from 0 to 11, and the AM/PM flag needs to be set properly for things to function.

I was also able to add support for writing to the date/time registers. This means that for example, you can boot the DS firmware and change the time from there, and melonDS's date and time setting will reflect that. Perhaps not very useful, but a nice touch regardless.

There is also the part where, in the DS, the RTC is powered by the battery. This means the RTC state isn't reset unless the battery runs out or is removed. I made melonDS save the RTC state between runs to mirror this: if you notice a new rtc.bin file, that's what it is.

Then there's the fun part: the RTC IRQ. Basically, the RTC has several alarm modes that will trigger an IRQ based on different conditions. The alarm interrupt mode will trigger the IRQ based on day of week, hour and/or minute, much like an alarm clock. Then there are other modes for simple per-minute interrupts or for firing at specific frequencies. All fine and dandy.

A prime example is libnds, or atleast the older versions of it, where they used the RTC IRQ to keep track of time. In more recent versions, they moved away from it, mainly because the RTC IRQ doesn't work on the 3DS in DS/DSi mode.

Regardless, this use case isn't difficult: they use the selected frequence steady interrupt mode to fire an IRQ every second. With support for this, these older libnds homebrews are no longer stuck in time.

I also looked into the DS firmware's alarm clock feature. The alarm clock screen does its own counting, the RTC IRQ is only really used when the DS is closed: while most of the DS hardware is turned off, the RTC still functions, and its interrupt can wake up the rest of the hardware (much like the 'lid open' interrupt that fires when you open your DS).

I had to rework melonDS's implementation of sleep mode to get this to work (and also, you know, to get the RTC to count while in sleep mode in general). Kind of a pointless detail, I doubt anybody out there will be using melonDS as their alarm clock, but still a nice touch. Also helps make for more accurate emulation in general, so hey, I'll take it.
Kamilo C says:
Oct 31st 2023
That was fast, thank you so much for all your work.
^.^ says:
Oct 31st 2023
I hope I'm not misremembering, but i think flashcards always bugged out when opening and closing the lid on the title screen of sm64ds, the screen would not turn back on... or maybe it was an option on that menu that would glitch things, idk.
melonDSuser says:
Oct 31st 2023
I also hope that the latency of the controls will be lower.
keisui says:
Oct 31st 2023
A Banana says:
Nov 2nd 2023
It actually fixed a bug with Pokemon Black/White where he was confused at what season we were in. Nice.
7-Eleven says:
Nov 2nd 2023
Wonderful! I love your attention to details and dedication in making things better

Please, resume work on MelonHLE and if possible write a frameskip function, I'd love to be able to enjoy the results of your hard work even on my crappy hardware...
fred says:
Nov 3rd 2023
DranyElnavio says:
Dec 11th 2023
SyeThunder says:
Jan 22nd 2024
Does this mean that time based events in Pokemon games like berries growing and sprite encounters will work now?
mightym0x says:
Feb 23rd 2024
Where is this option?
Feline Fang says:
Apr 4th 2024
How do we access this option?
Omar Alamri says:
May 22nd 2024
I’m just trying my
Omar Alamri says:
May 22nd 2024
The only way I could do that was if you had to do a lot more work and then you would be done by yourself so
Omar Alamri says:
May 22nd 2024
I want to play DS games
SenninBoogeyman says:
May 29th 2024
i hate to sound dumb, but does this mean i could successfully soft reset for shiny starters in hg/ss ?
Post a comment