Here is DOWN-COUNTER for Fleet Forth.
Code: Select all
: DOWN-COUNTER
2VARIABLE ( -- )
DOES> ( -- ADR )
JIFFY@ DROP
OVER 2+ @
OVER - 0 MIN
2PICK +!
OVER 2+ ! ;
That '0 MIN' is to handle the case when the jiffy timer resets to zero when it reaches twenty four hours.
A down-counter, a child word of DOWN-COUNTER , has two cells of storage. The first cell holds a value. The second cell holds the low cell of the Commodore 64 jiffy clock from the last time the down-counter was executed. Each time a down-counter is executed the amount of time, in jiffies, which passed from the last time is subtracted from the value in the first cell.
Code: Select all
DOWN-COUNTER DELAY1 OK
300 DELAY1 ! OK
DELAY1 ? 209 OK
DELAY1 ? 118 OK
DELAY1 ? 5 OK
DELAY1 ? -445 OK
Of course, the values returned each time I type "DELAY1 ?" depends on how long I wait.
Code: Select all
: DOWN-COUNTER
2VARIABLE ( ++ )
DOES> ( -- ADR )
JIFFY@ DROP \ Only need low cell of jiffy clock
OVER 2@ SWAP 2PICK - \ new.time value -delta.time
0 MIN \ Compensate for reset at midnight.
+ 2PICK 2! ; \ add negative time difference to
\ old value and store new time
\ and new value to down-counter
\ variable and leave address.
JIFFY@ leaves the value of the Commodore 64 jiffy clock on the stack as a double number. Yes the jiffy clock resets to zero after twenty four hours.
2PICK is syntactically equivalent to 2 PICK .
Code: Select all
CODE 2PICK ( N1 N2 N3 -- N1 N2 N3 N1 )
4 ,X LDA 5 ,X LDY
AYPUSH JMP END-CODE
A DOWN-COUNTER is used when some code is run periodically, but there is a portion which must be run less often.
Code: Select all
DOWN-COUNTER DELAY
.
.
.
DOWN-COUNTER @ 0< \
IF \ If this code fragment is run often enough
#3600 DOWN-COUNTER ! \ <DO.SOMETHING> only runs
<DO.SOMETHING> \ once a minute.
THEN \
.
.
.