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: Select all
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 ;
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.
Because DJIFFIES waits until the count goes negative, DJIFFIES and JIFFIES wait one jiffy more than what is requested. Not a big problem. There is an easy solution. Just subtract one from the initial value returned by JIFFY@ DROP .
Code: Select all
// DJIFFIES JIFFIES
// TAKES POSITIVE DOUBLE NUMBER
// AND DELAYS THAT MANY JIFFIES
: DJIFFIES ( D+ -- )
JIFFY@ DROP 1-
BEGIN
PAUSE
JIFFY@ DROP DUP>R -
// COMPENSATE FOR DAILY RESET
0 MIN
S>D D+ R> OVER 0<
UNTIL
DROP 2DROP ;
: JIFFIES ( U -- )
0 DJIFFIES ;
Now DJIFFIES and JIFFIES will wait the requested number of jiffies.
Note: // (double forward slash) is a Commodore 64 Forth alias for \ (backslash) .