;*******************************************************************************
;* Module    : DELAY1MS.SUB
;* Programmer: Tony Papadimitriou <tonyp@acm.org>
;* Purpose   : Hard delay 1 msec, regardless of CPU (HC08/9S08) or system clock
;* Language  : Motorola/Freescale/NXP HC08/9S08 Assembly Language (aspisys.com/ASM8)
;* Status    : FREEWARE Copyright (c) 2017 by Tony Papadimitriou <tonyp@acm.org>
;* Original  : http://www.aspisys.com/code/hc08/delay1ms.html
;* Note(s)   : Use: #Include delay1ms.sub
;*           :
;*           : Example call:
;*           :                call      Delay1ms
;*           :
;* History   : 10.01.15 v1.00 Original
;*           : 14.09.17       Added 6 cycles for JSR for a little greater accuracy
;*******************************************************************************

#ifmain ;-----------------------------------------------------------------------
                    #ListOff
                    #Uses     mcu.inc
                    #ListOn
#endif ;------------------------------------------------------------------------
?_OBJECT_?
                              #Cycles 6           ;reset the cycles counter
Delay1ms            proc
                    pshhx
                    ldhx      #DELAY@@
                              #Cycles
Loop@@              @cop                          ;kick the watchdog
                    aix       #-1
                    cphx      #0
                    bne       Loop@@
                              #temp :cycles
                    pulhx
                    rtc

DELAY@@             equ       BUS_KHZ-:cycles-:ocycles/:temp

                    #sp
;*******************************************************************************
                    #Exit
;*******************************************************************************
                    @EndStats