	opt c- o+
NumStars	equ	80
vbl_vec:	equ	$70
key_vec:	equ	$118
hbl_vec:	equ	$120
bpwidth:	equ	320
bpheight:	equ	200
start:
	clr.l   -(SP)           ;Go into supervisor mode
        move.w  #$20,-(SP)
        trap    #1
        addq.l  #6,SP
        move.l  D0,savesup
	dc.w	$a00a

	lea	palsave,a0
	move.l	#$ff8240,a1
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+

	move.w	#4,-(sp)
	trap	#14
	addq.l	#2,sp
	move.w	d0,ressave

	move.w	#0,-(sp)	;set to low res
	move.l	#-1,-(sp)
	move.l	#-1,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp
	bsr	getscrn
	lea	greypalette,a0
	bsr	mydocolors


mainloop:
	bsr	stars
	bsr	keyboard
	bsr	flip_scr
	bra	mainloop

flip_scr:
	move.l	seen,d1
	move.l	d1,d2
	add.l	#10000,d1
	add.l	#18000,d2
skloop:
	lea	$ff8205,a0
	moveq	#0,d0
	move.b	(a0),d0
	asl.l	#8,d0
	addq.l	#2,a0
	move.b	(a0),d0
	asl.l	#8,d0
	addq.l	#2,a0
	move.b	(a0),d0
	cmp.l	d1,d0
	blt.s	skloop
	cmp.l	d2,d0
	bgt.s	skloop
        move.l  seen,A0
        move.l  hiden,seen
        move.l  A0,hiden
        move.l  clrbuffseen,A0
        move.l  clrbuffhiden,clrbuffseen
        move.l  A0,clrbuffhiden
	move.b	seen+1,$ff8201
	move.b	seen+2,$ff8203
	rts


STARS:
		move.l	#4,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
*
*		tst.w	$28c8
*		beq.s	NY

		subq.w	#1,flip
		bpl.s	dz
		move.w	#1,flip
		add.l	#2,SROTx
dz:
		subq.w	#1,flip2
		bpl.s	ny
		move.w	#0,flip2
		add.l	#2,srotz
NY:
;		add.l	#2,SROTY
		move.l	#StarBuff,a0
*		move.w	#NumStars-1,d7
		bsr	ClrStars	;checks out
		bsr	GetSinCos	;checks out
		bsr	Rotate1		;checks out
		bsr	Rotate2		;checks out
		move.l	SrcPtr,a0
		move.w	#NumStars-1,d7emulator
		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	;checks out
		move.l	SrcPtr,a1
		move.l	#StarBuff,a0
		move.w	#NumStars-1,d7emulator
		bsr	Perspective	;checks out
		move.l	#StarBuff,a0
*		move.l	seen,a1
		move.w	#NumStars-1,d7emulator
		bsr	PlotStars	;checks out
		rts

PlotStars:
		move.l	clrbuffhiden,a1
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	#0,d4
		cmp.l	#-1024,d3
		blt.s	DoPlot
		moveq	#2,d4
		cmp.l	#1024,d3
		blt.s	DoPlot
		moveq	#4,d4
DoPlot:
	move.l	a1,d5
	move.l	hiden,a1

	move.w	d0,d3         ;duplicate in D3
	andi.w	#15,d3        ;mask off lower 4 bits

	lsr.w	#1,d0             ;x coord / 2
	and.w	#$00f8,d0      ;strip lower 3 bits
	add.w	d0,a1        ;move x offset to A0

	lsl.w	#5,d1
	add.w	d1,a1
	lsl.w	#2,d1
	add.w	d1,a1


	move.w	#$8000,d0     ;set ON pixel mask
	lsr.w	d3,d0          ;shift right D3 times
	add.w	d4,a1
	or.w	d0,(a1)
	exg	d5,a1

	move.l	d5,(a1)+
PlotNext:
*	dbra	d7,PlotLoop
	subq.w	#1,d7emulator
	bpl	plotloop
	move.l	#-1,(a1)+
	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
		subq.w	#1,d7emulator
		bpl	clipstars
		rts

ClrStars:	move.l	ClrBuffhiden,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
		asr.l	#8,d0
		asr.l	#1,d0
		muls	HSPREAD(pc),d0
*		asr.l	d6,d0
		asr.l	#8,d0
		asr.l	#1,d0
		add.l	CENTREX(pc),d0
		move.l	d0,(a0)+
		move.l	d3,d1
		muls	d5,d1
*		asr.l	d6,d1
		asr.l	#8,d1
		asr.l	#1,d1
		muls	VSPREAD(pc),d1
*		asr.l	d6,d1
		asr.l	#8,d1
		asr.l	#1,d1
		move.l	CENTREY(pc),d0
		sub.l	d1,d0
		move.l	d0,(a0)+
		move.l	d4,(a0)+
*		dbra	d7,Perspective
		subq.w	#1,d7emulator
		bpl	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,d5
		move.w	SCOSX,d0
		muls	SCOSY,d0
		asr.l	d5,d0	
		move.w	SSINX,d1
		muls	SSINY,d1
		asr.l	d5,d1
		muls	SSINZ,d1
		asr.l	d5,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	d5,d0
		move.w	SSINX,d1
		muls	SCOSY,d1
		asr.l	d5,d1
		muls	SSINZ,d1
		asr.l	d5,d1
		sub.w	d1,d0
		move.w	d0,SVAL2	(-CosX*SinY) - SinZ*(SinX*CosY)
	
		move.w	SSINX,d0
		muls	SCOSZ,d0
		asr.l	d5,d0
		move.w	d0,SVAL3	SinX*CosZ
		move.w	SSINY,d0
		muls	SCOSZ,d0
		asr.l	d5,d0
		move.w	d0,SVAL4	SinY*CosZ
		move.w	SCOSY,d0
		muls	SCOSZ,d0
		asr.l	d5,d0
		move.w	d0,SVAL5	CosY*CosZ

		move.w	SSINZ,SVAL6	SinZ

		move.w	SSINX,d0
		neg.w	d0
		muls	SCOSY,d0
		asr.l	d5,d0
		move.w	SCOSX,d1
		muls	SSINY,d1
		asr.l	d5,d1
		muls	SSINZ,d1
		asr.l	d5,d1
		sub.w	d1,d0
		move.w	d0,SVAL7	(-SinX*CosY) - SinZ*(SinY*CosX) 

		move.w	SSINX,d0
		muls	SSINY,d0
		asr.l	d5,d0
		move.w	SCOSX,d1
		muls	SCOSY,d1
		asr.l	d5,d1
		muls	SSINZ,d1
		asr.l	d5,d1
		sub.w	d1,d0
		move.w	d0,SVAL8	(SinY*SinY) - SinZ*(CosX*CosY)
		move.w	SCOSX,d0
		muls	SCOSZ,d0
		asr.l	d5,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
ClrBuff2:	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

****************************************
keyboard:
	move.w	#$ff,-(sp)
	move.w	#$6,-(sp)
	trap	#1
	addq.l	#4,sp
	tst.l	d0
	beq	keyback
	swap	d0

	cmp.b	#16,d0		*space
	beq	gem		*yes/no
keyback:
	rts

paused:	dc.w	-1
kdelay:	dc.w	5

*****************************************
*	number in d0.l			*
*	d1.l is base			*
*	d2,x cursor			*
*	d3,y cursor			*
*****************************************
prtnum:
	movem.l	d0/d1,-(sp)
	lea	posstring,a0
	add.w	#32,d3
	move.b	d3,2(a0)	
	move.b	d3,10(a0)	
	add.w	#32,d2
	move.b	d2,3(a0)
	move.b	d2,11(a0)	
	move.l	a0,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp
	movem.l	(sp)+,d0/d1

	lea	linebuffer,a0
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
loop1:
	divu	d1,d0
	swap	d0
	addi	#'0',d0
	cmpi.w	#'9',d0
	ble.s	ok
	addq.w	#7,d0
ok:
	move.b	d0,-(a0)
	clr	d0
	swap	d0
	bne	loop1
loop2:
	move.l	a0,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp
	rts


linebuffer:
	ds.l	5
digits:
	dc.b	0
posstring:	dc.b	27,'Y',0,0,' ',' ',' ',' ',27,'Y',0,0,0
even



***********************************
mydocolors:
	move.l	#$ff8240,a1
	moveq	#15,d0
mdcloop:
	move.w	(a0)+,(a1)+
	dbf	d0,mdcloop
	rts

************get screen address***************
getscrn:move.w  #2,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,seen
	sub.l	#$8000,d0
	move.l	d0,hiden
	move.l	#clrbuff,clrbuffseen
	move.l	#clrbuff2,clrbuffhiden

	move.l	seen,a0
	move.l	hiden,a1
	move.w	#7999,d0
cl:
	move.l	#0,(a0)+
	move.l	#0,(a1)+
	dbf	d0,cl
	rts


******************************************
*               RETURN TO GEM
gem:
	move.b	#%11111110,$ff820a
	move.l	#$ff8240,a0
	lea	palsave,a1
	moveq	#15,d7
rspl:
	move.w	(a1)+,d0
	and.w	#$0777,d0
	move.w	d0,(a0)+
	dbf	d7,rspl

*	move.l	vblslot,a0
*	move.l	#0,(a0)

        move.w  ressave,-(sp)
        move.l  seen,-(sp)
        move.l  seen,-(sp)
        move.w  #5,-(sp)
        trap    #14
        add.l  #12,sp

	move.l  savesup,-(SP)    ;Back to user mode
        move.w  #$20,-(SP)
        trap    #1
        addq.l  #6,SP
*illegal
	clr.w	-(sp)
	trap	#1

rvsave:	dc.l	0
resave:	dc.l	0
	even
savesup:	dc.l	0
ressave:	dc.w	0
filename:	dc.l	0
greypalette:
*	incbin	'ste.pal'
	dc.w	$0000,$0222,$0444,$000f,$0fff,$000f,$000f
	dc.w	$000f,$000f,$000f,$000f,$000f,$000f,$000f,$000f,$000f
	dc.w	$000f,$000f
save_usp:
	dc.l	0
wfnc:
	dc.w	0
save_stk:
	dc.l	0
pointer:	dc.l	0
flip:	dc.w	0
flip2:	dc.w	0
seen:	dc.l	0
hiden:	dc.l	0
clrbuffseen:	dc.l	0
clrbuffhiden:	dc.l	0
screen:	dc.l	0
	ds.w	16
*	section	bss
number:	ds.w	1
palsave:
	ds.w	16
vblslot:	ds.l	1
d7emulator:	ds.w	1
pauseimg:
	 dc.w	$7BC7,$1CFB,$C73C,$03E7,$00F1,$C89E,$F800,$8228
	 dc.w	$A282,$28A2,$0088,$808A,$28A0,$8000,$73CF,$A0F3
	 dc.w	$CFBC,$0088,$80F3,$E89C,$F000,$0A08,$A282,$28A4
	 dc.w	$0088,$8082,$2882,$8000,$F208,$9CFB,$E8A2,$0087
	 dc.w	$0082,$273C,$F800


