	
	;***********************************************************************
	;* reset start
	;***********************************************************************
	
	.org	0
	
	;***********************************************************************
	;* user code start
	;***********************************************************************
	
userLevel
	
	
			
	;draw header
		
	gpci	r7,2			;next 4 + X
	br	drawText
	movei	r0,stringHeader		;text	delay slot	
	movei	r1,0			;x      delay slot
	moveih	r0,>stringHeader	;       delay slot
	movei	r2,0			;y      delay slot
	
	;---------------------------------------------------------------------------------------
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base
	
	nop
	
	esadr	r6,r5
	
	;send reset reciver first
	
	moveih	r0,2
	nop
	est	r0,0		;reset	
	
	nop
	nop
	
	;send cmd
	
	movei	r0,0		;cmd 0 
	movei	r1,0		;
	movei	r2,0
	moveih	r0,1		;start send 
	moveih	r1,0		;
	moveih	r2,0
	
	est	r1,1		;low argument 0
	est	r2,2		;high argument 0
	est	r0,0		;cmd + start + expect recive 48
	
	
	gpci	r7,2			;next 4 + X
	br	waste	
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
	;---------------------------------------------------------------------------------------
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base
	
	nop
	
	esadr	r6,r5
	
	;send reset reciver first
	
	moveih	r0,2
	nop
	est	r0,0		;reset	
	
	nop
	nop
	
	;send cmd
	
	movei	r0,8		;cmd 8 
	movei	r1,$aa		;check pattern
	movei	r2,0
	moveih	r0,1		;start send 
	moveih	r1,1		;2.7-3.6V
	moveih	r2,0
	
	;est	r1,1		;low argument 0
	;est	r2,2		;high argument 0
	;est	r0,0		;cmd + start + expect recive 48

	;gpci	r7,2			;next 4 + X
	;br	waitAndDrawResponse	
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;++

	movei	r2,$3
	nop
	moveih	r2,$0
	nop

startup
	nop
	push	r2
		
		
	;---------------------------------------------------------------------------------------
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base
	
	nop
	
	esadr	r6,r5
	
	;send reset reciver first
	
	moveih	r0,2
	nop
	est	r0,0		;reset	
	
	nop
	nop
	
	;send cmd
	
	movei	r0,55		;cmd 55 
	movei	r1,0		;argument 0
	moveih	r0,1		;start send 
	
	est	r1,1		;low argument 0
	est	r1,2		;high argument 0
	est	r0,0		;cmd + start + expect recive 48

	gpci	r7,2			;next 4 + X
	br	waitAndDrawResponse	
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;---------------------------------------------------------------------------------------
	
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base
	
	nop
	
	esadr	r6,r5
	
	;send reset reciver first
	
	moveih	r0,2
	nop
	est	r0,0		;reset	
	
	nop
	nop
	
	;send cmd
	
	movei	r0,41		;acmd 41
	movei	r1,0		;argument 0
	movei	r2,$ff
	moveih	r0,1		;start send 
	moveih	r1,0
	moveih	r2,$40	;$41		;HCS + S18R
	
	nop
	est	r1,1		;low argument 0
	est	r2,2		;high argument 0
	est	r0,0		;cmd + start + expect recive 48

	gpci	r7,2			;next 4 + X
	br	waitAndDrawResponse	
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
	gpci	r7,2			;next 4 + X
	br	waste	
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	rqpop
	nop
	nop
	pop	r2
	nop
	
	subi	r2,1
	brts	startup
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
		
	;---------------------------------------------------------------------------------------
		
	movei	r5,$00
	movei	r6,$00
	moveih	r5,$00
	moveih	r6,$A0		;sd card base
	
	nop
	
	esadr	r6,r5
	
	;send reset reciver first
	
	moveih	r0,2
	nop
	est	r0,0		;reset	
	
	nop
	nop
	
	;send cmd
	
	movei	r0,2		;cmd 2
	movei	r1,0		;argument 0
	moveih	r0,5		;start send , recive 136 bit
	
	est	r1,1		;low argument 0
	est	r1,2		;high argument 0
	est	r0,0		;cmd + start + expect recive 48

	gpci	r7,2			;next 4 + X
	br	waitAndDrawResponse	
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	
loop	br	loop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
CursorY

	word	0
	
;***	
	
waste
	
	movei	r1,$3f
	
wasteLoopHigh	
	
	movei	r0,$ff
	nop
	moveih	r0,$ff
	nop

wasteLoop

	subi	r0,1
	brts	wasteLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	subi	r1,1
	brts	wasteLoopHigh
	nop		;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			
	
;******************************
;*
;* waitAndDrawResponse
;*
;*	

waitAndDrawResponse

	push	r7
	
	movei	r0,200		;waste some time to sync
wloop	
	nop
	nop
	nop
	subi	r0,1
	brts	wloop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	
	;wait for respons
	
	movei	r2,0
	movei	r3,0	; count wait
rloop
	erqldi	0	;RO RE SP 
	movei	r1,6	;110
	eld	r0
	movei	r4,$ff
	and	r0,r0,r1
	
	moveih	r4,$01
	addi	r2,1	
	addtqi	r3,r3,0
	
	nop
	cmpeq	r3,r4	;timeout if $fff cycles 
	brts	rloopTimeout
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,0
	brts	rloop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
rloopTimeout

	;print out

	erqldi	1	;low arg
	erqldi	2	;high arg
	erqldi	3	;recive cmd
	erqldi	4	;crc exp - crc
	eld	r4	
	eld	r5
	eld	r6
	eld	r7
	
	nop
	
	push	r7
	push	r6
	push	r4	;low arg
	push	r5	;high arg
	push	r0	;status
	push	r2
	push	r3
	
	;draw status
		
	;get y	
		
	movei	r6,CursorY	
	nop
	moveih	r6,>CursorY	
	nop
	rqldi	r6,0
	nop
	nop
	ld	r2
		
	gpci	r7,2			;next 4 + X
	br	drawText
	movei	r0,stringCmd		;text	delay slot	
	movei	r1,0			;x      delay slot
	moveih	r0,>stringCmd		;delay slot
	nop				;delay slot

	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	rqpop
	nop
	nop
	nop	
	
	;y add
	
	movei	r6,CursorY	
	nop
	moveih	r6,>CursorY	
	nop
	rqldi	r6,0
	nop
	nop
	ld	r2
	movei	r0,$f
	addi	r2,1
	nop
	and	r2,r2,r0
	nop
	st	r6,r2
	
	rqpop
	nop
	nop
	pop	r7
	nop
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
;******************************
;*
;* readFromPS2
;*
;* return
;*
;* r0 = value 
;*	

readFromPS2

	push	r1
	push	r2
	push	r3
	
	movei	r2,$30
	nop
	moveih	r2,$20	;read 2030 - ctrl ps data
	nop
	
readFromPS2ScanTest	

	rqldi	r2,0
	nop
	nop
	ld	r0
	nop
	extri	r0,8	;data in
	brtc	readFromPS2ScanTest
	movei	r3,0	;delay slot
	nop		;delay slot
	moveih	r3,$1	;$100 = accept data 	delay slot
	nop		;delay slot
	
	st	r2,r3	;write 2030 - accept data	
	moveih	r0,$0	;clear upper part of value
	
	rqpop
	rqpop
	rqpop
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;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
	

;*********************************************************************
;* data
;*********************************************************************	
		
stringHeader
	
	.string	"goldmomo_endlos sd card test"
	word	0
	
stringCmd
	
	.string	"cmd delay %x%x value sts %x arg %x%x rcmd %x recrc %x (press key)"
	word	0	
	