;draw an image on screen

.entry_point
	ld HL,crtc_config
	call GFX_SetCRTC
	call GFX_cls
	call draw_image
	ret


.crtc_config
	db #01,#20,#02,#2A,4,#26,5,0,#06,32,#07,38,#0C,#30,#0D,#00,9,7,#FF
	
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:

	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
	
.draw_image
	ld HL,image
	ld DE,#C000
	ld B,32
.draw_image_char
	push BC
	push DE
.draw_image_line
	ld B,8
.draw_image_byte
	push BC
	push DE
	ld BC,64
	ldir
	pop DE
	ld A,8
	add D
	ld D,A
	pop BC
	djnz draw_image_byte
	pop DE
	ex DE,HL
	ld BC,64
	add HL,BC
	ex DE,HL
	pop BC
	djnz draw_image_char
	ret
	
.image
	incbin "gfx/bg2.pm1"