;setup level palette
;
;HL = palette - A = level (0-3)
;
.GFX_SetupLevelPalette

	;first , compute palette address
	ex DE,HL
	
	ld L,A
	ld H,0
	ld C,A
	ld B,H
	
	add HL,HL	;2
	add HL,HL	;4
	add HL,HL	;8
	add HL,HL	;16
	
	add HL,BC	;17
	
	add HL,DE	;+index
	

	push HL
	;change floating color first
	ld B,#7F
	ld A,(lvl_floating_color)
	ld E,A
	ld D,0
	add HL,DE
	ld A,(HL)
	call GFX_ConvertColor 
	ld (FL_COLOR),A
	pop HL
	
;copy reserved colors
	
	ld B,4
	ld DE,BG_COLOR

	;reserved colors , just copy them
.GFX_SetupLevelPaletteReservedColors
	ld A,(HL)
	call GFX_ConvertColor
	ld (DE),A
	inc DE
	inc HL
	djnz GFX_SetupLevelPaletteReservedColors
	
	;for other , just setup CRTC color
	ld BC,#7F04
.GFX_SetupLevelPaletteLoop
	ld A,(HL)
	inc HL
	call GFX_ConvertColor
	out (C),C
	out (C),A
	inc C
	ld A,C
	cp 17
	jr nz,GFX_SetupLevelPaletteLoop

	ret

;show screen (R6)
.GFX_ShowScreen
IF debug
	ld A,24 ;32 ;24
ELSE
	ld A,24
ENDIF
	call GFX_SendR6
	ld A,32
	jr GFX_SendR1
.GFX_HideScreen
	xor A
	
.GFX_SendR1
	ld BC,#BC01
	out (C),C
	inc B
	out (C),A
	ret
	
	
.GFX_SendR6
	ld BC,#BC06	
	out (C),C
	inc B
	out (C),A
	ret

;Wait for VBL
.GFX_WaitVbl
	ld B,#F5
.GFX_WaitVblLoop
	in A,(C)
	rra
	jr nc,GFX_WaitVblLoop
	ret
	
;Wait VBL end
.GFX_WaitVblEnd
	;call GFX_WaitVbl
	ld B,#F5
.GFX_WaitVblEndLoop
	in A,(C)
	rra
	jr c,GFX_WaitVblEndLoop
	ret

;convert color from firmware to harware
;A = firmware color
; Return hardware color in A

.GFX_ConvertColor

	push HL
	push DE
	ld E,A
	ld D,0
	ld HL,GFX_color_table
	add HL,DE	
	ld A,(HL)
	pop DE
	pop HL
	ret

;clear screen range from #C000 to #FFFF
GFX_Cls:
	ld HL,#C000
	xor A
	ld (HL),A
	ld E,L
	ld D,H
	inc DE
	ld BC,#3FFF
	ldir
	ret

;setup crtc
; HL = setup data (reg,data) ending with #FF
GFX_SetCRTC:

	call GFX_WaitVbl ;correct CRTC synchronisations ?

	call CRTC_SetCRTC_Entry
	
	ld B,3
.GFX_SetCRTC_SyncLoop
	push BC
	call GFX_WaitVbl
	call GFX_WaitVblEnd
	pop BC
	djnz GFX_SetCRTC_SyncLoop
	ret

.CRTC_SetCRTC_Entry
	ld B,#BC
	ld A,#FF
.CRTC_loop
	ld C,(HL)
	cp C
	ret z

	out (C),C
	inc B
	inc HL

	ld C,(HL)
	out (C),C
	dec B
	inc HL

	jr CRTC_loop

;setup palette for old
;use firmware colors definitions
;HL = color list + border
;if color = #FF -> end
GFX_SetColors:
	
	ld B,17
	xor A

	push HL
	pop IX

.Colors_loop	
	push BC
	push AF

	ld A,(IX)
	cp #FF
	jr z,GFX_SetColors_Quit
	
	and 31
	ld E,A
	ld D,0

	ld HL,GFX_color_table
	add HL,DE

	ld C,(HL)
	ld B,#7F

	pop AF

	out (C),A
	out (C),C

	pop BC

	inc A
	inc IX
	djnz Colors_loop
	ret
.GFX_SetColors_Quit
	pop AF
	pop BC
	ret

;table to convert from firmware to hardware colors
.GFX_color_table
;software color table
;only 27 colors

db #54 ;Black 	
db #44 ;Blue 
db #55 ;Bright Blue 
db #5C ;Red 	
db #58 ;Magenta 
db #5D ;Mauve 
db #4C ;Bright Red 
db #45 ;Purple 
db #4D ;Bright Magenta 
db #56 ;Green 
db #46 ;Cyan 
db #57 ;Sky Blue 
db #5E ;Yellow 
db #40 ;White	
db #5F ;Pastel Blue
db #4E ;Orange
db #47 ;Pink	
db #4F ;Pastel Magenta	
db #52 ;Bright Green 
db #42 ;Sea Green 
db #53 ;Bright Cyan
db #5A ;Lime
db #59 ;Pastel Green
db #5B ;Pastel Cyan 
db #4A ;Bright Yellow
db #43 ;Pastel Yellow 	
db #4B ;Bright White