
; Blitter Example 9	Bobs With Depth

;				Displays a 3 bitplane bob, with background
;				preservation.

		include		hardware.i
		include		HW_Macros.i
		include		HW_Start.i

; First, 'poke' address of bit plane into Copper list

Main		COPBPLC		CopPlanes,Screen,(320/8)*256,3

; Enable bitplane, Copper and Blitter DMA.

		move.w		#SETIT!DMAEN!COPEN!BPLEN!BLTEN,DMACON(a5)

; Install vert blank interrupt handler and enable it

		move.l		#Level3,$6c
		move.w		#SETIT!INTEN!VERTB,INTENA(a5)

; Now strobe the Copper list.

		move.l		#MyCopper,COP1LCH(a5)	address of list
		move.w		#0,COPJMP1(a5)		strobe Copper

; Wait for user to press the left mouse button

mouse		btst		#6,CIAAPRA
		bne.s		mouse

; And exit.

		rts

;		***************************
;		*     Level 3 Interrupt	  *
;		***************************

Level3		lea		$dff000,a5		a5->hardware regs

		bsr		TestJoy
		
		bsr		BlitBob

		move.w		INTREQR(a5),d0		get bits
		and.w		#VERTB!COPER!BLIT,d0	mask level 3 bits
		move.w		d0,INTREQ(a5)		clear request

		rte					back to user mode


;		***************************
;		*   Interrogate Joystick  *
;		***************************

; Update bobs x,y position according to joystick

; Corrupts d0, d1 and d2.

TestJoy		moveq.l		#0,d0			clear
		move.w		JOY1DAT(a5),d0		read stick

		btst		#1,d0			right ?
		beq.s		test_left		if not jump!

		cmp.w		#288,Bob_x		at left edge?
		beq		test_updown		yep, ignore!
		addq.w		#1,Bob_x		set right bit

test_left	btst		#9,d0			left ?
		beq.s		test_updown		if not jump

		tst.w		Bob_x			at left edge?
		beq.s		test_updown		yep, ignore!
		subq.w		#1,Bob_x		set left bit

test_updown	move.l		d0,d1			copy JOY1DAT
		lsr.w		#1,d1			shift u/d bits
		eor.w		d1,d0			exclusive or 'em
		btst		#0,d0			down ?
		beq.s		test_down		if not jump

		cmp.w		#238,Bob_y		at bottom?
		beq.s		test_down		yep, ignore!
		addq.w		#1,Bob_y		set down bit

test_down	btst		#8,d0			up ?
		beq.s		no_joy			if not jump

		tst.w		Bob_y			at top?
		beq.s		no_joy			yep, ignore!
		subq.w		#1,Bob_y		set up bit

no_joy		rts

;		***************************
;		*     Blit Graphic	  *
;		***************************

; Entry		d0= x pixel coordinate of bob
;		d1= y pixel coordinate of bob

BlitBob		move.l		Bob_Restore,d0		get restore addr
		beq.s		SaveBob			skip if 0

; Restore background at previous position

BWait		btst		#14,DMACONR(a5)		wait for Blitter
		bne.s		BWait

		moveq.l		#2,d3			num planes - 1

		move.l		#BobSave,BLTAPTH(a5)	A address
		move.w		#0,BLTAMOD(a5)		A Modulo
		move.w		#34,BLTDMOD(a5)		D Modulo
		move.w		#-1,BLTAFWM(a5)		A first mask
		move.w		#-1,BLTALWM(a5)		A last mask
		move.w		#$09f0,BLTCON0(a5)	use A,D: D=A
		move.w		#0,BLTCON1(a5)

BLoop		move.l		d0,BLTDPTH(a5)		D address
		move.w		#18<<6!3,BLTSIZE(a5)	3 words by 10 lines

		add.l		#(320/8)*256,d0		bump for next plane

BWait0		btst		#14,DMACONR(a5)		wait for Blitter
		bne.s		BWait0

		dbra		d3,BLoop

; Calculate start address, scroll for new position. This is saved at the
;label Bob_Restore and used to restore the background during the next pass.

SaveBob		moveq.l		#0,d0			clear registers
		move.l		d0,d1
		move.w		Bob_x,d0		get x position
		move.w		Bob_y,d1		get y position
		mulu		#40,d1			start of line offset
		ror.l		#4,d0			isolate scrl & offset
		asl.w		#1,d0			multiply offset by 2
		add.w		d0,d1			byte offset
		add.l		#Screen,d1		dest start address
		move.l		d1,Bob_Restore		save for next restore
		swap		d0			get scroll bits
		move.w		d0,d2			copy scroll bits
		or.w		#$0fca,d0		add minterm & usage

; Copy background at location bob is about to splat into

BWait1		btst		#14,DMACONR(a5)		wait for Blitter
		bne.s		BWait1

		moveq.l		#2,d3			num planes - 1
		move.l		d1,d4			d4=screen address

		move.l		#BobSave,BLTDPTH(a5)	D address
		move.w		#34,BLTAMOD(a5)		A Modulo
		move.w		#0,BLTDMOD(a5)		D Modulo
		move.w		#-1,BLTAFWM(a5)		A first mask
		move.w		#-1,BLTALWM(a5)		A last mask
		move.w		#$09f0,BLTCON0(a5)	use A,D: D=A
		move.w		#0,BLTCON1(a5)

BLoop1		move.l		d4,BLTAPTH(a5)		A address
		move.w		#18<<6!3,BLTSIZE(a5)	3 words by 10 lines

		add.l		#(320/8)*256,d4		bump for next plane

BWait11		btst		#14,DMACONR(a5)		wait for Blitter
		bne.s		BWait11

		dbra		d3,BLoop1

; Now blit the bob onto screen

		moveq.l		#2,d3
		move.l		d1,d4

		move.l		#Bob,BLTBPTH(a5)	B address
		move.w		#-2,BLTAMOD(a5)		A modulo
		move.w		#-2,BLTBMOD(a5)		B modulo
		move.w		#34,BLTCMOD(a5)		C modulo
		move.w		#34,BLTDMOD(a5)		D modulo
		move.w		#-1,BLTAFWM(a5)		A first mask
		move.w		#0,BLTALWM(a5)		A last mask
		move.w		d0,BLTCON0(a5)		Use A,B,C,D: D=AB+aC
		move.w		d2,BLTCON1(a5)		no special modes

BLoop2		move.l		#BobMask,BLTAPTH(a5)	A address
		move.l		d4,BLTCPTH(a5)		C address
		move.l		d4,BLTDPTH(a5)		D address
		move.w		#18<<6!3,BLTSIZE(a5)	3 words by 10 lines

		add.l		#(320/8)*256,d4		bump for next plane

BWait2		btst		#14,DMACONR(a5)		wait for Blitter
		bne.s		BWait2

		dbra		d3,BLoop2

		rts


;		***************************
;		*       Fixed Data	  *
;		***************************

Bob_x		dc.w		0
Bob_y		dc.w		0
Bob_Restore	dc.l		0

;		***************************
;		*     CHIP Memory Data    *
;		***************************

section		data custom,chip		****  Use this for A68K  ****

;		Section		custom,data_C	**** Use this for Devpac ****

MyCopper	CMOVE		DIWSTRT,$2c81		PAL -- 256 lines
		CMOVE		DIWSTOP,$2cc1
		CMOVE		DDFSTRT,$0038		LoRes
		CMOVE		DDFSTOP,$00d0
		CMOVE		BPL1MOD,$0000		No modulos
		CMOVE		BPL2MOD,$0000
		CMOVE		BPLCON0,$3200		3 bitplanes & colour
		CMOVE		BPLCON1,$0000		No scrolling
		CMOVE		BPLCON2,$0000		Ignore priority

CopPlanes	CMOVE		BPL1PTH,0		Bit plane pointer
		CMOVE		BPL1PTL,0
		CMOVE		BPL2PTH,0		Bit plane pointer
		CMOVE		BPL2PTL,0
		CMOVE		BPL3PTH,0		Bit plane pointer
		CMOVE		BPL3PTL,0

		ds.w		16*2			space for 16 colours
		
		CEND					end of list

; Raw screen data: 320x256x3, CMAP behind.

Screen		incbin		BltPic1.bm

Bob		incbin		Bob1.bm

BobMask		incbin		Bob1Mask.bm


BobSave		ds.w		3*18*3			

		end

		
