    ifndef _tools_asm
        #define _tools_asm



;**************************************************
;
; (C) Copyright notice
;
; Written by Richard Prinz.
; Copyright 2003 by Richard Prinz.
; OE1RIB Richard.Prinz@MIN.at
;
; Permission to use, copy, and modify this program
; without restriction is hereby granted, as long as
;
; 1) this copyright notice appears in each copy of
;    the program source code and
;
; 2) the source code is not used for commercial
;    purposes.
;
; This program is freely distributable without
; licensing fees and is provided without guarantee
; or warrantee expressed or implied.
;
; This program is -not- in the public domain.
;
;**************************************************


;**************************************************
; delay
; parameter
; DELAY_H   msb
; DELAY_L   lsb
; of the 16 bit number of milliseconds to wait.
; so it is possible to wait from 1ms to 65535ms
; (about 1min) 1min is exact EA60 (60000)
;
; at 4MHz, 1000 instruction cycles are exact 1ms
; completely exact time is not possible. the
; routine always needs 5 instructions at the
; beginning and 1 instruction at the end. so the
; total delay instruction count is always + 6
;**************************************************

Delay
        ifdef Debug
            return
        else
            comf        DELAY_H,f   ; 1     1
            comf        DELAY_L,f   ; 1     1
            incf        DELAY_L,f   ; 1     1
            btfsc       STATUS,Z    ; 1/2   1/2
            incf        DELAY_H,f   ; 1     1
; ------------------------------------- = 5 always at etry


; inner loop for 1000 instruction cycles
; at 4MHz each instruction takes 4 clock
; cycles
; 1000 instruction cycles = 1ms
; ---------------------------------------Cycles-----
Delay01
            movlw       D'248'      ; 1     1
            movwf       DELAY_T     ; 1     1
Delay02
            nop                     ; 1     248x1
            decfsz      DELAY_T,f   ; 1/2   247x1 + 1x2
            goto        Delay02     ; 2     247x2
            nop                     ; 1     1
            nop                     ; 1     1
; ------------------------------------- = 995 cycles

            incf        DELAY_L,f   ; 1     1
            btfsc       STATUS,Z    ; 1/2   1/2
            incfsz      DELAY_H,f   ; 1/2   1/2
            goto        Delay01     ; 2     2 in the loop always 5
; ------------------------------------- = 5 cycles

            return                  ; 2     on exit the 5 from above + 1
; ------------------------------------- = 6 cycles

        endif
DelayEnd        
        if (high (Delay) != high (DelayEnd) )
            MESSG "Procedure (Delay) hits page boundary!"
        endif



    endif
    

