; ---
; ---	FADE
; ---
.local	FADE

	stx	jsr+1
	sty	jsr+2

	ldx	#6
	jsr	wait

	mva	#16	cnt

lcol	mva	<tcol	fcnt
hcol	mva	>tcol	fcnt+1

	ldx	#2
	jsr	wait

jsr	jsr	$ffff

	dec	cnt
	bne	lcol

	ldx	#6

wait	lda:cmp:req cloc
	dex
	bne	wait
	rts

cnt	brk
.end


; ---
; ---	FADE IN
; ---
.local	FADE_IN

	ldx	<fade
	ldy	>fade
	jmp	:fade

fade	ldy	#0
	mva	(fcnt),y	fadr

	iny
	lda	(fcnt),y	
	sne:rts

	sta	fadr+1

	lda	(fadr),y
	tax

	iny
	eor	(fcnt),y
	beq	skp

	dey

	pha

	and	#$f0
	beq	plus1

plus16	txa
	add	#$10
	tax

plus1	pla
	and	#$0f
	seq
	inx

	txa
	sta	(fadr),y

skp	adw	fcnt	#3

	jmp	fade
.end


; ---
; ---	FADE OUT
; ---
.local	FADE_OUT

	ldx	<fade
	ldy	>fade
	jmp	:fade

fade	ldy	#0
	mva	(fcnt),y	fadr

	iny
	lda	(fcnt),y
	sne:rts

	sta	fadr+1

	lda	(fadr),y
	tax

	and	#$0f
	beq	skp0

	dex
skp0
	txa
	and	#$f0
	beq	skp1

	txa
	sub	#$10
	tax
skp1
	txa

	sta	(fadr),y

	adw	fcnt	#3

	jmp	fade
.end


; ---
; ---	SAVE COLOR
; ---
.local	SAVE_COLOR

	mva	fade.lcol+1	fcnt
	mva	fade.hcol+1	fcnt+1

loop	ldy	#0
	mva	(fcnt),y	fadr

	iny
	lda	(fcnt),y
	sne:rts

	sta	fadr+1

	lda	(fadr),y

	iny
	sta	(fcnt),y

	dey
	mva	#0	(fadr),y	; black screen (all colors = $00)

	adw	fcnt	#3

	jmp	loop
.end

	.use DLI,NMI
tcol
	dta t(c0,c1,c2,c3,c4,c5,c6,c7,c8)
	dta t(0)
