
; Display Example 13. -- Horizontal Scrolling -- 

; Display is   320x256x4 ( 40x256x4 bytes )
; Bit Plane is 640x256x4 ( 80x256x4 bytes )

; Bitplane Modulos = bit plane width - Display width - 2 = 80-40-2 = 38 bytes

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

; First, 'poke' address of bit planes into Copper list. Use a loop to do this

Main		COPBPLC		CopPlanes,bitplane,(640/8)*256,4

; Define address of Copper List

		STARTCOP	#MyCopper

; Enable bitplane and Copper DMA.

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

; Install vertical blank interrupt handler and enable it.

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

; Wait for user to press the left mouse button

mouse		btst		#6,CIAAPRA
		bne.s		mouse

; And exit.

		rts

;		************************
;		* Vert Blank Interrupt *
;		************************

; This handler first calls a routine that sets up the bit plane pointers and
;scroll values according to the value stored in 'Scroll'. Second, a routine
;is called that changes the value stored in 'Scroll'.

NewLevel3	lea		$dff000,a5		a5->hardware base

		bsr		DoScroll
		bsr		UpdateScroll
		bsr		UpdateScroll
		
		move.w		INTREQR(a5),d0		get bits
		and.w		#VERTB!COPER!BLIT,d0
		move.w		d0,INTREQ(a5)		clear request
		rte					back to user mode

;		***************************
;		*   Scroll The Bitplane   *
;		***************************

; Uses the value stored in scroll to determine the position of the bip plane

DoScroll	move.l		Scroll,d0		num pixels to scroll
		divu		#16,d0
		
; Now high word of d0 contains scroll value in pixels and the low word the
;number of words to scroll. The number of words must be converted into the
;number of bytes:

		asl.w		#1,d0			x2

		moveq.l		#0,d1
		move.w		d0,d1			d1=num bytes
		
		swap		d0			get num bits
		moveq.l		#15,d2
		sub.w		d0,d2			make a copy
		move.w		d2,d0 
		asl.w		#4,d0
		or.w		d2,d0

; At this point d0.w contains the scroll value and d1 the number of bytes
;to add to the start address of the bit plane data. Initialise the scroll
;value and bit plane pointers in the copper list

		move.w		d0,CopScroll+2		scroll values
		
		add.l		#bitplane,d1		d1=bpl start addr
		moveq.l		#4-1,d0			bpl counter - 1
		lea		CopPlanes,a0		a0->into Copper
StuffBpls	move.w		d1,6(a0)		low word of address
		swap		d1
		move.w		d1,2(a0)		high word of address
		swap		d1
		add.l		#80*256,d1		addr of next plane
		addq.l		#8,a0			next pointer in list
		dbra		d0,StuffBpls

		rts

;		***************************
;		*    Update Scroll Value  *
;		***************************

; Continually counts from 0 to 320 and back again. A separate routine is used
;to convert the scroll value and modify the Copper list

UpdateScroll	move.l		Scroll,d0
		move.l		direction,d1
		move.l		terminate,d2

		add.l		d1,d0		bump counter
		move.l		d0,Scroll	save new value
		cmp.l		d0,d2		change direction?
		bne.s		no_change	no, exit routine
		
		move.l		spare,terminate	switch direction limits
		move.l		d2,spare
		neg.l		d1
		move.l		d1,direction	change direction

no_change	rts

;		***************************
;		*    Program Variables    *
;		***************************

Scroll		dc.l		0
direction	dc.l		1		1=right, -1=left
terminate	dc.l		320		change direction at ...
spare		dc.l		0

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

;section		data custom,chip
		Section		Fish,DATA_C

MyCopper	CWAIT		0,40			wait for line 40

		CMOVE		DIWSTRT,$2c81		PAL -- 256 lines
		CMOVE		DIWSTOP,$2cc1
		CMOVE		DDFSTRT,$0030		LoRes + scroll
		CMOVE		DDFSTOP,$00d0
		CMOVE		BPL1MOD,38		modulo = 38 bytes
		CMOVE		BPL2MOD,38		modulo = 38 bytes
		CMOVE		BPLCON0,$4200		4 bitplanes & colour
CopScroll	CMOVE		BPLCON1,$00ff		Scroll values
		CMOVE		BPLCON2,$0000		Ignore priority

CopPlanes	CMOVE		BPL1PTH,0		Bit plane pointers
		CMOVE		BPL1PTL,0		filled in by the
		CMOVE		BPL2PTH,0		program!
		CMOVE		BPL2PTL,0
		CMOVE		BPL3PTH,0
		CMOVE		BPL3PTL,0
		CMOVE		BPL4PTH,0
		CMOVE		BPL4PTL,0

CopColours	CMOVE		COLOR00,$0000		The colours will be
		CMOVE		COLOR01,$0000		filled in by the
		CMOVE		COLOR02,$0000		program!
		CMOVE		COLOR03,$0000
		CMOVE		COLOR04,$0000
		CMOVE		COLOR05,$0000
		CMOVE		COLOR06,$0000
		CMOVE		COLOR07,$0000
		CMOVE		COLOR08,$0000
		CMOVE		COLOR09,$0000
		CMOVE		COLOR10,$0000
		CMOVE		COLOR11,$0000
		CMOVE		COLOR12,$0000
		CMOVE		COLOR13,$0000
		CMOVE		COLOR14,$0000
		CMOVE		COLOR15,$0000

		CEND					end of list

bitplane	incbin		'pic4.bm'

		end

		
