Timer Alarm

A place where you can keep others updated about your NES-related projects through screenshots, videos or information in general.

Moderator: Moderators

Post Reply
Dacicus
Posts: 32
Joined: Sat Dec 20, 2008 4:59 pm

Timer Alarm

Post by Dacicus »

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:

Code: Select all

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.
Timer_Alarm.png
Timer_Alarm.png (7.29 KiB) Viewed 1455 times
Attachments
Alarm_20.nes
(24.02 KiB) Downloaded 50 times
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Timer Alarm

Post by Dwedit »

Dot clock:
5369318 Hz

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!
ap9
Posts: 43
Joined: Sat Jun 01, 2013 11:55 am
Location: Maine, U.S.A.
Contact:

Re: Timer Alarm

Post by ap9 »

The 24 bit fraction adder for 0.01663926 (0x044279) is more than enough accuracy for a stated 40Hz tolerance of the NTSC master crystal.
User avatar
Ben Boldt
Posts: 1149
Joined: Tue Mar 22, 2016 8:27 pm
Location: Minnesota, USA

Re: Timer Alarm

Post by Ben Boldt »

Do you plan to make a real cartridge out of this? Have you considered using a real-time clock chip?
Dacicus
Posts: 32
Joined: Sat Dec 20, 2008 4:59 pm

Re: Timer Alarm

Post by Dacicus »

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.
User avatar
TmEE
Posts: 960
Joined: Wed Feb 13, 2008 9:10 am
Location: Norway (50 and 60Hz compatible :P)
Contact:

Re: Timer Alarm

Post by TmEE »

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.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Timer Alarm

Post by Dwedit »

Dacicus wrote: Fri Aug 19, 2022 11:23 pm
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!
User avatar
Movax12
Posts: 541
Joined: Sun Jan 02, 2011 11:50 am

Re: Timer Alarm

Post by Movax12 »

I find this interesting and have tried to do an accurate clock too. Not sure I am doing it right, does this make sense?

Code: Select all

;   framerate = 60.098811862348404716732985230828 frames/sec
;   = 0.01663926405550947066275456212502 seconds/frame
;   = 1.663926405550947066275456212502 hundredths / frame

;   fractional part = 0.663926405550947066275456212502
;   fractional part of 65535 ($FFFF) =
;   0.663926405550947066275456212502 * 65535 = 43,510.416987781315988362022886333
;   43,510 = $A9F6
;   
;   converted back: 
;   43510/65535 = 0.66392004272526131074998092622263
;   0.66392004272526131074998092622263 - 0.663926405550947066275456212502 = 0.00000636282568575552547528627937
;   rounding error would have to add up to 100 to make a difference in the second counter
;   100/0.00000636282568575552547528627937 = ~15,716,287 frames before off by one second?

lda fractional
clc
adc #$F6
sta fractional
lda fractional + 1
adc #$A9
sta fractional + 1
lda hundredths
adc #$01
sta hundredths
cmp #100
bcc :+++
    ; hundredths >= 100
    sec
    sbc #100
    sta hundredths
    inc seconds
    lda seconds
    cmp #60
    bcc :++
    ; seconds >= 60
        ldx #0
        stx seconds
        inc minutes
        lda minutes
        cmp #60
        bcc :+
        ; minutes >= 60
            stx minutes
            inc hours
        :
    :
:
Post Reply