;
; Ex2.asm  ---- Example program 2 for the MiniDRAGON (c)2002, EVBplus.com
;		Written by Wayne Chu
;
;     Function:	Flash the word "HELP" on the 7-segment LED
;
#include        reg9s12.h
REGBLK:		equ	$0000
SPEED:		equ	$FFFF	; change this number to change flashing speed
;
STACK:	equ	$2000
;
        org     $E000		; program code starts at flash memory address $E000
	jmp	start
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
;
display_data:
	fcb	$11,$0e,$14,$18,$20,$20   ; 7-segment code for "HELP  "
start:
	
;       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
	
;**********


	lds	#STACK
	ldx	#REGBLK
	ldaa	#$ff
	staa	ddrh,x		; make port H an output port

	ldx	#display_data
back:	pshx
	ldaa	0,x
	jsr	seven_segment
	ldx	#REGBLK
	staa 	pth,x
	jsr	delay_250ms
	pulx
	inx
	cpx	#display_data+6
	bne	back
	jmp	start
	
*
delay_250ms:
	ldab	#250
dly1:	ldy	#6000		; 6000 x 4 = 24,000 cycles = 1ms
dly:	dey			; 1 cycle
	bne	dly		; 3 cycles
	decb
	bne	dly1
	rts
	org	$EFFE
	fdb	start
  	end
