;*********************************************************************	
; includes
;*********************************************************************	

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "include\ucore_ctrl.i"

;*********************************************************************	
; sd header is placed here
;*********************************************************************	

	.include "boot_level_sdHeader.s"

;*********************************************************************
;* start at address 32 here
;*********************************************************************

start	
	;setup irq vector
	
	movei	r0,irq
	nop
	moveih	r0,>irq
	nop
	setssr	r0,2

	;setup video (720x450)
	
	gpci	r7,2		;next 4 + X
	br	setupVideo
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;clear screen
	
	movei	r2,$00	;destination 
	movei	r3,$00
	moveih	r2,$00
	moveih	r3,$f0	;p(0,0) = f000 0000
	
	gpci	r7,2	;next 4 + X
	br	memset
	movei	r1,$33	; count		delay slot	
	movei	r0,$00	; value		delay slot
	moveih	r1,$9e	; count		delay slot
	nop		;		delay slot
	
	;draw header
	
	gpci	r7,2		;next 4 + X
	br	drawTextFormated
	movei	r0,textHeader	;delay slot
	movei	r1,0		;delay slot
	moveih	r0,>textHeader	;delay slot
	movei	r2,0		;delay slot
	
	;setup audio
	
	gpci	r7,2		;next 4 + X
	br	initAudio
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;setup audio irq
	
	movei	r0,UcIrqMsk
	movei	r1,$4			;enable audio irq
	moveih	r0,>UcIrqMsk
	nop
	st	r0,r1			;
	
	;set audio dac counter = 1 (96/2 = 48KHz)
	
	movei	r6,UcAudioDACBase
	nop
	moveih	r6,>UcAudioDACBase
	movei	r1,$1
	movei	r0,0
	moveih	r1,$2	;address 2
	
	
	stinc	r6,r0	;dac left high = 0
	nop
	stinc	r6,r0	;dac left low = 0
	nop
	stinc	r6,r0	;dac right high = 0
	nop
	stinc	r6,r0	;dac right low = 0	
	nop
	st	r6,r1	;dac address/ctrl = $0201	;dac counter = 2
	
	
	;enable irqs
	
	sei
	
	;
		
end	;push leds off
	
	movei	r7,UcRSwichtWLedG
	movei	r6,$0
	moveih	r7,>UcRSwichtWLedG
	nop
	st	r7,r6	

	br	end
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	

	
;***********************************************************************
;* irq code start
;***********************************************************************
	
irq
	cli
	
	;save all register
	
	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
		
	;irq begin
	
	;push leds on
	
	movei	r0,UcRSwichtWLedG
	movei	r1,$ff
	movei	r2,sampleAdd
	moveih	r0,>UcRSwichtWLedG
	moveih	r1,$ff
	moveih	r2,>sampleAdd
	nop
	st	r0,r1	
	rqldi	r0,0		;get sw
	nop
	nop
	ld	r0
	nop
	st	r2,r0		;store sw to sample add
	
		
	;push dac data
	
	movei	r4,sinTable
	movei	r3,0			;sintable offset
	moveih	r4,>sinTable
	movei	r5,95			;128 push ins
	
	
fillDACBuffer	
	
	;get next sample
	
	movei	r0,sampleOffset
	movei	r1,sampleAdd
	moveih	r0,>sampleOffset
	moveih	r1,>sampleAdd
	
	rqldi	r0,0	;sample offset
	rqldi	r1,0	;sample add
	nop
	ld	r6	;sample offset
	ld	r7	;sample add
	nop
	add	r6,r6,r7
	nop
	st	r0,r6	;
	
	lsri	r6,10
	nop
	or	r3,r6,r6
	movei	r7,$1
	
	;get sin value
	
	add	r0,r4,r3	
	movei	r6,UcAudioDACBase
	rqldi	r0,0
	moveih	r6,>UcAudioDACBase
	movei	r0,0
	ld	r1
	moveih	r7,$1	
			
	stinc	r6,r1	;dac left high = sin
	nop
	stinc	r6,r0	;dac left low = 0
	nop
	stinc	r6,r1	;dac right high = sin
	nop
	stinc	r6,r0	;dac right low = 0
	nop
	st	r6,r7	;dac address/ctrl = $0101	;ctrl = enable audio
	
	subi	r5,1
	brts	fillDACBuffer
	nop		;delay slot
	stinc	r6,r0	;delay slot	;dac right low = 0
	nop		;delay slot
	st	r6,r7	;delay slot	;dac address/ctrl = $0101	;ctrl = enable audio
	
	;clear stored audio irq
	
	movei	r7,UcIrqMemory
	movei	r0,4
	moveih	r7,>UcIrqMemory
	nop
	st	r7,r0	
	
	;restore all used register and go back
	
	rqpop		
	rqpop	
	rqpop	
	pop	r7
	pop	r6
	pop	r5
	
	rqpop		
	rqpop	
	rqpop	
	pop	r4
	pop	r3
	pop	r2
	
	rqpop		
	rqpop	
	rti
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	sei		;delay slot
	
sampleOffset	word	$0000
sampleAdd	word	$0100	
	
sinTable

	word	0
	word	3212
	word	6393
	word	9512
	word	12540
	word	15446
	word	18205
	word	20787
	word	23170
	word	25330
	word	27245
	word	28898
	word	30273
	word	31357
	word	32138
	word	32610
	word	32767
	word	32610
	word	32138
	word	31357
	word	30273
	word	28898
	word	27245
	word	25330
	word	23170
	word	20787
	word	18205
	word	15446
	word	12540
	word	9512
	word	6393
	word	3212
	word	0
	word	-3212
	word	-6393
	word	-9512
	word	-12540
	word	-15446
	word	-18205
	word	-20787
	word	-23170
	word	-25330
	word	-27245
	word	-28898
	word	-30273
	word	-31357
	word	-32138
	word	-32610
	word	-32768
	word	-32610
	word	-32138
	word	-31357
	word	-30273
	word	-28898
	word	-27245
	word	-25330
	word	-23170
	word	-20787
	word	-18205
	word	-15446
	word	-12540
	word	-9512
	word	-6393
	word	-3212
	
;*********************************************************************
;* functions
;*********************************************************************

	.include	"components\i2c.s"
	.include	"components\drawText.s"

;******************************
;*
;* initAudio
;*
;* return
;*
;* r0 = 0 (PASS)
;*

initAudio	

	push	r7
	
	movei	r6,audioDefault	
	movei	r5,2			;y cursor	
	moveih	r6,>audioDefault
	nop
	
initAudioI2CWriteLoop
	
	rqldi	r6,0
	rqldi	r6,1
	nop
	ld	r2		;address
	ld	r3		;data
	
	cmpeqi	r2,$ff		;stop at sentinel ($ff)
	brts	initAudioI2CWriteFinish
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
	gpci	r7,2		;next 4 + X
	br	i2cWM8731WriteWord	;r2 = address;	r3 = data; return r0 = result
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	push	r0	;result
	push	r2	;address
	push	r3	;value
	
	gpci	r7,2			;next 4 + X
	br	drawTextFormated	;r0 = text; r1 = cursor x; r2 = cursor y	
	movei	r0,i2cSetupString	;delay slot
	movei	r1,0			;x = 0		delay slot
	moveih	r0,>i2cSetupString	;delay slot
	or	r2,r5,r5		;y = y + 2	delay slot
	
	rqpop
	rqpop
	rqpop
	pop	r3
	pop	r2
	pop	r0
		
	br	initAudioI2CWriteLoop
	addi	r6,2		;next lut address	delay slot
	addi	r5,1		;next y counter		delay slot
	nop			;delay slot
	nop			;delay slot

	movei	r0,0
	
initAudioI2CWriteFinish	
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* i2cWM8731WriteWord
;*
;* r2 = address
;* r3 = data
;* 
;* return:
;*
;* r0 = result
;*

i2cWM8731WriteWord

	push	r1
	push	r6
	push	r7
	
	;start
	
	gpci	r7,2	;next 4 + X
	br	i2cSendStart
	movei	r6,UcI2CSoftPortBCtrl		;delay slot
	nop					;delay slot
	moveih	r6,>UcI2CSoftPortBCtrl		;I2C ports b	;delay slot
	nop					;delay slot
	
	;write id
	
	gpci	r7,2	;next 4 + X
	br	i2cSend8WACK
	movei	r1,$34		;WRITE			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$01		;delay slot
	nop			;delay slot
	
	;write address
	
	add	r1,r2,r2	;adr*2
	extri	r3,8
	addtqi	r1,r1,0		;+ bit8 of data
	
	gpci	r7,2	;next 4 + X
	br	i2cSend8WACK
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$02		;delay slot
	nop			;delay slot
		
	;write data low

	gpci	r7,2	;next 4 + X
	br	i2cSend8WACK
	or	r1,r3,r3	;WRITE		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	i2cSend8WACKFailsWr
	movei	r0,$ff		;delay slot
	nop			;delay slot
	moveih	r0,$04		;delay slot
	nop			;delay slot
	
	;finished without fail
	
	movei	r0,0		;all fine
	
	;stop
	
i2cSend8WACKFailsWr

	gpci	r7,2	;next 4 + X
	br	i2cSendStop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r1

	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	

;******************************
;*
;* drawChar
;* 
;* r0 	 	value
;* r1		x 	
;* r2		y
;*
;* trash r0

drawChar	

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	
	;destination base (sram base 0)
	
	muli	r1,8
	
	movei	r4,$00	;destination 
	movei	r5,$00
	moveih	r4,$00
	moveih	r5,$f0	;p(0,0) = f000 0000
	
	add	r4,r4,r1
	addtqi	r5,r5,0
	
	movei	r1,$00
	nop
	moveih	r1,$2d	;720*16
	nop
	
	mul	r1,r1,r2;*y
	gmulhi	r2
	
	add	r4,r4,r1
	addt	r5,r5,r2
		
	;font source
	
	movei	r2,$68	;source 
	movei	r3,$01
	moveih	r2,$50
	moveih	r3,$e0	;p(0,0) = e001 5068
	
	muli	r0,8		;value * 8
	nop
	add	r2,r2,r0	;source 
	addtqi	r3,r3,0		;

	;

	movei	r0,15		;16 [y]
dcYloop	
	
	movei	r1,7		;8 [x]
dcXloop
	esadr	r3,r2
	erqldi	0
	eld	r6
	nop
	
	extri	r6,15
	brts	dcNoTransp
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r6,0
	
dcNoTransp	
	
		
	esadr	r5,r4
	est	r6,0
	
	subi	r1,1		;x-
	brts	dcXloop
	addi	r4,1		;delay slot
	addtqi	r5,r5,0		;delay slot
	addi	r2,1		;delay slot
	addtqi	r3,r3,0		;delay slot
	
	;
		
	movei	r1,$c8
	nop
	moveih	r1,$2		;r1 = 720-8
	nop
	
	add	r4,r4,r1	;dest + 720-8
	addtqi	r5,r5,0		;
	
	movei	r1,$e8
	nop
	moveih	r1,$2		;r1 = 752-8
	nop	
	
	subi	r0,1		;y-
	brts	dcYloop
	add	r2,r2,r1	;source + 752-8		delay slot
	addtqi	r3,r3,0		;			delay slot
	nop			;delay slot
	nop			;delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r4
	
	rqpop
	nop
	nop
	pop	r3
	
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* r0 	 	value
;* r1		count-1 (8 words))
;* r3:r2	dest
;
memset

clearLoop

	esadr	r3,r2
	est	r0,0
	est	r0,1
	est	r0,2
	est	r0,3
	est	r0,4
	est	r0,5
	est	r0,6
	est	r0,7
	subi	r1,1
	brts	clearLoop
	addi	r2,8	;delay slot
	addtqi	r3,r3,0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* setupVideo
;*

setupVideo

	
	movei	r0,videoDefault		;source
	movei	r1,UcTimer2Ctrl		;dest-1
	moveih	r0,>videoDefault
	moveih	r1,>UcTimer2Ctrl			
	movei	r2,26			;27-1
	
setupVideoLoop

	rqldi	r0,0
	addi	r0,1
	addi	r1,1
	ld	r3
	subi	r2,1
	brts	setupVideoLoop
	st	r1,r3	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;*********************************************************************
;* data
;*********************************************************************
	
videoDefault		;AD NAME					NEW				DEFAULT
	
	word	$0001	;10 VIDEO_ON					1				1 (ON)
	word	$0097	;11 HSYNC (D = 11 .. 0)				152-1				152-1
	word	$017f	;12 HSTART (D = 11 .. 0)			384-1				384-1
	word	$017d	;13 HMEMSTART (D = 11 .. 0)			382-1				382-1
	word	$071f	;14 HSTOP (D = 11 .. 0)				1824-1				1824-1
	word	$076f	;15 HTOTAL (D = 11 .. 0)			1904-1				1904-1
	word	$0002	;16 VSYNC (D = 10 .. 0)				3-1				3-1
	word	$001e	;17 VSTART (D = 10 .. 0)			31-1				31-1
	word	$03a2	;18 VSTOP  (D = 10 .. 0)			931-1				931-1
	word	$03a3	;19 VTOTAL  (D = 10 .. 0)			932-1				932-1
	word	$0000	;1a LC ADDER LOW				$0000				$0000
	word	$0080	;1b LC ADDER HIGH				$0080				$0040	
	word	$0000	;1c LC ADDER LATCH				X				X
	word	$0000	;1d LC START (10 .. 0) Pixeloffset		$0000				$0000	
	word	$0000	;1e						X				X
	word	$0000	;1f						X				X
	word	$0008	;20 HLOADSTART 					8				8
	word	$0170	;21 HLOADSTOP					360+8				180+8
	word	$001e	;22 VLOADSTART 					31-1				31-1
	word	$03a2	;23 VLOADSTOP 					931-1				931-1
	word	$0002	;24 VLOADNEXT					2				4
	word	$0000	;25 MEM_STARTADR_store low			$0000				$0000
	word	$f000	;26 MEM_STARTADR_store high			$f000				$f000
	word	$0000	;27 MEM_LINEOFFSET_store low			$0000				$0000
	word	$0000	;28 MEM_LINEOFFSET_store high			$0000				$0000
	word	$0002	;29 MEM_MODE_store 				2				2 (X1R5G5B5X1R5G5B5)
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X				X
	

audioDefault
	
	;
				;A =  8 7654 3210
	.data   $9,$0000	;9 =  0 0000 0000 	(inactivate interface)
	.data	$0,$0097	;0 =  0 1001 0111	;left line in mute
	.data	$1,$0097	;1 =  0 1001 0111	;right line in mute
	.data	$2,$0079	;2 =  0 0111 1001	;+6db left out
	.data	$3,$0079	;3 =  0 0111 1001	;0db right out
	.data	$4,$0012	;4 =  0 0001 0010	;0=0 mic boost off;
							;1=1 enable line input mute
							;2=1 line input to adc
							;3=0 disable bypass
							;4=1 DAC select
							;5=0 side tone disable
							;76=0 -6db sidetone						
	.data	$5,$0000	;5 =  0 0000 0000	;DAC soft Mute off, no Filer
	.data	$6,$0067	;6 =  0 0110 0111 	;DAC Power, Output Power, Device Power on
	.data	$7,$0009	;7 =  0 0000 1001 	;left justified, 24 bit
	.data	$8,$001e	;8 =  0 0001 1110	;0=0 normal mode
							;1=1 bosr (384/192)
							;5432=0111 96khz
							;78=00 no clk dividing	
	.data   $9,$0001	;9 =  0 0000 0001 	(activate interface)
	.data	$ff,$0000	;stop
	
		
textHeader	

	.string		"I2C Sound push sinus test"
	word	0

i2cSetupString
	.string		"i2c write value %x to address %x result %x"
	word	0
	
textReaded
	.space	32