	Incdir	Df1:
	Include Source/CustomRegisters
	Section	Hidden,Code_c

Ciaapra = $BFE001
Openlibrary = -30-522	
Disable	    = -120
Enable	    = -126
Startlist   = 38
Execbase = 4

	Move.l	Execbase,a6
	Jsr	Disable(a6)

;-------- Switch Copper DMA ----------

	Move.l	#Clstart,Cop1lc
	Clr	Copjmp1

	Move.w	#$8780,Dmacon
	Move.w	#$0020,Dmacon
	Lea	Spr0data,a0
	Moveq	#7,d0
Clop
	Clr.l	(a0)
	Addq.l	#8,a0
	Dbf	d0,Clop

;------ Wait for Vertical position 1 --------

Wait:	Move.l	$dff004,d2
	And.l	#$0001ff00,d2	
	Cmp.l	#$00000100,d2
	Bne.s	Wait

	Addq	#2,Zrot
	And	#$1fe,Zrot
	Addq	#4,Yrot
	And	#$1fe,Yrot
	Subq	#2,Xrot
	And	#$1fe,Xrot
	Bsr	TD_rout

	Btst	#6,Ciaapra
	Bne	Wait

;------ Restore old Copper list -----------

	Move.l	Execbase,a6
	Move.l	#Grname,a1
	Clr.l	d0
	Jsr	Openlibrary(a6)
	Move.l	d0,a4
	Move.l	Startlist(a4),Cop1lc
	Clr.w	Copjmp1
	Move.w	#$83E0,Dmacon
	Jsr	Enable(a6)
	Clr.l	d0
	Rts

;------------ 3D graphics ---------------
TD_rout
;---------- Clear the current workspace

	Move.l	Current(pc),d0
	Move	d0,Screen+2
	Swap	d0
	Move	d0,Screen+6
	Eor.l	#$3000,Current
	Move.l	Current(pc),a0
	Move.l	#$1f00000,Bltcon0
	Move.l	a0,Bltdpth
	Clr	Bltadat
	Clr	Bltdmod
	Move	#256*64+20,Bltsize

;---------- Perform the 3D calculations

	Move	#No_points-1,d7
	Lea	Points(pc),a4	
	Lea	Sintable+$40(pc),a1
	Lea	Rotated_coords(pc),a2
	Lea	Perspective(pc),a3
TD_loop
	Move	(a4)+,d0
	Move	d0,d2
	Move	(a4)+,d1
	Move	d1,d3

	Move	Zrot(pc),d6
	Move	$40(a1,d6),d4
	Move	-$40(a1,d6),d5
	Muls	d4,d0
	Muls	d5,d1
	Sub.l	d1,d0
	Add.l	d0,d0
	Swap	d0		;d0 holds intermediate x coord
	Muls	d5,d2
	Muls	d4,d3
	Add.l	d3,d2
	Add.l	d2,d2
	Swap	d2		;d2 holds intermediate y coord
	Move	d2,d4

	Move	(a4)+,d1	;z coord
	Move	d1,d3
	Move	Xrot(pc),d6
	Move	$40(a1,d6),d5
	Move	-$40(a1,d6),d6
	Muls	d5,d2
	Muls	d6,d1
	Sub.l	d1,d2
	Add.l	d2,d2
	Swap	d2		;d2 holds the final y coord
	Muls	d5,d3
	Muls	d6,d4
	Add.l	d4,d3
	Add.l	d3,d3
	Swap	d3		;d3 holds intermediate z coord

	Move	d0,d1
	Move	d3,d4
	Move	Yrot(pc),d6
	Move	$40(a1,d6),d5
	Move	-$40(a1,d6),d6
	Muls	d5,d3
	Muls	d6,d0
	Sub.l	d0,d3
	Add.l	d3,d3
	Swap	d3		;d3 holds the final z coord
	Muls	d6,d4
	Muls	d5,d1
	Add.l	d4,d1
	Add.l	d1,d1
	Swap	d1		;d1 holds the final x coord

	Add	Depth(pc),d3
	Add	d3,d3
	Move	(a3,d3),d5
	Muls	d5,d1
	Muls	d5,d2
	Add.l	d1,d1
	Swap	d1
	Add	#160,d1
	Add.l	d2,d2
	Swap	d2
	Add	#128,d2
	
	Move	d1,(a2)+
	Move	d2,(a2)+
	Dbf	d7,TD_loop

;---------- Plot the surfaces (lines)

	Move	#No_connects-1,d7
	Lea	$dff000,a5
	Moveq	#40,d0
	Lea	Bits(pc),a1
	Lea	Connect(pc),a2
	Lea	Rotated_coords(pc),a3

	Lea	Faces(pc),a6
B_wait2
	Btst	#14,Dmaconr
	Bne.s	B_wait2

	Move	#$ffff,Bltafwm
	Move	d0,$60(a5)	;Bltcmod
	Move	d0,$66(a5)	;Bltdmod
	Move	#$ffff,$72(a5)	;Bltbdat

	Move	#No_faces,Face
Draw_loop1
	Move.l	(a6)+,d7
	Move.l	(a6)+,a4
	Move	(a4),d6
	Move	(a2,d6),d4
	Move	2(a2,d6),d5
	Move	(a3,d4),d0
	Sub	(a3,d5),d0
	Move	2(a3,d4),d1
	Sub	2(a3,d5),d1

	Move	2(a4),d6
	Move	(a2,d6),d4
	Move	2(a2,d6),d5
	Move	(a3,d4),d2
	Sub	(a3,d5),d2
	Move	2(a3,d4),d3
	Sub	2(a3,d5),d3

	Muls	d3,d0
	Muls	d2,d1

	Cmp	d0,d1
	Blt.s	Nodraw2

Draw_loop2
	Move	(a4)+,d5
	Move	(a2,d5),d6
	Move	(a3,d6),d0
	Move	2(a3,d6),d1

	Move	2(a2,d5),d6
	Move	(a3,d6),d2
	Move	2(a3,d6),d3
	Cmp	d0,d2
	Bne.s	Draw
	Cmp	d1,d3
	Beq.s	Nodraw1
Draw
	Bsr	Line
Nodraw1
	Dbf	d7,Draw_loop2
Nodraw2
	Subq	#1,Face
	Tst	Face
	Bne	Draw_loop1
Bwit
	Btst	#14,Dmaconr
	Bne.s	Bwit
	Rts

;----------- Line Draw ------------
Line
	Moveq	#40,d4
	Mulu	d1,d4
	Moveq	#-$10,d5
	And	d0,d5
	Lsr	#3,d5
	Add	d5,d4
	Add.l	a0,d4

	Moveq	#0,d5
	Sub	d1,d3
	Roxl.b	d5
	Tst	d3
	Bge.s	Y2gy1
	Neg	d3
Y2gy1
	Sub	d0,d2
	Roxl.b	d5
	Tst	d2
	Bge.s	X2gx1
	Neg	d2
X2gx1

	Move	d3,d1
	Sub	d2,d1
	Bge.s	Dygdx
	Exg	d2,d3
Dygdx
	Roxl.b	d5
	Move.b	Octant_table(pc,d5),d5
	Add	d2,d2
Wblit
	Btst	#14,Dmaconr
	Bne.s	Wblit

	Move	d2,$62(a5)	;Bltbmod
	Sub	d3,d2
	Bge.s	Signn1
	Or.b	#$40,d5
Signn1
	Move	d2,$52(a5)	;Bltaptl
	Sub	d3,d2
	Move	d2,$64(a5)	;Bltamod

	Move	#$8000,$74(a5)	;Bltadat
	Add	d0,d0
	Move	(a1,d0),$40(a5)	;Bltcon0
	Move	d5,$42(a5)	;Bltcon1
	Move.l	d4,$48(a5)	;Bltcpth
	Move.l	d4,$54(a5)	;Bltdpth
	Addq	#1,d3
	Lsl	#6,d3
	Addq	#2,d3
	Move	d3,$58(a5)	;Bltsize
	Rts

;---------- Constants -------------

Octant_table
	Dc.b	1,17,9,21,5,25,13,29

Grname:	Dc.b	"graphics.library",0

	Even

;----------- Variables ------------

Xrot	Dc.w	$100
Yrot	Dc.w	0
Zrot	Dc.w	0
Current	Dc.l	$70000
Depth	Dc.w	180
Depthpt	Dc.w	0
Face	Dc.w	0

;---------- Copperlists -----------
Clstart:
	Wait	0,20
	Mov	$1a81,Diwstrt
	Mov	$39c1,Diwstop
	Mov	$0038,Ddfstrt
	Mov	$00d0,Diwstop
Screen
	Mov	0,Bpl1ptl
	Mov	7,Bpl1pth
	Mov	$1200,Bplcon0
	Mov	0,Bpl1mod
	Mov	0,Color00
	Mov	$fff,Color01
	Wait	224,255
	Wait	$fe,$ff

;--------- Binaries -------------
No_points	= 6
No_connects	= 12
No_faces	= 8

Points
	Dc.w	100,100,0
	Dc.w	-100,100,0
	Dc.w	-100,-100,0
	Dc.w	100,-100,0
	Dc.w	0,0,150
	Dc.w	0,0,-150

Connect
	Dc.w	0,4
	Dc.w	4,8
	Dc.w	8,12
	Dc.w	12,0

	Dc.w	0,16
	Dc.w	4,16
	Dc.w	8,16
	Dc.w	12,16

	Dc.w	0,20
	Dc.w	4,20
	Dc.w	8,20
	Dc.w	12,20

Faces
	Dc.l	2,Face1
	Dc.l	2,Face2
	Dc.l	2,Face3
	Dc.l	2,Face4
	Dc.l	2,Face5
	Dc.l	2,Face6
	Dc.l	2,Face7
	Dc.l	2,Face8

Face1	Dc.w	0,20,16

Face2	Dc.w	4,24,20

Face3	Dc.w	8,28,24

Face4	Dc.w	12,16,28

Face5	Dc.w	36,0,32

Face6	Dc.w	40,4,36

Face7	Dc.w	44,8,40

Face8	Dc.w	32,12,44

Rotated_coords	
	Dcb.w	No_points*2,0
Sintable
	Incbin	Binary/Sin
	Incbin	Binary/Sin
Perspective
	Incbin	Binary/Perspective

Mul40
A set 0
	Rept	320
	Dc.w	A*40
A set A+1
	Endr

A set 0
Bits
	Rept	320
	Dc.w	((A&$f)*$1000)+$bca
A set A+1
	Endr

Size
A set 0
	Rept	320
	Dc.w	(A*64)+2
A set A+1
	Endr

