
	opt	o+,ow-	get the most out of Devpac

;                                                             
;              This program plots 3 sine curves                 
;  It uses a table of sine values 'borrowed' from a routine     
;  written by Marcus Lynn (Shadow) for Amiga Computing. Cheers  
;  Marcus, thats a lot of keyboard bashing. This program uses   
;  direct programming of the hardware to set up bit plane.      
; 			                
; 		MARK, 2.2.1990                 
; 			                
;                                                               


start

; Reserve memory for copper list and one bit plane

	move.l	execbase,a6	a6-->exec lib
	move.l	#40*256,d0	d0=size of bitplane
	moveq.l	#chip,d1	make sure its chip ram
	jsr	allocmem(a6)	get memory
	move.l	d0,bpl1	save pointer
	beq	quit_fast	leave if error
	move.l	#80,d0	d0=size of copper list
	moveq.l	#chip,d1	make sure its chip ram
	jsr	allocmem(a6)	get memory
	move.l	d0,clist	save pointer
	beq	quit	leave if error
	
; Build copper list

	move.l	d0,a0	a0-->copper list
	move.l	#bpl1pth,d1	d1-->DMA register
	move.w	d1,(a0)+	write into copper list
	move.l	bpl1,d0	d0-->bit plane
	swap	d0	get MSB of address
	move.w	d0,(a0)+	write into copper list
	move.l	#bpl1ptl,d1	d1-->DMA register
	move.w	d1,(a0)+	write into copper list
	swap	d0	get LSB of address
	move.w	d0,(a0)+	write into copper list
	move.l	#$fffffffe,d0	end of copper list
	move.l	d0,(a0)	write into copper list
	
; DMA off and o/s out

	jsr	forbid(a6)	switch out o/s
	lea	$dff000,a5	a5-->1st DMA register
	move.w	#$01e0,dmacon(a5) stop DMA
	
; Initialise copper list

	move.l	clist,cop1lc(a5) copper DMA-->our list
	clr.w	copjmp1(a5)	 start our list
	
; Initialise bitplane

	move.w	#$3081,diwstrt(a5) start of our screen
	move.w	#$30c1,diwstop(a5) end of our screen
	move.w	#$0038,ddfstrt(a5) start of DMA
	move.w	#$00d0,ddfstop(a5) end of DMA
	move.w	#%0001001000000000,bplcon0(a5) 1 bitplane
	clr.w	bplcon1(a5)	no scroll
	clr.w	bplcon2(a5)	no priority
	clr.w	bpl1mod(a5)	no modulo
	clr.w	bpl2mod(a5)	no modulo
	move.w	#$8380,dmacon(a5)   enable DMA
	
; Clear this bit plane

	move.l	bpl1,a0	a0-->bitmap
	move.w	#(10*256)-1,d2	d2=counter
	moveq.l	#0,d0	use d0 to clr bitmap
clrloop	move.l	d0,(a0)+	blank next piece
	dbra	d2,clrloop	loop back until finished

; Draw the sine trace

	moveq.l	#1,d3	init unplot x-coord
	moveq.l	#50,d4	init plot x-coord
sinloop	move.l	vposr(a5),d0	d0=beam position
	and.l	#$0001ff00,d0	mask off line number
	cmp.l	#$00001000,d0	is it line 16 ?
	bne	sinloop	if not loop back
	move.l	d3,d0	d0=x-coord
	bsr	calc	calculate point
	move.w	d2,d3	update x-coord
	bsr	unplot	unplot this point
	move.l	d4,d0	d0=x-coord
	bsr	calc	calculate point
	move.w	d2,d4	update x-coord
	bsr	plot	plot this point
	btst	#6,ciaapra	mouse button pressed?
	bne	sinloop	loop back if not
	

; Activate system copper list

	move.l	#grname,a1	a1-->library name
	moveq.l	#0,d0	any version
	jsr	openlibrary(a6) open graphics lib
	move.l	d0,a4	a4-->graphics lib
	move.l	startlist(a4),cop1lc(a5) DMA-->sys list
	clr.w	copjmp1(a5)	start sys list
	move.w	#$83e0,dmacon(a5) enable all DMA
	jsr	permit(a6)	bring back o/s
	
; Give reserved memory back to system

	move.l	clist,a1	a1-->mem to free
	move.l	#80,d0	d0=size of mem chunk
	jsr	freemem(a6)	free it
	
quit	move.l	bpl1,a1	a1-->mem to free
	move.l	#40*256,d0	d0=size of mem chunk
	jsr	freemem(a6)	free it
	
quit_fast	rts

; Subroutines

calc	move.w	d0,d2	copy x-coord
	asl.w	#1,d0	double it
	bsr	GETSIN	get SIN(x)
	muls.w	#40,d1	scale y-coord
	divs	#16384,d1	INT y-coord
	addi.w	#100,d1	add y offset
	move.w	d2,d0	d0=x-coord
	addi.w	#50,d0	add x offset
	addi.w	#1,d2	increase x-coord
	cmpi.w	#180,d2	max x-coord ?
	bls.s	.ok	if not dont worry
	moveq.l	#1,d2	else set x-coord to min
.ok	rts		all done !

plot	move.l	bpl1,a0	a0-->bitmap
	mulu.w	#40,d1	d1=addr of start of line
	divu.w	#8,d0	calculate offset 
	add.w	d0,d1	add this to line addr
	and.l	#$ffff,d1	mask off LSW
	adda.l	d1,a0	add disp to addr of bitmap
	swap	d0	get bit offset in byte
	move.b	#128,d2	d2=10000000 binary
	lsr.b	d0,d2	shift bit to correct poss
	or.b	d2,(a0)	plot point
	rts

unplot	move.l	bpl1,a0	a0-->bitmap
	mulu.w	#40,d1	d1=addr of start of line
	divu.w	#8,d0	calculate offset 
	add.w	d0,d1	add this to line addr
	and.l	#$ffff,d1	mask off LSW
	adda.l	d1,a0	add disp to addr of bitmap
	swap	d0	get bit offset in byte
	move.b	#127,d2	d2=01111111 binary
	ror.b	d0,d2	shift bit to correct poss
	and.b	d2,(a0)	plot point
	rts
                             
; Get Sin Value for Angle in d0      	

GETSIN:	MOVE.L 	#SINTAB,A1	A1-->sine table
	TST 	D0	is angle -ve
	BPL 	SIN1	if not dont worry
	ADD 	#360,D0	otherwise shift it
SIN1:	LSL 	#1,D0	table entries are words
	MOVE 	(A1,D0),D1	fetch sine D0 into D1
	RTS		and return
;                                                                
;                     Data for Sintable		 
; 	As using Floating Point Numbers would	 
; 	Be Tooooo Slow for the Computer to	 
; 	attain a suitable frame rate the values	 
; 	are first all multiplied by 16384	 
; 	which just happens to be 14 shifts	 
; 	left  (very helpfull) which gets	 
; 	rid of the decimal point and then	 
; 	when multiplied with X or Y are then	 
; 	Divided by 16384 (14 shifts right)	 
; 		Using this technique fast	 
; 	3d graphics are be possible.		 

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,14968,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,16384
	DC.W 16382
	DC.W 16374,16362,16344,16322,16294,16262,16225,16182
	DC.W 16135,16083,16026,15964,15897,15826,15749,15668	
	DC.W 15582,15491,15396,15296,15191,15082,14967,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,-14968,-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,-16384
	DC.W -16382
	DC.W -16374,-16362,-16344,-16322,-16294,-16262,-16225,-16182
	DC.W -16135,-16083,-16026,-15964,-15897,-15826,-15749,-15668	
	DC.W -15582,-15491,-15396,-15296,-15191,-15082,-14967,-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

; Program variable area

clist	dc.l	0
bpl1	dc.l	0
grname	dc.b	'graphics.library',0
	even

;CHIP REGISTERS

intena	=	$9a
intreq	=	$1e
dmacon	=	$96
dmaconr	=	$2
color00	=	$180
vhposr	=	$6
vposr	=	$4

;COPPER REGISTERS

cop1lc	=	$80
cop2lc	=	$84
copjmp1	=	$88
copjmp2	=	$8a

;BITPLANE REGISTERS

bplcon0	=	$100
bplcon1	=	$102
bplcon2	=	$104
bpl1pth	=	$0e0
bpl1ptl	=	$0e2
bpl1mod	=	$108
bpl2mod	=	$10a
diwstrt	=	$08e
diwstop	=	$090
ddfstrt	=	$092
ddfstop	=	$094

;BLITTER REGISTERS

bltcon0	=	$40
bltcon1	=	$42
bltcpth	=	$48
bltcptl	=	$4a
bltbpth	=	$4c
bltbptl	=	$4e
bltapth	=	$50
bltaptl	=	$52
bltdpth	=	$54
bltdptl	=	$56
bltcmod	=	$60
bltbmod	=	$62
bltamod	=	$64
bltdmod	=	$66
bltsize	=	$58
bltcdat	=	$70
bltbdat	=	$72
bltadat	=	$74
bltafwm	=	$44
bltalwm	=	$46

;EXEC LIB. OFFSETS

openlibrary	=	-30-522
forbid	=	-30-102
permit	=	-30-108
allocmem	=	-30-168
freemem	=	-30-180

;GRAPHICS LIB. OFFSETS

startlist	=	38

;OTHER STUFF

execbase	=	$4
chip	=	$2
ciaapra	=	$bfe001

