	opt c- o+
mminx:		equ	0
mmaxx:		equ	319
mminy:		equ	0
mmaxy:		equ	199
cxmin:		equ	0
cxmax:		equ	319
cymin:		equ	0
cymax:		equ	199
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
	bsr	plotinit
	lea	greypalette,a0
	bsr	mydocolors
	bsr	putrout
	bsr	putscreen

	lea	warpstruct,a0
	move.l	#0,wsrcx(a0)
	move.l	#0,wsrcy(a0)
	move.l	#100,wsrcdx(a0)
	move.l	#100,wsrcdy(a0)

	move.l	#139,wdstx1(a0)
	move.l	#119,wdsty1(a0)

	move.l	#178,wdstx2(a0)
	move.l	#119,wdsty2(a0)

	move.l	#178,wdstx3(a0)
	move.l	#80,wdsty3(a0)

	move.l	#139,wdstx4(a0)
	move.l	#80,wdsty4(a0)

	bsr	warp
	move.w	#15,color

mainloop:
	bsr	testdraw
	lea	ptrans_vrt,a0
	lea	warpstruct,a1
	move.l	#0,wsrcx(a1)
	move.l	#0,wsrcy(a1)
	move.l	#100,wsrcdx(a1)
	move.l	#100,wsrcdy(a1)

	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdstx1(a1)
	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdsty1(a1)

	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdstx2(a1)
	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdsty2(a1)

	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdstx3(a1)
	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdsty3(a1)

	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdstx4(a1)
	moveq	#0,d0
	move.w	(a0)+,d0
	move.l	d0,wdsty4(a1)

	bsr	 warp

*	bsr	prtc

	bsr	keyboard
	bra	mainloop
prtc:

	lea	ptrans_vrt,a0
	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#0,d2
	moveq	#0,d3
	bsr	prtnum
	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#6,d2
	moveq	#0,d3
	bsr	prtnum

	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#0,d2
	moveq	#1,d3
	bsr	prtnum
	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#6,d2
	moveq	#1,d3
	bsr	prtnum

	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#0,d2
	moveq	#2,d3
	bsr	prtnum
	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#6,d2
	moveq	#2,d3
	bsr	prtnum

	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#0,d2
	moveq	#3,d3
	bsr	prtnum
	moveq	#0,d0
	move.w	(a0)+,d0
	moveq	#10,d1
	moveq	#6,d2
	moveq	#3,d3
	bsr	prtnum
	rts

*	These are the offsets in warpstruct	*
wsrcx:		equ	0
wsrcy:		equ	4
wsrcdx:		equ	8
wsrcdy:		equ	12
wdstx1:		equ	16
wdstx2:		equ	20
wdstx3:		equ	24
wdstx4:		equ	28
wdsty1:		equ	32
wdsty2:		equ	36
wdsty3:		equ	40
wdsty4:		equ	44
wa:		equ	48
wb:		equ	52
wc:		equ	56
wd:		equ	60
we:		equ	64
wf:		equ	68
wg:		equ	72
wh:		equ	76


warpstruct:
	ds.l	20
*****************************************************************
*	Here we go with the first attempt at a Bilinear warp	*
*	algorithm.						*
*								*
*	warpstruct should be initialised with all the correct	*
*	values.							*
*****************************************************************
warp:
	lea	warpstruct,a6
	move.l	wdstx1(a6),d0
	neg.l	d0
	add.l	wdstx2(a6),d0		;a=(-x1+x2)/sdx
	asl.l	#8,d0			;scale up
	divs.l	wsrcdx(a6),d0
	move.l	d0,wa(a6)

	move.l	wdstx1(a6),d0
	neg.l	d0
	add.l	wdstx4(a6),d0		;b=(-x1+x4)/sdy
	asl.l	#8,d0			;scale up
	divs.l	wsrcdy(a6),d0
	move.l	d0,wb(a6)

bp:	move.l	wdstx1(a6),d0
	sub.l	wdstx2(a6),d0
	add.l	wdstx3(a6),d0
	sub.l	wdstx4(a6),d0
	asl.l	#8,d0			;scale up
	move.l	wsrcdx(a6),d1
	muls.l	wsrcdy(a6),d1		;c=(x1-x2+x3-x4)/sdx*sdy
	asr.l	#8,d1
	divs.l	d1,d0
	move.l	d0,wc(a6)

	move.l	wdstx1(a6),wd(a6)	;d=x1

	move.l	wdsty1(a6),d0
	neg.l	d0
	add.l	wdsty2(a6),d0		;e=(-y1+y2)/sdx
	asl.l	#8,d0			;scale up
	divs.l	wsrcdy(a6),d0
	move.l	d0,we(a6)

	move.l	wdsty1(a6),d0
	neg.l	d0
	add.l	wdsty4(a6),d0		;f=(-y1+y4)/sdy
	asl.l	#8,d0			;scale up
	divs.l	wsrcdy(a6),d0
	move.l	d0,wf(a6)

	move.l	wdsty1(a6),d0
	sub.l	wdsty2(a6),d0
	add.l	wdsty3(a6),d0
	sub.l	wdsty4(a6),d0
	asl.l	#8,d0			;scale up
	move.l	wsrcdx(a6),d1
	muls.l	wsrcdy(a6),d1		;g=(y1-y2+y3-y4)/sdx*sdy
	asr.l	#8,d1
	divs.l	d1,d0
	move.l	d0,wg(a6)

	move.l	wdsty1(a6),wh(a6)	;h=y1

	move.l	wsrcdy(a6),d7
	moveq	#0,d0			;d0=y
yloop:
	move.l	wsrcdx(a6),d6
	moveq	#0,d2			;d2=x
xloop:
	move.w	d0,pixely
	move.w	d2,pixelx	;z=readpixel((x+0.5),y)
	bsr	getpoint

	move.l	wa(a6),d4
	muls.l	d2,d4		;a*x
	asr.l	#8,d4
	move.l	wb(a6),d5
	muls.l	d0,d5		;+b*y
	asr.l	#8,d5
	add.l	d5,d4
	move.l	wc(a6),d5
	muls.l	d2,d5		;+c*x*y
	asr.l	#8,d5
	muls.l	d0,d5
	asr.l	#8,d5
	add.l	d5,d4

	add.l	wd(a6),d4	;+d
	move.w	d4,pixelx


	move.l	we(a6),d4
	muls.l	d2,d4		;a*x
	asr.l	#8,d4
	move.l	wf(a6),d5
	muls.l	d0,d5		;+b*y
	asr.l	#8,d5
	add.l	d5,d4
	move.l	wg(a6),d5
	muls.l	d2,d5		;+c*x*y
	asr.l	#8,d5
	muls.l	d0,d5
	asr.l	#8,d5
	add.l	d5,d4
	add.l	wh(a6),d4	;+d
	move.w	d4,pixely
	bsr	putpoint

	addq.l	#1,d2
	subq.l	#1,d6
	bpl	xloop
	addq.l	#1,d0
	subq.l	#1,d7
	bpl	yloop
	rts

putscreen:
	move.w	#7999,d7
	lea	pic,a0
	move.l	seen,a1
psl:
	move.l	(a0)+,(a1)+
	dbf	d7,psl
	rts




*	blitter stuff	*
halftone:	equ	0
src_xinc:	equ	32
src_yinc:	equ	34
src_addr:	equ	36
endmask1:	equ	40
endmask2:	equ	42
endmask3:	equ	44
dst_xinc:	equ	46
dst_yinc:	equ	48
dst_addr:	equ	50
x_count:	equ	54
y_count:	equ	56
hop:		equ	58
op:		equ	59
line_num:	equ	60
skew:		equ	61
fhop_source:	equ	1
fhop_halftone:	equ	0
fskewfxsr:	equ	7
fskewnfsr:	equ	6
flinebusy:	equ	7
flinehog:	equ	6
flinesmudge:	equ	5
mhop_source:	equ	$02
mhop_halftone:	equ	$01
mskewfxsr:	equ	$80
mskewnfsr:	equ	$40
mlinebusy:	equ	$80
mlinehog:	equ	$40
mlinesmudge:	equ	$20

*magic:		equ	1650
*magic2:		equ	1650
*numstars:	equ	12
speed:		equ	350
zclip:		equ	100



testdraw:
*	move.l	whichob,a5
	lea	box,a5
	move.l	obviewpb(a5),a0
	cmpi.w	#1,buttondat
	bne.s	mcin
	sub.w	#50,uniz
	bra.s	mouserots
mcin:
	cmpi.w	#2,buttondat
	bne.s	mouserots
	add.w	#50,uniz
mouserots:
	move.w	mx,unitheta
	move.w	my,uniphi
	bra.s	dobefore
dobefore:
	move.w	unitheta,theta(a0)
	move.w	uniphi,phi(a0)
	move.w	unirho,rho(a0)
	move.w	uniz,obz(a5)
	move.w	obx(a5),plx
	move.w	oby(a5),ply
	move.w	obz(a5),plz
drawobject:
	bsr	init_vtrans
	lea	box,a3 		;Point to verticies
	bsr	trans_vert	;Transform verticies
	lea	box,a0
	lea	vtrans_vrt,a1	;list of transformed vertices
	lea	ptrans_vrt,a2	;list of onscreen coords
*	rts
objectisdots:
	move.w	#15,color
	move.w	numdots(a0),d0	;get the number of points
	subq.w	#1,d0
nohides:
	move.w	(a2)+,pixelx
	move.w	(a2)+,pixely
	bsr	putpoint
	dbf	d0,nohides
	rts

*cxmin:		equ	0
*cxmax:		equ	319
*cymin:		equ	0
*cymax:		equ	150


***************************************************************************
*		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)
	move.l	obviewpb(a3),a5
	move.l	obvert(a3),a3
	move.w	(a3)+,d6	;get number of verts
	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
	movem.w	(a3)+,d0-d2
	bsr	view_trans	;Apply view transformatrmed coordinates
	add.w	plx,d3
	add.w	ply,d4
	add.w	plz,d5
	add.w	myviewx,d3
	add.w	myviewy,d4
	move.w	d3,(a1)+
	move	d4,(a1)+
*	cmpi.w	#zclip,d5
*	bge	okz	
*	move.w	#zclip,d5
*okz:
	move	d5,(a1)+
	move.w	#0,(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

*******************************
*		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.w	d0,d3			;get x
	muls	c_theta(a5),d3
	move.w	d1,d4			;get y
	muls	s_theta(a5),d4
	add.l	d4,d3
	swap	d3
	rol.l	#1,d3			;thats	x
	move.w	d1,d4			;get y
	muls	c_theta(a5),d4
	muls	s_theta(a5),d0		;x
	sub.l	d0,d4
	swap	d4
	rol.l	#1,d4			;thats	y
	move.w	d4,d1			;x
	move.w	d3,d0			;y

	move.w	d1,d3			;get y
	muls	c_phi(a5),d3
	move.w	d2,d4			;get z
	muls	s_phi(a5),d4
	add.l	d4,d3
	swap	d3
	rol.l	#1,d3			;thats	y
	move.w	d2,d4			;thats z
	muls	c_phi(a5),d4
	muls	s_phi(a5),d1		;y
	sub.l	d1,d4
	swap	d4
	rol.l	#1,d4			;thats	z
	move.w	d4,d2			;y

	move.w	d0,d4			;x
	muls	c_rho(a5),d4
	move.w	d2,d5
	muls	s_rho(a5),d5		;z
	sub.l	d5,d4
	swap	d4
	rol.l	#1,d4			;thats	x
	move.w	d4,d5
	muls	s_rho(a5),d0
	muls	c_rho(a5),d2
	add.l	d0,d2
	swap	d2
	rol.l	#1,d2		;thats	z
	move.w	d2,d5
	move.w	d3,d7
	move.w	d4,d3
	move.w	d7,d4
	rts

worldview_trans:
	tst.w	theta(a5)
	beq	skiptheta
	move.w	d0,d3			;get x
	muls	c_theta(a5),d3
	move.w	d1,d4			;get y
	muls	s_theta(a5),d4
	add.l	d4,d3
	swap	d3
	rol.l	#1,d3			;thats	y
	move.w	d1,d4			;get y
	muls	c_theta(a5),d4
	muls	s_theta(a5),d0		;x
	sub.l	d0,d4
	swap	d4
	rol.l	#1,d4			;thats	x
	move.w	d4,d1			;x
	move.w	d3,d0			;y

skiptheta:
	tst.w	rho(a5)
	beq	skiprho
	move.w	d0,d4			;x
	muls	c_rho(a5),d4
	move.w	d2,d5
	muls	s_rho(a5),d5		;z
	sub.l	d5,d4
	swap	d4
	rol.l	#1,d4			;thats	z
	move.w	d4,d7
	move.w	d0,d4
	muls	s_rho(a5),d4
	move.w	d2,d3
	muls	c_rho(a5),d3
	add.l	d4,d3
	swap	d3
	rol.l	#1,d3		;thats	x
	move.w	d3,d2
	move.w	d7,d0
skiprho:
	tst.w	phi(a5)
	beq	skipphi
	move.w	d1,d3			;get y
	muls	c_phi(a5),d3
	move.w	d2,d4			;get z
	muls	s_phi(a5),d4
	add.l	d4,d3
	swap	d3
	rol.l	#1,d3			;thats	z
	move.w	d2,d4			;thats z
	muls	c_phi(a5),d4
	muls	s_phi(a5),d1		;y
	sub.l	d1,d4
	swap	d4
	rol.l	#1,d4			;thats	y
	move.w	d4,d2			;y
	move.w	d3,d1
skipphi:
	move.w	d0,d3
	move.w	d1,d4
	move.w	d2,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	#8,d3	  
	divs	d5,d3
	
	swap	d4		;Calculate Ys
	clr	d4
	asr.l	#8,d4
	divs	d5,d4

	add	#159,d3		;Adjust for centre of screen
	add	#100,d4
	rts

***************************************************************************
*		INITIALISE VIEW TRANSFORM
*	Initialise the viewing transformation.
*	expects	address of object data in a5
init_vtrans:
	move.l	obviewpb(a5),a5
	move	theta(a5),d3		;Calculate SIN amd COS theta
	bsr	trig
	move	d4,s_theta(a5)
	move	d5,c_theta(a5)
	
	move	phi(a5),d3	;Calculate SIN and COS phi
	bsr	trig
	move	d4,s_phi(a5)
	move	d5,c_phi(a5)

	move	rho(a5),d3	;Calculate Vl
	bsr	trig
	move.w	d4,s_rho(a5)
	move.w	d5,c_rho(a5)
	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:	addi.w	#$40,d3		;Find cCOS of angle
	bsr	find_sin
	move	d4,d5
	subi.w	#$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
	addi.w	#$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
	subi.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

vtrans_vrt:	ds.w	1700	;View transformed coordinates	
ptrans_vrt:	ds.w	1700	;Perspective transformed coordinates
***************************************************************************
filled:		equ	0
line:		equ	1
dots:		equ	2
balls:		equ	3
obviewpb:	equ	0	;offsets for object linked list desc'n
obx:		equ	4
oby:		equ	6
obz:		equ	8
obvert:		equ	10
obfacelist:	equ	14
obvrtnum:	equ	0
obfacenum:	equ	0
centrepoint:	equ	20
style:		equ	18
numdots:	equ	20
linecolinfo:	equ	20
obcolors:	equ	22
hidenflag:	equ	26
activeflag:	equ	28
canitbehit:	equ	30
obw:		equ	32
onscreenx:	equ	34
onscreeny:	equ	36
obint:		equ	38
obxvel:		equ	40
obyvel:		equ	42
obzvel:		equ	44
worldvpb:	ds.w	20
boxvpb:		ds.w	20
box:
	dc.l	boxvpb
	dc.w	0,0,500
	dc.l	box2vrt
	dc.l	box2face
	dc.w	dots
	dc.w	4
	dc.l	box2palette
	dc.w	1
box2palette:
	dc.w	$0000,$040f,$010d,$0c3f,$021f,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
box2vrt:
	dc.w	4
	dc.w	-40,40,0
	dc.w	40,40,0
	dc.w	40,-40,0
	dc.w	-40,-40,0
box2face:
	dc.w	8
	dc.w	1,3,0,1,2,3
	dc.w	2,3,4,5,6,7
	dc.w	1,3,8,9,10,11
	dc.w	2,3,12,13,14,15

	dc.w	3,3,3,2,1,0
	dc.w	4,3,7,6,5,4
	dc.w	3,3,11,10,9,8
	dc.w	4,3,15,14,13,12


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
s_rho:		equ	14
c_rho:		equ	16
x1s:	dc.w	0
y1s:	dc.w	0
x2s:	dc.w	0
y2s:	dc.w	0
yadd:	dc.l	0

unitheta:	dc.w	0
uniphi:		dc.w	0
unirho:		dc.w	0
uniz:		dc.w	500
thetainc:	dc.w	0
phiinc:	dc.w	0
rhoinc:	dc.w	0
clz:		dc.w	1500
mytheta:	dc.w	0
myphi:		dc.w	0
myrho:		dc.w	0
lifetime:	dc.w	-1
zfinal:		dc.w	0
halflife:	dc.w	0
kx:	dc.w	0
px:	dc.w	0
ky:	dc.w	0
py:	dc.w	0
kz:	dc.w	0
pz:	dc.w	0
rx:	dc.w	0
ry:	dc.w	0
rz:	dc.w	0
hdf:		dc.w	0
numfacesave:	dc.w	0
facepoints:	dc.w	0
facecolor1:	dc.w	0
facecolor2:	dc.w	0
cbits:	dc.w	0
cbits2:	dc.w	0
xsav:	dc.w	0
ysav:	dc.w	0

plx:	dc.w	0
ply:	dc.w	0
plz:	dc.w	0
myviewx:	dc.w	0
myviewy:	dc.w	0



****************************************
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

*****************************************
*	number in d0.l			*
*	d1.l is base			*
*	d2,x cursor			*
*	d3,y cursor			*
*****************************************
prtnum:
	move.l	a0,-(sp)
	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
	move.l	(sp)+,a0
	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
	rts

oldmouse:	dc.l	0
******************************************
*               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.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	oldmouse,16(a0)

*	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

buttondat:	dc.w	0
mx:		dc.w	0
my:	dc.w	0
*************puts my mouse interupt in***************** 
putrout:move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	16(a0),oldmouse
        move.l  #myrout,16(a0)
        rts
lastb:		dc.w	-1
mousehasgoneup:	dc.w	0

**************my ubeut mouse interupt*******************
myrout:
	movem.l	d0-d2/a0-a1,-(sp)
	moveq	#0,d0	
	move.b	(a0),d0
	cmpi.b	#$fa,d0
	bne	myr1
	move.w	#1,buttondat
	bra	myrxy
myr1:
	cmpi.b	#$f9,d0
	bne	myr2
	move.w	#2,buttondat
	bra	myrxy
myr2:
	cmpi.b	#$fb,d0
	bne	myr3
	move.w	#3,buttondat
	bra	myrxy
myr3:
	move.w	#1,mousehasgoneup
	move.w	#0,buttondat
myrxy:
	moveq	#0,d0
	move.b	1(a0),d0
	ext.w	d0
	moveq	#0,d1
	move.b	2(a0),d1
	ext.w	d1
	add.w	d0,mx
	add.w	d1,my
*	sub.w	d1,my2
myr4:	move.w	mx,d0
	move.w	my,d1
	bsr	extra
	move.w	d0,mx
	move.w	d1,my
	movem.l	(sp)+,d0-d2/a0-a1
	rts

extra:  cmpi.w	#mminx,d0
        bge	extra1
        move.w	#mminx,d0
        bra	extra2
extra1: cmpi.w   #mmaxx,d0
        ble	extra2
        move.w  #mmaxx,d0
extra2:	cmpi.w  #mminy,d1
        bge	extra3
        move.w  #mminy,d1
        bra	extra4
extra3: cmpi.w   #mmaxy,d1
        ble	extra4
        move.w  #mmaxy,d1
extra4:
        rts



********************************************
* Atari ST hi-speed plotting subroutine    *
* For Low-resolution mode only		   *
********************************************
plotinit:
	move.l	seen,a0
	move.b	#200,d1
	lea	ytable1,a1
iloop:
	move.l	a0,(a1)+
	adda.w	#160,a0
	subq.b	#1,d1
	bne	iloop
	rts


getpoint:
	movem.l d0-d5/a0-a1,-(sp)
	move.w	pixelx,d0
	cmp.w	#cxmax,d0
	bgt	pltxit
	cmpi.w	#cxmin,d0
	blt	pltxit
	move.w	d0,d3
	andi.w	#15,d3
	move.w	pixely,d1
	cmp.w	#cymax,d1
	bgt	pltxit
	cmpi.w	#cymin,d1
	blt	pltxit
	lsl.w	#2,d1

	move.w	d0,d2
	lsr.w	#1,d0
	and.w	#$00f8,d0
	move.w	d0,a0
	lea	ytable1,a1
	adda.l	(a1,d1.w),a0

	and.w	#$000f,d2	;get last 4 pixels
	moveq	#15,d1
	sub.w	d2,d1
	moveq	#0,d0
gp1:
	move.w	(a0)+,d2
	btst	d1,d2
	beq.s	gp2
	or.w	#1,d0
gp2:
	move.w	(a0)+,d2
	btst	d1,d2
	beq.s	gp3
	or.w	#2,d0
gp3:
	move.w	(a0)+,d2
	btst	d1,d2
	beq.s	gp4
	or.w	#4,d0
gp4:
	move.w	(a0)+,d2
	btst	d1,d2
	beq.s	gp5
	or.w	#8,d0
gp5:
	move.w	d0,color
	movem.l	(sp)+,d0-d5/a0-a1
	rts

**********************************************
putpoint:
	movem.l d0-d5/a0-a1,-(sp)
	move.w	pixelx,d0
	cmp.w	#cxmax,d0
	bgt	pltxit
	cmpi.w	#cxmin,d0
	blt	pltxit
	move.w	d0,d3
	andi.w	#15,d3
	move.w	pixely,d1
	cmp.w	#cymax,d1
	bgt	pltxit
	cmpi.w	#cymin,d1
	blt	pltxit
	lsl.w	#2,d1
	move.w	color,d2

	lsr.w	#1,d0
	and.w	#$00f8,d0
	move.w	d0,a0
	lea	ytable1,a1
	adda.l	(a1,d1.w),a0

	move.w #$8000,d4
	lsr.w d3,d4
	move.l #$80008000,d5
	lsr.l d3,d5
	eor.l #$ffffffff,d5
******************************
*    Turn off old pixel!     *
******************************
myphere:
	and.l d5,(a0)	     ;turn off old pixel
	and.l d5,4(a0)	     ;(both longwords)

******************************
* Now turn on the new pixel! *
******************************
	btst #0,d2
	beq bit2
	or.w d4,(a0)
bit2:
	btst #1,d2
	beq bit4
	or.w d4,2(a0)
bit4:
	btst #2,d2
	beq bit8
	or.w d4,4(a0)
bit8:
	btst #3,d2
	beq pltxit
	or.w d4,6(a0)
pltxit:
	movem.l (sp)+,d0-d5/a0-a1
	rts



rvsave:	dc.l	0
resave:	dc.l	0
	even
savesup:	dc.l	0
ressave:	dc.w	0
filename:	dc.l	0
greypalette:
;Palette of ANDROID.NEO: Saved by NEOchrome V2.20 by Chaos, Inc.
		dc.w	$0217,$0777,$0667,$0557,$0447,$0336,$0225,$0114
		dc.w	$0003,$0077,$0106,$0005,$0004,$0323,$0212,$0000


save_usp:
	dc.l	0
wfnc:
	dc.w	0
save_stk:
	dc.l	0
pointer:	dc.l	0
pixelx:		dc.l	0
pixely:		dc.l	0
color:	dc.w	15
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
pic:
	incbin	'copyb2.img'

*	section	bss
x1:	ds.w	1
y1:	ds.w	1
x2:	ds.w	1
y2:	ds.w	1

number:	ds.w	1
palsave:
	ds.w	16
vblslot:	ds.l	1
d7emulator:	ds.w	1
ytable1:	ds.w	200
