
* BLiTTER BASE ADDRESS

blitter         equ     $FF8A00

* BLiTTER REGISTER OFFSETS

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

s2w:	equ	100
s2h:	equ	75
sw:	equ	384
sh:	equ	480
*****************************************************************
*	Falcon warp Demo.					*
*	Version 1.0						*
*****************************************************************
	include	'fequates.s'
start:
	move.l	4(a7),a3		base page
	move.l	#mystack,a7
	move.l	$c(a3),d0		text len
	add.l	$14(a3),d0		data len
	add.l	$1c(a3),d0		BSS len
	add.l	#$100,d0		basepage
	move.l	d0,-(sp)
	move.l	a3,-(sp)
	clr.w	-(sp)
	move.w	#$4a,-(sp)
	trap	#1			shrink memory
	lea	12(sp),sp

	clr.l	-(sp)		;as usual the first thing to
	move.w	#$20,-(sp)	;do is go into supervisor mode
	trap	#1
	addq.l	#6,sp
	move.l	d0,savesp

	bsr	putrout
	bsr	setupscreen
	bsr	putbaby
mainloop:
	move.w	#00,pixelx
	move.w	#0,pixely
	move.w	#380,width
	move.w	#450,height

	bsr	clearblock
	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	#75,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

	bra	exit

putbaby:
	lea	baby,a0
	move.l	viewscreen,a1
	move.w	#s2h,d0
	subq.w	#1,d0
	move.l	a0,a2
	move.l	a1,a3
pbl:
	rept	s2w
	move.w	(a2)+,(a3)+
	endr
	lea	768(a1),a1
	move.l	a1,a3
	dbf	d0,pbl
	rts


****************************************
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	exit		*yes/no
keyback:
	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


*	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

*	add.w	#10,unirho
*	bra	dobefore

	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	#384/2,d3		;Adjust for centre of screen
	add	#480/2,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	200
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


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

mminx:	equ	0
mmaxx:	equ	384
mminy:	equ	0
mmaxy:	equ	480

getpoint:
	movem.l	d0/d1/a0,-(sp)
	moveq	#0,d0
	move.l	d0,d1
	move.w	pixelx,d0
	add.l	d0,d0
	move.w	pixely,d1
	mulu	#s2w*2,d1
	add.l	d1,d0
	lea	baby,a0
	add.l	d0,a0
	move.w	(a0),color
	movem.l	(sp)+,d0/d1/a0
	rts
putpoint:
	movem.l	d0/d1/a0,-(sp)
	moveq	#0,d0
	move.l	d0,d1
	move.w	pixelx,d0
	add.l	d0,d0
	move.w	pixely,d1
	mulu	#sw*2,d1
	add.l	d1,d0
	move.l	viewscreen,a0
	add.l	d0,a0
	move.w	color,(a0)
	movem.l	(sp)+,d0/d1/a0
	rts
clearblock:

	lea	blitter,a6
.waitblit:
	btst	#7,line_num(a6)
	bne	.waitblit

	move.l	viewscreen,a0
	moveq	#0,d0
	move.w	pixelx,d0
	add.l	d0,d0
	add.l	d0,a0
	moveq	#0,d0
	move.w	pixely,d0
	mulu	#sw*2,d0
	add.l	d0,a0
*	move.l	a0,src_addr(a6)
	move.l	a0,dst_addr(a6)
*	move.w	#2,src_xinc(a6)
	move.w	#2,dst_xinc(a6)
	move.w	#sw*2,d0
	move.w	width,d1
	subq.w	#1,d1
	add.w	d1,d1
	sub.w	d1,d0
*	move.w	d0,src_yinc(a6)
	move.w	d0,dst_yinc(a6)
	move.w	width,x_count(a6)
	move.w	height,y_count(a6)
	move.w	#-1,endmask1(a6)
	move.w	#-1,endmask2(a6)
	move.w	#-1,endmask3(a6)
	move.b	#0,skew(a6)
	move.b	#$0,op(a6)
	move.b	#2,hop(a6)
	bset	#6,line_num(a6)
	bset	#7,line_num(a6)
	rts



*********************************************************
*	General vanilla block routine			*
*	goes down					*
*	Set:						*
*		sBlockx					*
*		sblocky					*
*		dBlockx					*
*		dblocky					*
*		source					*
*		width					*
*		height					*
*********************************************************
putvblockd:
	cmpi.w	#480,sblocky
	ble	.ok
	move.w	dblockx,sblockx
	move.w	dblocky,sblocky
.ok:
	move.l	source,a0
	move.l	source,a0
	move.l	viewscreen,a1
	moveq	#0,d0
	moveq	#0,d1
	move.w	sblockx,d0	;x
	move.w	sblocky,d1	;y
	add.l	d0,d0
	move.l	d1,d3
	mulu	#sw*2,d3
	add.l	d0,d3
	add.l	d3,a0

	moveq	#0,d0
	moveq	#0,d1
	move.w	dblockx,d0	;x
	move.w	dblocky,d1	;y
	add.l	d0,d0
	move.l	d1,d3
	mulu	#sw*2,d3
	add.l	d0,d3
	add.l	d3,a1	;at this stage a0 is source ad and a1 dest

	lea	blitter,a6
.waitblit:
	btst	#7,line_num(a6)
	bne	.waitblit

	move.l	a0,src_addr(a6)
	move.l	a1,dst_addr(a6)
	move.w	#2,src_xinc(a6)
	move.w	#2,dst_xinc(a6)
	move.w	#sw*2,d0
	move.w	width,d1
	subq.w	#1,d1
	add.w	d1,d1
	sub.w	d1,d0
	move.w	d0,src_yinc(a6)
	move.w	d0,dst_yinc(a6)
	move.w	width,x_count(a6)
	move.w	height,y_count(a6)
	move.w	#-1,endmask1(a6)
	move.w	#-1,endmask2(a6)
	move.w	#-1,endmask3(a6)
	move.b	#0,skew(a6)
	move.b	#$3,op(a6)
	move.b	#2,hop(a6)
	bset	#6,line_num(a6)
	bset	#7,line_num(a6)
	rts


exit:
	move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	oldmouse,16(a0)

	tst.l	viewscreen
	bmi	skipdm1
	move.l	viewscreen,d0
	bsr	deallocate
skipdm1:
	tst.l	screen1
	bmi	skipdm2
	move.l	screen1,d0
	bsr	deallocate
skipdm2:
	tst.l	screen2
	bmi	skipdm3
	move.l	screen2,d0
	bsr	deallocate
skipdm3:
	bsr	restoreregs

	move.l	savedta,-(sp)
	move.w	#$1a,-(sp)	;set disk transfer address
	trap	#1
	addq.l	#6,sp

	move.l	savesp,-(sp)
	move.w	#$20,-(sp)
	trap	#1
	addq.l	#6,sp

	clr.w	-(sp)
	trap	#1

deallocate:
	move.l	d0,-(sp)
	move.w	#$49,-(sp)
	trap	#1
	addq.l	#6,sp
	rts

clearmem:
	move.l	#(sw*sh),d0
	subq.l	#1,d0
	moveq	#0,d1
cml:
	move.w	d1,(a0)+
	subq.l	#1,d0
	bpl	cml
	rts

*********************************************************
*	One call does it all				*
*	Allocates memory for 3 screens 			*
*	targa load/hold area is internal.		*
*	Then sets all the hardware stuff to create	*
*	a 384*480 16bit screen				*
*********************************************************
setupscreen:
	move.l	#((sw*2)*sh)+4+((sw*2)*100),-(sp)	;thats 1 screen size
	move.w	#$48,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.l	d0
	bmi	exit
	and.l	#$fffffffc,d0
	move.l	d0,viewscreen
	move.l	d0,a0
	bsr	clearmem

	move.l	#((sw*2)*sh)+4+((sw*2)*100),-(sp)	;thats 1 screen size
	move.w	#$48,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.l	d0
	bmi	exit
	and.l	#$fffffffc,d0
	move.l	d0,screen1


	move.l	#((sw*2)*sh)+4+((sw*2)*100),-(sp)	;thats 1 screen size
	move.w	#$48,-(sp)
	trap	#1
	addq.l	#6,sp
	tst.l	d0
	bmi	exit
	and.l	#$fffffffc,d0
	move.l	d0,screen2

	bsr	saveregs
	bsr	setregs
	rts





viewscreen:	dc.l	-1
screen1:	dc.l	-1
screen2:	dc.l	-1

restoreregs:
	move.w	shht,hht
	move.w	shss,hss
	move.w	shbb,hbb
	move.w	shbe,hbe
	move.w	shdb,hdb
	move.w	shde,hde
	move.w	shfs,hfs
	move.w	shee,hee

	move.w	svid_bh,vid_bh
	move.w	svid_bm,vid_bm
	move.w	svid_bl,vid_bl

	move.w	svwrap,vwrap

	move.w	svft,vft
	move.w	svss,vss
	move.w	svbb,vbb
	move.w	svbe,vbe
	move.w	svdb,vdb
	move.w	svde,vde

	move.w	ssp_shift,sp_shift
	move.w	sst_shift,st_shift

	move.w	svco,vco

	lea	savepal,a0
	lea	st_palette,a1
	rept	16
	move.w	(a0)+,(a1)+
	endr

	rts

setregs:
	move.l	#0,sp_palette
	move.l	viewscreen,d0
	move.l	d0,d1
	move.l	d1,d2
	lsr.l	#8,d0
	lsr.l	#8,d0
	move.w	d0,vid_bh
	lsr.l	#8,d1
	and.l	#$000000ff,d1
	move.w	d1,vid_bm
	and.l	#$000000ff,d2
	move.w	d2,vid_bl

	move.w	#524,vft
	move.w	#518,vss
	move.w	#513,vbb
	move.w	#22,vbe
	move.w	#27,vdb
	move.w	#507,vde

	move.w	#254,hht
	move.w	#214,hss
	move.w	#203,hbb
	move.w	#39,hbe
	move.w	#2,hdb
	move.w	#179,hde
	move.w	#$0100,sp_shift
	move.w	#2,vco
	move.w	#384,vwrap
	move.w	sp_palette,dummy
	rts


saveregs:
	move.w	vid_bh,svid_bh
	move.w	vid_bm,svid_bm
	move.w	vid_bl,svid_bl

	lea	savepal,a1
	lea	st_palette,a0
	rept	16
	move.w	(a0)+,(a1)+
	endr

	move.w	vft,svft
	move.w	vss,svss
	move.w	vbb,svbb
	move.w	vbe,svbe
	move.w	vdb,svdb
	move.w	vde,svde
	move.w	hht,shht
	move.w	hss,shss
	move.w	hbb,shbb
	move.w	hbe,shbe
	move.w	hdb,shdb
	move.w	hde,shde
	move.w	hfs,shfs
	move.w	hee,shee
	move.w	st_shift,sst_shift
	move.w	sp_shift,ssp_shift
	move.w	vco,svco
	move.w	vwrap,svwrap
	rts



	SECTION BSS
		ds.l	1000
mystack:	ds.w	1	;stacks go backwards
svft:	ds.w	1
svss:	ds.w	1
svbb:	ds.w	1
svbe:	ds.w	1
svdb:	ds.w	1
svde:	ds.w	1
shht:	ds.w	1
shss:	ds.w	1
shbb:	ds.w	1
shbe:	ds.w	1
shdb:	ds.w	1
shde:	ds.w	1
shfs:	ds.w	1
shee	ds.w	1
sst_shift:	ds.w	1
ssp_shift:	ds.w	1
svco:	ds.w	1
svwrap:	ds.w	1
svid_bh:	ds.w	1
svid_bm:	ds.w	1
svid_bl:	ds.w	1
savepal:	ds.w	16
dummy:		ds.w	1
savesp:		ds.l	1
dta:		ds.b	44
savedta:	ds.l	1
dblockx:		ds.w	1
dblocky:		ds.w	1
sblockx:	ds.w	1
sblocky:	ds.w	1
source:		ds.l	1
width:		ds.w	1
height:		ds.w	1
handle:		ds.w	1
oldmouse:	ds.l	1
pixelx:		ds.w	1
pixely:		ds.w	1
color:		ds.w	1
	section data
baby:
	incbin	'baby.raw'