code
	move.w	#78,opcode
	move.w	#1,sintin
	move.w	#1,sintout
	move.w	#1,saddrin
	move.w	#0,saddrout
	move.w	#3,intn
	bsr	aes



*		ENTER SUPERVISOR MODE
*
start:	clr.l	-(SP)		;Go into supervisor mode
	move.w	#$20,-(SP)
	trap	#1
	addq.l	#6,SP
	move.l	D0,spv_sp

	bsr	putrout
	move.w	#-1,ynflg
	jsr	linea
	jsr	getscrn
	dc.w	$a00a
	jsr	clearsc
	jsr	colors
*	jsr	loadscr
	jsr	copysc

	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	
	move.w	#-1,ob2
	move.w	#-1,ob3
	move.w	#1,ob4
	move.w	#1,ob1
	move.w	#0,xmin
	move.w	#0,ymin
	move.w	#319,xmax
	move.w	#159,ymax
	move.w	#160,whx
	move.w	#80,why
	move.w	#100,speed
	move.w	#150,fspeed
	move.w	#-20,nuch
	move.w	#20,much
	move.w	#10,sheilds
	move.w	#200,off
	move.w	#0,doffs
	move.w	#0,yvel
	move.w	#0,xvel
	lea	view_pb,a6
	move.w	#2000,rho(a6)
	lea	view_pb2,a6
	move.w	#2000,rho(a6)
	lea	view_pb3,a6
	move.w	#2000,rho(a6)
	move.w	#0,ob2x
	move.w	#0,ob2y
	move.w	#0,ob2z

	move.w	#0,score
	move.w	#50,missvel
	move.w	#1,whx2
	move.w	#0,design

***************************************************************************
ptest:	lea.l	view_pb,a6
	move.w	#1000,rho(a6)
	move.w	$26e0,theta(a6)
	move.w	$26e2,phi(a6)
	bsr	plot_obj
	tst.w	$26e6
	bne	stop
	bra	ptest

stop:	bra	gem




****************

plotit:
	cmpi.w	#2,$26e6
	beq	mouseon
	move.w	#$ff,-(sp)
	move.w	#6,-(sp)
	trap	#1
	addq.l	#4,sp
	tst.w	d0
	bne	return
	

	bsr	plot_obj
pltbk:
	bra	ptest



mouseon:
	move.w	#1,whx2
	move.w	$26e0,xsav2
	move.w	$26e2,ysav2
	move.w	#160,$26e0
	move.w	#100,$26e2

	move.w	#0,mminx
	move.w	#0,mminy
	move.w	#319,mmaxx
	move.w	#199,mmaxy
	bsr	phome2
	move.l	intin,a0
	move.l	contrl,a1
	move.w	#0,(a1)
	move.w	#1,6(a1)
	move.w	#0,(a0)
	dc.w	$a009
*	move.w	#-1,ob4
panel:	cmpi.w	#1,$26e6
	bne	panel
	move.w	$26e0,xsav
	move.w	$26e2,ysav
axis1:	cmpi.w	#10,xsav
	blt	x_y
	cmpi.w	#47,xsav
	bgt	x_y
	cmpi.w	#175,ysav
	blt	x_y
	cmpi.w	#185,ysav
	bgt	x_y
	cmpi.w	#$0000,$ff8248
	beq	axison
axisoff:move.w	#$0000,$ff8248
	move.w	#-1,ob1
	dc.w	$a00a
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	bsr	del
	move.w	#10,whx2
	bra	pltbk
axison:	move.w	#$0700,$ff8248
	move.w	#1,ob1
	dc.w	$a00a
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	bsr	del
	bra	pltbk
x_y:	cmpi.w	#64,xsav
	blt	x_z
	cmpi.w	#89,xsav
	bgt	x_z
	cmpi.w	#163,ysav
	blt	x_z
	cmpi.w	#172,ysav
	bgt	x_z
	move.w	#$0700,$ff824a
	move.w	#$0000,$ff824c
	move.w	#$0000,$ff824e
	dc.w	$a00a
	move.w	#0,xory
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk
x_z:	cmpi.w	#64,xsav
	blt	y_z
	cmpi.w	#89,xsav
	bgt	y_z
	cmpi.w	#175,ysav
	blt	y_z
	cmpi.w	#184,ysav
	bgt	y_z
	move.w	#$0000,$ff824a
	move.w	#$0700,$ff824c
	move.w	#$0000,$ff824e
	dc.w	$a00a
	move.w	#1,xory
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk
y_z:	cmpi.w	#64,xsav
	blt	in
	cmpi.w	#89,xsav
	bgt	in
	cmpi.w	#187,ysav
	blt	in
	cmpi.w	#196,ysav
	bgt	in
	move.w	#$0000,$ff824a
	move.w	#$0000,$ff824c
	move.w	#$0700,$ff824e
	dc.w	$a00a
	move.w	#2,xory
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk


in:	cmpi.w	#107,xsav
	blt	out
	cmpi.w	#132,xsav
	bgt	out
	cmpi.w	#175,ysav
	blt	out
	cmpi.w	#184,ysav
	bgt	out
	move.w	#$0700,$ff8250
	move.w	#$0000,$ff8252
morin:	lea	view_pb,a6
	lea	view_pb2,a5
	lea	view_pb3,a4
	addi.w	#20,rho(a6)
	addi.w	#20,rho(a5)
	addi.w	#20,rho(a4)
	bsr	plot_obj
	tst.w	$26e6
	bne	morin
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk


out:	cmpi.w	#107,xsav
	blt	mouse
	cmpi.w	#132,xsav
	bgt	mouse
	cmpi.w	#187,ysav
	blt	mouse
	cmpi.w	#196,ysav
	bgt	mouse
	move.w	#$0000,$ff8250
	move.w	#$0700,$ff8252
morout:	lea	view_pb,a6
	lea	view_pb2,a5
	lea	view_pb3,a4
	subi.w	#20,rho(a6)
	subi.w	#20,rho(a5)
	subi.w	#20,rho(a4)
	bsr	plot_obj
	tst.w	$26e6
	bne	morout
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk

mouse:	cmpi.w	#143,xsav
	blt	disk
	cmpi.w	#175,xsav
	bgt	disk
	cmpi.w	#162,ysav
	blt	disk
	cmpi.w	#197,ysav
	bgt	disk
	cmpi.w	#$0000,$ff8254
	bgt	mouseo
	move.w	#$0700,$ff8254
	dc.w	$a00a
	move.w	#1,mouseflg
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	bra	pltbk
mouseo:	move.w	#$0000,$ff8254
	dc.w	$a00a
	move.w	#-1,mouseflg
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk

disk:	cmpi.w	#178,xsav
	blt	info
	cmpi.w	#208,xsav
	bgt	info
	cmpi.w	#162,ysav
	blt	info
	cmpi.w	#197,ysav
	bgt	info
	cmpi.w	#$0000,$ff8256
	bgt	diskoff
	move.w	#$0700,$ff8256
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk
diskoff:move.w	#$0000,$ff8256
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk

info:	cmpi.w	#215,xsav
	blt	undo
	cmpi.w	#250,xsav
	bgt	undo
	cmpi.w	#167,ysav
	blt	undo
	cmpi.w	#177,ysav
	bgt	undo
	cmpi.w	#$0000,$ff8258
	bgt	infooff
	move.w	#$0700,$ff8258
	move.w	#1,infoflg
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk
infooff:move.w	#$0000,$ff8258
	move.w	#-1,infoflg
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	bra	pltbk


undo:	cmpi.w	#215,xsav
	blt	clear
	cmpi.w	#250,xsav
	bgt	clear
	cmpi.w	#182,ysav
	blt	clear
	cmpi.w	#192,ysav
	bgt	clear
	move.w	#$0700,$ff825a
	subq.w	#1,design
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	move.w	#$0000,$ff825a
	bra	pltbk

clear:	cmpi.w	#264,xsav
	blt	pback
	cmpi.w	#308,xsav
	bgt	pback
	cmpi.w	#172,ysav
	blt	pback
	cmpi.w	#186,ysav
	bgt	pback
	move.w	#$0700,$ff825c
	move.w	#0,design
	move.w	#-1,ob3
	dc.w	$a00a
	bsr	del
	move.w	#-255,mminx
	move.w	#-255,mminy
	move.w	#255,mmaxx
	move.w	#255,mmaxy
	move.w	xsav2,$26e0
	move.w	ysav2,$26e2
	move.w	#10,whx2
	move.w	#$0000,$ff825c
	bra	pltbk



pback:	dc.w	$a00a
	bsr	del
	move.w	#10,whx2
	bra	pltbk



del:	move.w	#60000,d0
mmm:	dbf	d0,mmm	
	rts
	
********************************************
aes:	move.l	#aespb,d1
	move.w	#$c8,d0
	trap	#2
	rts





return:	bra	gem		;Return to GEM

***************************************************************************

*		PLOT AN OBJECT

*		On entry, a4 = pointer to object defintion

*	Plota 3-D object on the screen.

plot_obj:
	bsr	flip_scr
	bsr	undrawit
	move.l	#view_pb,view
	bsr	init_vtrans
	tst.w	ob1
	bmi	plot2
	lea.l	view_pb,a6
	move.w	rho(a6),dist
	move.l	#view_pb,viewsav
	move.l	#axis,a4
	move.w	(a4)+,d6 	;Get number of verticies in object
	move.l	(a4)+,a3 	;Point to verticies
	move.w	(a4)+,numsav
	move.l	(a4)+,linesav
	move.w	ob1y,yoffs
	move.w	ob1x,xoffs
	move.w	ob1z,zoffs
	bsr	trans_vert	;Transform verticies
	move.w	#1,plm1
	move.w	#1,plm2
	move.w	#0,plm3
	move.w	#1,plm4
pd:	bsr	drawit

plot2:	tst.w	ob2
	bmi	plot3
	move.l	#view_pb2,view
	bsr	init_vtrans
	lea.l	view_pb2,a6
	move.w	rho(a6),dist
	move.l	#view_pb2,viewsav
	move.l	#pen,a4
	move.w	(a4)+,d6 	;Get number of verticies in object
	move.w	d6,numsav
	move.l	(a4)+,a3 	;Point to verticies
	move.w	#1,special
	move.w	#0,yoffs
	move.w	#0,xoffs
	move.w	#0,zoffs
	move.w	ob2y,yoffs2
	move.w	ob2x,xoffs2
	move.w	ob2z,zoffs2
	bsr	trans_vert	;Transform verticies
	move.w	#-1,special
	move.w	#1,plm1
	move.w	#1,plm2
	move.w	#0,plm3
	move.w	#0,plm4
p1d:	bsr	drawit

plot3:	tst.w	ob3
	bmi	phome
	move.l	#view_pb3,view
	bsr	init_vtrans
	lea.l	view_pb3,a6
	move.w	rho(a6),dist
	move.l	#view_pb3,viewsav
	move.w	design,d6 	;Get number of verticies in object
	move.w	d6,numsav
	move.l	#design2,a3 	;Point to verticies
	move.w	ob3y,yoffs
	move.w	ob3x,xoffs
	move.w	ob3z,zoffs
	bsr	trans_vert	;Transform verticies
	move.w	#1,plm1
	move.w	#1,plm2
	move.w	#1,plm3
	move.w	#1,plm4
p2d:	bsr	drawit
	


phome:
*	bsr	putnums
	rts



phome2:	move.w	#-1,-(sp)
	move.l	seen,-(sp)
	move.l	seen,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp
	rts

**********************************
putnums:
	tst.w	infoflg
	bmi	pthome
	move.l	#thetap,pos
	move.l	#greencol,col
	lea	view_pb,a6
	move.w	ob2x,number
	bsr	prtnum

	move.l	#phip,pos
	move.l	#greencol,col
	lea	view_pb,a6
	move.w	ob2y,number
	bsr	prtnum

	move.l	#rhop,pos
	move.l	#greencol,col
	lea	view_pb,a6
	move.w	ob2z,number
	bsr	prtnum

pthome:	rts



*************************************

undrawit:
	move.l	hiden,a6
*	adda	#2080,a6
	move.w	#63,d0	
	move.l	#0,d1
	move.l	#0,d2
	move.l	#0,d3
	move.l	#0,d4
	move.l	#0,d5
	move.l	#0,d6
	move.l	#0,d7
	move.l	#0,a0
	move.l	#0,a1
	move.l	#0,a2
clrit:	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	movem.l	d1-d7/a0-a2,(a6)
	add.l	#40,a6
	dbf	d0,clrit
unb:	rts











		
****************************************************************
drawit:	move.w	#1,ynflg
	move.l	a0,a6		;add of transformed vertices
	move.l	a6,vsav
	move.l	linepar,a0
	move.w	plm1,24(a0)	;note d6 cont. nuber of vert
	move.w	plm2,26(a0)
	move.w	plm3,28(a0)
	move.w	plm4,30(a0)
	move.w	#$ffff,34(a0)
	move.w	#0,36(a0)
	move.w	#-1,32(a0)
	move.w	numsav,d7
	subq.w	#1,d7
	move.l	linesav,a5
dr1:	move.w	(a5)+,d4
	move.w	(a5)+,d5
	asl.w	#2,d4
	asl.w	#2,d5
	move.w	(a6,d4.w),d0
	move.w	2(a6,d4.w),d1
	move.w	(a6,d5.w),d2
	move.w	2(a6,d5.w),d3
	movem.l	d0-d7/a0-a6,-(sp)
	bsr	clipit
	movem.l	(sp)+,d0-d7/a0-a6
*	dc.w	$a003		;draw the line
	move.l	linepar,a0
	dbf	d7,dr1
	rts
*/**//*/*//*//*/*//*//*/*/*//*/*/*/*//*/*/*/*//*//*//*///*/*/
bitcode:clr.l	d4
	cmpi.w	xmin,d0
	bge	b1
	or.b	#8,d4
b1:	cmpi.w	xmax,d0
	ble	b2
	or.b	#4,d4
b2:	cmpi.w	ymin,d1
	bge	b3
	or.b	#1,d4
b3:	cmpi.w	ymax,d1
	ble	b4
	or.b	#2,d4
b4:	move.w	d4,cbits
	rts
*****
bit2:	clr.l	d5
	cmpi.w	xmin,d2
	bge	b5
	or.b	#8,d5
b5:	cmpi.w	xmax,d2
	ble	b6
	or.b	#4,d5
b6:	cmpi.w	ymin,d3
	bge	b7
	or.b	#1,d5
b7:	cmpi.w	ymax,d3
	ble	b8
	or.b	#2,d5
b8:	move.w	d5,cbits2
	rts
********************************************************
clipit:	bsr	bitcode
	bsr	bit2
clipit2:or.b	d4,d5
	tst.b	d5
	beq	cliphome	;finish if
	move.w	cbits,d4
	move.w	d2,xsav
	move.w	d3,ysav
	move.w	cbits2,d5
clip1:	and.b	d4,d5
	tst.b	d5
	bgt	clipb		;home if line completely out
	move.w	cbits,d4
	move.w	cbits2,d5
	sub.w	d0,d2		;d1 has dx
	move.w	d2,dx
	sub.w	d1,d3		;d3 has dy
	move.w	d3,dy
	move.w	xsav,d2
	move.w	ysav,d3
	tst.w	d4
	beq	clip2	;if x1<xmin
	cmpi.w	xmin,d0	;y1=y1+dy*(xmin-x1)/dx
	bge	c1	
	move.w	xmin,d7		;put xmin in d7
	sub.w	d0,d7
	muls	dy,d7		;multiply by dy
	divs	dx,d7
	add.w	d1,d7
	move.w	d7,d1
	move.w	xmin,d0		;x1=xmin
	bra	c4
c1:	cmpi.w	xmax,d0		;if x1>xmax
	ble	c2		;y1=y1+dy*(xmax-x1)/dx
	move.w	xmax,d7		;put xmax in d7
	sub.w	d0,d7
	muls	dy,d7		;multiply by dy
	divs	dx,d7
	add.w	d1,d7
	move.w	d7,d1
	move.w	xmax,d0		;x1=xmax
	bra	c4
c2:	cmpi.w	ymin,d1		;if y1<ymin
	bge	c3
	move.w	ymin,d7		;put ymin in d7
	sub.w	d1,d7
	muls	dx,d7		;multiply by dx
	divs	dy,d7
	add.w	d0,d7
	move.w	d7,d0
	move.w	ymin,d1		;y1=ymin
	bra	c4
c3:	cmpi.w	ymax,d1		;if y1>xmax
	ble	c4
	move.w	ymax,d7		;put ymax in d7
	sub.w	d1,d7
	muls	dx,d7		;multiply by dy
	divs	dy,d7
	add.w	d0,d7
	move.w	d7,d0
	move.w	ymax,d1		;y1=ymax
c4:	bsr	bitcode
	bra	cliploop
******
clip2:	cmpi.w	xmin,d2	;y1=y1+dy*(xmin-x1)/dx
	bge	cc1	
	move.w	xmin,d7		;put xmin in d7
	sub.w	d2,d7
	muls	dy,d7		;multiply by dy
	divs	dx,d7
	add.w	d3,d7
	move.w	d7,d3
	move.w	xmin,d2		;x2=xmin
	bra	cc4
cc1:	cmpi.w	xmax,d2		;if x2>xmax
	ble	cc2
	move.w	xmax,d7		;put xmax in d7
	sub.w	d2,d7
	muls	dy,d7		;multiply by dy
	divs	dx,d7
	add.w	d3,d7
	move.w	d7,d3
	move.w	xmax,d2		;x1=xmax
	bra	cc4
cc2:	cmpi.w	ymin,d3		;if y1<ymin
	bge	cc3
	move.w	ymin,d7		;put ymin in d7
	sub.w	d3,d7
	muls	dx,d7		;multiply by dx
	divs	dy,d7
	add.w	d2,d7
	move.w	d7,d2
	move.w	ymin,d3		;y1=ymin
	bra	cc4
cc3:	cmpi.w	ymax,d3		;if y1>xmax
	ble	cc4
	move.w	ymax,d7		;put ymax in d7
	sub.w	d3,d7
	muls	dx,d7		;multiply by dy
	divs	dy,d7
	add.w	d2,d7
	move.w	d7,d2
	move.w	ymax,d3		;y1=ymax
*	bra	cliploop	
cc4:	bsr	bit2
	bra	cliploop

cliploop:
	bra	clipit2
cliphome:
*	movem.l	d0-d7/a0-a6,-(sp)
	move.l	linepar,a0
	move.w	d0,38(a0)
	move.w	d1,40(a0)
	move.w	d2,42(a0)
	move.w	d3,44(a0)
	dc.w	$a003
*	movem.l	(sp)+,d0-d7/a0-a6

clipb:	rts
***************************************************************************

*		TRANSFORM A LIST OF VERTICIES

*		On entry,   d6 = number of verticies
*			    a3 = pointer to verticies
*		On exit , a0 = pointer to transformed verticies

*	Transform a list of verticies in 3-D space according to the
* viewing transformation.

trans_vert:
	lea.l	vtrans_vrt,a1	;Point to vertex tables
	lea.l	ptrans_vrt,a0
	move.l	a0,-(SP)

	subq	#1,d6		;Pre-decrement vertex counter
trans_vert0:
	move	(a3)+,d0	;Get x-coordinate of vertex
	move	(a3)+,d1	;Get y-coordinate
	move	(a3)+,d2	;Get z-coordinate
	tst.w	special
	bmi	trvrt1
	add.w	xoffs2,d0
	add.w	yoffs2,d1
	add.w	zoffs2,d2

trvrt1:	bsr	view_trans	;Apply view transformatrmed coordinates
	add.w	xoffs,d3
	move.w	d3,(a1)+
	add.w	yoffs,d4
	move	d4,(a1)+
	add.w	zoffs,d5
	move	d5,(a1)+

	bsr	prsp_trans	;Apply perspective transformation
	move	d3,(a0)+	;Save transformed x-coordinate		
	move	d4,(a0)+	;Save transformed y-coordinate
		
	dbf	d6,trans_vert0
	move.l	(SP)+,a0	;Restore transformed vertex pointer

	rts

vtrans_vrt:	blk.w	1000	;View transformed coordinates	
ptrans_vrt:	blk.w	1000	;Perspective transformed coordinates

**********************************
axis:	dc.w	6
	dc.l	axisvrt
	dc.w	3
	dc.l	axlist

axisvrt:
	dc.w	100,0,0
	dc.w	-100,0,0
	dc.w	0,100,0
	dc.w	0,-100,0
	dc.w	0,0,-100
	dc.w	0,0,100

axlist:	dc.w	0,1
	dc.w	2,3
	dc.w	4,5
************************************
pen:	dc.w	4
	dc.l	penvrt
penvrt:	dc.w	-5,40,0
	dc.w	0,0,0
	dc.w	5,40,0
	dc.w	-5,40,0



*		VIEW TRANSFORM ON A VERTEX

*		On entry, d0 = x-coordinate of vertex
*			  d1 = y-coordinate of vertex
*			  d2 = z-coordinate of vertex
*		On exit , d3 = x-coordinate view transformed
*			  d4 = y-coordinate view transformed
*			  d5 = z-coordinate view transformed

*	Apply the view transform to a vertex.

view_trans:
	move.l	viewsav,a5
	move	d0,d7		;Calculate Xe
	muls	Va(a5),d7
	move.l	d7,d3
	move	d1,d7
	muls	Vb(a5),d7
	add.l	d7,d3
	swap	d3
	rol.l	#1,d3
		
	move	d0,d7		;Calculate Ye
	muls	Ve(a5),d7
	move.l	d7,d4
	move	d1,d7
	muls	Vf(a5),d7
	add.l	d7,d4
	move	d2,d7
	muls	Vg(a5),d7
	add.l	d7,d4
	swap	d4
	rol	#1,d4
		
	move	d0,d7		;Calculate Ze
	muls	Vi(a5),d7
	move.l	d7,d5
	move	d1,d7
	muls	Vj(a5),d7
	add.l	d7,d5
	move	d2,d7
	muls	Vk(a5),d7
	add.l	d7,d5
	swap	d5
	rol.l	#1,d5
	add	Vl(a5),d5
		
	rts
		
***************************************************************************

*		PERSPECTIVE TRANSFORM

*		On entry, d3 = view transformed x-coordinate
*			  d4 = view transformed y-coordinate
*			  d5 = view transformed z-coordinate
*		On exit , d3 = screen x-coordinate ( Xe )
*			  d4 = screen y-coordinate ( Ye )

*	Calculate the screen coordinates of the vertex. The coordinates are
* centered about the centre of the screen.
*	Note, the distance to the screen is 2048.

prsp_trans:
	swap	d3		;Calculate Xs
	clr	d3		
	asr.l	#6,d3	  
*	asr.l	#2,d3
	divs	d5,d3
		
	swap	d4		;Calculate Ys
	clr	d4
	asr.l	#6,d4
*	asr.l	#2,d4
	divs	d5,d4

	add	whx,d3		;Adjust for centre of screen
	add	why,d4

	rts
***************************************************************************

*		VIEW PARAMETER BLOCK

*	This is the parameter block for the view coordinate system
* and view transformation.

view_pb:blk.w	40		;Parameter block declaration
view_pb2:blk.w	40
view_pb3:blk.w	40

rho:	equ	0
theta:	equ	2
phi:	equ	4
s_theta:equ	6
c_theta:equ	8
s_phi:	equ	10
c_phi:	equ	12
va:	equ	14
vb:	equ	16
Ve:	equ	18
Vf:	equ	20
Vg:	equ	22
Vi:	equ	24
Vj:	equ	26
Vk:	equ	28
Vl:	equ	30

***************************************************************************

*		INITIALISE VIEW TRANSFORM

*	Initialise the viewing transformation.

init_vtrans:
	move.l	view,a5	;Point to parameter block
	move	theta(a5),d3	;Calculate SIN amd COS theta
	bsr	trig
	move	d4,s_theta(a5)
	move	d5,c_theta(a5)
	
	neg	d4		;Calcualte Va and Vb
	move	d4,Va(a5)
	move	d5,Vb(a5)

	move	phi(a5),d3	;Calculate SIN and COS phi
	bsr	trig
	move	d4,s_phi(a5)
	move	d5,c_phi(a5)

	move	d4,-(SP)	;Save SIN and COS phi
	move	d5,-(SP)
		
	neg	d5		;calculate Vg and Vk
	move	d4,Vg(a5)
	move	d5,Vk(a5)
		
	muls	c_theta(a5),d5  ;Calculate Ve
	swap	d5
	rol.l	#1,d5
	move	d5,Ve(a5)
		
	muls	c_theta(a5),d4  ;Calculate Vi
	swap	d4
	rol.l	#1,d4
	neg	d4
	move	d4,Vi(a5)
		
	move	(SP)+,d5	;Restore SIN and COS phi
	move	(SP)+,d4

	muls	Va(a5),d5	;Calculate Vf
	swap	d5
	rol.l	#1,d5
	move	d5,Vf(a5)
		
	muls	Va(a5),d4	;Calculate Vj
	swap	d4
	rol.l	#1,d4
	move	d4,Vj(a5)
		
	move	rho(a5),Vl(a5)	;Calculate Vl
	
	rts

***************************************************************************

*		CALCULATE SINE AND COSINE OF ANGLE

*		On entry, d3 = angle
*		On exit ,   d4 = sine of angle
*			    d5 = cosine of angle

*	Find the sine and cosine of an angle.

trig:	add	#$40,d3		;Find cCOS of angle
	bsr	find_sin
	move	d4,d5
	sub	#$40,d3		;Find SIN of angle
	bsr	find_sin

	rts
		
***************************************************************************

*		FIND SINE OF AN ANGLE

*		On entry, d3 = angle
*		On exit ,   d5 = cosine of angle 

*	Find the cosine of an angle. The value of the sign is a 16 bit
* signed number.
*	Note, to scale down, divide by 15 bits, ie &8000.

find_cos:
	move	d4,-(SP)	;Save SIN
	move	d3,-(SP)	;Save angle
	add	#$40,d3		;Find COS of angle
	bsr	find_sin
	move	d4,d5
		
	move	(SP)+,d3	;Restore angle
	move	(SP)+,d4	;Restore SIN
		
	rts	

***************************************************************************

*		FIND THE SIN OF AN ANGLE

*		On entry, d3 = angle
*		On exit ,   d4 = SIN of angle

*	Find the SIN of an angle. The value of the sign is a 16 bit
* signed number.
*	Note, to scale down, divide by 15 bits, ie &8000.

find_sin:
	move	d3,-(SP)	;Save angle	

	and.l	#$7F,d3		;Move to quadrants 1 and 2
	btst	#6,d3		;Test if in 1st quadrant
	beq	quad0		;Branch if in 1st quadrant
	bclr	#6,d3		;Reflect angle into 1st quadrant
	sub.b	#$3F,d3
	neg.b	d3
		
quad0:	asl	#1,d3		;Get value of SIN from table
	lea.l	sin_vt,a6
	move	0(a6,d3),d4
		
	move	(SP)+,d3

	btst	#7,d3		;Adjust value if 3rd or 4th quadrant
	beq	quad1
	neg	d4		;Negate SIN value
		
quad1:	rts

***************************************************************************

*		SIN LOOK UP TABLE

*	This is the SIN value look up table. Values for the 1st quadrant
* are given only. The values range from 0 - 32758 for angles 0 - 63.
* Thus, the value of a sine in the 1st quadrant is a 15 bit number.
* Therefore, the table can be used to construct a 16 bit signed value of
* SIN for angles 0 - 255.

sin_vt:	dc.w	0,804,1608,2411
	dc.w	3212,4011,4808,5602
	dc.w	6393,7180,7962,8740
	dc.w	9512,10279,11039,11793
	dc.w	12540,13279,14010,14733
	dc.w	15447,16151,16846,17531
	dc.w	18205,18868,19520,20160
	dc.w	20788,21403,22006,22595
	dc.w	23170,23732,24279,24812
	dc.w	25330,25832,26320,26791
	dc.w	27246,27684,28106,28511
	dc.w	28899,29269,29622,29957
	dc.w	30274,30572,30853,31114
	dc.w	31357,31581,31786,31972
	dc.w	32138,32286,32413,32522
	dc.w	32610,32679,32729,32758


***************************************************************************

*		RETURN TO GEM

gem:	
	move.w	#-1,-(sp)
	move.l	screen1,-(sp)
	move.l	screen1,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp

	move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
        move.l  msave,16(a0)
	

	move.l	spv_sp,-(SP)	;Back to user mode
	move.w	#$20,-(SP)
	trap	#1
	addq.l	#6,(SP)
illegal
        CLR.W   -(SP)		;RETURN TO GEM
   
        TRAP    #1

spv_sp:	blk.l	1		;supervisor SP store


***************************************************************************

*		FLIP SCREEN

*	Flip displayed screen.

flip_scr:move.l	seen,A0
	move.l	hiden,seen
	move.l	A0,hiden

	move.w	#-1,-(sp)
	move.l	seen,-(sp)
	move.l	hiden,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp	
	move.w	#37,-(SP)		;Wait for vertical sync
	trap	#14
	addq	#2,SP

	rts
****************************************************



*************************************************
linea:	dc.w	$a000
	move.l	a0,linepar
	move.l	4(a0),contrl
	move.l	8(a0),intin
	move.l	12(a0),ptsin
	rts

*************************************************

***********************fancy clear screen****************
clearsc:move.l  #99,d0
        move.l  screen,a0
cl1:    move.w  #159,d1
cl2:    move.w  #0,(a0)+
        dbf     d1,cl2
        addi.l  #160,a0
        dbf     d0,cl1
        move.w  #99,d0
        move.l  screen,a0
        addi.l  #160,a0
cl3:    move.w  #159,d1
cl4:    move.w  #0,(a0)+
        dbf     d1,cl4
        addi.l  #160,a0
        dbf     d0,cl3
        rts


********************fancy copy screen*************
copysc: move.w  #99,d0
        move.l  screen,a0
        move.l  screen2,a1
cop1:   move.w  #159,d1
cop2:   move.w  (a1)+,(a0)+
        dbf     d1,cop2
        addi.l  #160,a0
        addi.l  #160,a1
        dbf     d0,cop1
        move.w  #99,d0
        move.l  screen,a0
        addi.l  #160,a0
        move.l  screen2,a1
        addi.l  #160,a1
cop3:   move.w  #159,d1
cop4:   move.w  (a1)+,(a0)+
        dbf     d1,cop4
        addi.l  #160,a0
        addi.l  #160,a1
        dbf     d0,cop3
        rts
**********************routine to print a dec nuber*************
prtnum: 
        move.l  #0,d7
        move.w  number,d7
        andi.l  #$ffff,d7
        move.l  #line,a5

dodec:  move.l  d7,d6
        divu    #10,d6
        move.w  d6,d7
        swap    d6
        addi.w  #$30,d6
        move.b  d6,(a5)+
        cmpi.w  #0,d7
        bne     dodec

        move.l  pos,-(sp)
        move.w  #9,-(sp)
        trap    #1
        addq.l  #6,sp
        move.l  col,-(sp)
        move.w  #9,-(sp)
        trap    #1
        addq.l  #6,sp

outr:    cmpa.l  #line,a5
        beq     decback

        move.b  -(a5),d7
        andi.w  #$ff,d7
        
        move.w  d7,-(sp)
        move.w  #2,-(sp)
        trap    #1
        addq.l  #4,sp

        bra     outr


decback:rts

*************puts my mouse interupt in***************** 


putrout:move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	16(a0),msave
        move.l  #myrout,16(a0)
        rts
**************my ubeut mouse interupt*******************

myrout: tst.b   $0027e7
        bne    .L home
        movem.l d0-d7/a0-a6,-(sp)
        move.b  (a0),d0
        move.b  d0,d1
        and.b   #$00f8,d1
        cmp.b   #$00f8,d1
        bne    .L nearhome
        and.w   #$0003,d0
        lsr.b   #1,d0
        bcc    .L test1
        bset    #$0001,d0
test1:  move.b  $0027de,d1
        and.w   #$0003,d1
        cmp.b   d1,d0
        beq    .S test2
        move.w  d1,-(sp)
        move.l  $002900,a1
        jsr     (a1)
        move.w  (sp)+,d1
        move.w  d0,$0026e6
        eor.b   d0,d1
        ror.b   #2,d1
        or.b    d1,d0
        move.b  d0,$0027de
test2:  move.b  $0001(a0),d0
        or.b    $0002(a0),d0
        bne    .S test3
        bclr    #$0005,$0027de
        bra    .S nearhome
test3:  bset    #$0005,$0027de
        move.w  $0026e0,d0
        move.b  $0001(a0),d1
        ext.w   d1
	muls	whx2,d1
        add.w   d1,d0
        move.w  $0026e2,d1
        move.b  $0002(a0),d3
	move.w	my2,d7
        ext.w   d3
	muls	whx2,d3
        add.w   d3,d1
	add.w	d3,d7
        bsr    .S extra
	move.w	d7,my2
        move.l  $002908,a1
        jsr     (a1)
        bsr    .S extra
        move.w  d0,$0026e0
        move.w  d1,$0026e2
        move.l  $002904,a1
        jsr     (a1)
nearhome:movem.l (sp)+,d0-d7/a0-a6
home:   rts
extra:  cmpi.w	mminx,d0
        bge    .S extra1
        move.w	mminx,d0
        bra    .S extra2
extra1: cmpi.w   mmaxx,d0
        ble    .S extra2
        move.w  mmaxx,d0
extra2:	cmpi.w  mminy,d1
        bge    .S extra3
        move.w  mminy,d1
        bra	extra4
extra3: cmpi.w  mmaxy,d1
        ble    .S extra4
        move.w  mmaxy,d1
extra4:	cmpi.w  #0,d7
        bge    .S extra5
        move.w  #0,d7
        rts
extra5: cmpi.w	#600,d7
        ble    .S extra6
        move.w  #600,d7
extra6:	rts


 rts

************get screen address***************
getscrn:move.w  #2,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,screen
        move.l  d0,screen1
        move.l  d0,screen2
        subi.l  #$8000,screen2
	move.l	screen1,seen
	move.l	screen2,hiden
        rts



************change colors****************
colors: move.l #palette,-(sp)
        move.w #6,-(sp)
        trap #14
        addq.l #6,sp
        rts

*****************wait for a key*********************
wait:   move.w  $26e6,d0
        cmpi.w  #2,d0
        beq     wback
        bra     wait
wback:  rts
***************turn mouse off***********************
mousof: dc.w    $a00a   
        rts



*********************************************************
*       a brilliant routine courtesy of janko to        *
*       insert the vbi addresses in the jump table      *
*       lea address of routine in a0 do a jsr           *
*       putvbl and thats it!!!                          *
*********************************************************


putvbl: lea     $452,a1
        move.w  #1,(a1)
        moveq   #7,d1
        lea     $456,a1
        move.l  (a1),a1
vbl3:   subi.b  #1,d1
        cmpi.b  #0,d1
        beq.s   vbl4
        move.l  (a1)+,d0
        cmpi.l  #0,d0
        bne.S   vbl3
        suba.l  #4,a1
        move.l  a0,(a1)
vbl4:   rts

*********************************************************
*       my routines for loading in the screens          *
*       note i use degas format even though I           *
*       hate the package,due to it's simple format      *
*       ie: 32l for pallette and straight 32k screen    *
*       dump                                            *
*********************************************************

loadscr:move    #0,-(SP)        ;Open file
        move.l  #filename,-(SP) ;Address of filename
        move    #$3D,-(SP)
        trap    #1              ;Get handle number in D0
        addq.l  #8,SP
                
        move    D0,-(SP)        ;Save handle

        move.l  #dummy,-(SP)    ;load pallette into safe area
        move.l  #34,-(SP)       ;length to read in
        move    D0,-(SP)        ;Handle
        move    #$3F,-(SP)      ;bios number
        trap    #1              ;Read file in
        add.l   #12,SP
        move    (sp),d0        
               
        move.l  screen2,-(SP)    ;load it straight onto screen
        move.l  #32000,-(SP)    ;length to read in
        move    D0,-(SP)        ;Handle
        move    #$3F,-(SP)      ;bios number
        trap    #1              ;Read file in
        add.l   #12,SP
                
        move    #$3E,-(SP)      ;Handle already on stack
        trap    #1              ;Close file
        addq.l  #4,SP
        rts

*******************blocks for rotated sprites*************
msave:	blk.l	1
tempsp: blk.w   500
onscreen:       blk.l   1
pblock: blk.l   40
block:	blk.l	40
balldo:	blk.w	1
dir:    blk.w   1
tmx:    blk.w   1
bynflg:	blk.w	1
bdel:   blk.w   1
screen: blk.l   1
off:	blk.w	1
ynflg:  blk.w   1
batadd: blk.l   1
screen2:blk.l   1
screen1:blk.l   1
dummy:  blk.l   32
xvel:	blk.w	1
yvel:	blk.w	1
thbrick:blk.w	1
brick:	blk.w	1
bbx:	blk.w	1
bby:	blk.w	1
filename:       dc.b    'panel.pi1',0  
even
tempad: blk.l   1       
line:   blk.b   10
pos:	blk.l	1
col:	blk.l	1
number:	blk.w	1
thetap:	dc.b	27,89,49,32,0
phip:	dc.b	27,89,50,32,0
rhop:	dc.b	27,89,51,32,0
redcol:		dc.b    27,98,11,0
greencol:dc.b	27,98,3,0

even
oldshad:blk.l   1
bitoffs:blk.w   1
tempsh: blk.l   1
whsp:   blk.w   1
first:  blk.w   1
intin:  blk.l   1
ptsin:  blk.l   1
putpix: equ     $a001
getpix: equ     $a002
lastx:  blk.w   1
lasty:  blk.w   1
area:	blk.w	200
balltemp:	blk.w	500
battemp:blk.w	500
ballx:	blk.w	1
bally:	blk.w	1
ballad:	blk.l	1
seen:	blk.l	1
hiden:	blk.l	1

linepar:blk.l	1
numsav:	blk.w	1
vsav:	blk.l	1
cbits:	blk.w	1
cbits2:	blk.w	1
dx:	blk.w	1
dy:	blk.w	1
xsav:	blk.w	1
ysav:	blk.w	1
xsav2:	blk.w	1
ysav2:	blk.w	1
whx:	blk.w	1
whx2:	blk.w	1
why:	blk.w	1
xmax:	blk.w	1
ymax:	blk.w	1
xmin:	blk.w	1
ymin:	blk.w	1
xoffs:	blk.w	1
yoffs:	blk.w	1
zoffs:	blk.w	1
xoffs2:	blk.w	1
yoffs2:	blk.w	1
zoffs2:	blk.w	1
current:blk.w	1
ob1:	blk.w	1
ob2:	blk.w	1
ob3:	blk.w	1
ob4:	blk.w	1
ob5:	blk.w	1
ob6:	blk.w	1
ob7:	blk.w	1
ob8:	blk.w	1
ob9:	blk.w	1
oba:	blk.w	1
miss1:	blk.w	1
miss2:	blk.w	1
miss3:	blk.w	1
ob1y:	blk.w	1
ob2y:	blk.w	1
ob3y:	blk.w	1
ob4y:	blk.w	1
ob5y:	blk.w	1
ob6y:	blk.w	1
ob7y:	blk.w	1
ob8y:	blk.w	1
ob9y:	blk.w	1
obay:	blk.w	1
miss1y:	blk.w	1
miss2y:	blk.w	1
miss3y:	blk.w	1

ob1x:	blk.w	1
ob2x:	blk.w	1
ob3x:	blk.w	1
ob4x:	blk.w	1
ob5x:	blk.w	1
ob6x:	blk.w	1
ob7x:	blk.w	1
ob8x:	blk.w	1
ob9x:	blk.w	1
obax:	blk.w	1
miss1x:	blk.w	1
miss2x:	blk.w	1
miss3x:	blk.w	1

ob1z:	blk.w	1
ob2z:	blk.w	1
ob3z:	blk.w	1
ob4z:	blk.w	1
ob5z:	blk.w	1
ob6z:	blk.w	1
ob7z:	blk.w	1
ob8z:	blk.w	1
ob9z:	blk.w	1
obaz:	blk.w	1
miss1z:	blk.w	1
miss2z:	blk.w	1
miss3z:	blk.w	1

viewsav:blk.l	1
view:	blk.l	1
speed:	blk.w	1
nuch:	blk.w	1
much:	blk.w	1
fx:	blk.w	1
fy:	blk.w	1
fhigh:	blk.w	1
fwide:	blk.w	1
whichface:blk.l	1
facetemp:blk.w	1000
dodgex:	blk.w	1
dodgey:	blk.w	1
chcnt:	blk.w	1
levob:	blk.l	1
fspeed:	blk.w	1
plm1:	blk.w	1
plm2:	blk.w	1
plm3:	blk.w	1
plm4:	blk.w	1
dist:	blk.w	1
doffs:	blk.w	1,0
missvel:blk.w	1
misscnt:blk.w	1
tests:	blk.w	1
my2:	blk.w	1
spsav:	blk.w	1
shtcnt:	blk.w	1,0
gotit:	blk.w	1,-1
sheilds:blk.w	1,0
exp:	blk.w	1,-1
score:	blk.w	1
contrl:	blk.l	1
infoflg:blk.w	1,-1
mouseflg:	blk.w	1,-1
xory:	blk.w	1,0
mminx:	blk.w	1
mminy:	blk.w	1
mmaxx:	blk.w	1
mmaxy:	blk.w	1
special:blk.w	1
design:	blk.w	1,0
design2:blk.w	1000,0
linesav:	blk.l	1,0

palette:dc.w	$0000,$0033,$0055,$0077,$0700,$0470,$0070,$0075
	dc.w	$0437,$0004,$0027,$0007,$0507,$0707,$0704,$0777


aespb:	dc.l	control,global,intn,intot,addrin,addrout
control:
opcode:	blk.w	1,0
sintin:	blk.w	1,0
sintout:	blk.w	1,0
saddrin:	blk.w	1,0
saddrout:	blk.l	1,0
blk.w	5,0

global:
apversion:	blk.w	1,0
apcount:	blk.w	1,0
apid:		blk.w	1,0
apprivate:	blk.l	1,0
apptree:	blk.l	1,0
ap1resv:	blk.l	1,0
ap2resv:	blk.l	1,0
ap3resv:	blk.l	1,0
ap4resv:	blk.l	1,0


intn:	blk.w	128,0
ptsn:	blk.w	128,0
intot:	blk.w	128,0
ptsot:	blk.w	128,0
addrin:	blk.w	128,0
addrout:blk.w	128,0

vdipb:	dc.l	control,intn,ptsn,intot,ptsot
grhandle:	blk.w	1



