;
; 3D-VECTOR-INTRO 
;
; BY ROBERT BABICZ 
; (BERECHNUNG,BLITTER-LINE.. BY TETRAGON)   
;
res=320					; Aufloesung
wpl=res/16			
color=0
Botline=$c0
startIT:
bsr	start_muzak			; Music on
bsr	Start				; Init screen 
bsr	Main				; HAUPTPROGRAMM
bsr	Reset				; Reset screen 
bsr	stop_muzak			; Music off
rts					; ENDE
;
pageup:
move.l	d0,-(a7)
move.l	#$70000,DrawPlane		; Rastport structure
move.l	#$78000,d0			; BitMap pointer
move.w	d0,LowBMPtr
swap	d0
move.w	d0,HiBMPtr			; Copper fixes the rest
bsr	WaitBot
move.l	(a7)+,d0
rts
;
pagedown:
move.l	d0,-(a7)
move.l	#$78000,DrawPlane		; Rastport structure
move.l	#$70000,d0			; BitMap pointer
move.w	d0,LowBMPtr
swap	d0
move.w	d0,HiBMPtr			; Copper fixes the rest
bsr	Waitbot
move.l	(a7)+,d0
rts

WaitBot:
move.w	#$222,color
cmp.b	#BotLine,$dff006
bne.s	WaitBot
move.w	#$000,color
rts

; Clear the screen 

clwork:
btst	#14,$dff002
bne.s	ClWork

move.l	DrawPlane,$dff054
move.l	#$01000000,$dff040
move.w	#0,$dff066
move.w	#200*64+wpl,$dff058

move.w	#$0f0,color
rts

; Initialize routine 

Start:
move.l	4.w,a6
jsr	-132(a6)

lea	GfxName,a1
moveq	#0,d0
jsr	-552(a6)			; Open Gfx Library
move.l	d0,GfxBase

move.l	d0,a6

move.l	$32(a6),OldCop
move.l	#Coplist,$32(a6)		; Meine Copperlist on !
rts

Reset:
move.l	GfxBase,a6
move.l	OldCop,$32(a6)
move.l	4.w,a6
jsr	-138(a6)
rts

Drawl:
movem.l	d0-d3/a0-a1,-(a7)
move.l	d2,d0
move.l	d3,d1				; X,y start
move.l	a2,d2
move.l	a3,d3				; X,y end
bsr	Draw
movem.l	(a7)+,d0-d3/a0-a1
rts

Draw:
movem.l	d2-d7/a2-a3,-(a7)
moveq	#$f,d4
and.w	d2,d4				; low 4 bits
sub.w	d3,d1				; Height
mulu	#res/8,d3			; Start address
sub.w	d2,d0				; Width
blt.s	No1
tst.w	d1
blt.s	No2
cmp.w	d0,d1
bge.s	No3
moveq	#$11,d7
bra.s	OctSel				; Octant #
No3:
moveq	#1,d7
exg	d1,d0
OctSel:
bra.s	No4
No2:
neg.w	d1
cmp.w	d0,d1
bge.s	Skip
moveq	#$19,d7
bra.s	No4
Skip:
moveq	#5,d7
exg	d1,d0
No4:
bra.s	OctsSel
No1:
neg.w	d0
tst.w	d1
blt.s	No11

cmp.w	d0,d1
bge.s	No12
moveq	#$15,d7
bra.s	OctSel2

No12:
moveq	#9,d7
exg	d1,d0
OctSel2:
bra.s	OctsSel

No11:
neg.w	d1
cmp.w	d0,d1
bge.s	No13
moveq	#$1d,d7
bra.s	OctsSel
No13:
moveq	#$d,d7
exg	d1,d0
OctsSel:
add.w	d1,d1
asr.w	#3,d2
ext.l	d2
add.l	d2,d3				; Total offset
move.w	d1,d2
sub.w	d0,d2
bge.s	NoMinus
ori.w	#$40,d7				; Sign = -
NoMinus:
lea	$dff000,a0
move.w	d2,a3
move.w	#$ffff,d6			; LinePtrn
WaitBl:
btst	#6,2(a0)
bne.s	WaitBl
move.w	d1,$62(a0)			; 4Y
move.w	d2,d1
sub.w	d0,d1
move.w	d1,$64(a0)			; 4Y-4X
moveq	#-1,d1
move.l	d1,$44(a0)			; AFWM+ALWM
move.w	#res/8,$60(a0)			; BitMap Width in bytes
move.w	d7,d5
addq.w	#1,d0
asl.w	#6,d0
addq.w	#2,d0				; Blitsize
move.w	d4,d2
swap	d4
asr.l	#4,d4				; First pixelpos
ori.w	#$b00,d4			; Use ABD
move.w	#$8000,$74(a0)			; Index
clr.w	d1
NoSpesh:
move.l	DrawPlane,d7			; Pointer
swap	d5
move.w	d4,d5
move.b	#$ca,d5			; MinTerms
swap	d5
add.l	d3,d7
WtBl2:
btst	#6,2(a0)
bne.s	WtBl2
move.l	d5,$40(a0)			; BltCon0 & 1
move.w	a3,$52(a0)			; 2Y-X
move.l	d7,$48(a0)
move.l	d7,$54(a0)			; Start address of line
move.w	d6,$72(a0)			; Pattern
move.w	d0,$58(a0)			; Size
movem.l	(a7)+,d2-d7/a2-a3
rts
sincos:
tst.w	d0				; Angle neg. add 360°
bpl.s	NoAddi
add.w	#360,d0
noaddi:
move.l	#sintab,a1			; Beginning ad. of sinetable
move.l	d0,d2				; Angle in d0 and d2
lsl.w	#1,d0				; Angle times 2 as index
move.w	0(a1,d0.w),d1			; Sine to D1
cmp.w	#270,d2				; Calc cosine through
blt.s	plus9				; displacement of sine value
sub.w	#270,d2				; by 90 degrees
bra	sendsin
plus9:
add.w	#90,d2
sendsin:
lsl.w	#1,d2
move.w	(a1,d2.w),d2			; Cosine to D2
rts					; And return

sin:
move.l	#sintab,a1
tst.w	d0
bpl.s	sin1
add.w	#360,d0
sin1:
lsl.w	#1,d0
move.w	(a1,d0.w),d1
rts

matinit:
moveq	#0,d1
move.w	#16384,d2			; The initial value for
move.w	d2,Matrix11			; the main diagonal of
move.w	d1,Matrix12			; the result matrix
move.w	d1,Matrix13			; all other elements 
move.w	d1,Matrix21			; at zero.
move.w	d2,Matrix22
move.w	d1,Matrix23
move.w	d1,Matrix31
move.w	d1,Matrix32
move.w	d2,Matrix33
rts

xrotate:
move.w	XAngle,d0		; Multiply matrix11-matrix33
jsr	SinCos			; with the rotation matrix
move.w	d1,sinx			; for a rotation about the X-Axis
move.w	d2,cosx
move.w	d1,d3
move.w	d2,d4
move.w	Matrix11,Rotx11		; The first column of the matrix
move.w	Matrix21,Rotx21		; Does not change with X rotation
move.w	Matrix31,Rotx31
muls	Matrix12,d2
muls	Matrix13,d1
sub.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx12
move.w	d3,d1
move.w	d4,d2
muls	Matrix22,d2
muls	Matrix23,d1
sub.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,rotx22
move.w	d3,d1
move.w	d4,d2
muls	Matrix32,d2
muls	Matrix33,d1
sub.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx32
move.w	d3,d1
move.w	d4,d2
muls	Matrix12,d1
muls	Matrix13,d2
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx13
move.w	d3,d1
move.w	d4,d2
muls	Matrix22,d1
muls	Matrix23,d2
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx23
muls	Matrix32,d3
muls	Matrix33,d4
add.l	d3,d4
lsl.l	#2,d4
swap	d4
move.w	d4,Rotx33
move.l	#Rotx11,a1
move.l	#Matrix11,a2
moveq	#8,d7			; Number of matrix elements

roxlop1:
move.w	(a1)+,(a2)+		; Copy result matrix, which
dbra	d7,roxlop1		; is still in ROTXnn, to MATRIXnn
rts

yrotate:
move.w	YAngle,d0		; Angle around which rotation is made
jsr	SinCos
move.w	d1,siny
move.w	d2,cosy
move.w	d1,d3			; Sine of Y-Angle
move.w	d2,d4			; Cosine of Y-angle

muls	Matrix11,d2
muls	Matrix13,d1
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx11
move.w	d3,d1
move.w	d4,d2

muls	Matrix21,d2
muls	Matrix23,d1
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx21
move.w	d3,d1
move.w	d4,d2

muls	Matrix31,d2
muls	Matrix33,d1
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx31
neg.w	d3
move.w	d3,d1				; -siny in the rotation mat.
move.w	d4,d2

move.w	Matrix12,Rotx12
move.w	Matrix22,Rotx22			; The second column
move.w	Matrix32,Rotx32			; of the starting matrix
					; does not change.
muls	Matrix11,d1
muls	Matrix13,d2
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx13
move.w	d3,d1
move.w	d4,d2

muls	Matrix21,d1
muls	Matrix23,d2
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx23

muls	Matrix31,d3
muls	Matrix33,d4
add.l	d3,d4
lsl.l	#2,d4
swap	d4
move.w	d4,Rotx33

moveq	#8,d7

move.l	#Rotx11,a1			; Address of result matrix
move.l	#Matrix11,a2			; Address of original matrix

yrotlop1:
move.w	(a1)+,(a2)+			; Copy result matrix
dbra	d7,yrotlop1			; to original matrix
rts

zrotate:
move.w	ZAngle,d0
jsr	SinCos
move.w	d1,SinZ
move.w	d2,CosZ
move.w	d1,d3
move.w	d2,d4

muls	Matrix11,d2
muls	Matrix12,d1
sub.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx11
move.w	d3,d1
move.w	d4,d2

muls	Matrix21,d2
muls	Matrix22,d1
sub.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx21
move.w	d3,d1
move.w	d4,d2

muls	Matrix31,d2
muls	Matrix32,d1
sub.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx31
move.w	d3,d1
move.w	d4,d2

muls	Matrix11,d1
muls	Matrix12,d2
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx12
move.w	d3,d1
move.w	d4,d2

muls	Matrix21,d1
muls	Matrix22,d2
add.l	d1,d2
lsl.l	#2,d2
swap	d2
move.w	d2,Rotx22

muls	Matrix31,d3
muls	Matrix32,d4
add.l	d3,d4
lsl.l	#2,d4
swap	d4
move.w	d4,Rotx32

move.w	Matrix13,Rotx13			; The third column remains
move.w	Matrix23,Rotx23			; Unchanged
move.w	Matrix33,Rotx33

moveq	#8,d7
move.l	#Rotx11,a1
move.l	#Matrix11,a2

zrotlop1:
move.w	(a1)+,(a2)+			; Copy to general
dbra	d7,zrotlop1			; rotation matrix
rts

rotate:
move.w	NumMark,d0			; Number of points to be
ext.l	d0				; transformed as counter
subq.l	#1,d0

move.l	datx,a1
move.l	daty,a2
move.l	datz,a3

move.l	pointx,a4
move.l	pointy,a5
move.l	pointz,a6

rotate1:
move.w	(a1)+,d1			; X-Coordinate
add.w	Offx,d1
move.w	d1,d4

move.w	(a2)+,d2			; Y-Coordinate
add.w	Offy,d2			; Translation to point[offx,offy,offz]
move.w	d2,d5

move.w	(a3)+,d3			; Z-Coordinate
add.w	offz,d3
move.w	d3,d6

muls	Matrix11,d1
muls	Matrix21,d2
muls	Matrix31,d3

add.l	d1,d2
add.l	d2,d3
lsl.l	#2,d3
swap	d3
add.w	xoffs,d3
move.w	d3,(a4)+			; Rotated X-Coordinate

move.w	d4,d1
move.w	d5,d2
move.w	d6,d3

muls	Matrix12,d1
muls	Matrix22,d2
muls	Matrix32,d3
add.l	d1,d2
add.l	d2,d3
lsl.l	#2,d3
swap	d3
add.w	yoffs,d3
move.w	d3,(a5)+		; Rotated Y-Coordinate

muls	Matrix13,d4
muls	Matrix23,d5
muls	Matrix33,d6
add.l	d4,d5
add.l	d5,d6
lsl.l	#2,d6
swap	d6
add.w	Zoffs,d6
move.w	d6,(a6)+		; Rotated Z-Coordinate

dbra	d0,rotate1
rts

pers:
move.l	pointx,a1		; Beginning address of point arrays
move.l	pointy,a2
move.l	pointz,a3

move.l	xplot,a4		; Start address of display coordinate
move.l	yplot,a5		; array.

move.w	NumMark,d0		; Number of points to be transformed
ext.l	d0
subq.l	#1,d0

perlop:
move.w	(a3)+,d5		; Z-coordinate of object
move.w	d5,d6
move.w	Dist,d4			; Enlargement factor
sub.w	d5,d4			; Dist minus Z-coordinate of obj.coord
ext.l	d4
lsl.l	#8,d4			; Times 256 for value fitting
move.w	Zobs,d3			; Projection center Z-coordinates
ext.l	d3

sub.l	d6,d3			; Minus z-coordinate of object
bne.s	pers1

moveq	#0,d1			; Catch division by zero
addq.l	#2,a1
addq.l	#2,a2
move.w	d1,(a4)+
move.w	d1,(a5)+
bra.s	perend1

pers1:
divs	d3,d4
move.w	d4,d3
move.w	(a1)+,d1		; X-Coordinate of object
move.w	d1,d2
neg.w	d1
muls	d1,d3			; Multiplied by perspective factor
lsr.l	#8,d3			; /256 save value fitting

add.w	d3,d2			; Add to x-coordinate
add.w	x0,d2			; Add screen offset (center point)
move.w	d2,(a4)+		; Display X-coordinate

move.w	(a2)+,d1		; Y-Coordinate of object
move.w	d1,d2
neg.w	d1
muls	d1,d4
lsr.l	#8,d4			; /256

add.w	d4,d2
neg.w	d2			; Display offset, mirror of Y-Axis
add.w	y0,d2			; Source at [X0,Y0]
move.w	d2,(a5)+		; Display Y-Coordinate
perend1:
dbra	d0,perlop		; Until all points transformed

move.w	#$ff0,color
rts

drawn1:
move.l	XPlot,a4		; Display X-Coordinate
move.l	YPlot,a5		; Display Y-Coordinate
move.w	numline,d0		; Number of lines
ext.l	d0
subq.l	#1,d0			; As counter
move.l	linxy,a6		; Address of line array

drlop:
move.l	(a6)+,d1		; First line (P1,P2)
subq.w	#1,d1			; Fit to list structure
lsl.w	#1,d1			; Times list element length (2)
move.w	(a4,d1.w),d2		; X-Coordinate of 2nd point
move.w	(a5,d1.w),d3		; Y-Coordinate of second point
swap	d1
subq.w	#1,d1
lsl.w	#1,d1
move.w	(a4,d1.w),a2		; X-Coordinate of first point
move.w	(a5,d1.w),a3		; Y-Coordinate of first point
bsr	Drawl			; Draw line from P1 to P2
dbra	d0,drlop		; Until all lines drawn
rts
wait1:
dbra	d0,wait1		; Delay loop
rts
; Sine table 
sintab:
dc.w	0,286,572,857,1143,1428,1713,1997,2280
dc.w	2563,2845,3126,3406,3686,3964,4240,4516
dc.w	4790,5063,5334,5604,5872,6138,6402,6664
dc.w	6924,7182,7438,7692,7943,8192,8438,8682
dc.w	8923,9162,9397,9630,9860,10087,10311,10531
dc.w	10749,10963,11174,11381,11585,11786,11982,12176
dc.w	12365,12551,12733,12911,13085,13255,13421,13583
dc.w	13741,13894,14044,14189,14330,14466,14598,14726
dc.w	14849,14962,15082,15191,15296,15396,15491,15582
dc.w	15668,15749,15826,15897,15964,16026,16083,16135
dc.w	16182,16225,16262,16294,16322,16344,16362,16374
dc.w	16382,16383

dc.w	16382,16374,16362,16344,16322,16294,16262,16225
dc.w	16182
dc.w	16135,16083,16026,15964,15897,15826,15749,15668
dc.w	15582,15491,15396,15296,15191,15082,14962,14849
dc.w	14726,14598,14466,14330,14189,14044,13894,13741
dc.w	13583,13421,13255,13085,12911,12733,12551,12365
dc.w	12176,11982,11786,11585,11381,11174,10963,10749
dc.w	10531,10311,10087,9860,9630,9397,9162,8923
dc.w	8682,8438,8192,7943,7692,7438,7182,6924
dc.w	6664,6402,6138,5872,5604,5334,5063,4790
dc.w	4516,4240,3964,3686,3406,3126,2845,2563
dc.w	2280,1997,1713,1428,1143,857,572,286,0

dc.w	-286,-572,-857,-1143,-1428,-1713,-1997,-2280
dc.w	-2563,-2845,-3126,-3406,-3686,-3964,-4240,-4516
dc.w	-4790,-5063,-5334,-5604,-5872,-6138,-6402,-6664
dc.w	-6924,-7182,-7438,-7692,-7943,-8192,-8438,-8682
dc.w	-8923,-9162,-9397,-9630,-9860,-10087,-10311,-10531
dc.w	-10749,-10963,-11174,-11381,-11585,-11786,-11982,-12176
dc.w	-12365,-12551,-12733,-12911,-13085,-13255,-13421,-13583
dc.w	-13741,-13894,-14044,-14189,-14330,-14466,-14598,-14726
dc.w	-14849,-14962,-15082,-15191,-15296,-15396,-15491,-15582
dc.w	-15668,-15749,-15826,-15897,-15964,-16026,-16083,-16135
dc.w	-16182,-16225,-16262,-16294,-16322,-16344,-16362,-16374
dc.w	-16382,-16383

dc.w	-16382,-16374,-16362,-16344,-16322,-16294,-16262,-16225
dc.w	-16182
dc.w	-16135,-16083,-16026,-15964,-15897,-15826,-15749,-15668
dc.w	-15582,-15491,-15396,-15296,-15191,-15082,-14962,-14849
dc.w	-14726,-14598,-14466,-14330,-14189,-14044,-13894,-13741
dc.w	-13583,-13421,-13255,-13085,-12911,-12733,-12551,-12365
dc.w	-12176,-11982,-11786,-11585,-11381,-11174,-10963,-10749
dc.w	-10531,-10311,-10087,-9860,-9630,-9397,-9162,-8923
dc.w	-8682,-8438,-8192,-7943,-7692,-7438,-7182,-6924
dc.w	-6664,-6402,-6138,-5872,-5604,-5334,-5063,-4790
dc.w	-4516,-4240,-3964,-3686,-3406,-3126,-2845,-2563
dc.w	-2280,-1997,-1713,-1428,-1143,-857,-572,-286,0

x0:dc.w	0
y0:dc.w	0
z0:dc.w	0
z1:dc.w	0

linxy:dc.l	0		; Address of line array

nummark:dc.w	0		; Number of points
numline:dc.w	0		; Number of lines

pointx:dc.l	0		; Variables of point arrays for world,
pointy:dc.l	0		; view, and screen coordinates
pointz:dc.l	0

xplot:dc.l	0
yplot:dc.l	0

datx:dc.l	0
daty:dc.l	0
datz:dc.l	0

sinx:dc.w	0		; Temporary storage for sin & cos
siny:dc.w	0		; values
sinz:dc.w	0

cosx:dc.w	0
cosy:dc.w	0
cosz:dc.w	0

var1:dc.w	0		; General variables
var2:dc.w	0
var3:dc.w	0

xangle:dc.w	0		; Variables for passing angles
yangle:dc.w	0		; to the rotation subroutine
zangle:dc.w	0

leftx:dc.w	0
lefty:dc.w	0
rightx:dc.w	0
righty:dc.w	0

dist:dc.w	0
zobs:dc.w	1500

rotx11:dc.w	16384		; Space here for the result matrix
rotx12:dc.w	0		; of matrix multiplication
rotx13:dc.w	0
rotx21:dc.w	0
rotx22:dc.w	16384
rotx23:dc.w	0
rotx31:dc.w	0
rotx32:dc.w	0
rotx33:dc.w	16384

matrix11:dc.w	0		; Space here for the general rotation
matrix12:dc.w	0		; matrix
matrix13:dc.w	0
matrix21:dc.w	0
matrix22:dc.w	0
matrix23:dc.w	0
matrix31:dc.w	0
matrix32:dc.w	0
matrix33:dc.w	0

DrawPlane:dc.l	0

CopList:
dc.w	$0180,$0000,$0182,$fff
dc.w	$0100,res/321*32768+$1200,$00e0
hibmptr:
dc.w	$0007,$00e2
lowbmptr:
dc.w	$0000,$0092,$0038,$0094,$00d0
dc.w	$008e,$2c81,$0090,$f4c1
dc.w	$0120
sp1h:dc.w 0,$0122
sp1l:dc.w 0,$0124
sp2h:dc.w 0,$0126
sp2l:dc.w 0,$0128
sp3h:dc.w 0,$012a
sp3l:dc.w 0,$012c,0,$012e,0,$0130,0,$0132,0,$0134,0
dc.w $0136,0,$0138,0,$013a,0,$013c,0,$013e,0
wave:
dc.w $0180,$0011,$2201,$ff00
dc.w $0180,$0022,$2401,$ff00
dc.w $0180,$0033,$2601,$ff00
dc.w $0180,$0044,$2801,$ff00
dc.w $0180,$0055,$2a01,$ff00
dc.w $0180,$0066,$2c01,$ff00
dc.w $0180,$0077,$2e01,$ff00
dc.w $0180,$0088,$4001,$ff00
dc.w $0180,$0099,$4201,$ff00
dc.w $0180,$00aa,$4401,$ff00
dc.w $0180,$00bb,$4601,$ff00
dc.w $0180,$00cc,$4801,$ff00
dc.w $0180,$00dd,$4a01,$ff00
dc.w $0180,$00ee,$4c01,$ff00
dc.w $0180,$00ff,$4e01,$ff00
dc.w $0180,$00ff,$6001,$ff00
dc.w $0180,$00ee,$6201,$ff00
dc.w $0180,$00dd,$6401,$ff00
dc.w $0180,$00cc,$6601,$ff00
dc.w $0180,$00bb,$6801,$ff00
dc.w $0180,$00aa,$6a01,$ff00
dc.w $0180,$0099,$6c01,$ff00
dc.w $0180,$0088,$6e01,$ff00
dc.w $0180,$0077,$8001,$ff00
dc.w $0180,$0066,$8201,$ff00
dc.w $0180,$0055,$8401,$ff00
dc.w $0180,$0044,$8601,$ff00
dc.w $0180,$0033,$8801,$ff00
dc.w $0180,$0022,$8a01,$ff00
dc.w $0180,$0011,$8c01,$ff00
dc.w $0180,$0000,$8e01,$ff00
dc.w $0180,$0000,$a001,$ff00
dc.w $0180,$0110,$a201,$ff00
dc.w $0180,$0220,$a401,$ff00
dc.w $0180,$0330,$a601,$ff00
dc.w $0180,$0440,$a801,$ff00
dc.w $0180,$0550,$aa01,$ff00
dc.w $0180,$0660,$ac01,$ff00
dc.w $0180,$0770,$ae01,$ff00
dc.w $0180,$0880,$c001,$ff00
dc.w $0180,$0990,$c201,$ff00
dc.w $0180,$0aa0,$c401,$ff00
dc.w $0180,$0bb0,$c601,$ff00
dc.w $0180,$0cc0,$c801,$ff00
dc.w $0180,$0dd0,$ca01,$ff00
dc.w $0180,$0ee0,$cc01,$ff00
dc.w $0180,$0ff0,$ce01,$ff00
dc.w $0180,$0ff0,$e001,$ff00
dc.w $0180,$0ee0,$e201,$ff00
dc.w $0180,$0cc0,$e401,$ff00
dc.w $0180,$0aa0,$e601,$ff00
dc.w $0180,$0880,$e801,$ff00
dc.w $0180,$0660,$ea01,$ff00
dc.w $0180,$0440,$ec01,$ff00
dc.w $0180,$0220,$ee01,$ff00
dc.w $0180,$0000
dc.w $ffdf,$fffe,$0001,$ff00
dc.w $0180,$0066,$0201,$ff00
dc.w $0180,$0055,$0401,$ff00
dc.w $0180,$0044,$0601,$ff00
dc.w $0180,$0033,$0801,$ff00
dc.w $0180,$0022,$0a01,$ff00
dc.w $0180,$0011,$0c01,$ff00
dc.w $0180,$0000,$0e01,$ff00
dc.w $0180,$0000
dc.w	$ffff,$fffe

GfxName:
dc	"graphics.library",0
even

GfxBase:dc.l	0

OldCop:dc.l	0

Main:
bsr	GetReso

bsr	MakeWrld		; Create the world system
bsr	WorldSet		; Pass the world parameters

bsr	SetRotDp		; Init obs.ref.point
bsr	PageUp
bsr	ClWork
bsr	PageDown
bsr	ClWork			; Init both pages

bsr	inp_chan		; Input and change parameters
move.w	#0,dist

mainlop1:
bsr	PointRot
bsr	Pers			; Do Perspective
bsr	DrawN1			; Draw It
bsr	PageUp			; Display It

bsr	Inp_Chan		; Input new parameters
bsr	ClWork
bsr	PointRot		; Rotate
bsr	Pers			; Perspective
bsr	DrawN1			; Drawit
bsr	PageDown
bsr	Inp_Chan		; Input Parameters
bsr	ClWork
btst	#6,$bfe001
bne.s	mainlop1

mainend:
bsr	PageUp
rts

inp_chan:
addq.w	#3,Hyangle	; geschwindigkeit
addq.w	#2,Hzangle
addq.w  #1,Hxangle
cmp.w	#360,Hxangle
blt.s   wei
sub.w	#360,Hxangle
wei:
cmp.w	#360,Hyangle
blt.s	Nosub
sub.w	#360,Hyangle
NosuB:
cmp.w	#360,hzangle
blt.s	Nosubz
sub.w	#360,Hzangle
NosuBz:
; Zoomen mit den Cursor-Tasten
cmp.w	#2000,dist
beq	cdist
cmp.w	#-2000,dist
beq	cdist
cmp.b	#$67,$bfec01
bne	weiter1
add.w	#10,Dist	
weiter1:
cmp.b	#$65,$bfec01
bne	weiter2
sub.w	#10,dist
weiter2:
;
NoClr:
;
cmp.w	#-50,f1
beq weiq
sub.w	#5,f1
weiq:
cmp.w	#-50,f2
beq weiq2
sub.w	#5,f2
weiq2:
cmp.w	#-50,f3
beq	weiq3
sub.w	#5,f3
weiq3:
cmp.w	#-50,f4
beq	weiq4
sub.w	#5,f4
weiq4:
;-------
cmp.w	#3,ha1
bne	wei1
lea	wave+2,a2
lea	wave+10,a4
move.w	(a2),d6
moveq	#54,d5
tlp:
move.w	(a4),(a2)
addq.l	#8,a2
addq.l	#8,a4
dbf	d5,tlp
move.w	d6,(a2)
clr.w	ha1
wei1:
add.w	#1,ha1
;-------
bsr	makewrld
move.w	#$00f,color
rts
cdist:
move.w	#0,dist
jmp	weiter2
ha1: dc.w 0
even
setrotdp:
moveq	#0,d1
move.w	d1,rotdpx
move.w	d1,rotdpy
move.w	d1,rotdpz
move.w	#0,hyangle		; Start rotation angle
move.w	#0,hxangle
move.w	#0,hzangle
rts
pointrot:
move.w	Hxangle,xangle	; Rotate the world around the angle
move.w	hyangle,yangle	; hxangle, hyangle, hzangle about the
move.w	hzangle,zangle
move.w	rotdpx,d0	; Rotation reference point
move.w	rotdpy,d1
move.w	rotdpz,d2
move.w	d0,xoffs
move.w	d1,yoffs
move.w	d2,zoffs	; Add for back transformation
neg.w	d0
neg.w	d1
neg.w	d2
move.w	d0,offx		; Subtract for transformation
move.w	d1,offy
move.w	d2,offz
bsr	Matinit
bsr	zrotate
bsr	yrotate
bsr	xrotate
bsr	rotate
move.w	#$fff,color
rts

Makewrld:
move.l	#ObjDatx,a1		; Create the world system by
move.l	#ObjDaty,a2
move.l	#ObjDatz,a3
move.l	#worldx,a4
move.l	#worldy,a5
move.l	#worldz,a6
move.w	hnummark,d0
ext.l	d0
subq.l	#1,d0
makewl1:
move.w	(a1)+,(a4)+		; Copying the object data into the
move.w	(a2)+,(a5)+		; world data
move.w	(a3)+,(a6)+
dbra	d0,makewl1
move.w	hnumline,d0
ext.l	d0
subq.l	#1,d0
move.l	#ObjLin,a1
move.l	#wlinxy,a2
makewl2:
move.l	(a1)+,(a2)+
dbra	d0,makewl2
rts

worldset:
move.l	#Worldx,datx		; Pass variables for rotation routine
move.l	#worldy,daty
move.l	#Worldz,datz
move.l	#viewx,pointx
move.l	#viewy,pointy
move.l	#viewz,pointz
move.l	#wlinxy,linxy
move.w	PictureX,x0
move.w	Picturey,Y0
move.w	proz,zobs
move.w	r1z1,dist
move.l	#screenx,xplot
move.l	#screeny,yplot
move.w	hnumline,numline
move.w	hnummark,nummark
rts

getReso:
move.w	#res/2,PictureX
move.w	#100,pictureY
rts
;
; OBJEKT DEFINITION 
;

ObjDatx:
dc.w	-25,30,30,-25
dc.w	-25,30,30,-25

ObjDaty:
dc.w	-25,-25,20,20
dc.w	-25,-25,20,20

ObjDatz:
dc.w	0,0,00,00
f1:dc.w	5
f2:dc.w 5
f3:dc.w 5
f4:dc.w 5

***** WELCHE PUNKTE SOLLEN VERBUNDEN WERDEN ****

ObjLin:
dc.w	1,2,2,3,3,4,4,1
dc.w	5,6,6,7,7,8,8,5
dc.w	1,5,2,6,3,7,4,8
dc.w    5,7,6,8,1,3,2,4
hnummark:dc.w	8	; Anzahl der Gesamt PUNKTE
hnumline:dc.w	16	; Anzahl der Linien

hxangle:dc.w	0	; Rotation angle of the object around X axis
hyangle:dc.w	0
hzangle:dc.w	0

xwplus:dc.w	10	; Angle increment around x axis
ywplus:dc.w	0
zwplus:dc.w	0

picturex:dc.w	res
picturey:dc.w	100	; Origin location on screen

rotdpx:dc.w	0
rotdpy:dc.w	0
rotdpz:dc.w	0	; Rotation datum point

r1z1:dc.w	0
normz:dc.w	1500

plusrot:dc.l	0
first:dc.l	0
second:dc.w	0
delta1:dc.w	0

flag:dc.b	1
even

diffz:dc.w	0
dx:dc.w		0
dy:dc.w		0
dz:dc.w		0

worldx:blk.w	100,0	; World coordinate array
worldy:blk.w	100,0
worldz:blk.w	100,0

viewx:blk.w	100,0	; View coordinate array
viewy:blk.w	100,0
viewz:blk.w	100,0

screenx:blk.w	100,0	; Display coordinate array
screeny:blk.w	100,0

wlinxy:blk.l	300,0	; Line array

prox:dc.w	2000	; Coordinates of the projection center
proy:dc.w	20	; on the positive Z-axis
proz:dc.w	2000

offx:dc.w	0
offy:dc.w	0
offz:dc.w	0
xoffs:dc.w	0
yoffs:dc.w	0
zoffs:dc.w	0

loopc:dc.l	0
end:
;
start_muzak:
	move.l	#$50000,muzakoffset	;** get offset

init0:	move.l	muzakoffset,a0		;** get highest used pattern
	add.l	#472,a0
	move.l	#$80,d0
	clr.l	d1
init1:	move.l	d1,d2
	subq.w	#1,d0
init2:	move.b	(a0)+,d1
	cmp.b	d2,d1
	bgt.s	init1
	dbf	d0,init2
	addq.b	#1,d2

init3:	move.l	muzakoffset,a0		;** calc samplepointers
	lea	pointers(pc),a1
	lsl.l	#8,d2
	lsl.l	#2,d2
	add.l	#600,d2
	add.l	a0,d2
	moveq	#14,d0
init4:	move.l	d2,(a1)+
	clr.l	d1
	move.w	42(a0),d1
	lsl.l	#1,d1
	add.l	d1,d2
	add.l	#30,a0
	dbf	d0,init4

init5:	clr.w	$dff0a8			;** clear used values
	clr.w	$dff0b8
	clr.w	$dff0c8
	clr.w	$dff0d8
	clr.w	timpos
	clr.l	trkpos
	clr.l	patpos

init6:	move.l	muzakoffset,a0		;** initialize timer irq
	move.b	470(a0),numpat+1	;number of patterns
	move.l	$6c.w,lev3save+2
	move.l	#lev3interrupt,$6c.w
	rts

stop_muzak:
	move.l	lev3save+2,$6c.w
	clr.w	$dff0a8
	clr.w	$dff0b8
	clr.w	$dff0c8
	clr.w	$dff0d8
	move.w	#$f,$dff096
	rts

lev3interrupt:
	bsr.s	replay_muzak
lev3save:
	jmp	$0

replay_muzak:
	movem.l	d0-d7/a0-a6,-(a7)
	addq.w	#1,timpos
speed:	cmp.w	#6,timpos
	beq.L	replaystep

chaneleffects:				;** seek effects
	lea	datach0(pc),a6
	tst.b	3(a6)
	beq.s	ceff1
	lea	$dff0a0,a5
	bsr.s	ceff5
ceff1:	lea	datach1(pc),a6
	tst.b	3(a6)
	beq.s	ceff2
	lea	$dff0b0,a5
	bsr.s	ceff5
ceff2:	lea	datach2(pc),a6
	tst.b	3(a6)
	beq.s	ceff3
	lea	$dff0c0,a5
	bsr.s	ceff5
ceff3:	lea	datach3(pc),a6
	tst.b	3(a6)
	beq.s	ceff4
	lea	$dff0d0,a5
	bsr.s	ceff5
ceff4:	movem.l	(a7)+,d0-d7/a0-a6
	rts

ceff5:	move.b	2(a6),d0		;room for some more
	and.b	#$f,d0			;implementations below
	tst.b	d0
	beq.s	arpreggiato
	cmp.b	#1,d0
	beq.L	pitchup
	cmp.b	#2,d0
	beq.L	pitchdown
	cmp.b	#12,d0
	beq.L	setvol
	cmp.b	#14,d0
	beq.L	setfilt
	cmp.b	#15,d0
	beq.L	setspeed
	rts

arpreggiato:				;** spread by time
	cmp.w	#1,timpos
	beq.s	arp1
	cmp.w	#2,timpos
	beq.s	arp2
	cmp.w	#3,timpos
	beq.s	arp3
	cmp.w	#4,timpos
	beq.s	arp1
	cmp.w	#5,timpos
	beq.s	arp2
	rts

arp1:	clr.l	d0			;** get higher note-values
	move.b	3(a6),d0		;   or play original
	lsr.b	#4,d0
	bra.s	arp4
arp2:	clr.l	d0
	move.b	3(a6),d0
	and.b	#$f,d0
	bra.s	arp4
arp3:	move.w	16(a6),d2
	bra.s	arp6
arp4:	lsl.w	#1,d0
	clr.l	d1
	move.w	16(a6),d1
	lea	notetable,a0
arp5:	move.w	(a0,d0.w),d2
	cmp.w	(a0),d1
	beq.s	arp6
	addq.l	#2,a0
	bra.s	arp5
arp6:	move.w	d2,6(a5)
	rts

pitchdown:
	bsr.s	newrou
	clr.l	d0
	move.b	3(a6),d0
	and.b	#$f,d0
	add.w	d0,(a4)
	cmp.w	#$358,(a4)
	bmi.s	ok1
	move.w	#$358,(a4)
ok1:	move.w	(a4),6(a5)
	rts

pitchup:bsr.s	newrou
	clr.l	d0
	move.b	3(a6),d0
	and.b	#$f,d0
	sub.w	d0,(a4)
	cmp.w	#$71,(a4)
	bpl.s	ok2
	move.w	#$71,(a4)
ok2:	move.w	(a4),6(a5)
	rts

setvol:	move.b	3(a6),8(a5)
	rts

setfilt:move.b	3(a6),d0
	and.b	#1,d0
	lsl.b	#1,d0
	and.b	#$fd,$bfe001
	or.b	d0,$bfe001
	rts

setspeed:
	clr.l	d0
	move.b	3(a6),d0
	and.b	#$f,d0
	move.w	d0,speed+2
	rts

newrou:	cmp.l	#datach0,a6
	bne.s	next1
	lea	voi1(pc),a4
move.w	#60,f1
	rts
next1:	cmp.l	#datach1,a6
	bne.s	next2
	lea	voi2(pc),a4
move.w	#60,f2
	rts
next2:	cmp.l	#datach2,a6
	bne.s	next3
	lea	voi3(pc),a4
move.w	#60,f3
	rts
next3:	lea	voi4(pc),a4
move.w	#60,f4
	rts

replaystep:				;** work next pattern-step
	clr.w	timpos
	move.l	muzakoffset,a0
	move.l	a0,a3
	add.l	#12,a3			;ptr to soundprefs
	move.l	a0,a2
	add.l	#472,a2			;ptr to pattern-table
	add.l	#600,a0			;ptr to first pattern
	clr.l	d1
	move.l	trkpos,d0		;get ptr to current pattern
	move.b	(a2,d0),d1
	lsl.l	#8,d1
	lsl.l	#2,d1
	add.l	patpos,d1		;get ptr to current step
	clr.w	enbits
	lea	$dff0a0,a5		;chanel 0
	lea	datach0(pc),a6
	bsr.L	chanelhandler
	lea	$dff0b0,a5		;chanel 1
	lea	datach1(pc),a6
	bsr.L	chanelhandler
	lea	$dff0c0,a5		;chanel 2
	lea	datach2(pc),a6
	bsr.L	chanelhandler
	lea	$dff0d0,a5		;chanel 3
	lea	datach3(pc),a6
	bsr.L	chanelhandler
	move.w	#400,d0			;** wait a while and set len
rep1:	dbf	d0,rep1			;   of oneshot to 1 word
	move.w	#$8000,d0
	or.w	enbits,d0
	move.w	d0,$dff096
	cmp.w	#1,datach0+14
	bne.s	rep2
	clr.w	datach0+14
	move.w	#1,$dff0a4
rep2:	cmp.w	#1,datach1+14
	bne.s	rep3
	clr.w	datach1+14
	move.w	#1,$dff0b4
rep3:	cmp.w	#1,datach2+14
	bne.s	rep4
	clr.w	datach2+14
	move.w	#1,$dff0c4
rep4:	cmp.w	#1,datach3+14
	bne.s	rep5
	clr.w	datach3+14
	move.w	#1,$dff0d4

rep5:	add.l	#16,patpos		;next step
	cmp.l	#64*16,patpos		;pattern finished ?
	bne.s	rep6
	clr.l	patpos
	addq.l	#1,trkpos		;next pattern in table
	clr.l	d0
	move.w	numpat,d0
	cmp.l	trkpos,d0		;song finished ?
	bne.s	rep6
	clr.l	trkpos
rep6:	movem.l	(a7)+,d0-d7/a0-a6
	rts

chanelhandler:
	move.l	(a0,d1.l),(a6)		;get period & action-word
	addq.l	#4,d1			;point to next chanel
	clr.l	d2
	move.b	2(a6),d2		;get nibble for soundnumber
	lsr.b	#4,d2
	beq.s	chan2			;no soundchange !
	move.l	d2,d4			;** calc ptr to sample
	lsl.l	#2,d2
	mulu	#30,d4
	lea	pointers-4(pc),a1
	move.l	(a1,d2.l),4(a6)		;store sample-address
	move.w	(a3,d4.l),8(a6)		;store sample-len in words
	move.w	2(a3,d4.l),18(a6)	;store sample-volume

	move.l	d0,-(a7)
	move.b	2(a6),d0
	and.b	#$f,d0
	cmp.b	#$c,d0
	bne.s	ok3
	move.b	3(a6),8(a5)
	bra.s	ok4
ok3:	move.w	2(a3,d4.l),8(a5)	;change chanel-volume
ok4:	move.l	(a7)+,d0

	clr.l	d3
	move.w	4(a3,d4),d3		;** calc repeatstart
	add.l	4(a6),d3
	move.l	d3,10(a6)		;store repeatstart
	move.w	6(a3,d4),14(a6)		;store repeatlength
	cmp.w	#1,14(a6)
	beq.s	chan2			;no sustainsound !
	move.l	10(a6),4(a6)		;repstart  = sndstart
	move.w	6(a3,d4),8(a6)		;replength = sndlength
chan2:	tst.w	(a6)
	beq.s	chan4			;no new note set !
	move.w	22(a6),$dff096		;clear dma
	tst.w	14(a6)
	bne.s	chan3			;no oneshot-sample
	move.w	#1,14(a6)		;allow resume (later)
chan3:	bsr.L	newrou
	move.w	(a6),(a4)
	move.w	(a6),16(a6)		;save note for effect
	move.l	4(a6),0(a5)		;set samplestart
	move.w	8(a6),4(a5)		;set samplelength
	move.w	(a6),6(a5)		;set period
	move.w	22(a6),d0
	or.w	d0,enbits		;store dma-bit
	move.w	18(a6),20(a6)		;volume trigger
chan4:	rts

datach0:	blk.w	11,0
		dc.w	1
datach1:	blk.w	11,0
		dc.w	2
datach2:	blk.w	11,0
		dc.w	4
datach3:	blk.w	11,0
		dc.w	8
voi1:		dc.w	0
voi2:		dc.w	0
voi3:		dc.w	0
voi4:		dc.w	0
pointers:	blk.l	15,0
notetable:	dc.w	856,808,762,720,678,640,604,570
		dc.w	538,508,480,453,428,404,381,360
		dc.w	339,320,302,285,269,254,240,226  
		dc.w	214,202,190,180,170,160,151,143
		dc.w	135,127,120,113,000
muzakoffset:	dc.l	0
trkpos:		dc.l	0
patpos:		dc.l	0
numpat:		dc.w	0
enbits:		dc.w	0
timpos:		dc.w	0
;
;
