*
* 5s_delay.asm  5 second delay timer for FOX11 board
*		with BUFFALO monitor, (c)2003, EVBplus.com
*		Written by Wayne Chu
*
*     Function:	5 second delay routine using output comparator OC2
*		The PB5 LED will be turned on immediately after running
*		the program. It will be turned off after 5 second delay. 
*		Change the DELAY_TIME to 18000 will delay 3 minutes
*
DB6:		equ	$40
PB5:		equ	$20
NOTDB6		equ	$BF
portb:  	equ     $1404 		; 74HC273 latch
*
*
        ORG  $c4
*** Vector jump table for BUFFALO monitor ***
JSCI    RMB   3
JSPI    RMB   3
JPAIE   RMB   3
JPAO    RMB   3
JTOF    RMB   3
JTOC5   RMB   3
JTOC4   RMB   3
JTOC3   RMB   3
JTOC2   RMB   3
JTOC1   RMB   3
JTIC3   RMB   3
JTIC2   RMB   3
JTIC1   RMB   3
JRTI    RMB   3
JIRQ    RMB   3
JXIRQ   RMB   3
JSWI    RMB   3
JILLOP  RMB   3
JCOP    RMB   3
JCLM    RMB   3

BUFFALO_mon:	equ	$e02b		; BUFFALO re-entry address
*
* 68HC11 control register address is the offset address from the
* register base address $1000, so index addressing mode can also be used.
*
toc2:   equ     $0018
tmsk1:	equ	$0022
tflg1:	equ	$0023	
REGBLK:	equ	$1000

*
*DELAY_TIME:	equ	18000	; 18000 X 10 ms= 180 sec = 3 min
DELAY_TIME:	equ	500	; 500 X 10 ms= 5 sec
TB10MS:		equ	20000	; time base of 20,000 instruction cycles
*				; = 20,000 x 0.5us = 10ms at 8MHz

	org	$00
*
flag_5s:	rmb	1
cnt_5s:		rmb	2
d10ms_flag:	rmb	1
*
STACK:	equ	$8FFF
*
*
	
	org	$D000		; user program starts at $D000
	
start:
	lds	#STACK
	ldx	#REGBLK
	ldaa	#DB6	 
	staa	tmsk1,x
	cli
*
	ldaa	#PB5
	staa	portb		; turn on LED PB0
	jsr	delay_5s
	ldaa	#$80
	staa	portb		; turn off LED PB0
	jmp	BUFFALO_mon     ; re-enter BUFFALO monitor
*
*
delay_5s:
	clr	cnt_5s
	clr	cnt_5s+1
	clr	flag_5s
delay:	ldaa	flag_5s
	beq	delay
	rts
*
tmr6oc2:
	ldx	cnt_5s
	inx
	stx	cnt_5s
	cpx	#DELAY_TIME
	bne	tmr6
	clr	cnt_5s
	clr	cnt_5s+1
	ldaa	#1
	staa	flag_5s

* in an interrupt servicing routine the x register will be saved automatically
* the rti instruction will pop the x register off stack.
*
tmr6:	
	ldx	#REGBLK	
	ldd	#TB10MS		; reload the count for 10 ms time base
	addd	toc2,x
	std	toc2,x
	bclr	tflg1,x NOTDB6
	rti
*
	
	org	JTOC2		; BUFFALO RAM vector for OC2
	jmp	tmr6oc2		; jmp to OC2 service routine

	end



