;*********************************************************************
;* Misc 16Cxx Library Subroutines
;* KG Feb '95
;*
;*
;*********************************************************************

; Decrement 16 bit LO & HI files pointed to by W
; Set zero bit if counter goes to h'0000'
; Uses W and FSR
;
; Note FSR is saved at interrupt and is restored before interrupt
; exit so it's ok to call this routine from main or interrupt routines
Dec16W	
	MOVWF	FSR			; Set up indirect address
	DECFSZ	INDF, f			; Decrement lo byte
	GOTO	E_DecNZ			; Lo didn't roll over, exit.	
	INCF	FSR, f			; Move to hi byte
	MOVFW	INDF			; Test if Hi is zero
	BZ	Dec_Ex			; If zero then expire and exit
	DECF	INDF, f			; Else decrement Hi
Dec_Ex	RETURN
E_DecNZ	CLRZ
	RETURN


; Wait_S
; Wait W seconds
; I/P W reg
; Uses W, Scratch 
Wait_S
	ifdef	Debug
	return
	else
        MOVWF   Scratch            	; Save no seconds to delay
LOOP0   MOVLW	d'100'          	; Load one second delay
        CALL    Wait10ms               
        DECFSZ  Scratch, f
        GOTO    LOOP0
        RETURN
	endif
;
; Wait10ms 
; I/P W reg (w=1 = 10ms)
; Uses W, Scratch1
Wait10ms    
ifndef  Debug        
	MOVWF	Scratch1
W_LOOP	CALL    W_10ms			; Wait 10ms
        DECFSZ  Scratch1, f
	GOTO	W_LOOP
        ENDIF
        RETURN
	
; Wait 10ms. Uses timer B
W_10ms	
	ifdef	debug
	clrwdt
	return
	else
	MOVLF16	TMRB_LO, D'10'
	BSF	TMRB_EXP		; run timer B for 10ms
W_10_C	BTFSS	TMRB_EXP	
	RETURN
	CLRWDT
	GOTO	W_10_C
	endif
	 
; Warble        
; Do a warble
; Uses W, Scratch
Warble   
	ifndef  Debug
        MOVLF   Scratch,	D'7'
TL 	Tone	d'10',	d'10'    
	Tone	d'8',	d'10'
        DECFSZ  Scratch, f
        GOTO    TL
	ENDIF
        RETURN

; Toggle speaker bit
TOGGLE  
	BTFSC   SpeakerPos           
	GOTO    B_Set           	; Bit is set so clear it
	BSF	SpeakerPos
ifdef 	SpeakerNeg
	bcf	SpeakerNeg		; Complimentary o/p 
	endif
        RETURN
B_Set   
	BCF	SpeakerPos
ifdef	SpeakerNeg
	bsf	SpeakerNeg		; Complimentary o/p
	endif
        RETURN

; ClearRam - Clear all File Registers from RamStart to RamEnd
ClearRam
	MOVLF	FSR, RamStart
ClrRamLoop
	clrf	INDF			; Clear the file
	incf	FSR, f			; Next file
	movfw	FSR
	xorlw	RamEnd			; Check if all registers done
	skpz
	goto	ClrRamLoop
	return

