	
	;***********************************************************************
	;* reset start
	;***********************************************************************
	
	.org	0
	
	.def	cmdToReciveWait	9
	
	;***********************************************************************
	;* 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
	
reInitSDCard	
	gpci	r7,2		;
	br	initSDCard	
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	cmpeqi	r0,0
	brtc	reInitSDCard
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;read sdcard to sram
	
	;r3:r2 current block
	
	movei	r2,0
	movei	r3,0
	moveih	r2,$20	;4 mega offset
	
movieLoop	
	
	push	r3
	push	r2
	
	movei	r1,$0
	movei	r0,$e0
	moveih	r1,$f0		;r1:r0 = $f000 10e0
	moveih	r0,$10
	
	gpci	r7,2		;
	br	sdCardReadBlocks	
	movei	r4,$10		;block cnt-1		delay slot
	nop			;delay slot
	nop			;delay slot
	moveih	r4,$1		;delay slot
	
	gpci	r7,2
	br 	sleep
	movei	r0,4		;5ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	rqpop
	rqpop
	nop
	pop	r2
	pop	r3
	
	movei	r0,$1a
	nop
	moveih	r0,$1
	nop
	
	add	r2,r2,r0
	addtqi	r3,r3,0
	
	br	movieLoop	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
		
	;no error check return
	
sdError
loop	br	loop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* sdCardReadBlocks
;*
;* r1:r0 = destination address 
;* r3:r2 = startblock
;* r4	 = block count - 1
;*	
;* return
;*
;* r0	 = 0 (load ok)
;*

sdCardReadBlocks

	;prolog

	push	r5
	push	r6
	push	r7
	
	;setup burst address
	
	movei	r5,$10
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base + $10
	nop
	
	esadr	r6,r5
	
	movei	r5,2		;enable burst
	
	est	r0,2		;low
	est	r1,3		;high
	est	r5,0		;enable burst (push total fifo out)
	
	; r3:r2 = startblock , r4	 = block count - 1
	
blockLoop

	push	r2
	push	r3
	push	r4
	
	;---> block loop (r3:r2 = current block)
	
	;reset reciver
	
	movei	r5,$10
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base + $15
	movei	r0,3		;reset reciver , burst enable
	
	esadr	r6,r5
	est	r0,0
	
	;is sdhc (no -> current block * 512)
	
	movei	r0,sdCardIsSDHC
	nop
	moveih	r0,>sdCardIsSDHC
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0
	nop
	cmpeqi	r0,0
	brtc	sdhcBlkCalcContinue	;is sdhc so in blocks
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;r3:r2 * 512
	
	;r3                  r2
	;aaaa aaaa bbbb bbbb cccc cccc dddd dddd
	;bbbb bbbc cccc cccd dddd ddd0 0000 0000 
	
	moveih	r3,0		;r3 = 0000 0000 bbbb bbbb *1
	or	r0,r2,r2	;r0 = cccc cccc dddd dddd *2
	moveih	r2,0		;r2 = dddd dddd 0000 0000 *3
	swp	r3,r3		;r3 = bbbb bbbb 0000 0000 *4
	swp	r2,r2		;r2 = dddd dddd cccc cccc *5
	add	r3,r3,r3	;r3 = bbbb bbb0 0000 0000 *6	
	lsri	r0,7		;r0 = 0000 000c cccc cccd *7
	add	r2,r2,r2	;r2 = dddd ddd0 0000 0000 *8
	or	r3,r3,r0	;r3 = bbbb bbbc cccc cccd *9
		
sdhcBlkCalcContinue

	;send single block read (CMD17)
			
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,17		;cmd		delay slot
	or	r1,r2,r2	;r1 arg low 	delay slot
	or	r2,r3,r3	;r2 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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	sdCardReadBlocksError
	movei	r0,1		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop	

	;wait for transfer finish
	
	movei	r5,$10
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base + $10
	nop
	
	esadr	r6,r5
	
waitSdReadFinish	
	erqldi	0
	eld	r0
	nop
	cmpeqi	r0,$f
	brtc	waitSdReadFinish
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;check fifos <8192-512
	
	movei	r5,$15
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base + $15
	nop
	
	esadr	r6,r5
	
waitSdFifoLoad	
	
	erqldi	0
	erqldi	1
	erqldi	2
	erqldi	3
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	
	cmplo	r0,r1		;r0<r1	
	movets	r1,r1,r0	;d = r1 else r0
	
	cmplo	r2,r3		;r2<r3	
	movets	r3,r3,r2	;d = r3 else r2
	
	nop
	
	cmplo	r1,r3		;r1<r3	
	movets	r3,r3,r1
	
	movei	r0,0
	nop
	moveih	r0,$1c
	nop
	
	;r3 is biggest fifo load
	cmplo	r3,r0	;r3 < 8192-1024
	brtc	waitSdFifoLoad	;not then wait again
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;<---
	
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r3
	pop	r2
	
	subi	r4,1
	brts	blockLoop
	addi	r2,1	;next block delay slot	
	addtqi	r3,r3,0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;wait for empty recive fifos, bustransfer finished (IMPLEMENT ME)
	
	;epilog

	movei	r0,0	;finish successfull
	
sdCardReadBlocksFinish
	
	rqpop	
	rqpop	
	rqpop
	pop	r7
	pop	r6
	pop	r5
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
sdCardReadBlocksError

	;celanup stack
	
	rqpop
	rqpop
	rqpop
	br	sdCardReadBlocksFinish
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
;******************************
;*
;* initSDCard
;*
;* return r0 
;*  
;* r0 = 0 (init success)
;*      1 - 2 	= CMD55, ACMD41 recive error
;*	3	= bringutp timeout (ACMD41) -> no sd card
;*	4	= reading CID fails
;*	5	= reading RCA fails
;*	6	= reading CSD fails
;*
	
initSDCard

	push	r7

	gpci	r7,2
	br 	sleep
	movei	r0,9		;10ms		delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
			
	;change clk to ~400khz
	
	movei	r5,$08
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base + $8
	
	nop
	
	esadr	r6,r5
	
	movei	r0,$ff		;clk msk	
	movei	r1,$00		;clk down cmp
	movei	r2,$00		;clk up cmp
	movei	r3,$80		;clk write cmp
	movei	r4,$bf		;clk read cmp
	
	moveih	r0,$01
	moveih	r2,$01
	moveih	r4,$01
	
	est	r0,0
	est	r1,1
	est	r2,2
	est	r3,3
	est	r4,4	
	
	;store clk
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	movei	r0,0
	moveih	r6,$A0		;sd card base 
	moveih	r0,$08		;write clk
	
	esadr	r6,r5
	est	r0,0
	
	;sleep
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;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,$ff	;max 256 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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$01		;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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$02		;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
	
	br	initSDCardError
	movei	r1,$03		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	br	loop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
bringupSDFinished	

	;r3 = bit 14 = 1 then SDHC
	
	movei	r0,sdCardIsSDHC
	movei	r4,0
	moveih	r0,>sdCardIsSDHC
	movei	r5,1
	
	extri	r3,14	;sdhc?
	movets	r4,r5,r4
	nop
	st	r0,r4	;save for reader
	
	;card is ready now
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait	;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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$04		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;get relative address
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait	;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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$05		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;r3 = high 16 bit is RCA
	
	movei	r0,sdCardRCA
	nop
	moveih	r0,>sdCardRCA
	nop
	st	r0,r3	;store rca
		
	;get CSD
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;delay slot
	nop					;delay slot
	nop					;delay slot
	nop					;delay slot
	
	;------------------- CMD9 (get CSD) -> check respons (need CSD?)
	
	movei	r0,sdCardRCA
	nop
	moveih	r0,>sdCardRCA
	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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$06		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD7 (select card) -> check respons
	
	;sleep
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;send
	
	movei	r0,sdCardRCA
	nop
	moveih	r0,>sdCardRCA
	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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$07		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;------------------- CMD16 (select block length) -> check respons
	
	;sleep
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;send
		
	movei	r1,0	
		
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,16		;cmd		delay slot
	moveih	r1,$2		;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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$08		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			
	
	;------------------- CMD55 with rca (to app) -> check respons
	
	;sleep
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	movei	r0,sdCardRCA
	nop
	moveih	r0,>sdCardRCA
	nop
	rqldi	r0,0
	nop
	nop
	ld	r2	;arg high	
	
	;send
		
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,55		;cmd		delay slot
	movei	r1,0		;arg low	delay slot
	nop			;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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$09		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	
	;------------------- ACMD6 (set bus width) -> check respons
	
	;sleep
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;send
		
	gpci	r7,2		;next 4 + X
	br	sdCardSendCmd	
	movei	r0,6		;cmd		delay slot
	movei	r1,2		;arg low (4bit)	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	r2,r0,r7	;error / time out
	nop
	cmpeqi	r2,0
	brtc	initSDCardError
	movei	r1,$0A		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	
	;sleep
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;change clk to 25MHz
	
	movei	r5,$08
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base + $8
	
	nop
	
	esadr	r6,r5
	
	movei	r0,$03		;clk msk	
	movei	r1,$00		;clk down cmp
	movei	r2,$02		;clk up cmp
	movei	r3,$01		;clk write cmp
	movei	r4,$01		;clk read cmp
	
	est	r0,0
	est	r1,1
	est	r2,2
	est	r3,3
	est	r4,4	
	
	;store clk
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	movei	r0,0
	moveih	r6,$A0		;sd card base 
	moveih	r0,$08		;write clk
	
	esadr	r6,r5
	est	r0,0
	
	;sleep
	
	gpci	r7,2
	br 	sleep
	movei	r0,cmdToReciveWait		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;init ok	
		
	movei	r0,0
	
initSDCardError

	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
;******************************
;*
;* 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	r2,r0,r4
	nop
	cmpeqi	r2,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
	
;******************************
;*
;* 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	
			
	
;** sd card releated	
	
sdCardIsSDHC
	word	0
	
sdCardRCA
	word	0
	