***************
* Fountain
* 6 bytes free.
****************

	include <includes/hardware.inc>
	include <macros/help.mac>

;;; ROM sets this address
screen0		equ $2000

 BEGIN_ZP
plot_color	ds 1
ptr		ds 2
rng_zp_high	ds 1
 END_ZP

x_pos		equ $400
y_pos		equ $500
vx		equ $600
vy		equ $700
ox_pos		equ $800
oy_pos		equ $900
col		equ $a00

 IFND LNX
	run $200-3
	jmp	bll_init
 ELSE
	run $200
 ENDIF

Start::
	bra	cont
	pha
	tsb	$fd80
	lda	$fd0a
	beq	.2
	cmp	#20
	bcs	.1
	lsr
//->	eor	#$f0		; blue bottom
.2
	sta	BLUERED0
.1

//->	asl			; blue background
//->	adc	#$20
//->	and	#$f0
//->	sta	BLUERED0

	pla
	rti

cont:
	ldx	#8
	stx	$fff9		; enable vectors
	sty	$fffe		; y = 2 => $202
	sty	$ffff
;;; --------------------
	dex
.pal
	txa
	asl
	stz	$fda0,x
	sta	$fda8,x
	sta	$fdb0,x
	lda	#$f
	sta	$fdb8,x
	dex
	bpl	.pal
	stx	$fdbf

	lda	#128
	tsb	$fd01		; enable HBL interrupt

;;; Init sound for RND and noise
	lda	#AUD_RESETDONE|AUD_RELOAD|AUD_CNTEN|AUD_4us
	sta	CONTRL_A
	sta	FEEDBACK_A
	sta	FREQ_A

	cli
	lda	#80		; pre-load math registers
	sta	MATHE_C
	stz	MATHE_E
main::
	inx
	stz	plot_color
	ldy	ox_pos,x
	lda	oy_pos,x
	jsr	plot

	lda	col,x
	sta	plot_color

	clc
	lda	x_pos,x
	sta	ox_pos,x
	adc	vx,x
	sta	x_pos,x
	tay

	lda	y_pos,x
	sta	oy_pos,x
	lda	vy,x
	cmp	#$80
	ror
	adc	oy_pos,x
	sta	y_pos,x

	jsr	plot

	inc	vy,x
	bmi	main
	lda	#4
	cmp	vy,x
	bcs	main
	sta	vy,x
	dec	col,x
	bpl	main

new_particle::
	jsr	rnd

	sta	VOLUME_A

	and	#7
	adc	#78
	sta	x_pos,x

.rnd_again
	jsr	rnd
	and	#3
	beq	.rnd_again
	dec
	dec
	sta	vx,x		; -1,0,1

	adc	#103		; variable y start
	sta	y_pos,x

	jsr	rnd
	and	#15
	adc	#4
	eor	#$ff
	inc

	sta	vy,x

	lda	#15
	sta	col,x
	bra	main


plot::
	sta	MATHE_E+1
	NOP8

	clc
	lda	MATHE_A+1
	sta	ptr
	lda	MATHE_A+2
	adc	#$20
	sta	ptr+1
	tya
	lsr
	tay

	lda	plot_color

	bcs	.1

	asl
	asl
	asl
	asl
.1
	sta	(ptr),y
//->	rts			; fall through to save one byte

rnd:
	LDA rng_zp_high
	ror
	eor $fd23
	STA rng_zp_high
	rts

End:
 IFND LNX
	;; Lynx rom clears to zero after boot loader!
	dc.b 0
 ENDIF
size	set End-Start
free	set 249-size

	IF free > 0
	REPT	free
	dc.b	$42
	ENDR
	ENDIF

 IFND LNX
	include "bll_init.inc"
 ENDIF
	echo "Size:%dsize  Free:%dfree"
