;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Copyright (c) 2002 TIMEX Corporation.
; 
; This document is the property of, and contains information proprietary to
; TIMEX Corporation.  It is to be returned upon request.  Reproduction by any
; means, disclosure, or use of its contents for other than the expressed
; purpose for which it is loaned, without the written consent of TIMEX
; Corporation, is strictly prohibited.
;
; File Name    : cntutil.asm
; Purpose      : Counter Common Utility Routines
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Module Name : cntAddDataBy1
; Description : Add 1 to the counter data.
; Assumptions : IYReg - Counter ASD Address.
; Input(s)    : None
; Output(s)   : None
;               ( Destroyed: AReg, IXReg, HLReg )
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        IF @DEF('SUBROUTINE')
            UNDEF SUBROUTINE
        ENDIF
        DEFINE  SUBROUTINE      "'cntAddDataBy1'"

        GLOBAL  cntAddDataBy1

cntAddDataBy1:

                push    SC
                
                ; Use decimal addition.
                UTL_DECIMAL_MATH_MODE
                
                ; Value to be added to the counter data.
                ld      A, #01h
                
                ; Compute the new counter data.
                ; Popping of SCReg is done inside the routine.
                jr      cntAddDataBy1EntryPoint
                


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Module Name : cntAddDataByAcceleration
; Description : Add counter data by the acceleration value.
; Assumptions : IYReg - Counter ASD Address.
;               COREEventArgument = Number of pulses.
; Input(s)    : None
; Output(s)   : None
;               ( Destroyed: AReg, IXReg, HLReg )
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        IF @DEF('SUBROUTINE')
            UNDEF SUBROUTINE
        ENDIF
        DEFINE  SUBROUTINE      "'cntAddDataByAcceleration'"

        GLOBAL  cntAddDataByAcceleration

cntAddDataByAcceleration:

                push    SC
                
                ; Use decimal addition.
                UTL_DECIMAL_MATH_MODE

                ;--------------------------------------------------------------
                ; Determine the acceleration factor for COREEventArgument and
                ;   write factor into AReg.
                ;--------------------------------------------------------------
                
                ;--------------------------------------------------------------
                ; Get starting address into the acceleration table then subtract 
                ;   it by 1 to get the exact acceleration data.  Take note that 
                ;   the least number of pulses that the system will send is 1.
                ;--------------------------------------------------------------
                ld      IX, #utlAccelerationTable1Min - 1
                    
                ; Get the number of pulses.
                ld      L, [COREEventArgument]
                
                ; Get the acceleration factor.
                ld      A, [IX + L]

cntAddDataBy1EntryPoint:
                ;--------------------------------------------------------------
                ; Note for using this as the entry point.
                ;      AReg - Value to be added to the current counter.
                ;      IYReg- Counter ASD address.
                ;      SCReg should be pushed.
                ;      bDecimalFlag should be set.
                ;--------------------------------------------------------------

                push    IY
                
                ; Set HLReg and IYReg to point to the data low address.
                add     IY, #CNTDATALOOFFSET
                ld      HL, IY
                
                ; Increment the counter.
                add     [HL], A
                inc     HL
                adc     [HL], #0
                
                ; Get the current counter data.
                ld      HL, [IY]

                ;--------------------------------------------------------------
                ; Check if counter data exceeds its maximum.  If it exceeds 
                ;   then compute for the excess data so that it would look 
                ;   like it has wraparound.     
                ;--------------------------------------------------------------
                cp      HL, #CNTMAXDATA
                jr      C, cntAddDataExit
                jr      Z, cntAddDataExit      
                
                ld      HL, IY
                sub     [HL], #@LOW(CNTMAXDATA+1)
                inc     HL
                sbc     [HL], #@HIGH(CNTMAXDATA)

cntAddDataExit:

                pop     IY                
                pop     SC

                ret


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Module Name : cntSubDataBy1
; Description : Subtract 1 to the counter data.
; Assumptions : IYReg - Counter ASD Address.
; Input(s)    : None
; Output(s)   : None
;               ( Destroyed: AReg, IXReg, HLReg )
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


        IF @DEF('SUBROUTINE')
            UNDEF SUBROUTINE
        ENDIF
        DEFINE  SUBROUTINE      "'cntSubDataBy1'"

        GLOBAL  cntSubDataBy1

cntSubDataBy1:

                push    SC
                
                ; Use decimal addition.
                UTL_DECIMAL_MATH_MODE
                
                ; Value to be subtracted to the counter data.
                ld      A, #01h
                
                ; Compute the new counter data.
                ; Popping of SCReg is done inside the routine.
                jr      cntSubDataBy1EntryPoint



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Module Name : cntSubDataByAcceleration
; Description : Subtract counter data by the acceleration value.
; Assumptions : IYReg - Counter ASD Address.
;               COREEventArgument = Number of pulses.
; Input(s)    : None
; Output(s)   : None
;               ( Destroyed: AReg, IXReg, HLReg )
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        IF @DEF('SUBROUTINE')
            UNDEF SUBROUTINE
        ENDIF
        DEFINE  SUBROUTINE      "'cntSubDataByAcceleration'"

        GLOBAL  cntSubDataByAcceleration

cntSubDataByAcceleration:

                push    SC
                    
                ; Use decimal addition.
                UTL_DECIMAL_MATH_MODE

                ;--------------------------------------------------------------
                ; Determine the acceleration factor for COREEventArgument and
                ;   write factor into AReg.
                ;--------------------------------------------------------------
                
                ;--------------------------------------------------------------
                ; Get starting address into the acceleration table then subtract 
                ;   it by 1 to get the exact acceleration data.  Take note that 
                ;   the least number of pulses that the system will send is 1.
                ;--------------------------------------------------------------
                ld      IX, #utlAccelerationTable1Min - 1
                    
                ; Get the number of pulses.
                ld      L, [COREEventArgument]
                
                ; Get the acceleration factor.
                ld      A, [IX + L]

cntSubDataBy1EntryPoint:
                ;--------------------------------------------------------------
                ; Note for using this as the entry point.
                ;      AReg - Value to be added to the current counter.
                ;      IYReg- Counter ASD address.
                ;      SCReg should be pushed.
                ;      bDecimalFlag should be set.
                ;--------------------------------------------------------------

                push    IY
                
                ; Set HLReg and IYReg to point to the data low address.
                add     IY, #CNTDATALOOFFSET
                ld      HL, IY
                                
                ; Decrement the counter.
                sub     [HL], A
                inc     HL
                sbc     [HL], #0
                
                ; Get the current counter data.
                ld      HL, [IY]

                ;--------------------------------------------------------------
                ; Check if counter data exceeds its minumum.  If it exceeds 
                ;   then compute for the excess data so that it would look 
                ;   like it has wraparound.     
                ;--------------------------------------------------------------
                cp      HL, #CNTMAXDATA
                jr      C, cntSubDataExit
                jr      Z, cntSubDataExit

                ld      HL, IY
                add     [HL], #@LOW(CNTMAXDATA + 1)
                inc     HL
                adc     [HL], #@HIGH(CNTMAXDATA)

cntSubDataExit:

                pop     IY
                pop     SC

                ret
