To use this file copy and paste this:
// #URL-lib "http://pin1.org/forthlib/flb/Interupts/rr-shed.flb" into BV Terminal 3
or here to download.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Round robbin scheduler using timer 0
// Words are placed into the schedule by using s-add. It fills an area
// in memory with the words added. The other words are:
// ps. - to see the words in the list
// s-del - removes a word from the list
// s-pause - pause a word from the schedule
// s-resume - opposite
// s-start - starts timer0 and the scheduler
// s-clr - clears schedule table, muts be run for initialisation
// s-interval - returns variable address to set T0 interval in ms
//
// NOTE this method will give interval time to each scheduled word
// in turn, so if there are 10 words to be scheduled and the interval
// is 5 then each word will be serviced at 50ms intervals
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// REQUIRES:
// #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb" sid=99
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb" sid=100
// #URL-lib "http://pin1.org/forthlib/flb/Interupts/interrupt.flb"
// CONSTANTS:
&e0004000 constant T0IR
&e0004004 constant T0TCR
&e0004008 constant T0TC
&e000400c constant T0PR
&e0004014 constant T0MCR
&e0004018 constant T0MR0
//
integer nextShed // next scheduled task
variable interval // in ms
80 constant s-size // in bytes, each cfa is 4 bytes
s-size vspace$ s-table // schedule table
Full Contents of File
&e0004000  constant  T0IR
&e0004004  constant  T0TCR
&e0004008  constant  T0TC
&e000400c  constant  T0PR
&e0004014  constant  T0MCR
&e0004018  constant  T0MR0
integer  nextShed       
variable  interval       
80  constant  s-size   
s-size  vspace$  s-table   
: =s  &fffffffe  and  =  ;
 
: <0>s-clr  s-table  s-size  0  fill  ;   
: (ps)
    dup  1  and     
    if
        ."    "
    then   
    &fffffffe  and  8  -  stype  cr
;   
       
: <0>ps.
    s-size  s-table  +  s-table
    do
        i  @
        dup  0=
        if
            drop  leave
        else
               
        then
    4  +loop   
;                   
: <0>s-add
    0  swap 
    s-size  s-table  +  s-table
    do
        i  @  0=   
        if
            i  !  1-  leave   
        then       
    4  +loop
    -1  =  if  -1  else  drop  0  then  ;       
; 
: (s-del)
      s-size  s-table  +  swap   
      do
          i  4  +  @  i  !
      4  +loop
;
     
: <0>s-del
    0  swap 
    s-size  s-table  +  s-table
    do
        dup  i  @  =s   
        if
            i 
            -1       
            leave
        then
    4  +loop
    -1  =  if  2drop  -1  then  ;       
;
: (s-sus)
    s-size  s-table  +  s-table
    do
        dup  i  @  =s   
        if
            drop       
            i  @         
            swap  1  =  if  1+  else  &fffffffe  and  then
            i  !         
        then
    4  +loop
;
: <0>s-pause  1  swap    ;
: <0>s-resume  0  swap    ;
: s-next 
    s-table  @  0=       
    if
        0
    else   
        nextshed  4  *       
        dup  s-size  <
        if
            s-table  +  @     
            dup  0=           
            if 
                0  =>  nextShed     
                drop  s-table  @             
            else 
                1  +>  nextShed     
            then
        else
            0  =>  nextShed 
        then
    then   
;           
: setShed
        0  =>  nextShed           
        TIMER0  intSel     
        TIMER0  intEn       
        efiq       
;
: T0int
        1  t0IR  !       
        s-next           
        ?dup  0  <> 
        if
            dup  1  and  0=  if  execute  else  drop  then   
        then   
        TIMER0  intEn 
;
: <0>s-start
        interval  @  0=  if  10  interval  !  then
        pclk  1000 
        2  T0TCR  !             
        1  T0TCR  !             
        interval  @  t0MR0  !       
        3  t0MCR  !             
       
        [']  T0int   
        TIMER0       
        int>>         
        setShed                     
; 
: <0>s-intervalinterval  ;