;***** install font and font functions *****

;init , install font and color tables
.FONT_Init

;************************
;*** copy interlaced font ***
;************************

	ld B,0	;256 double lines to process
	ld HL,temp_font
	ld DE,font
		
.FONT_Init_LoopA
	;copy simply 2 first bytes
repeat 2
	ld A,(HL)
	ld (DE),A
	inc HL
	inc DE
rend
	;next two bytes reversed
	ld C,(HL)
	inc HL
	ld A,(HL)
	inc HL
	
	ld (DE),A
	inc DE
	ld A,C
	ld (DE),A
	inc DE
	
	djnz FONT_Init_LoopA
	
;*********************
;*** Init color tables ***
;*********************

	call TRASH_InitColorTables
	
	ret
	
;***************************************************
;*** Draw a string at adress regarding hardware scrolling***
;***************************************************
;
;HL = string address
;DE = screen address

.FONT_DrawString

	ld BC,font
	
.FONT_DrawString_loop
	ld A,(HL)
	and 127		;remove end bit
	
	push HL
	push DE
	
	ld L,A
	ld H,0
	add HL,HL	;*2
	add HL,HL	;*4
	add HL,HL	;*8
	add HL,HL	;*16
	
	add HL,BC
	
;now draw char 4*2 lines
repeat 3
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	
	ld A,(HL)
	ld (DE),A
	inc L
	
	set 3,D
	
	ld A,(HL)
	ld (DE),A
	inc L
	dec E
	
	ld A,(HL)
	ld (DE),A
	inc L
	
	ld A,8
	add D
	ld D,A
rend
	
	ld A,(HL)
	ld (DE),A
	inc L
	inc E
	
	ld A,(HL)
	ld (DE),A
	inc L
	
	set 3,D
	
	ld A,(HL)
	ld (DE),A
	inc L
	dec E
	
	ld A,(HL)
	ld (DE),A
	inc L
	
	pop DE
	
;need twice because hard scroll
	ld A,D
	and %11000111
	ld D,A
	
	inc DE
	inc DE
	
	ld A,D
	and %11000111
	ld D,A

	pop HL
	
	bit 7,(HL)
	inc HL
	jp z,FONT_DrawString_loop
	
	ret
	
.colors
	db 0			;0
	db %10000000	;1
	db %00001000	;2
	db %10001000	;3
	
;**********************************	
;*** Draw a string with a color table ***
;*********************************
; HL = string address - DE = screen address - BC = table address

FONT_DrawStringEX:


.FONT_DrawStringEX_loop

	push HL
	push DE
	push BC

	ld A,(HL)
	and	127
	
	ld BC,font
	ld L,A
	ld H,0
	add HL,HL	;*2
	add HL,HL	;*4
	add HL,HL	;*8
	add HL,HL	;*16
	
	add HL,BC
	
	pop BC
	
;draw char

repeat 3

	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	inc E
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	
	set 3,D
	
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	dec E
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	
	ld A,8
	add D
	ld D,A
	
rend

	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	inc E
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	
	set 3,D
	
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	dec E
	ld C,(HL)
	ld A,(BC)
	ld (DE),A
	inc L
	
	pop DE
	
;need twice because hard scroll
	ld A,D
	and %11000111
	ld D,A
	
	inc DE
	inc DE
	
	ld A,D
	and %11000111
	ld D,A
	
	pop HL
	
	bit 7,(HL)
	inc HL
	jp z,FONT_DrawStringEX_loop
	
	ret

;************************************************	
;*** Get adress of a char according harwdware scroll***
;************************************************
; HL = XY	
	
GFX_GetCharAddress:
	push HL
	ld H,0
	add HL,HL	;*2
	add HL,HL	;*4
	push HL
	add HL,HL	;*8
	push HL
	add HL,HL	;*16
	push HL
	add HL,HL	;*32
	add HL,HL	;*64
	
	pop DE
	add HL,DE	;64+16 = 80
	pop DE
	add HL,DE	;80+8	= 88
	pop DE
	add HL,DE	;88+4 = 92
	
	pop DE		;get X
	ld E,D
	ld D,0
	ex DE,HL	
	add HL,HL	;X is word
	add HL,DE	;X+Y
	
	ld DE,(crtc_R12_offset)
	ex DE,HL
	
	add HL,HL	;CRTC offset is in char so multiplie
	add HL,DE	;offset + address
	
	ld A,H
	and %00000111	;cap address toi 2048
	or  %11000000	;C0 page
	
	ld H,A	;result is in HL
	
	ret	
.crtc_R12_offset
	dw	0

;get binary string from byte
; BC = target
.FONT_GetBinFromByte
	ld L,C
	ld H,B
	ld B,8
	
.FONT_GetBinFromByte_loop
	ld E,8
	rl A
	;jr nc,FONT_GetBinFromByteNoBit
	rl e
	ld (HL),E
	inc HL
	djnz FONT_GetBinFromByte_loop
	
	ret
	
	

;get hex string from byte
;A=digit
;BC = target
.FONT_GetHexFromByte
	push AF
	srl A
	srl A
	srl A
	srl A
	ld E,A
	ld D,0
	ld HL,hex_string
	add HL,DE
	ld A,(HL)
	ld (BC),A
	inc BC
	pop AF
	and 15
	ld E,A
	ld HL,hex_string
	add HL,DE
	ld A,(HL)
	ld (BC),A
	ret

.hex_string
	db "0123456789ABCDEF"
