	opt	o+,ow-

; This program pulls together some of the routines found in this drawer.

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	fill bp with 0's
	jsr	allocmem(a6)	get memory
	move.l	d0,bpl1	save pointer
	addi.l	#40,d0
	move.l	d0,scrnpoint
	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
	lea	bars,a1	a1-->copper bar list
	moveq.l	#13,d0	d0=lines in list
pnextln	move.l	(a1)+,(a0)+	copy next line
	dbra	d0,pnextln	until all done
	
; 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	#60,d5	d5=line counter
	moveq.l	#2,d6	d6=direction
	move.l	clist,a3	a3-->copperlist
	moveq.l	#1,d3	init unplot x-coord
	moveq.l	#10,d4	init plot x-coord
	move.l	#MSG,msgpoint
	bsr	printchar
	move.l	#7,scrlcount
sinloop	move.l	vposr(a5),d2
	and.l	#$1ff00,d2
	cmp.l	#$1000,d2
	bne	sinloop
	add.b	d6,$c(a3)	move bars
	add.b	d6,$14(a3)
	add.b	d6,$1c(a3)
	add.b	d6,$24(a3)
	add.b	d6,$2c(a3)
	add.b	d6,$34(a3)
	subi.w	#1,d5	decrease counter
	tst.w	d5	change direction ?
	bne.s	not_yet	if not dont worry
	moveq.l	#35,d5	reset counter
	neg.b	d6	change direction
not_yet	
	
	lea	star_data,a1
	moveq.l	#19,d2
ll1	move.l	(a1)+,d0
	move.w	(a1)+,d1
	bsr	unplot
	dbra	d2,ll1
	move.l	#19,d2	d2=star counter
	lea	star_data,a1	a1-->star data
ll2	move.l	(a1),d0	d0=move,x
	move.w	d0,d1	d1=x
	swap	d0	d0=x,move
	sub.w	d0,d1	d1=x-move=new x
	bcc	no_wrap	need to wrap round ?
	addi.w	#300,d1	if so do the rap
no_wrap	swap	d0	d0=move,x
	move.w	d1,d0	d0=move,new x
	move.l	d0,(a1)+	overwrite old value
	move.w	(a1)+,d1	d1=y
	bsr	plot	plot new point
	dbra	d2,ll2	go back until all done
	move.l	d3,d0	d0=x-coord
	bsr	calc	calculate point
	move.w	d2,d3	update x-coord
	bsr	unplot	unplot this point
	and.b	d1,(a0)+
	and.b	d1,(a0)+
	and.b	d1,(a0)+
	and.b	d1,(a0)+
	move.l	d4,d0	d0=x-coord
	bsr	calc	calculate point
	move.w	d2,d4	update x-coord
	bsr	plot	plot this point
	or.b	d1,(a0)+
	or.b	d1,(a0)+
	or.b	d1,(a0)+
	or.b	d1,(a0)+
	bsr	scrolly
	subq.b	#1,scrlcount
	bne	button
	bsr	printchar
	move.b	#7,scrlcount
button	btst	#6,ciaapra
	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	move.l	#0,d0	set return code
	rts

; Subroutines

calc	move.w	d0,d2	copy x-coord
;	asl.w	#1,d0	double it
	bsr	GETSIN	get SIN(x)
	muls.w	#120,d1	scale y-coord
	divs	#16384,d1	INT y-coord
	addi.w	#128,d1	add y offset
	move.w	d2,d0	d0=x-coord
	addi.w	#5,d0	add x offset
	addi.w	#1,d2	increase x-coord
	cmpi.w	#250,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
	and.l	#$ffff,d0
	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,d1	d2=10000000 binary
	lsr.b	d0,d1	shift bit to correct poss
	or.b	d1,(a0)+	plot point
	rts

unplot	move.l	bpl1,a0	a0-->bitmap
	mulu.w	#40,d1	d1=addr of start of line
	and.l	#$ffff,d0
	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,d1	d2=01111111 binary
	ror.b	d0,d1	shift mask
	and.b	d1,(a0)+	unplot point
	rts

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


printchar	move.l	msgpoint,a0
	MOVEQ.L	#0,D0
	MOVE.B	(A0),D0
	CMPI.B	#' ',D0
	BNE.S	NOSPC
	MOVE.B	#'@',D0
NOSPC	SUB.W	#'@',D0
	MULU.W	#8,D0
	LEA	alphadat,A0
	ADDA.L	D0,A0
	moveq.l	#7,d0
	move.l	#40,d1
	move.l	bpl1,a1
	adda.l	#39,a1
nextln	move.b	(a0)+,(a1)
	adda.l	d1,a1
	dbra	d0,nextln
	addq.l	#1,msgpoint
	move.l	msgpoint,a0
	tst.b	(a0)
	bne.s	more
	move.l	#MSG,msgpoint
more	RTS

scrolly	move.l	scrnpoint,a0
	move.l	#7,d1
lp1	move.l	#19,d0
	move.w	#0,ccr
lp2	roxl.w	-(a0)
	dbra	d0,lp2
	adda.l	#80,a0
	dbra	d1,lp1
	rts

; Program variable area

clist	dc.l	0
bpl1	dc.l	0
scrnpoint	dc.l	0
grname	dc.b	'graphics.library',0
	even
bars	DC.W	color00,$0000		BLACK
	DC.W	$320F,$FFFE
	DC.W	color00,$0F00		RED
	DC.W	$3C0F,$FFFE
	DC.W	color00,$0000		BLACK
	DC.W	$460F,$FFFE
	DC.W	color00,$0E61		ORANGE
	DC.W	$500F,$FFFE
	DC.W	color00,$0000		BLACK
	DC.W	$5A0F,$FFFE
	DC.W	color00,$0FB0		GOLD
	DC.W	$640F,$FFFE		
	DC.W	color00,$0000		BLACK
	DC.W	$FFFF,$FFFE

star_data	dc.w	2,150,12
	dc.w	3,10,24
	dc.w	6,199,36
	dc.w	8,174,48
	dc.w	2,9,60
	dc.w	7,257,72
	dc.w	5,211,84
	dc.w	6,5,96
	dc.w	7,169,108
	dc.w	2,270,120
	dc.w	1,101,132
	dc.w	4,130,144
	dc.w	6,50,156
	dc.w	3,140,168
	dc.w	4,240,180
	dc.w	1,127,192
	dc.w	6,277,204
	dc.w	7,51,216
	dc.w	5,1,228
	dc.w	2,189,240
	dc.w	2,299,252

                             
; Get Sin Value for Angle in d0      	


;                                                                
;                     Data for Sintable		 

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


msgpoint	dc.l	0
scrlcount	dc.b	7
	even
	DC.L	0

alphadat	dc.b	0,0,0,0,0,0,0,0		space	
	dc.b	0,56,68,68,68,124,68,68	a
	dc.b	0,112,72,80,120,76,68,120	b
	dc.b	0,60,64,64,64,64,64,60	c
	dc.b	0,112,72,68,68,68,72,112	d
	dc.b	0,60,64,64,48,64,64,60	e
	dc.b	0,60,64,64,48,64,64,64	f
	dc.b	0,60,64,64,64,92,68,56	g
	dc.b	0,68,68,68,124,68,68,68	h
	dc.b	0,124,16,16,16,16,16,124	i
	dc.b	0,118,8,8,8,8,72,48	j
	dc.b	0,68,72,80,96,80,72,68	k
	dc.b	0,64,64,64,64,64,64,60	l
	dc.b	0,68,108,84,68,68,68,68	m
	dc.b	0,68,100,84,84,84,76,68	n
	dc.b	0,16,40,68,68,68,40,16	o
	dc.b	0,120,68,68,120,64,64,64	p
	dc.b	0,24,36,66,66,74,36,26	q
	dc.b	0,120,68,68,120,80,72,68	r
	dc.b	0,60,64,64,56,4,4,120	s
	dc.b	0,124,16,16,16,16,16,16	t
	dc.b	0,68,68,68,68,68,68,58	u
	dc.b	0,68,68,68,68,40,40,16	v
	dc.b	0,68,68,68,68,84,108,68	w
	dc.b	0,68,68,40,16,40,68,68	x
	dc.b	0,68,68,40,16,16,16,16	y
	dc.b	0,124,4,8,16,32,64,124	z

MSG 	DC.B	'                                              '
	dc.b	' WELL WHAT SHOULD I WRITE TO MAKE SURE THIS '
	DC.B	'IS WORKING PROPERLY        THANKS FOR THE COPIES '
	DC.B	'OF TYPEY AND SETKEY SANJAY'
	DC.B	'                                             ' 
	DC.B	'     NOW THE BORING PART    '
	DC.B	'                                              ',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
   
