
***	CODE		INTERLEAVED_BLIT
***	CODER		MARC.B
***	DATE		13th JUNE `92

		
;THIS BLITS A GRAPHIC INTO THE DISPLAY USING INTERLEAVED BLITS
;THE GRAPHIC IS DRAWN ONE LINE AT A TIME EVEN LINES STARTING
;AT THE TOP OF THE DISPLAY AND ODD LINES AT THE BOTTOM
	   


***	constants	***

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



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

	lea		$dff000,a5		custom base address
	

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

	
	move.l		#gfxname,a1
	moveq		#0,d0
	movea.l	4.w,a6
	jsr	-$198(a6)
	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 


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


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

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

	move.l	#copper,$80(a5)
	move.l	#0,$88(a5)
	movea.l	4.w,a6
	jsr	-$84(a6)	;Forbid

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

	clr.l		offset
	move.l		#51000,offset1
	move.b		#0,finished

	
	
wait
	move.l		4(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,$BFE001		;check left mouse button
	beq		clean_up		;end if pressed
	cmp.b		#$ff,finished
	bne		blit_down
	bra		wait

**********************************************************
*		   BLITTER ROUTINES			 *
**********************************************************

;this routine sets up the blits that form the display
;blit_down handles the lines from top to bottom
;by adding an offset to the bob and bitplane base address's
;then increasing the offset by two lines which is equal
;to 5*40*2
;   5 lines because its interleaved
;   40 bytes per line
;   2 lines needed to increase
;
;blit_up works in the same way except the offset starts off
;equal to the total bob size minus one line the offset is then
;decremented by two lines each pass , when the offset1 =0
;the operation is complete and the finished flag is set
;in order to prevent these routines being called again


blit_down

	lea		bob,a2
	move.l		bitbase,a3
	add.l		offset,a2
	add.l		offset,a3
	add.l		#400,offset
	bsr		blitter
	
blit_up
	
	lea		bob,a2
	move.l		bitbase,a3
	add.l		offset1,a2
	add.l		offset1,a3
	sub.l		#400,offset1
	bsr		blitter
	cmp.l		#0,offset1
	ble		end_blit
	bra		wait

end_blit
	move.b		#$ff,finished
	bra		wait
	
	

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

clean_up

	move.l	systemcopper,$80(a5)
	move.l	#0,$88(a5)
	movea.l	4.w,a6
	jsr	-$8a(a6)	;Permit
	

exit1

exit
	move.l		gfxbase,a1		close the graphics library
	movea.l	4.w,a6
	jsr	-$19e(a6)	;CloseLibrary
	
the_end
	rts					;exit from program


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




blitter
	bsr		blitter_busy		;check blitter status
	MOVE.L	A2,$50(A5)	; BLTAPT	; DMA a pointer (bob)
	MOVE.L	A3,$54(A5)	; BLTDPT	; DMA d pointer (screen)
	MOVE.W	#0,$64(A5)	; BLTAMOD
	MOVE.W	#0,$66(A5)	; BLTDMOD
	MOVE.W	#0,$42(A5)	; BLTCON1
	MOVE.W	#$9F0,$40(A5)	; BLTCON0
	MOVE.W	#$FFFF,$44(A5)	; BLTAFWM	; NO MASK
	MOVE.W	#$FFFF,$46(A5)	; BLTALWM	; NO MASK
	MOVE.W	#(5*64)+(320/16),$58(A5)	; BLTSIZE
	rts

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



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


	SECTION		copper.list,CODE_C

copper
	dc.l	$E00000,$E20000,$E40000,$E60000,$E80000,$EA0000	; BPLPOINTERS
	dc.l	$EC0000,$EE0000,$F00000,$F20000

	DC.L	$1200000,$1220000			; SPRITE POINTERS
	dc.l	$1240000,$1260000,$1280000,$12A0000,$12C0000
	dc.l	$12E0000,$1300000,$1320000,$1340000,$1360000
	dc.l	$1380000,$13A0000,$13C0000,$13E0000

	DC.L	$2B01FFFE
	dc.l	$8E2C81,$902CC1,$1005200,$1040024,$920038,$9400D0
	dc.l	$1020000,$10800A0,$10A00A0

color_map
		dc.w	$180,$000,$182,$fff,$184,$0f0,$186,$026
		dc.w	$188,$038,$18a,$139,$18c,$24b,$18e,$04d
		dc.w	$190,$0f0,$192,$ddd,$194,$bbb,$196,$999
		dc.w	$198,$777,$19a,$555,$19c,$0f0,$19e,$0f0
		dc.w	$1a0,$0f0,$1a2,$0f0,$1a4,$0f0,$1a6,$0f0
		dc.w	$1a8,$0f0,$1aa,$0f0,$1ac,$222,$1ae,$333
		dc.w	$1b0,$444,$1b2,$555,$1b4,$666,$1b6,$777
		dc.w	$1b8,$888,$1ba,$999,$1bc,$0f2,$1be,$0f0
			
	dc.w	$1fc,0
	
	dc.l	$fffffffe		;wait for the impossible
endcopper

copperlen	equ	endcopper-copper


	

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


gfxbase		dc.l 0
bitbase		dc.l planen
systemcopper	dc.l 0
offset		dc.l 0
offset1		dc.l 0
finished	dc.b 0



gfxname		dc.b 'graphics.library',0

bob	incbin 	"mts_inter"

	section	gfxbau,bss_C

planen:
	ds.b	Plane_Size*5

	end
	
