;******************************
;*
;* drawChar
;* 
;* r0 	 	value
;* r1		x 	
;* r2		y
;* trash r0
;*
;* need 'drawFrame' 	to get destination address 
;* need 'fontAddress' 	to get source font
;* need 'screenWidth'	to calculate correct line modulo 
;* need 'fontcode'	to set color
;*

drawChar	

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;destination base (r5:r4)
	
	muli	r1,8	;x * 8
	
	movei	r4,drawFrame
	movei	r6,screenWidth
	moveih	r4,>drawFrame
	moveih	r6,>screenWidth	
	rqldi	r4,1	;low
	rqldi	r4,0	;high
	rqldi	r6,0	;
	ld	r4	;low	destination
	ld	r5	;high	destination
	ld	r6	;screen width
	
	muli	r2,16		;y * 16	
	add	r4,r4,r1	;address+x
	addtqi	r5,r5,0
	
	mul	r7,r6,r2	;[y*16]* screenwidth 
	gmulhi	r2
	
	add	r4,r4,r7	;address+(y*screenwidth*16)
	addt	r5,r5,r2
		
	;font source (r3:r2)
	
	movei	r2,fontAddress
	movei	r7,fontCode
	moveih	r2,>fontAddress
	moveih	r7,>fontCode
	rqldi	r2,1	;low
	rqldi	r2,0	;high
	rqldi	r7,0	;color
	ld	r2	;low
	ld	r3	;high
	ld	r7	;color
	;

	movei	r1,15		;16 [y]
dcYloop	
	
	esadr	r3,r2
	erqldb	r0		;request 16 pixel (only 8 used)	
	extri	r0,0		;is even address?	
	push	r5
	esadr	r5,r4		;set store address
	eld	r5		;get value
	tnt
	swptc	r5,r5	;swap bytes
	nop
	
	add	r5,r5,r5	;shift left out
	estts	r7,0		;store color 
	add	r5,r5,r5	;shift left out
	estts	r7,1		;store color 
	add	r5,r5,r5	;shift left out
	estts	r7,2		;store color 
	add	r5,r5,r5	;shift left out
	estts	r7,3		;store color 
	add	r5,r5,r5	;shift left out
	estts	r7,4		;store color 
	add	r5,r5,r5	;shift left out
	estts	r7,5		;store color 
	add	r5,r5,r5	;shift left out
	estts	r7,6		;store color 
	add	r5,r5,r5	;shift left out
	estts	r7,7		;store color 
		
	;
	
	rqpop			;restore r5
	subi	r1,1		;y-	
	brts	dcYloop
	pop	r5		;				delay slot
	addi	r0,94		;				delay slot
	add	r4,r4,r6	;dest + screen width - 8	delay slot
	addtqi	r5,r5,0		;				delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r5
	
	rqpop
	rqpop
	nop
	pop	r4
	pop	r3
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot
	