	Incdir	Df1:
	Include Source/CustomRegisters
	Section	ThreeD,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	#2,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
	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

	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
	Muls	$40(a1,d6),d0
	Muls	-$40(a1,d6),d1
	Sub.l	d1,d0
	Lsl.l	d0
	Swap	d0		;d0 holds intermediate x coord
	Muls	-$40(a1,d6),d2
	Muls	$40(a1,d6),d3
	Add.l	d3,d2
	Lsl.l	d2
	Swap	d2		;d2 holds intermediate y coord
	Move	d2,d4

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

	Muls	$40(a1,d6),d3
	Muls	-$40(a1,d6),d4
	Add.l	d4,d3
	Lsl.l	d3
	Swap	d3		;d3 holds intermediate z coord

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

	Add	#145,d3
	And	#$fffe,d3
	Move	(a3,d3),d5
	Muls	d5,d1
	Muls	d5,d2
	Lsl.l	d1
	Swap	d1
	Add	#160,d1
	Lsl.l	d2
	Swap	d2
	Add	#128,d2
	
	Move	d1,(a2)+
	Move	d2,(a2)+
	Dbf	d7,TD_loop

	Move	#No_connects-1,d7
	Lea	$dff000,a5
	Lea	Connect(pc),a3
	Lea	Rotated_coords(pc),a4
	Move.l	#40,a1
	Move.l	#$ffff,a2
B_wait2
	Btst	#14,Dmaconr
	Bne.s	B_wait2

	Move	#$ffff,Bltafwm
	Move	a1,$60(a5)	;Bltcmod
	Move	a1,$66(a5)	;Bltdmod
	Move	a2,$72(a5)	;Bltbdat
Draw_loop
	Move	(a3)+,d6
	Move	(a4,d6),d0
	Move	2(a4,d6),d1
	Move	(a3)+,d6
	Move	(a4,d6),d2
	Move	2(a4,d6),d3
	Cmp	d0,d2
	Bne.s	Draw
	Cmp	d1,d3
	Beq.s	Nodraw	
Draw
	Bsr	Line
Nodraw
	Dbf	d7,Draw_loop
Bwit
	Btst	#14,Dmaconr
	Bne.s	Bwit
	Rts

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

	Clr.l	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
	And	#$000f,d0
	Ror.w	#4,d0
	Or	#$0bca,d0
	Move	d0,$40(a5)	;Bltcon0
	Move	d5,$42(a5)	;Bltcon1
	Move.l	d4,$48(a5)	;Bltcpth
	Move.l	d4,$54(a5)	;Bltdpth
	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

;---------- 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 = 15
No_connects = 19

Points
	Dc.w	0,80,0
	Dc.w	0,50,0
	Dc.w	20,30,0
	Dc.w	55,-5,0
	Dc.w	45,-15,0
	Dc.w	35,-15,0
	Dc.w	25,-20,-15
	Dc.w	-25,-20,-15
	Dc.w	-35,-15,0
	Dc.w	-45,-15,0
	Dc.w	-55,-5,0
	Dc.w	-20,30,0
	Dc.w	27,-12,10
	Dc.w	-27,-12,10
	Dc.w	0,-30,-15
Connect
	Dc.w	0,4
	Dc.w	4,8
	Dc.w	8,12
	Dc.w	12,16
	Dc.w	16,20
	Dc.w	20,24
	Dc.w	24,28
	Dc.w	28,32
	Dc.w	32,36
	Dc.w	36,40
	Dc.w	40,44
	Dc.w	44,4

	Dc.w	20,48
	Dc.w	48,52
	Dc.w	52,32

	Dc.w	48,4
	Dc.w	52,4
	Dc.w	24,4
	Dc.w	28,4

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

