;**************************************
;*            TIMERS.ASM              *
;* (c)1990 by Tracy Allen 76670,326   *
;* creates 49 timers that operate in  *
;* in sync with the background task.  *
;* Each of the 49 clocks count seconds*
;* elapsed from 0 to 65536 (18.2 hrs) *
;* Each operates independently.       *
;* syntax for BASIC CALL to read & set*
;* CALL 62750'E    enables timer hook *
;* POKE 62975,201  disables timer hook*
;* CALL 62750'A    zeros all 49 timers*
;* CALL 62750,N%,X%'S     sets N%th   *
;*                 timer to value X%  *
;* CALL 62750,N%,varptr(X%)'G    gets *
;*                 current time from  *
;*                 timer number N%, & *
;*                 returns value in X%*
;**************************************
;
        org 62750
timers: mov b,a
        ldax d
        cpi ':'
        rnz
        inx d
        inx d
        ldax d
        cpi 255
        rnz
        inx d
        ldax d
        cpi 'G'
        jz wtget        ;G, get timer N
        cpi 'S'
        jz wtrst        ;S, set timer N
        cpi 'E'
        jz warm ;E enable timing hook
        cpi 'A'
        jz wrst ;A All timers zeroed
        ret
;
;
;A zeros all 49 timers
Wrst:   xra a   ;zeros all
        mvi b,63h       ;63h=99d length
        lxi h,Wttick    ; 1st buffer
wrst1:  mov m,a 
        inx h
        dcr b
        rz
        jmp wrst1
;
;E warm arms the timer, attches to
;background timer hook
Warm:   lxi h,watch
        shld thook+1
        mvi a,195
        sta thook
        ret
;
;
;G get Nth timer value
;  syntax CALL watchs,N,varptr(X%)'G
;  1<=N<=49 & X% is integer variable
;  All timer addresses must be on 1 pg!
;  They are on one page as ORGed
Wtget:  mov a,b
        push h
        lxi d,wttick-1
        rlc     ;multipy * 2
        add e   ;to calculate address
        mov e,a
        lhli    ;get time
        pop d
        shli    ;store in variable
        ret
;
;
;S set the Nth timer to value X%
;  call watchs,N,X%'S
Wtrst:  mov a,b
        cpi 50  ;only allow 1-49
        rnc
        lxi d,wttick-1
        rlc     ;multipy A * 2
        add e
        mov e,a
        shli    ;set watch
        ret
;
;
;
;next subroutine is the business end 
;of the multiple timers.  It is 
;executed 256 times per second to keep
;all the timers current.
;It is executed as part of the t100's
;background task in reponse to the
;CCK interrupt.
;WT1 to WT49, 2-byte timers
;odd addresses 62861 to 62957
;are lo byte of a timer.
;even addresses 62862 to 62958
;are high byte of a timer.
;
thook:  equ 62975       ;time hook
Wttick: equ 62860       ;timer 256ths
Wt1:    equ 62861       ;lowByte timr 1
;
Watch:  push psw        ;runs
        push h  ;from CCK hook
        lxi h,wttick
        dcr m
        jnz watchx      ;not 1 second
        mvi a,31h       ;31h=49d watchs
watcht: inx h   ;next watch low byte
        inr m   ;tick
        inx h   ;high byte
        jnz watcha      ;no carry
        inr m   ;carry tick hi byte
watcha: dcr a   ;more watchs?
        jnz watcht      ;yes
watchx: pop h   ;exit to bkgrnd task
        pop psw
        ret     ;to background task
