	SECTION		chipmemory,data_c	
							;BY BLAINE EVANS
							;1 ERITH WAY
							;PONTYBODKIN
							;NR MOLD
							;CLWYD
							;CH7 4TR
							;TEL O352-771673
	opt	c-,d+

Height			equ	256
Width			equ	40
Depth			equ	5
ScreenSize		equ	Height*Width
BlitHeight		equ	1

	move.l	4.w,a6			;FIND EXEC BASE
	lea	gfxlib(pc),a1		;LOAD GRAPHICS LIBRARY IN A1
	moveq	#$00,d0			;VERSION 0
	jsr	-552(a6)		;OPEN LIBRARY
	move.l	d0,_gfxbase		;STORE D0 (GFX ADDRESS )
	beq	nolib_exit		;ELSE EXIT
	bsr	Foreground_addresses	;BRANCH TO FOREGROUND ADDRESSES
	bsr	Init
	lea	$dff000,a6
	move.l	_gfxbase,a0		;LOAD ADDRESS OF GRAPHICS LIB IN A0
	move.l	50(a0),Oldcop		;STORE CURRENT COPPER,ETC TO RETRIEVE LATER
S
	move.l	#Newcop,50(a0)		;POINT TO OUR COPPERLIST

Wait					;WAIT LOOP
	cmp.b	#255,$dff006		;VERTICAL BLANKING GAP 
	bne.s	wait			;
;	move.w	#$fff,$180(a6)		;REMOVE TO MEASURE RASTER TIME
	bsr	Direction
;	move.w	#$000,$180(a6)		;REMOVE TO MEASURE RASTER TIME
	btst	#$6,$bfe001		;QUIT LEFT MOUSE PRESSED
	bne.s	Wait
ended					;ELSE LOOP TO WAIT
	move.l	_gfxbase,a0		;LOAD GRAPHICS LIB ADDRESS IN A0
	move.l	oldcop,50(a0)		;RESTORE OLD COPPER TO RETURN TO EDITTER
nomem_exit	
	move.l	4.w,a6			;FIND EXEC BASE
	move.l	_gfxbase,a1		;LOAD GRAPHICS BASE IN A1
	jsr	-414(a6)		;CLOSE LIBRARY
nolib_exit
	rts				;RETURN TO EDITER
Foreground_addresses			;START OF FOREGROUND DATA
	move.l	#Foreground,d0
	move.w	d0,Bpl0ptl		;LOW WORD IN BIT PLANE 0 LOW
	swap	d0				
	move.w	d0,Bpl0pth		;HIGH WORD IN BIT PLANE 0 HIGH
	swap	d0
	add.l	#ScreenSize,d0
	move.w	d0,Bpl1ptl		;LOW WORD IN BIT PLANE 1 LOW
	swap	d0				
	move.w	d0,Bpl1pth		;HIGH WORD IN BIT PLANE 1 HIGH
	swap	d0
	add.l	#ScreenSize,d0
	move.w	d0,Bpl2ptl		;LOW WORD IN BIT PLANE 2 LOW
	swap	d0				
	move.w	d0,Bpl2pth		;HIGH WORD IN BIT PLANE 2 HIGH
	swap	d0
	add.l	#ScreenSize,d0
	move.w	d0,Bpl3ptl		;LOW WORD IN BIT PLANE 3 LOW
	swap	d0				
	move.w	d0,Bpl3pth		;HIGH WORD IN BIT PLANE 3 HIGH
	swap	d0
	add.l	#ScreenSize,d0
	move.w	d0,Bpl4ptl		;LOW WORD IN BIT PLANE 4 LOW
	swap	d0				
	move.w	d0,Bpl4pth		;HIGH WORD IN BIT PLANE 4 HIGH
	swap	d0
	rts
Init
	move.l	#Foreground,d0		; initialse 
	move.l	d0,Address
	move.l	#Graphics,d1
	move.l	d1,Graph_Address
	move.w	#0,Count
	rts
Direction				; which way ?
	move.l	Graph_Address,a2	; graphics address
	move.l	Address,a3		; memory address
	add.w	#1,Count		; counter for direction
	cmp.w	#127,Count
	ble	GoingDown
	cmp.w	#128,Count		; bottom out
	beq	OddLines
	cmp.w	#257,Count
	bge	WipeClear			; up
	sub.l	#2*Width,a2		; every other line
	move.l	a2,Graph_Address
	sub.l	#2*Width,a3
	move.l	a3,Address
	bsr	DoBob	
	rts	
OddLines				; at bottom skip 1 line to fill
	add.l	#1*Width,a2		; what was missed going down
	move.l	a2,Graph_Address
	add.l	#1*Width,a3
	move.l	a3,Address
	bsr	DoBob	
	rts
GoingDown
	add.l	#2*Width,a2		; down 
	move.l	a2,Graph_Address
	add.l	#2*Width,a3
	move.l	a3,Address
	bsr	DoBob	
	rts
WipeClear					; reset addresses to beginning
	move.l	#Blanks,d1
	move.l	d1,Graph_Address
	cmp.w	#257+127,Count
	ble	DownBlank
	cmp.w	#257+128,Count		; bottom out
	beq	OddLineBlank
	cmp.w	#257+257,Count
	bge	StartAgain		; up
	sub.l	#2*Width,a3
	move.l	a3,Address
	bsr	BlitErase
	rts
DownBlank
	add.l	#2*Width,a3
	move.l	a3,Address
	bsr	BlitErase
	rts
OddLineBlank
	add.l	#1*Width,a3
	move.l	a3,Address
	bsr	BlitErase
	rts
StartAgain
	move.l	#Foreground,d0		; re-initialse 
	move.l	d0,Address
	move.l	#Graphics,d1
	move.l	d1,Graph_Address
	move.w	#0,Count
	rts

DoBob
	move.w	#(Depth)-1,d7		* no of planes -1
	lea	$dff000,a6		* custom base
	move.l	Address,a3		* mem address
	move.l	Graph_Address,a2	* graph address
Bob_loop
	bsr	Blit_Bob		
	lea	ScreenSize(a2),a2	*find next plane
	lea	ScreenSize(a3),a3
	dbra	d7,Bob_loop
	lea	-ScreenSize(a2),a2
	lea	-ScreenSize(a3),a3
	rts
Blit_Bob				* LOAD BLITTER WITH VALUES
	bsr	Wfblit
	move.l	a2,$50(a6)		* bltapt   * Source
	move.l	a3,$54(a6)		* bltdpt   * Designation
	move.w	#00,$64(a6)		* bltamod  * Source Modula
	move.w	#Width-Width,$66(a6)	* bltdmod  * Designation Modula
	move.l	#$09f00000,$40(a6)	* bltcon0  * Bplcon0
	move.l	#00,$42(a6)		* bltcon1  * Bplcon1
	move.w	#$ffff,$44(a6)		* bltafwm  * first word mask
	move.w	#$ffff,$46(a6)		* bltalwm  * last word mask
	move.w	#(BlitHeight*64)+(Width/2),$58(a6)	* bltsize  * Bltsize
	rts
Wfblit
	btst	#14,2(a6)		* dmaconr
	bne.s	Wfblit			* Test Blitter State
	rts	
BlitErase
	move.w	#(Depth)-1,d7		* no of planes -1
	lea	$dff000,a6		* custom base
	move.l	Address,a3		* mem address
	move.l	Graph_Address,a2	* graph address
Blit_loop
	bsr	Erase_Bob		
	lea	ScreenSize(a3),a3
	dbra	d7,Blit_loop
	lea	-ScreenSize(a3),a3
	rts
Erase_Bob				* LOAD BLITTER WITH VALUES
	bsr	Wfblit
	move.l	a2,$50(a6)		* bltapt   * Source
	move.l	a3,$54(a6)		* bltdpt   * Designation
	move.w	#00,$64(a6)		* bltamod  * Source Modula
	move.w	#Width-Width,$66(a6)	* bltdmod  * Designation Modula
	move.l	#$09f00000,$40(a6)	* bltcon0  * Bplcon0
	move.l	#00,$42(a6)		* bltcon1  * Bplcon1
	move.w	#$ffff,$44(a6)		* bltafwm  * first word mask
	move.w	#$ffff,$46(a6)		* bltalwm  * last word mask
	move.w	#(BlitHeight*64)+(Width/2),$58(a6)	* bltsize  * Bltsize
	rts

gfxlib	dc.b	"graphics.library",0	
	even
_gfxbase	dc.l	0			;LONG WORD TO STORE GFX ADDRESS
oldcop		dc.l	0			;OLD COPPERLIST ADDRESS
Address		dc.l	0
Graph_Address	dc.l	0
Count		dc.w	0
	even


Newcop
	dc.w	$0100,%0101001000000000		
	dc.w	$0102
Scroll	dc.w	$0000				;SCROLL VALUE
	dc.w	$0104,%0000000000000001		;NO PRIORITIES
	dc.w	$0108,$0000,$010a,$0000		;MODULAS
	dc.w	$0092,$0038,$0094,$00d0		;256*40 SCREEN
	dc.w	$008e,$2c81,$0090,$2cc1		;VISIBLE AREA
						
Planes
	dc.w	$00e0
Bpl0pth	dc.w	$0000,$00e2
Bpl0ptl	dc.w	$0000,$00e4
Bpl1pth	dc.w	$0000,$00e6
Bpl1ptl	dc.w	$0000,$00e8
Bpl2pth	dc.w	$0000,$00ea
Bpl2ptl	dc.w	$0000,$00ec
Bpl3pth	dc.w	$0000,$00ee
Bpl3ptl	dc.w	$0000,$00f0
Bpl4pth	dc.w	$0000,$00f2
Bpl4ptl	dc.w	$0000,$00f4
Bpl5pth	dc.w	$0000,$00f6
Bpl5ptl	dc.w	$0000

Sprites						;SPRITES
	dc.w	$0120
Spl0pth	dc.w	$0000,$0122
Spl0ptl	dc.w	$0000,$0124
Spl1pth	dc.w	$0000,$0126
Spl1ptl	dc.w	$0000
	dc.w	$0128,$0000,$012a,$0000
	dc.w	$012c,$0000,$012e,$0000
	dc.w	$0130,$0000,$0132,$0000
	dc.w	$0134,$0000,$0136,$0000
	dc.w	$0138,$0000,$013a,$0000
	dc.w	$013c,$0000,$013e,$0000

	dc.w	$0180,$0000,$0182,$0eca,$0184,$0db9,$0186,$0c98
	dc.w	$0188,$0b87,$018a,$0a76,$018c,$0865,$018e,$0754
	dc.w	$0190,$0644,$0192,$0533,$0194,$0422,$0196,$0322
	dc.w	$0198,$0211,$019a,$0fa2,$019c,$0d81,$019e,$0c71
	dc.w	$01a0,$0a51,$01a2,$0940,$01a4,$0730,$01a6,$0333
	dc.w	$01a8,$0444,$01aa,$0555,$01ac,$0666,$01ae,$0777
	dc.w	$01b0,$0888,$01b2,$0999,$01b4,$0aaa,$01b6,$0bbb
	dc.w	$01b8,$0ccc,$01ba,$0ddd,$01bc,$0eee,$01be,$0fff

	dc.w	$ffff,$fffe			;END OF COPPERLIST

Foreground
		dcb.b	ScreenSize*1,$00
		dcb.b	ScreenSize*1,$00
		dcb.b	ScreenSize*1,$00
		dcb.b	ScreenSize*1,$00
		dcb.b	ScreenSize*1,$00
Graphics	
	incbin	Owl.raw
Blanks
	dcb.b	(Width*1),$00


