
***	CODE		GRAPHIC_POUR
***	CODER		MARC.B
***	DATE		3rd JUNE `92

		
	   


***	constants	***

chip		equ	2
clear		equ	$10000
Plane_Size	equ	320/8*256
bob_size	equ	320/8*256


*********************************************
*		INCLUDE FILES		    *
*********************************************

	incdir		sys:include/
	include		source:include/hardware.i
	include		exec/exec.i
	include		exec/exec_lib.i

	lea	$dff000,a5			;custom base address
	

**********************************************************
*	OPEN GFX LIBRARY AND SAVE SYSTEM COPPER		 *
**********************************************************

	
	move.l		#gfxname,a1
	moveq		#0,d0
	CALLEXEC	OpenLibrary
	tst		d0			
	beq		the_end			
	move.l		d0,gfxbase		
	move.l		d0,a0			;put gfxbase in a0 
	move.l		38(a0),systemcopper	;save the copper address 



****************************************************
*	ALLOCATE MEMORY FOR BIT PLANES		   *
****************************************************

	
	move.l		#Plane_Size*5,d0	;memory for 1 bitplanes
	move.l		#chip+clear,d1		;chip memory and wipe it 
	CALLEXEC	AllocMem
	tst		d0			;fail (naah !!)
	beq		exit			;yep (sell the AMIGA !!!)
	move.l		d0,bitbase		;save pointer


*****************************************************************
*	LOAD BITPLANE AND SPRITE POINTERS INTO COPPER LIST      *
*****************************************************************


	move.l		bitbase,d0
	lea		copper,a0
	move.w		d0,6(a0)	
	swap		d0
	move.w		d0,2(a0)
	swap		d0
	add.l		#Plane_Size,d0
	move.w		d0,14(a0)
	swap		d0
	move.w		d0,10(a0)
	swap		d0
	add.l		#Plane_Size,d0
	move.w		d0,22(a0)
	swap		d0
	move.w		d0,18(a0)
	swap		d0
	add.l		#Plane_Size,d0
	move.w		d0,30(a0)
	swap		d0
	move.w		d0,26(a0)
	swap		d0
	add.l		#Plane_Size,d0
	move.w		d0,38(a0)
	swap		d0
	move.w		d0,34(a0)
	swap		d0
	
	

***************************************************************
*		START CUSTOM COPPER LIST		      *
***************************************************************	

	move.l		#copper,COP1LCH(a5)
	move.l		#0,COPJMP1(a5)
	CALLEXEC	Forbid

*************************************************************
*		    MAIN CODE LOOP    			    *
*************************************************************

	clr.l		offset
	clr.l		bob_offset
	move.l		#256,line
	move.b		#0,finished

	
	
wait
	move.l		VPOSR(a5),d0		;read current beam position
	and.l		#$0001ff00,d0		;mask all but the vertical pos
	lsr.l		#8,d0		
	cmp.w		#$0106,d0		;wait for end of display
	bne		wait
	btst		#6,CIAAPRA		;check left mouse button
	beq		clean_up		;end if pressed
	cmp.b		#$ff,finished
	beq		wait
	bsr		blitter_prep
	bra		wait

blitter_prep
	
	move.l		#0,offset
	move.l		line,d3
	sub.l		#1,d3
	move.l		d3,line
	tst		d3
	beq		no_blit
	bsr		blitter
	add.l		#40,bob_offset
	rts

no_blit
	move.b		#$ff,finished
	rts


**********************************************************
*			RESTORE SYSTEM 			 *
**********************************************************

clean_up

	move.l		systemcopper,COP1LCH(a5)
	move.l		#0,COPJMP1(a5)
	CALLEXEC	Permit
	

exit1
	
	move.l		#Plane_Size*5,d0
	move.l		bitbase,a1
	CALLEXEC	FreeMem

exit
	move.l		gfxbase,a1		;close the graphics library
	CALLEXEC	CloseLibrary
	
the_end
	rts					;exit from program


************************************************************
*		BLITTER OPERATION			   *
************************************************************

blitter
	move.l		#4,d7
	move.l		bitbase,a3
	add.l		offset,a3
	add.l		#40,offset
	lea		bob,a2
	add.l		bob_offset,a2
	bsr		blitter_loop
	dbra		d3,blitter
	rts
	
blitter_loop
	bsr 		blit_bob
	add.l		#bob_size,a2		;find next bob plane
	add.l		#Plane_Size,a3		;find next screen plane
	dbra		d7,blitter_loop		;do 'till end
	rts

blit_bob
	bsr		blitter_busy		;check blitter status
	move.l		a2,BLTAPTH(a5)		;DMA a pointer (bob)
	move.l		a3,BLTDPTH(a5)		;DMA d pointer (screen)
	move.w		#0,BLTAMOD(a5)		;no bob offset
	move.w		#0,BLTDMOD(a5)		;set screen offset
	move.w		#0,BLTCON1(a5)
	move.w		#$ffff,BLTAFWM(a5)	
	move.w		#$ffff,BLTALWM(a5)
	move.w		#$09f0,BLTCON0(a5)	
	move.w		#(1*64)+(320/16),BLTSIZE(a5)	
	rts

blitter_busy
	btst		#14,DMACONR(a5)		;blitter working ?
	bne		blitter_busy		;yes then wait......
	rts					



***********************************************************
*		   CUSTOM COPPER LIST			  *
***********************************************************


	SECTION		copper.list,CODE_C

copper
	dc.w	BPL1PTH,0000		;next 4 blank words
	dc.w	BPL1PTL,0000		;are where we load
	dc.w	BPL2PTH,0000		;the bitplane pointers
	dc.w	BPL2PTL,0000
	dc.w	BPL3PTH,0000
	dc.w	BPL3PTL,0000
	dc.w	BPL4PTH,0000
	dc.w	BPL4PTL,0000
	dc.w	BPL5PTH,0000
	dc.w	BPL5PTL,0000
	dc.w	SPR0PTH,0000
	dc.w	SPR0PTL,0000		;sprite 0 pointer
	dc.w	SPR1PTH,0000
	dc.w	SPR1PTL,0000		;sprite 1 pointer
	dc.w	SPR2PTH,0000
	dc.w	SPR2PTL,0000		;sprite 2 pointer
	dc.w	SPR3PTH,0000
	dc.w	SPR3PTL,0000		;sprite 3 pointer
	dc.w	SPR4PTH,0000
	dc.w	SPR4PTL,0000		;sprite 4 pointer
	dc.w	SPR5PTH,0000
	dc.w	SPR5PTL,0000		;sprite 5 pointer
	dc.w	SPR6PTH,0000
	dc.w	SPR6PTL,0000		;sprite 6 pointer
	dc.w	SPR7PTH,0000
	dc.w	SPR7PTL,0000		;sprite 7 pointer
	dc.l 	$2b01fffe		;wait for line 43
	dc.w	DIWSTRT,$2c81		;where we start displaying
	dc.w	DIWSTOP,$2cc1		;where we stop!
	dc.w	BPLCON0,$5200		;this is for Amiga 1000 owners
	dc.w	BPLCON2,$0024		
	dc.w	DDFSTRT,$0038		;where to start the horizontal display
	dc.w	DDFSTOP,$00d0		;and where we stop
	dc.w	BPLCON1,$0000
	dc.w	BPL1MOD,$0000		;not a scroller
	dc.w	BPL2MOD,$0000		;so these are blank

color_map

		dc.w	$180,$000,$182,$fff,$184,$002,$186,$006
		dc.w	$188,$016,$18a,$027,$18c,$037,$18e,$058
		dc.w	$190,$121,$192,$131,$194,$242,$196,$252
		dc.w	$198,$363,$19a,$475,$19c,$210,$19e,$321
		dc.w	$1a0,$432,$1a2,$543,$1a4,$654,$1a6,$765
		dc.w	$1a8,$876,$1aa,$987,$1ac,$222,$1ae,$333
		dc.w	$1b0,$444,$1b2,$555,$1b4,$666,$1b6,$777
		dc.w	$1b8,$888,$1ba,$999,$1bc,$021,$1be,$010
	

	
	dc.l	$fffffffe		;wait for the impossible
endcopper

copperlen	equ	endcopper-copper


	

	
************************************************************
*			VARIABLES	 		   *
************************************************************


gfxbase		dc.l 0
bitbase		dc.l 0
systemcopper	dc.l 0
offset		dc.l 0
bob_offset	dc.l 0
line		dc.l 0
finished	dc.b 0


gfxname		dc.b 'graphics.library',0

bob	incbin 	source:m.bateson/graphics/mts1




end
	
