	opt c-

NumStars	equ	80

STARS:
		move.l	#6,SVELOCITY
		move.l	#BPWIDTH/2,CENTREX
		move.l	#BPHEIGHT/2,CENTREY
;		move.l	#$FFFFFFC4,SROTX
;		move.l	#150,SROTY
;		move.l	#0,SROTZ
		move.l	#$00200020,HSPREAD
		move.l	#STARS1,SRCPTR

		btst	#10,$dff016
		bne.s	NY

		add.l	#2,SROTX

NY:
;		add.l	#2,SROTY
		move.l	#StarBuff,a0
		move.w	#NumStars-1,d7
		bsr	ClrStars
		bsr	GetSinCos
		bsr	Rotate1
		bsr	Rotate2
		move.l	SrcPtr,a0
		move.w	#NumStars-1,d7
		move.l	RDX,d0
		asr.l	#3,d0
		move.l	RDY,d1
		asr.l	#3,d1
		move.l	RDZ,d2
		asr.l	#3,d2
		bsr	ClipStars
		move.l	SrcPtr,a1
		move.l	#StarBuff,a0
		move.w	#NumStars-1,d7
		bsr	Perspective
		move.l	#StarBuff,a0
		move.w	#NumStars-1,d7
		bsr	PlotStars
		move.w	#$00f0,$dff180
		rts

PlotStars:	lea	ClrBuff(pc),a2
PlotLoop:	move.l	(a0)+,d0
		move.l	(a0)+,d1
		move.l	(a0)+,d3
		tst.w	d0
		blt.s	PlotNext
		tst.w	d1
		blt.s	PlotNext
		cmp.w	#319,d0
		bgt.s	PlotNext
		cmp.w	#199,d1
		bgt.s	PlotNext
		moveq	#1,d4
		cmp.l	#-1024,d3
		blt.s	DoPlot
		moveq	#2,d4
		cmp.l	#1024,d3
		blt.s	DoPlot
		moveq	#3,d4
DoPlot:		move.w	#$8000,d3
		moveq	#15,d2
		and.w	d0,d2
		asr.w	#3,d0
		and.l	#$FFFFFFFE,d0
		lsr.w	d2,d3
		muls	#40,d1
		add.l	d0,d1
		btst	#0,d4
		beq.s	Plot2
		lea	BP1,a1
		add.l	d1,a1
		or.w	d3,(a1)
		move.l	a1,(a2)+
Plot2:		btst	#1,d4
		beq.s	PlotNext
		lea	BP2,a1
		add.l	d1,a1
		or.w	d3,(a1)
		move.l	a1,(a2)+
PlotNext:	dbra	d7,PlotLoop
		move.l	#-1,(a2)+
		rts

ClipStars:	movem.l	(a0),d3-d5

		add.l	d0,d3
		cmp.l	#2560,d3
		ble.s	TstX2
		sub.l	#5120,d3
TstX2:		cmp.l	#-2560,d3
		bge.s	TstY
		add.l	#5120,d3

TstY:		add.l	d1,d4
		cmp.l	#2560,d4
		ble.s	TstY2
		sub.l	#5120,d4
TstY2:		cmp.l	#-2560,d4
		bge.s	TstZ1
		add.l	#5120,d4

TstZ1:		add.l	d2,d5
		cmp.l	#2560,d5
		ble.s	TstZ2
		sub.l	#5120,d5
TstZ2:		cmp.l	#-2560,d5
		bge.s	NextTst
		add.l	#5120,d5
NextTst:	move.l	d3,(a0)+
		move.l	d4,(a0)+
		move.l	d5,(a0)+
		dbra	d7,ClipStars
		rts

ClrStars:	lea	ClrBuff(pc),a0
		moveq	#0,d0
SClrLoop:	tst.l	(a0)
		ble.s	EndSClr
		move.l	(a0)+,a1
		clr.w	(a1)
		bra.s	SClrLoop
EndSClr:	rts

Perspective:	move.l	(a1)+,d2		RDX
		move.l	(a1)+,d3		RDY
		move.l	(a1)+,d4		RDZ

		move.l	ZOOM1(pc),d1
		sub.l	d4,d1
		asr.l	#4,d1
		move.l	ZOOM2(pc),d0
		asr.l	#4,d0
		divs	d1,d0
		move.w	d0,d5			FZoom

		moveq	#9,d6
		move.l	d2,d0
		muls	d5,d0
		asr.l	d6,d0
		muls	HSPREAD(pc),d0
		asr.l	d6,d0
		add.l	CENTREX(pc),d0
		move.l	d0,(a0)+
		move.l	d3,d1
		muls	d5,d1
		asr.l	d6,d1
		muls	VSPREAD(pc),d1
		asr.l	d6,d1
		move.l	CENTREY(pc),d0
		sub.l	d1,d0
		move.l	d0,(a0)+
		move.l	d4,(a0)+
		dbra	d7,Perspective
		rts

ROTATE2:	moveq	#0,d3
		moveq	#0,d4
		move.l	SVELOCITY,d5

		move.w	d3,d0
		muls	SVAL1,d0
		move.w	d4,d1
		muls	SVAL2,d1
		move.w	d5,d2
		muls	SVAL3,d2
		add.l	d1,d0
		add.l	d0,d2
		move.l	d2,RDX

		move.w	d3,d0
		muls	SVAL4,d0
		move.w	d4,d1
		muls	SVAL5,d1
		move.w	d5,d2
		muls	SVAL6,d2
		add.l	d1,d0
		add.l	d0,d2
		move.l	d2,RDY

		move.w	d3,d0
		muls	SVAL7,d0
		move.w	d4,d1
		muls	SVAL8,d1
		move.w	d5,d2
		muls	SVAL9,d2
		add.l	d1,d0
		add.l	d0,d2
		move.l	d2,RDZ
		rts

ROTATE1:	moveq	#9,d6
		move.w	SCOSX,d0
		muls	SCOSY,d0
		asr.l	d6,d0	
		move.w	SSINX,d1
		muls	SSINY,d1
		asr.l	d6,d1
		muls	SSINZ,d1
		asr.l	d6,d1
		sub.w	d1,d0
		move.w	d0,SVAL1	(CosX*CosY) - SinZ*(SinX*SinY)

		move.w	SCOSX,d0	
		neg.w	d0
		muls	SSINY,d0
		asr.l	d6,d0
		move.w	SSINX,d1
		muls	SCOSY,d1
		asr.l	d6,d1
		muls	SSINZ,d1
		asr.l	d6,d1
		sub.w	d1,d0
		move.w	d0,SVAL2	(-CosX*SinY) - SinZ*(SinX*CosY)
	
		move.w	SSINX,d0
		muls	SCOSZ,d0
		asr.l	d6,d0
		move.w	d0,SVAL3	SinX*CosZ
		move.w	SSINY,d0
		muls	SCOSZ,d0
		asr.l	d6,d0
		move.w	d0,SVAL4	SinY*CosZ
		move.w	SCOSY,d0
		muls	SCOSZ,d0
		asr.l	d6,d0
		move.w	d0,SVAL5	CosY*CosZ

		move.w	SSINZ,SVAL6	SinZ

		move.w	SSINX,d0
		neg.w	d0
		muls	SCOSY,d0
		asr.l	d6,d0
		move.w	SCOSX,d1
		muls	SSINY,d1
		asr.l	d6,d1
		muls	SSINZ,d1
		asr.l	d6,d1
		sub.w	d1,d0
		move.w	d0,SVAL7	(-SinX*CosY) - SinZ*(SinY*CosX) 

		move.w	SSINX,d0
		muls	SSINY,d0
		asr.l	d6,d0
		move.w	SCOSX,d1
		muls	SCOSY,d1
		asr.l	d6,d1
		muls	SSINZ,d1
		asr.l	d6,d1
		sub.w	d1,d0
		move.w	d0,SVAL8	(SinY*SinY) - SinZ*(CosX*CosY)
		move.w	SCOSX,d0
		muls	SCOSZ,d0
		asr.l	d6,d0
		move.w	d0,SVAL9	CosX*CosZ
		rts

GetSinCos:	cmp.l	#0,SROTX
		bge.s	RotX_Pos
		add.l	#720,SROTX
RotX_Pos:	cmp.l	#719,SROTX
		ble.s	DoRot_Y
		sub.l	#720,SROTX
DoRot_Y:	cmp.l	#0,SROTY
		bge.s	RotY_Pos
		add.l	#720,SROTY
RotY_Pos:	cmp.l	#719,SROTY
		ble.s	DoRot_Z
		sub.l	#720,SROTY
DoRot_Z:	cmp.l	#0,SROTZ
		bge.s	RotZ_Pos
		add.l	#720,SROTZ
RotZ_Pos:	cmp.l	#719,SROTZ
		ble.s	GetSCVals
		sub.l	#720,SROTZ
GetSCVals:	lea	SINTABLE,a0
		move.l	SROTX,d0
		move.w	0(a0,d0.l),SSINX
		add.l	#180,d0
		cmp.l	#719,d0
		ble.s	GetCosX
		sub.l	#720,d0
GetCosX:	move.w	0(a0,d0.l),SCOSX
		move.l	SROTY,d0
		move.w	0(a0,d0.l),SSINY
		add.l	#180,d0
		cmp.l	#719,d0
		ble.s	GetCosY
		sub.l	#720,d0
GetCosY:	move.w	0(a0,d0.l),SCOSY
		move.l	SROTZ,d0
		move.w	0(a0,d0.l),SSINZ
		add.l	#180,d0
		cmp.l	#719,d0
		ble	GetCosZ
		sub.l	#720,d0
GetCosZ:	move.w	0(a0,d0.l),SCOSZ
		rts

SROTX:		ds.l	1
SROTY:		ds.l	1
SROTZ:		ds.l	1
SSINX:		ds.w	1
SCOSX:		ds.w	1
SSINY:		ds.w	1
SCOSY:		ds.w	1
SSINZ:		ds.w	1
SCOSZ:		ds.w	1
HSPREAD:	dc.w	$001E
VSPREAD:	dc.w	$001E
CENTREX:	ds.l	1
CENTREY:	ds.l	1
SVELOCITY	ds.l	1
RDX:		dc.l	$000007F5
RDY:		dc.l	$FFFFFE00
RDZ:		dc.l	$000003CD
ZOOM2:		dc.l	$00100000		00140000
ZOOM1:		dc.l	$00000d00		00000C00
SVAL1:		dc.w	$006E
SVAL2:		dc.w	0
SVAL3:		dc.w	$FFC0
SVAL4:		dc.w	0
SVAL5:		dc.w	$0080
SVAL6:		dc.w	0
SVAL7:		dc.w	$0040
SVAL8:		dc.w	0
SVAL9:		dc.w	$006E
StarBuff:	dc.l	$000000F7
		dc.l	$0000004D
		dc.l	$000003CD
StarBuff2:	ds.l	NumStars*3
ClrBuff:	ds.l	NumStars*3
SrcPtr		ds.l	1

STARS1:		dc.l	$0000079E,$0000023B,$FFFFFB35,$FFFFF79E,$0000063B
		dc.l	$FFFFF935,$0000039E,$0000043B,$00000935,$0000039E
		dc.l	$0000059E,$0000003B,$FFFFFF35,$0000059E,$0000063B
		dc.l	$0000079E,$0000023B,$00000535,$0000079E,$0000023B
		dc.l	$00000135,$FFFFF99E,$0000003B,$FFFFFF35,$FFFFF79E
		dc.l	$FFFFFB9E,$0000083B,$00000735,$FFFFF79E,$FFFFF63B
		dc.l	$00000535,$FFFFFB9E,$0000023B,$00000335,$FFFFF99E
		dc.l	$0000003B,$FFFFF935,$0000019E,$0000063B,$FFFFF735
		dc.l	$00000135,$FFFFFB9E,$FFFFF63B,$FFFFFF35,$FFFFF99E
		dc.l	$FFFFFA3B,$FFFFFD35,$0000079E,$0000023B,$00000335
		dc.l	$FFFFFC3B,$FFFFFD35,$FFFFF79E,$0000063B,$FFFFFB35
		dc.l	$0000063B,$00000135,$0000099E,$0000043B,$00000135

STARS2:		dc.l	$FFFFF6A5,$FFFFFE00,$FFFFFC8B,$FFFFFAA5,$FFFFFE00
		dc.l	$0000028B,$FFFFFCA5,$FFFFFE00,$0000008B,$FFFFFAA5
		dc.l	$FFFFFE00,$FFFFFE8B,$FFFFF6A5,$FFFFFE00,$0000048B
		dc.l	$FFFFF6A5,$FFFFFE00,$0000068B,$FFFFF6A5,$FFFFFE00
		dc.l	$FFFFFA8B,$000006A5,$FFFFFE00,$FFFFF68B,$000002A5
		dc.l	$FFFFFE00,$FFFFFA8B,$000004A5,$FFFFFE00,$FFFFF88B
		dc.l	$FFFFFEA5,$FFFFFE00,$0000088B,$FFFFFAA5,$FFFFFE00
		dc.l	$0000068B,$FFFFFEA5,$FFFFFE00,$0000048B,$FFFFFCA5
		dc.l	$FFFFFE00,$0000028B,$FFFFF8A5,$FFFFFE00,$0000028B
		dc.l	$000008A5,$FFFFFE00,$0000008B,$000000A5,$FFFFFE00
		dc.l	$0000028B,$FFFFFEA5,$FFFFFE00,$0000008B,$FFFFFCA5
		dc.l	$FFFFFE00,$FFFFFE8B,$FFFFFAA5,$FFFFFE00,$FFFFFC8B
STARS3:		dc.l	$FFFFF600,$FFFFFC00,0,$FFFFFA00,$00000400,$00000600
		dc.l	$FFFFFC00,$FFFFFC00,$00000400,$FFFFFA00,$00000400
		dc.l	$00000200,$00000A00,$FFFFFC00,$00000800,$FFFFF600
		dc.l	$00000400,$00000A00,$00000A00,$FFFFFC00,$00001200
		dc.l	$00000600,$00000400,$00000E00,$00000200,$FFFFFC00
		dc.l	$FFFFFE00,$00000400,$00000400,$FFFFFC00,$FFFFFE00
		dc.l	$FFFFFC00,$FFFFF800,$FFFFFA00,$00000400,$FFFFF600
		dc.l	$FFFFFE00,$FFFFFC00,$FFFFF400,$FFFFFC00,$00000400
		dc.l	$FFFFF200,$FFFFF800,$FFFFFC00,$FFFFF200,$00000800
		dc.l	$00000400,$FFFFF000,0,$FFFFFC00,$00000600,$FFFFFE00
		dc.l	$00000400,$00000400,$FFFFFC00,$FFFFFC00,$00000200
		dc.l	$FFFFFA00,$00000400,0
STARS4:		dc.l	$FFFFF600,$FFFFF600,0,$FFFFFA00,$FFFFFA00,$00000600
		dc.l	$FFFFFE00,$00000400,$FFFFFA00,$FFFFFC00,$FFFFFE00
		dc.l	$00000200,$FFFFF000,$00000800,$FFFFFA00,$00000600
		dc.l	$FFFFFE00,$FFFFFE00,$00000400,$FFFFFC00,$00000400
		dc.l	$00000200,$00000A00,$FFFFF600,$00000800,$FFFFF600
		dc.l	$FFFFFC00,$00000800,$00000400,$FFFFFA00,$00000200
		dc.l	$00000600,$FFFFF800,$00000E00,$00000600,$00000800
		dc.l	$FFFFFC00,$FFFFF800,$FFFFFA00,$FFFFFE00,$FFFFF600
		dc.l	$FFFFFE00,$00000A00,$FFFFF400,$FFFFFC00,$FFFFFA00
		dc.l	$00000A00,$00000A00,$00000200,$FFFFFE00,$00001200
		dc.l	$FFFFF200,$FFFFF800,$FFFFF800,$FFFFF200,$00000200
		dc.l	$00000200,$FFFFFA00,$FFFFFA00,0

SinTable	incbin	sintable
