;******************************
;*
;* drawChar8x8
;* 
;* 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 
;*

drawChar8x8

	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,8		;y * 8	
	add	r4,r4,r1	;address+x
	addtqi	r5,r5,0
	
	mul	r7,r6,r2	;[y*8]* screenwidth 
	gmulhi	r2
	
	add	r4,r4,r7	;address+(y*screenwidth*8)
	addt	r5,r5,r2
		
	;font source (r3:r2)
	
	dexti	>fontAddress
	movei	r2,fontAddress
	nop
	rqldi	r2,1	;low
	rqldi	r2,0	;high
	nop
	ld	r2	;low
	ld	r3	;high
		
	muli	r0,8		;value * 8
	nop
	add	r2,r2,r0	;add to source 
	addtqi	r3,r3,0		;

	subi	r6,8		;screenwidth - 8
	movei	r0,7		;8 [y]
dcYloop	
	
	esadr	r3,r2
	erqldi	0		;request 8 pixel
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	
	addi	r2,8		
	addtqi	r3,r3,0	
	
	;store back
	
	eld	r7
	eld	r1
	esadr	r5,r4
	extri	r7,15		
	estts	r7,0
	extri	r1,15		
	estts	r1,1
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,2
	extri	r1,15		
	estts	r1,3
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,4
	extri	r1,15		
	estts	r1,5
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,6
	extri	r1,15		
	estts	r1,7
	
	addi	r4,8
	addtqi	r5,r5,0
		
	;
	
	movei	r1,$d0
	subi	r0,1		;y-
	moveih	r1,$1		;r1 = 472-8
	brts	dcYloop
	add	r2,r2,r1	;source + 752-8			delay slot
	addtqi	r3,r3,0		;				delay slot
	add	r4,r4,r6	;dest + screen width - 8	delay slot
	addtqi	r5,r5,0		;				delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* drawChar8x8
;* 
;* 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 
;*

drawChar16x16

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;destination base (r5:r4)
	
	muli	r1,16	;x * 16
	
	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,17		;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)
	
	dexti	>fontAddress
	movei	r2,fontAddress
	nop
	rqldi	r2,1	;low
	rqldi	r2,0	;high
	nop
	ld	r2	;low
	ld	r3	;high
		
	muli	r0,16		;value * 16
	nop
	add	r2,r2,r0	;add to source 
	addtqi	r3,r3,0		;

	subi	r6,8		;screenwidth -8 (inner loop will add 8 too)
	movei	r0,15		;16 [y]
dcY16loop	
	
	esadr	r3,r2
	erqldi	0		;request 16 pixel
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	erqldi	8		
	erqldi	9
	erqldi	10
	erqldi	11
	erqldi	12
	erqldi	13
	erqldi	14
	erqldi	15
	
	addi	r2,16		
	addtqi	r3,r3,0	
	
	;store back
	
	eld	r7
	eld	r1
	esadr	r5,r4
	extri	r7,15		
	estts	r7,0
	extri	r1,15		
	estts	r1,1
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,2
	extri	r1,15		
	estts	r1,3
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,4
	extri	r1,15		
	estts	r1,5
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,6
	extri	r1,15		
	estts	r1,7
	
	addi	r4,8
	addtqi	r5,r5,0
	
	eld	r7
	eld	r1
	esadr	r5,r4
	extri	r7,15		
	estts	r7,0
	extri	r1,15		
	estts	r1,1
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,2
	extri	r1,15		
	estts	r1,3
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,4
	extri	r1,15		
	estts	r1,5
	
	eld	r7
	eld	r1
	extri	r7,15		
	estts	r7,6
	extri	r1,15		
	estts	r1,7
		
	;
	
	movei	r1,$b0
	subi	r0,1		;y-
	moveih	r1,$3		;r1 = 960-16
	brts	dcY16loop
	add	r2,r2,r1	;source + 752-16		delay slot
	addtqi	r3,r3,0		;				delay slot
	add	r4,r4,r6	;dest + screen width - 16	delay slot
	addtqi	r5,r5,0		;				delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	poparqp	r7
	poparqp	r6
	poparqp	r5
	poparqp	r4
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot	