
org #400
write direct "camode2.bin",#400

run start

nolist

.start
	di

	ld SP,stack

	ld BC,#7F8C+2
	out (C),C

	ld BC,#7F00
	ld A,#54
	ld DE,#104B
	out (C),C
	out (C),A
	inc C
	out (C),C
	out (C),E
	out (C),D
	out (C),A

	ld HL,crtc
	call GFX_SetCRTC


	call draw_image


	jr $

.crtc
db #01,48,#02,#32,#06,34,#07,#23,#0C,#2C,#0D,#00,#FF

;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 C,34 ;272/8
	ld DE,#8000
	ld HL,image

.di_loop1
	ld B,48
	push BC
	push HL
.di_loop2
	push HL
	push BC
	ld BC,96

repeat 8
;line draw
	ld A,(HL)
	ld (DE),A
	inc HL
	inc DE
	ld A,(HL)
	ld (DE),A
	dec DE
	dec HL
	ld A,8
	add D
	ld D,A
	add HL,BC
rend
	pop BC
	call compute_adress
	pop HL
	inc HL
	inc HL

	dec B
	jp nz,di_loop2

	pop HL
	ld BC,768
	add HL,BC
	pop BC
	dec C
	jp nz,di_loop1


	ret

	

	

.compute_adress
	ld A,D
	and %111
	ld D,A

	inc DE
	inc DE

	bit 3,D
	jr z,no_wrap

	ld A,#C0
	ld (cur_address),A
.no_wrap
	ld A,D
	and %111
	ld D,A
	ld A,(cur_address)
	or D
	ld D,A
	ret

cur_address db #80

ds 256,0
.stack


.image
	incbin "gfx/ca-mode2.pm2"

list
;/end	