;[]=======================================================================[]
;
; Draw text string to the screen 640x256 in 16colors
; Input:
;	hl - pointer to the C string (0 - end string)
;	de - x position
;	bc - y position
;	a  - background and foreground color
;		d7 - d4 background color (0-15)
;		d3 - d0 foreground color (0-15)
;
; Output:
;	hl - pointer to the next byte behind string
;
; Destroy registers:
;	'hl, 'de, 'bc

TextOut:
		push	iy
		ld	yl, c			; y position
		ld	b, a			; store colors
		in	a, (YPORT)		; store Y_PORT
		push	af
		in	a, (PAGE1)
		ld	yh, a			; store PAGE1
		call	PreparePrintColors
		push	de			; x position
		exx
		pop	bc
		srl	b
		rr	c			; start address for x position
		set	6, b			; print address start from 0x4000
		ld	hl, BackgroundBuffer	; background buffer
		ld	de, ForegroundBuffer	; foreground buffer
		exx
		ld	c, l			; pointer to the string
		ld	b, h
; now:
;	bc  - pointer to the C string
;       'hl - pointer to the background buffer
;       'de - pointer to the foreground buffer
;       'bc - x position
;  	yl  - y position

		di				; disable interrupts
		ld	d, d			; set acceleration work lenght
		ld	a, 0x08
		ld	b, b
		ld	a, (bc)			; test on null string
		inc	bc
		or	a
		jr	z, .exit
.loop1:
		ld	l, a          		; font is always aligned to 256 bytes
		ld	h, FONT_BASE/256	; pointer to the specialy font
		push	bc
		ld	b, (hl)			; character size x
		inc	h
		ld	e, (hl)
		inc	h
		ld	a, (hl)			; offset to character image
		add	a, FONT_BASE/256
		ld	d, a
		ld	hl, 0x0008		; line lenght in character image
		ex	de, hl
		ld	a, VPAGE
		out	(PAGE1), a		; set video memory to 0x4000-0x8000
.loop2:
		ld	l, l			; linear load
		ld	a, (hl)			; read 8 bytes to accel memory
		ld	b, b
		exx
		ld	a, yl
		out	(YPORT), a		; set y position out
		ld	l, l			; linear or
		or	(hl)			; with BackgroundBuffer
		ld	b, b
		ex	de, hl
		ld	l, l			; linear exclusive or
		xor	(hl)			; with ForegroundBuffer
		ld	a, a			; vertical write
		ld	(bc), a			; write 8 bytes from accel memory to vram
		ld	b, b
		ex	de, hl
		inc	bc			; next vertical line in video ram
		exx
		add	hl, de			; next line in character image
		djnz	.loop2
		pop	bc
		ld	a, yh
		out	(PAGE1), a
		ld	a, (bc)
		inc	bc
		or	a
		jr	nz, .loop1		; end string ?
.exit:
		ei				; enable interrupts
		ld	l, c			; return pointer to the end string
		ld	h, b
		pop	af
		out	(YPORT), a		; restore YPORT
		pop	iy
		ret

;[]=======================================================================[]
;
; Prepare text colors for accel text out
; Input:
;	b  - background and foreground color
;		d7 - d4 background color (0-15)
;		d3 - d0 foreground color (0-15)
;
; Destroy registers:
;	a, bc, 'hl

PreparePrintColors:
		ld	a, b
.prev_color:	cp	0x00			; color is prepared ?
		ret	z
		ld	(.prev_color + 1), a
		and	0x0F			; foreground color
		ld	c, a
		rlca
		rlca
		rlca
		rlca
		or	c
		exx
		cpl
		ld	hl, ForegroundBuffer	; foreground buffer
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		exx
		ld	a, b
		and	0xF0			; background color
		ld	b, a
		rrca
		rrca
		rrca
		rrca
		or	b
		exx
		xor	(hl)			; exclusive or with foreground color
		ld	hl, BackgroundBuffer	; background buffer
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		inc	hl
		ld	(hl), a
		exx
		ret

ForegroundBuffer:
		times 8 db 0
BackgroundBuffer:
		times 8 db 0

