;******************************
;*
;* drawText
;*
;* r0		text
;* r1 	 	cursor x
;* r2		cursor y

drawTextFormated

	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
	
	cmpeqi	r0,122	;z -> byte string		
	brts	textProcessCmdByteString	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,90	;z -> external byte string		
	brts	textProcessExternalCmdByteString	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,117	;u -> unsigned dec	
	brts	textProcessCmdUDec		
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,97	;a -> font code	
	brts	textProcessCmdFCode		
	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
;* 	
;* %%	
;*
;* r1  x
;* r2  y
;* r4  text address
;* r6  data (stack)
;* 
	
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
	

	br	textProcessCmdReturn
	addi	r1,1	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* textProcessCmdFCode
;* 	
;* %a	
;*
;* r1  x
;* r2  y
;* r4  text address
;* r6  data (stack)
;* 

textProcessCmdFCode
	
	push	r3
	
	rqldi	r6,0
	dexti	>fontCode
	movei	r3,fontCode
	ld	r0	;code
	
	addi	r6,1	;next value if exist
	st	r3,r0	;store fontcode
	
	br	textProcessCmdReturn
	rqpop		;delay slot
	nop		;delay slot	
	addi	r4,1	;delay slot
	pop	r3	;delay slot
	
	
;******************************
;*
;* textProcessCmdChar
;* 	
;* %c	
;*
;* r1  x
;* r2  y
;* r4  text address
;* r6  data (stack)
;* 
	
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

	br	textProcessCmdReturn
	addi	r4,1	;delay slot
	addi	r1,1	;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* textProcessExternalCmdByteString
;* 	
;* %Z	
;*
;* r1  x
;* r2  y
;* r4  text address
;* r6  data (stack)
;*  
	
textProcessExternalCmdByteString

	push	r3
	push	r5
	push	r6

	rqldi	r6,0	;high
	rqldi	r6,1	;low
	nop
	ld	r5	;text address low   (byte data)
	ld	r6	;text address high 
	nop
	
textProcessExternalCmdByteStringNextByte	
	
	esadr	r6,r5
	
	;upper value
	
	erqldi	0
	addi	r5,1
	addtqi	r6,r6,0
	eld	r0
	nop
	or	r3,r0,r0	;save value
	lsri	r0,8		;upper byte
	nop
	cmpeqi	r0,0
	brts	textProcessExternalCmdByteStringEnd
	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
	
	;lower value
	
	or	r0,r3,r3
	addi	r1,1	;x++ 
	moveih	r0,$0	;clear upper vlaue
	nop
	cmpeqi	r0,0
	brts	textProcessExternalCmdByteStringEnd
	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
	
	br	textProcessExternalCmdByteStringNextByte
	nop		;next word	delay slot
	addi	r1,1	;x++		delay slot
	nop		;delay slot
	nop		;delay slot
	
	;end 
	
textProcessExternalCmdByteStringEnd

	rqpop
	rqpop
	rqpop
	pop	r6
	pop	r5
	pop	r3
	
	br	textProcessCmdReturn		
	addi	r4,1	;next input char			delay slot
	addi	r6,2	;remove high / low from stack		delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* textProcessCmdByteString
;* 	
;* %z	
;*
;* r1  x
;* r2  y
;* r4  text address
;* r6  data (stack)
;*  
	
textProcessCmdByteString

	push	r3
	push	r5

	rqldi	r6,0
	addi	r6,1	;next value if exist
	nop
	ld	r5	;text address (byte data)
	nop
	
textProcessCmdByteStringNextByte	
	
	;upper value
	
	rqldi	r5,0
	nop
	nop
	ld	r0
	nop
	or	r3,r0,r0	;save value
	lsri	r0,8		;upper byte
	nop
	cmpeqi	r0,0
	brts	textProcessCmdByteStringEnd
	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
	
	;lower value
	
	or	r0,r3,r3
	addi	r1,1	;x++ 
	moveih	r0,$0	;clear upper vlaue
	nop
	cmpeqi	r0,0
	brts	textProcessCmdByteStringEnd
	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
	
	br	textProcessCmdByteStringNextByte
	addi	r5,1	;next word	delay slot
	addi	r1,1	;x++		delay slot
	nop		;delay slot
	nop		;delay slot
	
	;end 
	
textProcessCmdByteStringEnd

	rqpop
	rqpop
	br	textProcessCmdReturn
	pop	r3	;delay slot
	pop	r5	;delay slot		
	addi	r4,1	;next input char	delay slot
	nop		;delay slot

;******************************
;*
;* textProcessCmdHex
;* 	
;* %x	
;*
;* r1  x
;* r2  y
;* r4  text address
;* r6  data (stack)
;* 		
	
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
	
;******************************
;*
;* textProcessCmdUDec
;* 	
;* %u	
;*
;* r1  x
;* r2  y
;* r4  text address
;* r6  data (stack)
;* 	
	
textProcessCmdUDec

	push	r5
	push	r4
	push	r3
	push	r7
	
	
	rqldi	r6,0
	addi	r6,1	;next value if exist
	movei	r4,$cd
	ld	r5
	moveih	r4,$cc	;r4 = $cccd
	
		
	movei	r7,4		;5 dec digits	
	
textProcessCmdUDecDecodeDigit
	
	or	r3,r5,r5
	mul	r5,r5,r4	;result = value * $cccd
	nop
	gmulhi	r5		;result >> 16
	nop
	lsrqi	r5,r5,3		;result >> 3
	nop
	or	r0,r5,r5
	nop
	muli	r0,10
	
		
	subi	r7,1
	brts	textProcessCmdUDecDecodeDigit
	nop			;delay slot
	sub	r0,r3,r0	;delay slot
	nop			;delay slot
	push	r0		;push digit ;delay slot
	
	;
	
	movei	r3,3		;4 dec digits with leading zero test	
	movei	r4,0		;leading zero
	
textProcessCmdUDecPrintDigit	
	
	rqpop	
	nop
	nop
	pop	r0
	nop
	cmpeq	r4,r0
	brts	textProcessCmdUDecSkipLeadingZero	;skip leading zero
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r4,$ff	;illegal value after first not leading zero
	
	
	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
	
	addi	r1,1
	
textProcessCmdUDecSkipLeadingZero
	
	subi	r3,1
	brts	textProcessCmdUDecPrintDigit
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	;last digit
	
	rqpop	
	nop
	nop
	pop	r0
	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
	addi	r1,1
	
	rqpop
	nop
	nop
	pop	r7
	
	rqpop
	rqpop
	rqpop
	pop	r3
	pop	r4	
	pop	r5	
	
	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot	
	addi	r4,1	;next input char	delay slot
	nop		;delay slot
	
fontCode
	word	$7fff	