*
* Tutor2.asm -- Tutorial program 2 for FOX11 board
*               with Wytec Phantom Monitor, (c)2003, EVBplus.com
*		Written By Wayne Chu
*
*     Function:	This program flashes the PB5 LED when the PA0 switch is down.
*		It shows how to use a subroutine call and change the contents
*		of the RAM location \fls_rate to vary flash rate.
*
*     Instructions: 
*		1. Use B \pa0_low command to set a breakpoint at the address
*		   \pa0_low.
*		2. Enter G \start command to run the program
*		   and nothing should happen. Then press and hold down the 
*		   switch PA0, the program should stop at the
*		   address \pa0_low, because the PA0 is normally high,
*		   it becomes low when it's down.
*		3. Remove the breakpoint address \pa0_low by using K
*		   command, such as K \pa0_low. After the breakpoint is
*		   removed, then enter G command to run the program again.
*               4. At this time, the PB5 LED should flash when the pa0 switch
*		   is down.
*		5. Stop the program by ESC key and use E command to
*		   change the value of the \fls_rate from $8B to $FF,
*		   such as E \fls_rate.
*		6. Run the program by entering G command and the LED should
*		   flash slower when the PA0 switch is down.
*		7. Repeat the steps 5 and 6 to change the fls_rate from 
*		   $FF to $40, and the LED should flash faster.
*
*
STACK:  	equ     $FF     ; STACK at top of data ram 
FLS_CONST: 	equ     25000   ; delay 100 ms, change this number
*                               ; will change LED flash rate
*				; 25000 x 8 cycles = 200,000 cycles = 100ms
*
PA0:		equ	1
PB0:		equ	1
PB5:		equ	$20
porta:		equ	0
portb:		equ	$1404
*
REGBLK: 	equ   	$1000
*
        	org     $40     ; data ram starts at address $40
*
fls_rate:	rmb     2	; 2 byte temp storage
*
        org     $F000           ; program code starts here
*
	
start:  lds     #STACK          ; initialize STACK point
        ldy     #FLS_CONST      ; y register = LED FLS_CONST
        sty     fls_rate        ; save FLS_CONST at fls_rate

        ldx     #REGBLK         	; x register points to register block
begin:	brclr	porta,x PA0 pa0_low	; pa0=low when pa0 switch is down
	ldaa	#PB0
	staa	portb
	jmp	begin

pa0_low:
	clr	portb		; turn off LED PB5
        jsr     delay100ms      ; keep LED off for 100 ms
	ldaa	#PB5
	staa	portb	        ; turn on LED
        jsr     delay100ms      ; keep LED lit for 100 ms
	jmp	begin


delay100ms:
	pshx
	ldx     fls_rate 	; re-store y register with FLS_CONST
*
* the delay loop is 8 cycles long = 4us at 8 MHz crystal, 2 MHz bus speed
dly:    dex			; 3 cycles
	nop			; 2 cycle
        bne     dly             ; 3 cylces
	pulx
        rts                     ; 100ms delay is done

	org	$FFFE
	fdb	start
	end



