; Semigraphics 24 plasma effect

; Original conversion to 6809 by Simon Jonassen

; Optimisations by:
;   John Kowalski (SOCK)
;   Simon Jonassen (SJ)
;   Stewart Orchard (SO)
;   Ciaran Anscomb (CA)

; SJ: original plasma routine converted from 6502 code
; SOCK: suggested various code changes to SJ
; CA: interlace successive updates (~ double perceived speed)
; SO: sintab & coltab pointers adjusted before each line (massive boost)
; SJ: precalculate now-constant coltab indices before y loop
;     (now renders in under 2 frames)

; Other optimisations over time by the cast.

fb0		equ		$0400
fb_w		equ		32
fb_h		equ		192
fb0_end		equ		fb0+fb_w*fb_h

pl_h		equ		192
pl_start	equ		fb0+((fb_h-pl_h)/2)*fb_w
pl_end		equ		pl_start+pl_h*fb_w

		org		$3f00
		setdp		$3f

;******************************************
; SCREEN POINTER
;******************************************

mainloop

stmod		ldu		#sintab

		; precalculate coltab offsets for this line
oiter		macro
		ldd		(\1-8)*2,u	; (u=sintab)
		sta		ctoffa_\1+2
		stb		ctoffb_\1+2
		endm

		oiter		0
		oiter		1
		oiter		2
		oiter		3
		jsr		note
		oiter		4
		oiter		5
		oiter		6
		oiter		7
		jsr		note
		oiter		8
		oiter		9
		oiter		10
		oiter		11
		jsr		note
		oiter		12
		oiter		13
		oiter		14
		oiter		15
		jsr		note
;******************************************
; YLOOP
;******************************************

y1loop		jsr		note

t1		lda		>sintab
t2		adda		>sintab

		sta		ctbase+2
ctbase		ldu		#coltab

xiter		macro
ctoffa_\1	lda		<0,u		; (u=coltab)
ctoffb_\1	ldb		<0,u		; (u=coltab)
		std		(\1-8)*2,x
		endm

		xiter		0
		xiter		1
		xiter		2
		xiter		3
		jsr		note
		xiter		4
		xiter		5
		xiter		6
		xiter		7
		jsr		note
		xiter		8
		xiter		9
		xiter		10
		xiter		11
		jsr		note
		xiter		12
		xiter		13
		xiter		14
		xiter		15
		jsr		note
		inc		t1+2
		inc		t2+2

		leax		2*fb_w,x	; skip line
		cmpx		#pl_end+16
		bhs		done_yloop
		jmp		y1loop

done_yloop	jsr		note

;******************************************
; END YLOOP
;******************************************

p1p2		ldd		#$0000
		inca
		subb		#3
		std		p1p2+1
		sta		t1+2
		stb		t2+2
		stb		stmod+2

		; wait for IRQ
;		lda		$ff02
;		sync
		pshs		x
		jsr		play
		puls		x



		cmpx		#pl_end+16
		beq		mainloop1

		; exit if any key pressed
		lda		$ff00
		ora		#$80
		coma
		bne		return

mainloop0
		ldx		#pl_start+16
		jmp		mainloop

mainloop1
		ldx		#pl_start+fb_w+16
		jmp		mainloop

return
		puls		cc,dp,pc

play	ldu 	#$0
v1ptr 	ldb	>$0000
pause	cmpb	#$fe		(end of data stream)
	bne	next
	ldd	#voice1		reset voice 1 pointer	
	std	v1ptr+1
	ldb	d,u		
	
next	inc	v1ptr+2 	increment lsb of pointer
	bne	next2
	inc	v1ptr+1		inc msb of voice pointer
next2	ldx	#table		(frequency table)
	abx			get value in freq tab (word values)
	abx			thats why 2x abx
	ldd	,x		get the freq itself
	std	>freq+1    	store inside irq code
	rts	


;****************************
; inner loop get played 
; all the time
;****************************
note
sum	ldd	#0		
freq	addd	#0		
 	std	sum+1		
	bcc	skip		

	lda	$ff20		;4      (load dac)
	eora	#$80		;2	(flip bit)
	sta	$ff20		;4      (output to dac)
skip	rts			;15



start

		pshs		cc,dp

;******************************************
;KILL IRQ'S
;******************************************
		orcc		#$50
;******************************************
;SETUP DP
;******************************************

		lda		#$3f
		tfr		a,dp

;******************************************
; SG24
;******************************************
		sta		$ffc5
		sta		$ffc3
;******************************************
; ALTERNATE CSS
;******************************************
		lda		#8
		sta		$ff22
;******************************************
;CLS
;******************************************
		ldx		#fb0
		ldd		#$8080
cls		std		,x++
		cmpx		#fb0_end
		bne		cls
;******************************************
; SETUP
;******************************************

;******************************************
; SOUND ON DAC 
;******************************************
		lda		$ff23
		ora		#$8	
		sta		$ff23
;******************************************
; RESET TUNE POINTER
;******************************************
		ldd		#voice1		
		std		v1ptr+1


		; wait until no key pressed
		clr		$ff02
waitnokey	lda		$ff00
		ora		#$80
		coma
		bne		waitnokey
		; note - column outputs left all zero

		bra		mainloop0

		rmb		(384-*%256)%256	; align with $xx80

black		equ		$80
dk_orange	equ		$20
br_orange	equ		$60
green		equ		$8f
yellow		equ		$9f
blue		equ		$af
red		equ		$bf
white		equ		$cf
cyan		equ		$df
magenta		equ		$ef
orange		equ		$ff

coltab_a	macro
		fill	black,2
		fill	dk_orange,2
		fill	red,2
		fill	orange,2
		fill	br_orange,2
		fill	yellow,2
		fill	green,2
		fill	cyan,2
		fill	green,2
		fill	yellow,2
		fill	br_orange,2
		fill	orange,2
		fill	red,2
		fill	dk_orange,2
		fill	black,2

		fill	dk_orange,2
		fill	red,2
		fill	blue,2
		fill	magenta,2
		fill	blue,2
		fill	red,2
		fill	orange,2
		fill	br_orange,2
		fill	yellow,2
		fill	green,2
		fill	cyan,2
		fill	green,2
		fill	yellow,2
		fill	br_orange,2
		fill	orange,2
		fill	red,2
		fill	dk_orange,2
		endm

coltab_b	macro
		fill	black,2
		fill	dk_orange,2
		fill	red,2
		fill	orange,2
		fill	br_orange,2
		fill	yellow,2
		fill	white,2
		fill	white,2
		fill	white,2
		fill	yellow,2
		fill	br_orange,2
		fill	orange,2
		fill	red,2
		fill	dk_orange,2

		fill	red,2
		fill	blue,2
		fill	red,2
		fill	magenta,2
		fill	blue,2
		fill	magenta,2
		fill	red,2
		fill	blue,2
		fill	red,2
		fill	orange,2
		fill	br_orange,2
		fill	yellow,2
		fill	green,2
		fill	yellow,2
		fill	br_orange,2
		fill	orange,2
		fill	red,2
		fill	dk_orange,2
		endm

		coltab_a
		coltab_b
coltab
		coltab_a
		coltab_b
		coltab_a
		coltab_b
		coltab_a
		coltab_b

		; already aligned

sintab_n	macro
		fcb	0,-1,-2,-3,-4,-5,-6,-7
		fcb	-8,-9,-10,-11,-12,-13,-14,-15
		fcb	-16,-17,-18,-19,-20,-21,-22,-22
		fcb	-23,-24,-25,-26,-27,-27,-28,-29
		fcb	-30,-30,-31,-32,-32,-33,-34,-34
		fcb	-35,-35,-36,-37,-37,-38,-38,-38
		fcb	-39,-39,-40,-40,-40,-40,-41,-41
		fcb	-41,-41,-42,-42,-42,-42,-42,-42
		fcb	-42,-42,-42,-42,-42,-42,-42,-41
		fcb	-41,-41,-41,-40,-40,-40,-40,-39
		fcb	-39,-38,-38,-38,-37,-37,-36,-35
		fcb	-35,-34,-34,-33,-32,-32,-31,-30
		fcb	-30,-29,-28,-27,-27,-26,-25,-24
		fcb	-23,-22,-22,-21,-20,-19,-18,-17
		fcb	-16,-15,-14,-13,-12,-11,-10,-9
		fcb	-8,-7,-6,-5,-4,-3,-2,-1
		endm

sintab_p	macro
		fcb	0,1,2,3,4,5,6,7
		fcb	8,9,10,11,12,13,14,15
		fcb	16,17,18,19,20,21,22,22
		fcb	23,24,25,26,27,27,28,29
		fcb	30,30,31,32,32,33,34,34
		fcb	35,35,36,37,37,38,38,38
		fcb	39,39,40,40,40,40,41,41
		fcb	41,41,42,42,42,42,42,42
		fcb	42,42,42,42,42,42,42,41
		fcb	41,41,41,40,40,40,40,39
		fcb	39,38,38,38,37,37,36,35
		fcb	35,34,34,33,32,32,31,30
		fcb	30,29,28,27,27,26,25,24
		fcb	23,22,22,21,20,19,18,17
		fcb	16,15,14,13,12,11,10,9
		fcb	8,7,6,5,4,3,2,1
		endm

		sintab_n
sintab
		sintab_p
		sintab_n
		fcb	0,1,2,3,4,5,6,7
		fcb	8,9,10,11,12,13,14,15

table	fdb	544,577,611,647,686,727,770,816,864,915,970,1028
	fdb	1089,1153,1222,1295,1372,1453,1540,1631,1728,1831,1940,2055
	fdb	2177,2307,2444,2589,2743,2906,3079,3262,3456,3662,3879,4110
	fdb	4355,4613,4888,5178,5486,5813,6158,6524,6912,7323,7759,8220
	fdb	8709,9227,9775,10357,10972,11625,12316,13049,13825,14647,15518,16440
	fdb	17418,18453,19551,20713,21945,23250,24633,26097,27649,29293,31035,32880
	fdb	34835,36907,39102,41427,43890,46500,49265,52194,55298,58586,62070,65761
	fdb	0,0,0


voice1	     fcb $c,$55,$c,$55,$18,$55,$c,$55,$c,$55,$18,$55,$c,$55,$c,$55
             fcb $14,$55,$8,$55,$8,$55,$14,$55,$8,$55,$8,$55,$14,$55,$8,$55
	     fcb $7,$55,$7,$55,$13,$55,$7,$55,$7,$55,$13,$55,$7,$55,$7,$55
             fcb $18,$55,$c,$55,$c,$55,$18,$55,$c,$55,$c,$55,$18,$55,$8,$55
             fcb $8,$55,$14,$55,$8,$55,$8,$55,$14,$55,$8,$55,$8,$55,$7,$55
             fcb $7,$55,$13,$55,$7,$55,$7,$55,$13,$55,$7,$55,$7,$55
	     fcb $c,$c,$18,$18,$c,$c,$18,$18,$c,$c,$18,$18
             fcb $14,$14,$8,$8,$14,$14,$8,$8,$14,$14,$8,$8
	     fcb $7,$7,$13,$13,$7,$7,$13,$13,$7,$7,$13,$13


             fcb $fe


		end	start
