	
	;***********************************************************************
	;* reset start
	;***********************************************************************
	
	.org	0
	
	.def	cmdToReciveWait	2
	
	;***********************************************************************
	;* user code start
	;***********************************************************************
	
userLevel

	nop
	nop
	nop
	nop
	nop

	gpci	r7,2		;next 4 + X
	br	setupVideo
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	gpci	r7,2
	br 	sleep
	movei	r0,9		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD0 reset -> no respons

	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,0		;cmd0		delay slot
	movei	r1,0		;arg low	delay slot
	movei	r2,0		;arg high	delay slot
	movei	r3,0		;48 bit		delay slot
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;2ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD8 reset -> ignore response

	movei	r3,0		;48 bit		
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,8		;cmd0		delay slot
	movei	r1,$aa		;arg low	delay slot
	movei	r2,0		;arg high	delay slot
	moveih	r1,1		;2.7-3.6V		delay slot
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;2ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- loop bingup while busy
	
	movei	r7,$f	;max 15 times
	nop
	
bringupSDBusy	

	push	r7
	
	;--
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD55 (switch to app) -> check respons
	
	;send
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,55		;cmd55		delay slot
	movei	r1,0		;arg low	delay slot
	movei	r2,0		;arg high	delay slot
	movei	r3,0		;48 bit		delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,55		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- ACMD41 (bringup) -> check respons
	
	;--
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;send
	
	movei	r0,41		;cmd41		
	movei	r1,0		;arg low	
	movei	r2,$ff		;VALL	arg high
	moveih	r1,0		;arg low	
	moveih	r2,$40		;HCS	arg high		
	movei	r3,0		;48 bit		
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	;r2 = recived arg low
	;r3 = recived arg high
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,41		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;
	
	rqpop
	nop
	nop
	pop	r7	;busy count
	
	;test busy flag
	
	extri	r3,15		;MSB in high result
	brts	bringupSDFinished	;not busy
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;its busy so try again
	
	subi	r7,1
	brts	bringupSDBusy
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;time out for CMD55, ACMD41
	
	gpci	r7,2		;next 4 + X
	br	drawText
	movei	r0,stringErrorEverBusy		;delay slot
	movei	r1,0				;delay slot
	moveih	r0,>stringErrorEverBusy		;delay slot
	movei	r2,0				;delay slot
	
	br	loop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
bringupSDFinished	
	
	;card is read \o/
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD2 (get CID) -> check respons
	
	;send
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,2		;cmd2		delay slot
	movei	r1,0		;arg low	delay slot
	movei	r2,0		;arg high	delay slot
	movei	r3,1		;136 bit		delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,2		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;print cid
	
	rqldi	r5,7
	rqldi	r5,6
	rqldi	r5,5
	ld	r0
	ld	r1
	ld	r2
	
	push	r0
	push	r1
	push	r2
	
	rqldi	r5,4
	rqldi	r5,3
	rqldi	r5,2
	ld	r0
	ld	r1
	ld	r2
	
	push	r0
	push	r1
	push	r2
	
	rqldi	r5,1
	rqldi	r5,0
	nop
	ld	r0
	ld	r1
	
	push	r0
	push	r1
	
	gpci	r7,2		;next 4 + X
	br	drawText
	movei	r0,stringCID		;delay slot
	movei	r1,0				;delay slot
	moveih	r0,>stringCID		;delay slot
	movei	r2,0				;delay slot
	
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	nop
	nop
	nop
	
	;get relative address
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD3 (get RCA) -> check respons
	
	;send
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,3		;cmd3		delay slot
	movei	r1,0		;arg low	delay slot
	movei	r2,0		;arg high	delay slot
	movei	r3,0		;48 bit		delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,3		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;r3 = high 16 bit is RCA
	
	movei	r0,cardRCA
	nop
	moveih	r0,>cardRCA
	nop
	st	r0,r3	;store rca
	
	push	r3
	
	;print rca 
	
	gpci	r7,2		;next 4 + X
	br	drawText
	movei	r0,stringRCA		;delay slot
	movei	r1,0				;delay slot
	moveih	r0,>stringRCA		;delay slot
	movei	r2,1				;delay slot
	
	rqpop
	nop
	nop
	nop
	
	;select card
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;get CSD
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD9 (get CSD) -> check respons
	
	movei	r0,cardRCA
	nop
	moveih	r0,>cardRCA
	nop
	rqldi	r0,0
	nop
	nop
	ld	r2	;arg high	
	
	;send
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,9		;cmd9		delay slot
	movei	r1,0		;arg low	delay slot
	nop			;arg high	delay slot
	movei	r3,1		;136 bit		delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,2		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;print cid
	
	rqldi	r5,7
	rqldi	r5,6
	rqldi	r5,5
	ld	r0
	ld	r1
	ld	r2
	
	push	r0
	push	r1
	push	r2
	
	rqldi	r5,4
	rqldi	r5,3
	rqldi	r5,2
	ld	r0
	ld	r1
	ld	r2
	
	push	r0
	push	r1
	push	r2
	
	rqldi	r5,1
	rqldi	r5,0
	nop
	ld	r0
	ld	r1
	
	push	r0
	push	r1
	
	gpci	r7,2		;next 4 + X
	br	drawText
	movei	r0,stringCSD		;delay slot
	movei	r1,0				;delay slot
	moveih	r0,>stringCSD		;delay slot
	movei	r2,2				;delay slot
	
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	nop
	nop
	nop
	
	;------------------- CMD7 (select card) -> check respons
	
	;send
	
	movei	r0,cardRCA
	nop
	moveih	r0,>cardRCA
	nop
	rqldi	r0,0
	nop
	nop
	ld	r2	;arg high	
	
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,7		;cmd3		delay slot
	movei	r1,0		;arg low	delay slot
	nop			;delay slot
	movei	r3,0		;48 bit		delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,7		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
	;switch to app
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD55 -> check respons
	
	movei	r0,cardRCA
	nop
	moveih	r0,>cardRCA
	nop
	rqldi	r0,0
	nop
	nop
	ld	r2	;arg high	
	
	;send
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,55		;cmd9		delay slot
	movei	r1,0		;arg low	delay slot
	nop			;arg high	delay slot
	movei	r3,0		;136 bit		delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,55		;delay slot
	nop			;delay slot
	moveih	r1,1		;delay slot
	nop			;delay slot
	
	;get SDR
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- ACMD51 -> check respons
	
	
	;send
	
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,51		;cmd9		delay slot
	movei	r1,0		;arg low	delay slot
	movei	r2,0				;arg high	delay slot
	movei	r3,0		;136 bit		delay slot
	
	;recive
	
	gpci	r7,2		;next 4 + X
	br	sdCardReciveCmd
	movei	r1,$ff		;			delay slot
	nop			;			delay slot
	moveih	r1,$ff		;65536 = timeout 	delay slot
	nop			;			delay slot
	
	;test recive result [ r0 = status (4 = ok | 2 = error | 1 = timeout) ]
	
	movei	r7,3
	nop
	and	r0,r0,r7	;error / time out
	nop
	cmpeqi	r0,0
	brtc	sdError
	movei	r1,51		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	push	r3
	push	r2
	
	gpci	r7,2		;next 4 + X
	br	drawText
	movei	r0,stringSCR		;delay slot
	movei	r1,0				;delay slot
	moveih	r0,>stringSCR		;delay slot
	movei	r2,3				;delay slot
	
	rqpop
	rqpop
	nop
	nop
	nop
	
	
	
	
	
	;no error check return

loop	br	loop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
cardRCA
	word	0
	
;--------------------------	
	
sdError	
	
	push	r1
	push	r0
	
	gpci	r7,2		;next 4 + X
	br	drawText
	movei	r0,stringErrorCmd		;delay slot
	movei	r1,0				;delay slot
	moveih	r0,>stringErrorCmd		;delay slot
	movei	r2,12				;delay slot
	
	br	loop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
stringErrorCmd
	
	.string	"error %x at cmd %x .. stop"
	word	0

stringErrorEverBusy

	.string "sdcard get not ready"
	word	0
	
stringCID

	.string "cid %x %x %x %x %x %x %x %x"
	word	0

stringCSD

	.string "csd %x %x %x %x %x %x %x %x"
	word	0
	
stringRCA

	.string "rca %x"
	word	0

stringSCR	
	
	.string "scr %x%x"
	word	0
	
;******************************
;*
;* sdCardSendCmd
;*
;* r0 = cmd
;* r1 = arg low
;* r2 = arg high
;* r3 = 0 -> set reciver to 48bit / 1 -> set reciver to 136 bit
;*
;*

sdCardSendCmd

	push	r5
	push	r6
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base
	
	moveih	r0,2		;reset $0002 CMDX
	
	swp	r3,r3		;r3 = 000x 0000
	esadr	r6,r5
	muli	r3,4		
	
	;reset first (reset read after write)
	
	est	r0,0		;reset	
	
	;
		
	moveih	r0,1		;start $0001 CMDX 
	est	r1,3		;low argument 
	or	r0,r0,r3	;add reciver mode
	est	r2,2		;high argument 
	est	r0,0		;cmd + start
	
	est	r0,7		;write sync (to wait for real startup)
	est	r0,7
	est	r0,7
	
	rqpop	
	rqpop
	jmpi	r7,0	
	pop	r6	;delay slot
	pop	r5	;delay slot
	nop		;delay slot
	nop		;delay slot
	
		
;******************************
;*
;* sdCardReciveCmd
;*
;* r1 = timeout count -1
;*
;* return
;*
;* r0 = status (4 = ok | 2 = error | 1 = timeout)
;* r1 = recived cmd						
;* r2 = recived arg low
;* r3 = recived arg high
;* r4 = expected crc, recived crc
;* r5 = ptr to 8 words (full data recived if mode is 136 bit)
;*


sdCardReciveCmd
		
	push	r6
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base	
	
	nop
	
	esadr	r6,r5
	
	
;wait for respons
	
sdCardReciveCmdRead

	erqldi	0	;status
	movei	r4,6	;110
	eld	r0	;RO RE SP (Recive Ok, Recive Error, Send in Progress)
	nop
	and	r0,r0,r4
	nop
	cmpeqi	r0,0	;if not 'not' ok or error retry
	brtc	sdCardReciveCmdFinish
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
	subi	r1,1	
	brts	sdCardReciveCmdRead
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	;timeout
	
	addi	r0,1	;set timeout in status
	
sdCardReciveCmdEpilog	
	
	rqpop	
	jmpi	r7,0	
	movei	r5,sdCardReciveCmdFullDataBuffer		;delay slot		
	pop	r6						;delay slot
	moveih	r5,>sdCardReciveCmdFullDataBuffer		;delay slot		
	nop							;delay slot
	
sdCardReciveCmdFinish

;* r1 = recived cmd
;* r2 = recived arg low
;* r3 = recived arg high
;* r4 = expected crc, recived crc
;* r5 = ptr to 8 words (full data recived if mode is 136 bit)

	;get 48 bit values
	
	;erqldi	3	;cmd
	erqldi	3	;arg low
	erqldi	2	;arg high
	erqldi	1	;ecrc crc
	
	or	r1,r0,r0
	nop
	lsri	r1,8
	
	movei	r5,$02
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base + 2
	
	;eld	r1
	eld	r2
	eld	r3
	eld	r4
	
	esadr	r6,r5		;base for 136bit mode
	
	movei	r5,sdCardReciveCmdFullDataBuffer
	erqldi	0
	moveih	r5,>sdCardReciveCmdFullDataBuffer
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	
	push	r7
	movei	r7,7
	
sdCardReciveCmdReadLongArg
		
	eld	r6	;127 .. 112
	subi	r7,1
	brts	sdCardReciveCmdReadLongArg
	st	r5,r6	;delay slot
	addi	r5,1	;delay slot			
	nop		;delay slot
	nop		;delay slot
	
	
	rqpop
	br	sdCardReciveCmdEpilog
	nop							;delay slot
	pop	r7						;delay slot
	nop							;delay slot
	nop							;delay slot
	
sdCardReciveCmdFullDataBuffer

	.space	8
	
;******************************
;*
;* sleep
;*	
;* r0 = (cnt in ms) -1	
;*
;*

sleep

	push	r1
	
sleepLoopOut
	
	movei	r1,$4c
	nop
	moveih	r1,$45
	nop
	
sleepLoopIn
	subi	r1,1
	brts	sleepLoopIn
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	subi	r0,1
	brts	sleepLoopOut
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop	
	jmpi	r7,0	
	nop		;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* drawText
;*
;* r0		text
;* r1 	 	x
;* r2		y

drawText

	push	r0
	push	r1
	push	r4
	push	r6
	push	r7
	
	getsp	r6,5

	or	r4,r0,r0	;text address to r4
	nop
	
textLoop

	rqldi	r4,0
	nop
	nop
	ld	r0
	nop
	cmpeqi	r0,0
	brts	textLoopEnd
	addi	r4,1	;next char 	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,37	;%
	brts	textProcessCmd
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	subi	r0,32	;map visible	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r1,1
textProcessCmdReturn
	
	br	textLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
textLoopEnd	
	
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r4
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;processing cmd string	
	
textProcessCmd

	rqldi	r4,0	;get next char
	nop
	nop
	ld	r0	
	nop
	
	cmpeqi	r0,37	;% again so print it
	brts	textProcessCmdProzent
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,99	;c -> char
	brts	textProcessCmdChar
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,120	;x -> hex
	brts	textProcessCmdHex
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	

	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;%%	
	
textProcessCmdProzent
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	subi	r0,32	;map visible	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r1,1
	
	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;%c	
	
textProcessCmdChar
	
	rqldi	r6,0
	addi	r6,1	;next value if exist
	nop
	ld	r0	
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	subi	r0,32	;map visible	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r4,1
	addi	r1,1
	
	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;%x	
	
textProcessCmdHex	
	
	push	r5
	push	r3
	
	rqldi	r6,0
	addi	r6,1	;next value if exist
	nop
	ld	r5
	
	movei	r3,3	;3 hex digits

textProcessCmdHexLoop	
	
	or	r0,r5,r5
	nop
	lsri	r0,12
	nop
	
	cmploi	r0,10
	brts	textProcessCmdHexNoABCDEF
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r0,7
	
textProcessCmdHexNoABCDEF	
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	addi	r0,16	;map visible +48-32	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
				
	subi	r3,1	
	brts	textProcessCmdHexLoop
	addi	r1,1	;x++		delay slot
	muli	r5,16	;r5 << 4	delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	rqpop
	br	textProcessCmdReturn
	pop	r3	;delay slot
	pop	r5	;delay slot		
	addi	r4,1	;next input char	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

	push	r7
	
	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
	
	; 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	clearLoop
	movei	r1,$33	; count		delay slot	
	movei	r0,$00	; value		delay slot
	moveih	r1,$9e	; count		delay slot
	nop		;		delay slot
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	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	
		