	Section	Demo,Code_C

	Opt	C-,O+,w-

numberofstars	= 200
star_speed		= 8
	
Start:	lea	$dff000,a5		; hardware base address

	Move.w	$1c(a5),OldInt	; Save Old Interupts
	Move.w	$02(a5),OldDma	; Save Old DMA

	Move.w	#$7fff,$9a(a5)	; Clear DMA
	Move.w	#$7fff,$96(a5)	; Clear Interupts
	Move.w	#$7fff,$9c(a5)	; Clear Interupt Requests

	move.w	#59,d7
.loop	move.w	$dff006,$dff180
	bsr	starfield
	dbf	d7,.loop

	
	Move.b	#$7f,$bfed01	; kill timers (rem me for disk)
	Move.l	$6c.w,OldV3		; save level 3 int

	Move.l	#my_level3,$6c.w	; put new copper interupt
	Move.l	#my_copper,$80(a5) 	; Address of copper 1
	Move.w	#$c010,$9a(a5)	; Start interupts

	Move.w	#$83ef,$96(a5)	; Start DMA ( 83ff for disk dma)
	Move.w	#1,$88(a5)		; Strobe for copper start

	move.l	#screen1,d0
	move.w	d0,bitplane0_lo
	swap	d0
	move.w	d0,bitplane0_hi
	move.l	#screen2,d0
	move.w	d0,bitplane1_lo
	swap	d0
	move.w	d0,bitplane1_hi

wait:	btst	#6,$bfe001
	bne.s	wait		; wait for lmb

	lea	$dff000,a5		; hardware base address
	move.l	#0,$80(a5)		; blank copper list
	move.l	#0,$84(a5)		; if gfx lib not open (slayer boot)
	move.w	#0,$180(a5)		; bgc to black
	
	Move.l	OldV3,$6c.w		; restore old l3
	
	Lea	GfxLib,a1		; Pointer to Library Text
	move.l	4.w,a6
	jsr	-132(a6)		; forbid
	Move.l	4.w,a6		; Exec
	Moveq.l	#0,d0		; Clear D0
	Jsr	-$228(a6)		; Open Library
	cmp.l	#0,d0
	beq	no_gfx_lib		; if gfx lib not open dont restore copper lib (slayer boot)
	
	Move.l	d0,a1			
	Move.l	$26(a1),$80(a5)	; restore copper 1
	Move.l	$32(a1),$84(a5)	; restore copper 2
no_gfx_lib:

	Move.w	OldInt,d0		; start old interupts
	Or.w	#$8000,d0
	Move.w	d0,$9a(a5)
	Move.w	OldDma,d0		; start old DMA
	Or.w	#$8000,d0
	Move.w	d0,$96(a5)
	Move.b	#$9b,$bfed01	; Start Timers
	Move.l	4.w,a6		; Close Gfx lib
	Jsr	-$19e(a6)
	move.l	4.w,a6
	jsr	-138(a6)		; permit

	move.w	#0,$bfec00		; clear kbd

	Moveq.l	#0,d0		; No errors
	Rts			; Exit


my_level3:
	Movem.l	d0-d7/a0-a6,-(a7)	; New copper
	Move.w	#$10,$dff09c	; Serviced Interupt

	bsr	starfield
	;move.w	#$444,$dff180


Exit:	Movem.l	(a7)+,d0-d7/a0-a6
	Rte			; Return from Interupt

	*********

starfield:	movem.l	d0-d7/a0-a6,-(a7)

	lea	starsxpos,a4
	move.l	#numberofstars,d3	
	lea	starsypos,a5
	lea	mul40,a6

	lea	screen1,a0
	lea	screen2,a2

starsloop:	move.w	(a4)+,d4		; x
	move.w	(a4)+,d5		; y
	move.w	(a4),d6		; z

	sub.w	#star_speed,(a4)+
	tst.w	d6
	ble	tobig

	ext.l	d4		; depth calc
	divs	d6,d4
	ext.l	d5
	divs	d6,d5

	add.w	#160,d4		; xpos (centre field)
	add.w	#120,d5		; ypos (centre field)
	
	tst.w	d4
	blt	tobig
	tst.w	d5
	blt	tobig
	cmp.w	#320,d4
	bgt	tobig
	cmp.w	#245,d5
	bgt	tobig
	
	lsl.w	#1,d5
	move.w	(a6,d5),d5
	move.w	d4,d7
	lsr.w	#3,d4		; x 8
	add.w	d4,d5
	not.b	d7
	
	move.w	(a5),d0
	move.w	d5,(a5)+
	move.w	(a5),d1
	move.w	d7,(a5)+
	
	move.l	a0,a1
	bclr	d1,(a1,d0.w)
	move.l	a2,a1
	bclr	d1,(a1,d0.w)

	move.l	a0,a1	
	cmp.w	#400,d6
	bgt	stars_depth1

	cmp.w	#200,d6
	bgt	stars_depth2

	move.l	a0,a1
	bset	d7,(a1,d5)
	move.l	a2,a1
	bset	d7,(a1,d5)

starscont:
	dbf	d3,starsloop

	move.w	(a5)+,d0
	move.w	(a5),d1

	move.l	a0,a1
	bclr	d1,(a1,d0.w)
	move.l	a2,a1
	bclr	d1,(a1,d0.w)

	movem.l	(a7)+,d0-d7/a0-a6
	rts

stars_depth2:
	move.l	a2,a1
stars_depth1:
	bset	d7,(a1,d5)
	bra	starscont



tobig:	bsr	newdepth
	move.w	d0,-6(a4)
	bsr	newdepth
	move.w	d0,-4(a4)
	move.w	startdepth,-2(a4)
	bra	starscont

newdepth:	move.w	$dff006,d0
	lea	starsdepth,a3
	muls	(a3),d0
	add.w	$dff006,d0
	move.w	d0,(a3)
	rts
	

	*********

startdepth:	dc.w	500

starsdepth:	ds.l	numberofstars*2
starsxpos:	ds.l	numberofstars*2
starsypos:	ds.l	numberofstars*2


	*********
	
my_copper:		dc.w	$100,$2200	; 4 bp - lo res
		dc.w	$104,$a		; bp control reg
		dc.w	$108,0		; bp modulo - odd
		dc.w	$10a,0		; bp modulo - even
		dc.w	$180,0		; colour 0 to black
		dc.w	$102,0
		dc.w	$1fc,0
		
		dc.w	$08e,$2c81	; bp window start left
		dc.w	$090,$2cc1	; bp window bot right
		dc.w	$094,$d0	; bp stop horz
		dc.w	$92,$38

		dc.w	$120,0,$122,0,$124,0,$126,0
		dc.w	$128,0,$12a,0,$12c,0,$12e,0
		dc.w	$130,0,$132,0,$134,0,$136,0
		dc.w	$138,0,$13a,0,$13c,0,$13e,0
		

		dc.w	  $e0
bitplane0_hi:	dc.w	0,$e2
bitplane0_lo:	dc.w	0,$e4
bitplane1_hi:	dc.w	0,$e6
bitplane1_lo:	dc.w	0
		
cmap:		dc.w	$180,$000
		dc.w	$182,$444
		dc.w	$184,$888
		dc.w	$186,$ccc

				
		dc.w	$ffe1,$fffe	; end of ntsc screen
		dc.w	$3001,$ff00	; end of pal screen
		dc.w	$9c,$8010	; irq set bits - restart copper
		dc.w	$ffff,$fffe	; end of copper list

oldint:		dc.l	0
olddma:		dc.l	0
oldv3:		dc.l	0
gfxlib:		dc.b	"graphics.library",0
		even
		
screen1:		ds.l	10*256
screen2:		ds.l	10*256

mul40:	
a set 0
	rept	256
	dc.w	a*40
a set a+1
	endr

	end
	





		
