This is a little project in which I attempted to make a timer that stays relatively synchronized with real time. Since the PPU framerate is slightly higher than 50 or 60 fps, it will run too quickly if using only those integer values for timekeeping. My approach is to have a running sum of the fractional part of the framerate, updated when the seconds value changes, and add an extra frame when the fraction is 0.5 or higher. In code:
sum += numerator
if sum >= denominator / 2 then
add extra frame
sum -= denominator
end
The fractions used for the calculations are 64740/655171 for NTSC, 45/6448 for PAL, and 48390/491381 for RGB (based on this wiki page). Feedback is welcome.
Total dots:
341*262 - 0.5 (Average of 'skipping first dot' and 'not skipping first dot' frames)
Frame rate = Dot Clock / Total Dots = 60.098811862348404716732985230828 Hz
Seconds per frame: Total Dots / Dot Clock = 0.016639264055509470662754562125022 s
Hundredths:
Add 4467075 every frame
Every time you are able to subtract 2684659 without borrowing is 1/100th of a second.
Now you have exact timing as long as the dot clock is exact.
The fractional number is under 16M, so it will fit in 3 bytes.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
Dwedit wrote: ↑Fri Aug 19, 2022 10:14 amHundredths:
Add 4467075 every frame
Every time you are able to subtract 2684659 without borrowing is 1/100th of a second.
Now you have exact timing as long as the dot clock is exact.
The fractional number is under 16M, so it will fit in 3 bytes.
That should be helpful for a time display in a future project that I have in mind. Thanks! For this project, I wasn't planning to display values more precise than a second.
Ben Boldt wrote: ↑Fri Aug 19, 2022 12:52 pmDo you plan to make a real cartridge out of this? Have you considered using a real-time clock chip?
No plans for a real cartridge. IMO, this project is too simplistic for that. One of my goals for this project was to get experience with using the PPU for timing, so I didn't consider additional chips.
I did a similar thing on Mega Drive, and found that temperature has a pretty big effect on the longterm stability but in the short term (tens of minutes) it kept track of time pretty well but over the course of a day it could be minutes ahead or behind, depending on what the weather/inside temperature was like.
Dwedit wrote: ↑Fri Aug 19, 2022 10:14 amHundredths:
Add 4467075 every frame
Every time you are able to subtract 2684659 without borrowing is 1/100th of a second.
Now you have exact timing as long as the dot clock is exact.
The fractional number is under 16M, so it will fit in 3 bytes.
That should be helpful for a time display in a future project that I have in mind. Thanks! For this project, I wasn't planning to display values more precise than a second.
Just thought of one other thing, initialize the counter to 2684659/2, then you will get an answer rounded to the nearest hundreth instead of truncated.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!