I've just streamlined
DJIFFIES , the word that takes a positive double number and waits that many jiffies ( 1/60th of a second on the Commodore 64 ) . It is also used by
JIFFIES , the word that takes an unsigned single number and waits that many jiffies.
DJIFFIES works by keeping the number of jiffies to delay on the stack as well as the latest jiffy clock value. Each time through the loop, it subtracts the difference from the amount of time to wait. The idea came from
DOWN-COUNTER on page 130 ( 140 of the PDF ) in the book
Real Time Forth by Tim Hendtlass.
When I was testing my multitasker, I noticed that the loop in
DJIFFIES runs several times per jiffy. With three background tasks, two using
DJIFFIES ( actually
JIFFIES ) for a delay and one counting how many times it runs, the entire round robin runs several times a jiffy. I realized that when the difference between the current jiffy clock value and the previous one is subtracted from the amount of time to delay, either 0 or -1 is added to the remaining time. I only needed to use the lower cell of the jiffy clock value. Here is the code:
Code:
SCR# 41
// DJIFFIES
HEX
// TAKES POSITIVE DOUBLE NUMBER
// AND DELAYS THAT MANY JIFFIES
: DJIFFIES ( D+ -- )
JIFFY@ DROP
BEGIN
PAUSE
JIFFY@ DROP DUP>R -
// COMPENSATE FOR RESET AT 24 HOURS
0 MIN
S>D D+ R> OVER 0<
UNTIL
DROP 2DROP ;
SCR# 42
// JIFFIES
HEX
: JIFFIES ( U -- )
0 DJIFFIES ;
JIFFIES takes an unsigned number and has a maximum delay of:
18 minutes 12 seconds and 15 jiffies.
DJIFFIES takes a positive double number and has a maximum delay of:
414 days 6 hours 3 minutes 14 seconds and 7 jiffies or
2,147,483,647 jiffies.