	opt	o+,ow-
; This program opens a 1 bitplane screen and then moves a star field
;over it

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)+
	dbra	d2,clrloop

again	move.l	vposr(a5),d2
	and.l	#$1ff00,d2
	cmp.l	#$1000,d2
	bne	again
	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
	btst	#6,ciaapra	check mouse button
	bne	again	go back if not pressed

; 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) allow 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

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

; Program variable area

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

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

