

;	org $1600

	opt	6809
	opt	cd
;start

;*********************************************
;SG8
;*********************************************
	sta	$ffc4		;sg12 off
	sta	$ffc3		;sg8 on

	ldx	#$4e0
	ldu	#hot
h1	ldd	,u++
	std	,x++
	cmpu	#hotend
	blo	h1



	ldx	#$a80
	ldu	#coco
h2	ldd	,u++
	std	,x++
	cmpu	#cocoend
	blo	h2


	; clear buffer

	ldx 	#dbuf_start
	ldd 	#0
clrlp	std 	,x++
	cmpx 	#dbuf_end
	blo 	clrlp

	
;******************************
; MOD - palette is always 
; same, no need to load
; more than once
;******************************
	ldy #palette		; point to palette

mloop  	lda	mycount
	bne	doit

hs2	lda	$ff03	
	bpl	hs2
	lda	$ff02
	

crap	ldx	#palette
	ldd	#$8080
	std	,x++
	stx	crap+1
	cmpx	#palend
	bne	cnt2



	jmp	$3300		;plasma jump


doit	dec	mycount	
	

	;draw random data at bottom of buffer
	;(this seeds the flames)


cnt2	ldx #dbuf_end-1
	ldb #28

	; pseudo-random number generator


;******************************
prng

rndx	lda #0
	inca
	sta rndx+1
rnda	eora #0
rndc	eora #0
	sta rnda+1
rndb	adda #0
	sta rndb+1
	lsra
	adda rndc+1
	eora rnda+1
	sta rndc+1

;******************************
; MOD
; no shifts.... just and
; for val between 0..31
; (32 byte palette)
;******************************
	anda  #$20
	sta ,-x
	decb
	bne prng
nodraw  


	; Update buffer and draw screen
	; Each pixel is calculated from pixels below
	;
	;  X
	; ABC
	;  D
	;
	; X = (A + B + C + D)/4
	;
	; Faster systems could use more pixels to calculate average
	; but 4 pixels gives reasonable effect



	ldu #fbuf_start+1	; screen pointer 1
	ldx #dbuf_start+33	

firem	macro
	ldd \1-1,x
;******************************
;MOD
;never overflows, so use addd
;******************************
	addd \1,x
	addd \1+1,x
	addd \1+64,x     ;32
	lsra
	lsra
	lsrb
	lsrb
	std \1-32,x
	std \1-64,x
	lda a,y
	ldb b,y
	std \1,u
	std \1+32,u
	endm

yloop

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26
	leax       64,x ;33+32	
	leau	64,u


	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+64	
	leau	64,u



	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+96	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26

	leax       64,x ;  33+128	
	leau	64,u


	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+160	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26

	leax       64,x ;  33+192	
	leau	64,u



	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+224	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+256	
	leau	64,u	

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+288	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26



	leax       64,x ;  33+320	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+352	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+384	
	leau	64,u


	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26



	leax       64,x ;  33+416	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+448	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26


	leax       64,x ;  33+480	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26

	leax       64,x ;  33+480	
	leau	64,u

	firem 2
	firem 4
	firem 6
	firem 8
	firem 10
	firem 12
	firem 14
	firem 16
	firem 18
	firem 20
	firem 22
	firem 24
	firem 26
	leax       64,x ;  33+480	
	leau	64,u

	JMP	mloop






;*****************************
	JMP	mloop
;*****************************

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



		align	$100
palette
	fill black,10
	fill dk_orange,1
	fill red,1
	fill orange,1
	fill br_orange,2
	fill yellow,2
	fill white,16

;****************************
	fill black,10
	fill dk_orange,1
	fill red,1
	fill orange,1
	fill br_orange,2
	fill yellow,2
	fill white,16

palend	equ	*


hot	fcb	$c5,$c0,$c0,$ca,$c5,$cf,$cf,$ca,$c5,$cf,$cf,$ca,$c0,$c0,$c0,$c0,$c0,$c0,$c0,$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