; Test.asm ---  Test program for the MiniDRAGON+ Rev. D board
;               (c)2005, EVBplus.com, written By Wayne Chu
;
;     Function: This is the factory test program for checking on-board
;		hardware. It checks the 2-pos DIP switches AN5-AN6,
;		2 pushbuttons AN3-AN4 and the LCD display.
;		It scans the switches and keypad. Shows their functions and
;               displays the key number on the 7-segment LED display while
;               playing a song. It allows to adjust the trimmer pot to change
;               LED display's brightness.
;
; The switches are software de-bounced for better reliability
;
; Instructions:
;       If you have a 4 x 4 keypad, connect it to the keypad header J22.
*       The middle 8 pins of the 10-pin keypad cable are keypad connections.
*       The pin 1 and pin 10 are not used.
*
;	1. After running the test program, the 7-segment LED display will
;	   count up from hex number 0 to F and the music will come out.
;          LCD display shows:	" S1=UP, S2=DOWN "
;                               "S3=FAST, S4=SLOW"

;	2. press the S1 pushbutton, the number counts up from hex 0 to F.
;	   press the S2 pushbutton, the number counts down from hex F to 0.
;
;	3. Short the S3, the count speed is 250ms per count (fast).
;	   Short the S4, the count speed is 750ms per count (slow).
;
;	4. Adjust the trimmer pot, the 7 segment LED display's brightness 
;	   should change.
;       5. Press the keypad, the 7-segment LED display will show key number.

;  LCD routines use 4-bit transfer via port M
;  PM3 ------- RS ( register select, 0 = register transfer, 1 = data transfer).
;  PM2 ------- Enable ( write pulse )
;  PM4 ------- Data Bit 4 of LCD
;  PM5 ------- Data Bit 5 of LCD
;  PM6 ------- Data Bit 6 of LCD
;  PM7 ------- Data Bit 7 of LCD
;
; Timing of 4-bit data transfer is shown on page 11 of the Seiko LCD
; application note on the distribution CD. The file name is Seikolcd.pdf
;
; WARNING: Beware of pinouts of LCD modules. The pin 1 and pin 2 of some new
;          modules are swapped. If you connect a wrong polarity LCD to the
;          MiniDRAGON, it could be damaged.
;
; There are two different keypads have been used with Wytec development boards.
; The following signal definitions only apply to Grayhill 4X4 keypad:

; PA0 connects ROW0 of the keypad via pin 1 of the 8-pin keypad header J29
; PA1 connects ROW1 of the keypad via pin 2 of the 8-pin keypad header J29
; PA2 connects ROW2 of the keypad via pin 3 of the 8-pin keypad header J29
; PA3 connects ROW3 of the keypad via pin 4 of the 8-pin keypad header J29

; PA4 connects COL0 of the keypad via pin 5 of the 8-pin keypad header J29
; PA5 connects COL1 of the keypad via pin 6 of the 8-pin keypad header J29
; PA6 connects COL2 of the keypad via pin 7 of the 8-pin keypad header J29
; PA7 connects COL3 of the keypad via pin 8 of the 8-pin keypad header J29

; The PA0-PA7 has a 100K pull-up resistor in each line.

; The following signal definitions only apply to Wytec 4X4 membrane keypad:

; PA0 connects COL0 of the keypad via pin 1 of the 8-pin keypad header J29
; PA1 connects COL1 of the keypad via pin 2 of the 8-pin keypad header J29
; PA2 connects COL2 of the keypad via pin 3 of the 8-pin keypad header J29
; PA3 connects COL3 of the keypad via pin 4 of the 8-pin keypad header J29

; PA4 connects ROW0 of the keypad via pin 5 of the 8-pin keypad header J29
; PA5 connects ROW1 of the keypad via pin 6 of the 8-pin keypad header J29
; PA6 connects ROW2 of the keypad via pin 7 of the 8-pin keypad header J29
; PA7 connects ROW3 of the keypad via pin 8 of the 8-pin keypad header J29

; The PA0-PA7 has an internal pull-up resistor on each line.

; To use the Grayhill keypad, remark the label Wytec_keypad and un-remark
; the label Grayhill_keypad

Wytec_keypad:		equ     1       ; use Wytec_keypad
;Grayhill_keypad:        equ     1      ; not to use Grayhill_keypad



SW1:	equ	$10		; AN4	
SW2:	equ	$08		; AN3	
SW3:	equ	$20		; AN5	
SW4:	equ	$40		; AN6	
;

MULTI_MODE:	equ	$10
SINGLE_MODE:	equ	0
SCAN_MODE:	equ	$20
NO_SCAN_MODE:	equ	0
CHANNEL_NUM:	equ	7		; reading input from AN07

DB0:		equ	1
DB1:		equ	2
DB2:		equ   	4
DB3:		equ   	8
DB4:		equ	$10
DB5:		equ	$20
DB6:		equ	$40
DB7:		equ	$80

OL5:		equ	DB2
OM5:		equ	DB3

ONE_MS:		equ	4000		; 4000 x 250ns = 1 ms at 24 MHz bus speed
FIVE_MS:	equ	20000
TEN_MS:		equ	40000
FIFTY_US:	equ	200

COUNT_UP:	equ	1
COUNT_DOWN:	equ	2
FAST:		equ	250		; 250 ms, fast counting speed
SLOW:		equ	750		; 750 ms, slow counting speed

TB1MS:		equ	24000		; 1ms time base of 24,000 instruction cycles
;					; 24000 x 1/24MHz = 1ms at 24 MHz bus speed
F3500HZ:	equ	3429
REGBLK:		equ	$0
#include        reg9s12.h		; include register equates



	org	$1000

ram:		rmb	1
temp:           rmb     1
spk_tone:	rmb	2
sound_dur:	rmb	1
xsound_save:	rmb	2
sound_repeat:	rmb	1
xsound_beg:	rmb	2
sound_start:	rmb	1
rest_note:	rmb	1
d1ms_flag:	rmb	1
xsave:		rmb	2
adctl_image:	rmb	1
brtness:	rmb	1
count_5ms:	rmb	1
count10:	rmb	1
disp_data:	rmb	1
loop_count:	rmb	2
direction:	rmb	1
speed:		rmb	2
switch_flag:	rmb	1
key_flag:       rmb     1
pmimg:		rmb	1
temp1:		rmb	1
ramend:		rmb	1
ALLRAM:		equ 	ramend-ram 	;total ram used

REG_SEL:	equ	DB3		; 0=reg, 1=data
NOT_REG_SEL:	equ	$F7     	; 11110111
ENABLE:		equ     DB2
NOT_ENABLE:	equ     $FB             ; 11111011

LCD:		equ	ptm
LCD_RS:		equ	ptm
LCD_EN:		equ	ptm

LCDimg:		equ	pmimg
LCD_RSimg:	equ	pmimg
LCD_ENimg:	equ	pmimg
STACK:	equ	$2000
;
; Segment conversion table:

; Binary number:	 	0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
; Converted to 7-segment char: 	0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
;
; Binary number:		$10,$11,$12,$13,$14,$15,$16,$17
; Converted to 7-segment char:   G   H   h   J   L   n   o   o
;
; Binary number:  		$18,$19,$1A,$1B,$1C,$1D,$1E,$1F,$20
; Converted to 7-segment char:   P   r   t   U   u   y   _  --  Blank


; for 24 MHz bus speed
; 1200000 / 261.63 Hz = note count
;
c3	equ	45866		; 261.63 Hz at 24 MHz
c3s	equ	43293		; 277.18 Hz at 24 MHz
d3	equ	40864		; 293.66 Hz at 24 MHz
d3s	equ	38569		; 311.13 Hz at 24 MHz
e3	equ	36404		; 329.63 Hz at 24 MHz
f3	equ	34361		; 349.23 Hz at 24 MHz
f3s	equ	32433		; 369.99 Hz at 24 MHz
g3	equ	30613		; 391.99 Hz at 24 MHz
g3s	equ	28894		; 415.31 Hz at 24 MHz
a3	equ	27273		; 440.00 Hz at 24 MHz
a3s	equ	25742		; 466.16 Hz at 24 MHz
b3	equ	24297		; 493.88 Hz at 24 MHz

c4	equ	22934		; 523.25 Hz at 24 MHz
c4s	equ	21646		; 554.37 Hz at 24 MHz
d4	equ	20431		; 587.33 Hz at 24 MHz
d4s	equ	19285		; 622.25 Hz at 24 MHz
e4	equ	18202		; 659.26 Hz at 24 MHz
f4	equ	17181		; 698.46 Hz at 24 MHz
f4s	equ	16216		; 739.99 Hz at 24 MHz
g4	equ	15306		; 783.99 Hz at 24 MHz
g4s	equ	14447		; 830.61 Hz at 24 MHz
a4	equ	13636		; 880.00 Hz at 24 MHz
a4s	equ	12871		; 932.32 Hz at 24 MHz
b4	equ	12149		; 987.77 Hz at 24 MHz

c5	equ	11467		; 1046.50 Hz at 24 MHz
c5s	equ	10823		; 1108.73 Hz at 24 MHz
d5	equ	10216		; 1174.66 Hz at 24 MHz
d5s	equ	9642		; 1244.51 Hz at 24 MHz
e5	equ	9101		; 1318.51 Hz at 24 MHz
f5	equ	8590		; 1396.91 Hz at 24 MHz
f5s	equ	8108		; 1479.98 Hz at 24 MHz
g5	equ	7653		; 1567.98 Hz at 24 MHz
g5s	equ	7225		; 1661.22 Hz at 24 MHz
a5	equ	6818		; 1760.00 Hz at 24 MHz
a5s	equ	6435		; 1864.66 Hz at 24 MHz
b5	equ	6074		; 1975.53 Hz at 24 MHz

c6	equ	5733		; 2093.00 Hz at 24 MHz
c6s	equ	5412		; 2217.46 Hz at 24 MHz
d6	equ	5109		; 2349.32 Hz at 24 MHz
d6s	equ	4821		; 2489.02 Hz at 24 MHz
e6	equ	4551		; 2637.02 Hz at 24 MHz
f6	equ	4295		; 2793.83 Hz at 24 MHz
f6s	equ	4054		; 2959.96 Hz at 24 MHz
g6	equ	3827		; 3135.97 Hz at 24 MHz
g6s	equ	3612		; 3322.44 Hz at 24 MHz
a6	equ	3409		; 3520.00 Hz at 24 MHz
a6s	equ	3218		; 3729.31 Hz at 24 MHz
b6	equ	3037		; 3951.07 Hz at 24 MHz

c7	equ	2867		; 4186.01 Hz at 24 MHz
c7s	equ	2706		; 4434.92 Hz at 24 MHz
d7	equ	2554		; 4698.64 Hz at 24 MHz
d7s	equ	2411		; 4978.03 Hz at 24 MHz
e7	equ	2275		; 5274.04 Hz at 24 MHz
f7	equ	2148		; 5587.66 Hz at 24 MHz
f7s	equ	2027		; 5919.92 Hz at 24 MHz
g7	equ	1913		; 6271.93 Hz at 24 MHz
g7s	equ	1806		; 6644.88 Hz at 24 MHz
a7	equ	1705		; 7040.00 Hz at 24 MHz
a7s	equ	1609		; 7458.63 Hz at 24 MHz
b7	equ	1519		; 7902.13 Hz at 24 MHz
c8	equ	1		; for rest note

note_c	equ	0
note_cs	equ	1
note_d	equ	2
note_ds	equ	3
note_e	equ	4
note_f	equ	5
note_fs	equ	6
note_g	equ	7
note_gs	equ	8
note_a	equ	9
note_as	equ	10
note_b	equ	11

; dur18= 1/8 note,  dur14= 1/4 note,  $fe= rest_note, $ff = end of song
	
dur18	equ	50
dur14	equ	100
	

	org	$400		; code starts at EEPROM memory address $0400
	jmp	start
;
NOTE_TABLE:
	fdb	c3,c3s,d3,d3s,e3,f3,f3s,g3,g3s,a3,a3s,b3
	fdb	0,0,0,0		; dummy byte
	fdb	c4,c4s,d4,d4s,e4,f4,f4s,g4,g4s,a4,a4s,b4
	fdb	0,0,0,0		; dummy byte
	fdb	c5,c5s,d5,d5s,e5,f5,f5s,g5,g5s,a5,a5s,b5
	fdb	0,0,0,0		; dummy byte
	fdb	c6,c6s,d6,d6s,e6,f6,f6s,g6,g6s,a6,a6s,b6
	fdb	0,0,0,0		; dummy byte
	fdb	c7,c7s,d7,d7s,e7,f7,f7s,g7,g7s,a7,a7s,b7
	fdb	0,0,0,0		; dummy byte
	fdb	c8
;

segm_ptrn:						; segment pattern
	fcb     $3f,$06,$5b,$4f,$66,$6d,$7d,$07		;0-7
;		 0,  1,  2,  3,  4,  5,  6,  7
        fcb     $7f,$6f,$77,$7c,$39,$5e,$79,$71		;8-$0f
;		 8,  9,  A,  b,  C,  d,  E,  F
       	fcb     $3d,$76,$74,$1e,$38,$54,$63,$5c		;10-17
;		 G,  H,  h,  J   L   n   o   o
       	fcb     $73,$50,$78,$3e,$1c,$6e,$08,$40		;18-1f
;		 P,  r,  t,  U,  u   Y   -   -
       	fcb     $00,$01,$48,$41,$09,$49			;20-23
;		blk, -,  =,  =,  =,  =
;
seven_segment:
	pshx
	pshb
	ldx	#segm_ptrn
	psha
	anda	#$3f
	tab
	abx
	ldaa	0,x		; get segment
	pulb
	andb	#$80		; add DP
	aba
	pulb
	pulx
	rts
;
#ifdef  Wytec_keypad
; keypad scan for Wytec 4X4 membrane keypad (pin 1-4 = col 0-3, pin 5-8 = row 0-3)
; at exit:  if a key is down, the carry bit =1 and the accumulator b
;	    holds the key number
;	    if no key is dwon the carry bit =0

keypad:
;	ldaa 	#00001111b
	ldaa 	#$0F		; pa0-pa3 are outputs, pa4-pa7 are inputs
	staa 	ddra

	ldab	#15
;	ldaa	#11110111b
	ldaa	#$F7		; pa3=low, pa0-pa2=high
	staa	temp		; save it at temp
next_row:
	staa	porta
	ldaa	#10		; add delay before checking key down
k_dly:	deca
	bne	k_dly

	ldaa	porta
	anda	#$F0		; only read 4 MSBs pa4-pa7
	cmpa	#$F0
	bne	keyin		; a key is pressed
	decb
	cmpb	#11
	beq	no_keyin	; after 4 tests, accu B will be 11

 	ror	temp
	ldaa	temp
	jmp	next_row

no_keyin:
	clc
	rts			; no key input

keyin:	rola
 	bcc	key_ok
 	subb    #4
 	jmp     keyin
key_ok:	sec
	rts
#endif
;

#ifdef  Grayhill_keypad
;
; keypad scan for Grayhill 4x4 keypad (pin 1-4 = row 0-3, pin 5-8 = col 0-3)
; at exit:  if a key is down, the carry bit =1 and the accumulator b
;	    holds the key number
;	    if no key is dwon the carry bit =0

keypad:
;	ldaa 	#00001111b
	ldaa 	#$0F		; pa0-pa3 are outputs, pa4-pa7 are inputs
	staa 	ddra

	ldab	#16
;	ldaa	#11110111b
	ldaa	#$F7		; pa3=low, pa0-pa2=high
	staa	temp		; save it at temp
next_row:
	staa	porta
	ldaa	#10		; add delay before checking key down
k_dly:	deca
	bne	k_dly

	ldaa	porta
	anda	#$F0		; only read 4 MSBs pa4-pa7
	cmpa	#$F0
	bne	keyin		; a key is pressed
	subb	#4
	beq	no_keyin	; after 4 tests, accu B will be 0

	ror	temp
	ldaa	temp
	jmp	next_row

no_keyin:
	clc
	rts			; no key input

keyin:	decb
	rola
	bcs	keyin
	sec
	rts
#endif



MSG1:   FCC     " S1=UP, S2=DOWN "
MSG2:   FCC     "S3=FAST, S4=SLOW"
lcd_ini:
	ldaa	#$ff
	staa	ddrm		; port M = output
	clra
	staa	pmimg
	staa	ptm

       	ldx	#inidsp 	; point to init. codes.
	pshb            	; output instruction command.
       	jsr    	sel_inst
       	ldab   	0,x
       	inx
onext:	ldaa   	0,x
       	jsr    	wrt_nibble 	; initiate write pulse.
       	inx
	jsr	delay_5ms       ; every nibble is delayed for 5ms
       	decb                    ; in reset sequence to simplify coding
       	bne    	onext
       	pulb
       	rts

inidsp: fcb	12		; number of high nibbles
*				; use high nibbles only, low nibbles are ignored
	fcb	$30		; 1st reset code, must delay 4.1ms after sending
        fcb	$30		; 2nd reste code, must delay 100us after sending

; all following 10 nibbles must be delay 40us each after sending
	fcb     $30             ; 3rd reset code,
	fcb	$20		; 4th reste code,
        fcb	$20   		; 4 bit mode, 2 line, 5X7 dot
	fcb	$80   		; 4 bit mode, 2 line, 5X7 dot
        fcb	$00		; cursor increment, disable display shift
	fcb	$60		; cursor increment, disable display shift
        fcb	$00		; display on, cursor off, no blinking
	fcb	$C0		; display on, cursor off, no blinking
	fcb	$00		; clear display memory, set cursor to home pos
	fcb	$10		; clear display memory, set cursor to home pos
*
sel_data:
	psha
;	bset	LCD_RSimg REG_SEL	; select instruction
	ldaa	LCD_RSimg
	oraa	#REG_SEL
	bra	sel_ins
sel_inst:
	psha
;	bclr	LCD_RSimg REG_SEL	; select instruction
	ldaa	LCD_RSimg
	anda	#NOT_REG_SEL
sel_ins:
	staa	LCD_RSimg
	staa	LCD_RS
	pula
        rts

lcd_line1:
	jsr    	sel_inst		; select instruction
       	ldaa   	#$80     		; starting address for the line1
	bra	line3
lcd_line2:
	jsr    	sel_inst
       	ldaa   	#$C0     		; starting address for the line2
line3: 	jsr    	wrt_byte

       	jsr    	sel_data
	jsr	msg_out
	rts
;
; at entry, x must point to the begining of the message,
;           b = number of the character to be sent out

msg_out:
	ldaa   	0,x
       	jsr    	wrt_byte
       	inx
       	decb
       	bne    	msg_out
	rts

wrt_nibble:
       	anda   	#$f0     		; mask out 4 low bits
       	staa   	temp1 			; save high nibble
       	ldaa   	LCDimg    		; get LCD port image
       	anda   	#$0C     		; need DB2 and DB3
       	oraa   	temp1    		; add it with high nibble
        staa   	LCDimg    		; save it
        staa   	LCD    			; output data to LCD port
        jsr	enable_pulse
 	rts
*

;       @ enter, a=data to output
;
wrt_byte:
	pshx
       	psha        			; save it tomporarily.
       	anda   #$f0     		; mask out 4 low bits.
       	staa   temp1 			; save nibble value.
       	ldaa   LCDimg    		; get LCD port image.
       	anda   	#$0C     		; need DB2 and DB3
       	oraa   temp1    		; add in low 4 bits.
       	staa   LCDimg    		; save it
       	staa   LCD    			; output data
;
	bsr	enable_pulse
	pula
 	asla            		; move low bits over.
	asla
	asla
	asla
       	staa   	temp1			; store temporarily.
;
       	ldaa   	LCDimg 			; get LCD port image.
       	anda   	#$0C     		; need DB2 and DB3
        oraa   	temp1  			; add in loiw 4 bits.
       	staa   	LCDimg    		; save it
       	staa   	LCD    			; output data
;
	bsr	enable_pulse
	jsr	delay_50us
 	pulx
       	rts
;
enable_pulse:
;	bset	LCD_ENimg ENABLE	; ENABLE=high
	ldaa	LCD_ENimg
	oraa	#ENABLE
	staa	LCD_ENimg
	staa	LCD_EN
	dey                             ; time delay for ENABLE pulse
	iny
	dey
	iny
;	bclr	LCD_ENimg ENABLE	; ENABLE=low
	ldaa	LCD_ENimg
	anda	#NOT_ENABLE
	staa	LCD_ENimg
	staa	LCD_EN
        rts
;
;
; this routine will read adc input on the pin AN07 and store 4 consecutive 
; 
adc_conv:
	adda	#SINGLE_MODE+NO_SCAN_MODE
;
; if you want to read multi-channel input, change above statement to
;	adda	#MULTI_MODE+NO_SCAN_MODE
;
	staa	adctl_image
	ldx	#REGBLK
	jsr	conv
	rts
	
conv:	ldaa	adctl_image
	staa	atd0ctl5,x
not_ready:
	brclr	atd0stat,x $80 not_ready
	rts
delay_10ms:  
	pshx
	ldx     #TEN_MS
  	bsr	del1
	pulx
	rts
delay_5ms:
	pshx
	ldx     #FIVE_MS
  	bsr	del1
	pulx
	rts
delay_50us:
	pshx
	ldx     #FIFTY_US
  	bsr	del1
	pulx
	rts
;
; 250ns delay at 24MHz bus speed  (6 cycles x 41.67ns/cyl =250ns)
;
del1:	dex				; 1 cycle
	inx				; 1 cycle
	dex				; 1 cycle
	bne	del1			; 3 cylces
	rts


clrall:	pshx
	ldx	#ram
clrr	clr	0,x
	inx
	deca
	bne	clrr
	pulx
	rts
;
start:	lds	#STACK
;	The internal PLL clock lets us set the speed of the processor.
;;      If you do not set the speed of the PLL as we are doing here, the
;;      default bus speed speed will be 4 Mhz (half the OscFreq).
;;
;;      The math used to set the PLL frequency:
;;        OscFreq = 8Mhz
;;        initSYNR = 2      PLL multiplier
;;        initREFDV = 0     PLL divider - 1
;;        PLLCLK = 2 * OscFreq * (initSYNR+1) / (initREFDV+1)
;;      Usually the OscFreq is 8Mhz, so we have PLLCLK = 2*8*3/1 = 48Mhz
;;      The bus clock runs at half of the PLL speed:
;;      Bus Clock = PLLCLK / 2 = 24 Mhz (for an OscFreq of 8 Mhz)
;;
;;      The effective cycle clock you should use for timing instructions is the
;;      bus clock of 24 Mhz. This is currently the fastest speed this device
;;      can safely use. Overclocking is not recommended.
;;
;;      If you set a slower speed, it will reduce power consumption. If low
;;      power consumption is important (like for battery-powered circuits), you
;;      should also put the processor to sleep as often as possible.
;;
;;      Set the PLL speed:
;;

;********PLL code for 24MHz bus speed from a 4/8/16 crystal
	sei
	ldx	#REGBLK
	bclr	clksel,x,%10000000	; clear bit 7, clock derived from oscclk
	bset	pllctl,x,%01000000	; Turn PLL on, bit 6 =1 PLL on, bit 6=0 PLL off
	ldaa	#$05			; 5+1=6 multiplier 
	staa	synr,x
	ldaa	#$03			; 3+1=4 divisor, 6/4 * 16 = 24Mhz for 16 MHz crystal
;	ldaa	#$01			; 1+1=2 divisor, 6/2 *  8 = 24Mhz for  8 MHz crystal
;	ldaa	#$00			; 0+1=1 divisor, 6/1 *  4 = 24Mhz for  4 MHz crystal 
	staa	refdv,x
wait_b3:
	brclr	crgflg,x,%00001000,wait_b3 	; Wait until bit 3 = 1
	bset	clksel,x,%10000000
	
;**********
	
	ldx	#timer6
	stx	$3E62		; initialize the int vetctor
	ldx	#timer5_spk
	stx	$3E64
	
	ldx	#REGBLK
	ldaa	#$ff		
	staa	ddrh,x		; porth = output
	clr	pth,x		; turn off LED display
 	bset	pucr,x,1	; enable pullup on portA
;
	ldaa	#$ff		; enable the digital input buffer
	staa	atd0dien,x	; ATD0 port.

	ldaa	#$80
	staa	tscr,x		; enable timer
	ldaa	#DB5+DB6
	staa	tios,x		; select t6 as an output compare	 
	staa	tmsk1,x

	bset 	atd0ctl2,x $80	; enable adc operation
	bset	atd0ctl3,x $40	; 8 conversion needed for AN07
	
	ldx	#F3500HZ	; 3.5kHz
	stx	spk_tone
	jsr	spk_off

	ldaa	#ALLRAM
	jsr	clrall
;
 	jsr	delay_10ms		; delay 20ms during power up
    	jsr	delay_10ms

    	jsr	lcd_ini			; initialize the LCD

	ldx    	#MSG1			; MSG1 for line1, x points to MSG1
        ldab    #16                     ; send out 16 characters
    	jsr	lcd_line1

    	ldx    	#MSG2			; MSG2 for line2, x points to MSG2
        ldab    #16                     ; send out 16 characters
     	jsr	lcd_line2

	cli
	jsr	spk_on
	jsr	start_sound	; start sound
	
	ldaa	#1
	staa	disp_data
	ldaa	#COUNT_UP
	staa	direction
	ldy	#SLOW
	sty	speed

	ldx	#REGBLK
begin:	ldaa	portad0,x		; get AD0 value.
	anda	#SW1+SW2+SW3+SW4
	cmpa	#SW1+SW2+SW3+SW4
	beq	no_switch			; no key down
	ldaa	switch_flag
	bne	continue		; key was down
	jsr	delay_10ms
	ldaa	portad0,x		; get AD0 value.
	anda	#SW1+SW2+SW3+SW4
	cmpa	#SW1+SW2+SW3+SW4
	beq	no_switch			; no key is down < 10ms, it was noice

; key is down more than 10 ms, it's valid 
	ldab	#1
	stab	switch_flag		; set key down flag

	tab
	anda	#SW1			; test Sw1
	beq	sw1_down
		
	tba
	anda	#SW2			; test Sw2
	beq	sw2_down
		
	tba
	anda	#SW3			; test Sw3
	beq	sw3_down
	
	tba
	anda	#SW4			; test Sw4
	beq	sw4_down
	jmp	continue

sw1_down:			
	ldaa	#COUNT_UP
	staa	direction
	jmp	continue
	
sw2_down:			
	ldaa	#COUNT_DOWN
	staa	direction
	jmp	continue
	
sw3_down:			
	ldy	#FAST
	sty	speed
	jmp	continue
	
sw4_down:			
	ldy	#SLOW
	sty	speed
	jmp	continue

no_switch:
	clr 	switch_flag
continue:
	ldaa	#CHANNEL_NUM	; set channel number before calling 
	jsr	adc_conv	
	ldaa	adr07h+REGBLK
	staa	brtness
;
	jsr     keypad
	bcc	nokey
	ldaa    Key_flag
	bne     key_down
   	jsr	delay_10ms
	jsr 	keypad
	bcc	nokey
	ldaa    #1
	staa    key_flag
; key down
key_down:
	tba
	jmp     key_out
	
nokey:  clr	key_flag
        ldaa	disp_data
key_out:
	jsr	seven_segment	; convert Accu A to segment pattern, no DP
	ldx	#REGBLK
	staa	pth,x
	ldaa	brtness		; was read from adc
	beq	turn_off	; if =0, turn off display 

back:	ldx	#13		; make approx. 3.25 us delay per ADC count
back1:	dex			; 1 cycle
	inx			; 1 cycle
	dex			; 1 cycles
	bne	back1		; 3 cycles
	deca
	bne	back

turn_off:
	ldx	#REGBLK
	clr	pth,x
;
wait:	tst	d1ms_flag
	beq	wait
	clr	d1ms_flag
	ldy	loop_count
	iny
	sty	loop_count
	cpy	speed
	blt	jmpbeg
	ldy	#0
	sty	loop_count
	ldab	direction
	cmpb	#COUNT_DOWN
	beq	check_down
	ldaa	disp_data
	inca
	anda	#$0f
	staa	disp_data
jmpbeg:	jmp	begin
;
check_down:
	ldaa	disp_data
	deca
	anda	#$0f
	staa	disp_data
	jmp	begin
;
spk_on:	
	pshx
	ldx	#REGBLK
	bclr 	tctl1,x OM5
	bset 	tctl1,x OL5		; toggle speaker
	pulx
	rts
spk_off:
	pshx
	ldx	#REGBLK
	bset 	tctl1,x OM5
	bclr 	tctl1,x OL5		; turn off speaker
	pulx
	rts

timer6:
	inc	count_5ms
	ldaa	count_5ms
	cmpa	#5
	bne	tmr3
	clr	count_5ms
;
; processing every 5ms
	ldaa	sound_start
	beq	tmr3
	ldaa	sound_dur		; duration
	deca
	staa	sound_dur
	bne	tmr3
	ldx	xsound_save
repeat:	ldab	0,x
	cmpb	#255
	beq	sound_end
	ldaa	1,x
	cmpa	#255
	beq	sound_end
	staa	sound_dur
	inx
	inx
	stx	xsound_save
	cmpb	#$fe
	bne	not_rest
	ldaa	#1
	staa	rest_note
	jsr	spk_off
	jmp	tmr3
not_rest:
	clr	rest_note
	jsr	spk_on
	ldx	#NOTE_TABLE
	aslb
	abx
	ldx	0,x
	stx	spk_tone
	jmp	tmr3	
sound_end:
	ldaa	sound_repeat
	beq	no_rep
	ldx	xsound_beg
	jmp	repeat

no_rep:	ldx	#F3500HZ	; 3.5kHz
	stx	spk_tone
	jsr	spk_off
	clr	sound_start

tmr3:	ldx	#REGBLK		; in interrupt servicing routine
	inc	d1ms_flag
	ldd	#TB1MS		; 1 ms time base
	addd	tc6,x
	std	tc6,x
	ldaa	#DB6
	staa	tflg1,x		; clear flag
	rti

timer5_spk:
	ldx	#REGBLK		; in interrupt servicing routine
	ldd	spk_tone
	addd	tc5,x
	std	tc5,x
	ldaa	#DB5
	staa	tflg1,x		; clear flag
	rti

start_sound:
	ldx	#SONG
	stx	xsound_beg
	ldaa	#1
	staa	sound_repeat
	ldab	0,x
	ldaa	1,x
	staa	sound_dur
	inx
	inx
	stx	xsound_save

	ldx	#NOTE_TABLE
	aslb
	abx
	ldx	0,x
	stx	spk_tone
	ldaa	#1
	staa	sound_start
	rts

SONG:	fcb	$20+note_e,dur18
	fcb	$20+note_ds,dur18
	fcb	$20+note_e,dur18
	fcb	$20+note_ds,dur18
	fcb	$20+note_e,dur18
	fcb	$10+note_b,dur18
	fcb	$20+note_d,dur18
	fcb	$20+note_c,dur18
	fcb	$10+note_a,dur14
;	fcb	$fe,dur18
;	fcb	255,255

	fcb	$00+note_e,dur18
	fcb	$00+note_a,dur18
	fcb	$10+note_c,dur18
	fcb	$10+note_e,dur18
	fcb	$10+note_a,dur18
	fcb	$10+note_b,dur14
	fcb	$00+note_gs,dur18
	fcb	$10+note_d,dur18
	fcb	$10+note_e,dur18
	fcb	$10+note_gs,dur18
	fcb	$10+note_b,dur18
	fcb	$20+note_c,dur14

	fcb	$00+note_e,dur18
	fcb	$00+note_a,dur18
	fcb	$10+note_e,dur18
	fcb	$fe,dur14
	fcb	255,255

	end

