	opt c+,d+
	
; woooooooooooow!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
; *** dual playfield & scroll demo ***

; custom chip register

intena	= $9a
intreqr	= $1e
dmacon 	= $96
colour00	= $180
vposr	= $4

; copper register

cop1lc	= $080
cop2lc	= $084
copjmp1	= $088
copjmp2	= $08a

; bitplane register

bplcon0	= $100
bplcon1	= $102
bplcon2	= $104
bpl1pth	= $0e0
bpl1ptl	= $0e2
bpl1mod	= $108
bpl2mod	= $10a
diwstrt	= $08e
diwstop	= $090
ddfstrt	= $092
ddfstop	= $094

; cia-a port a register (mouse button)

ciaapra	= $bfe001

; exec base library offsets

openlib	= -30-522
forbid	= -30-102
permit	= -30-108
allocmem	= -30-168
freemem	= -30-180

; graphics base

startlist	= 38

; misc labels

execbase	= 4
planesize	= 52*345
planewidth	= 52
clsize	= 5*4
chip	= 2
clear	= chip+$10000

; *** pre-program ***

start:

; request memory for bitplanes

	move.l execbase,a6
	move.l #planesize*2,d0
	move.l #clear,d1
	jsr allocmem(a6)
	move.l d0,planeadr
	beq ende
	
; request memory for the copperlist

	moveq #clsize,d0
	moveq #chip,d1
	jsr allocmem(a6)
	move.l d0,cladr
	beq freeplane

; build copperlist

	moveq #1,d4	; two bitplanes
	move.l d0,a0
	move.l planeadr,d1
	move.w #bpl1pth,d3
makecl:	move.w d3,(a0)+
	addq.w #2,d3
	swap d1
	move.w d1,(a0)+
	move.w d3,(a0)+
	addq.w #2,d3
	swap d1
	move.w d1,(a0)+
	add.l #planesize,d1
	dbf d4,makecl
	move.l #$fffffffe,(a0)
	
; *** main program ***

; dma and task switching off

	jsr forbid(a6)
	lea $dff000,a5
	move.l #$01e0,dmacon(a5)
	
; copper initialisation

	move.l cladr,cop1lc(a5)
	clr.w copjmp1(a5)
	
; playfield initialisation
	
	move.w #0,colour00(a5)
	move.w #$0f00,colour00+2(a5)
	move.w #$000f,colour00+18(a5)
	move.w #$1a64,diwstrt(a5)
	move.w #$39d1,diwstop(a5)
	move.w #$0020,ddfstrt(a5)
	move.w #$00d8,ddfstop(a5)
	move.w #%0010011000000000,bplcon0(a5)
	clr.w bplcon1(a5)
	clr.w bplcon2(a5)
	move.w #4,bpl1mod(a5)
	move.w #4,bpl2mod(a5)
	
; dma on
	move.w #$8180,dmacon(a5)

; bitplanes filled with checker pattern

	move.l planeadr,a0
	move.w #planesize/2-1,d0
	move.w #13*16,d1
	move.l #$ffff0000,d2
	move.w d1,d3
fill:	move.l d2,(a0)+
	subq #1,d3
	bne.s continue
	swap d2
	move.w d1,d3
continue:	dbf d0,fill

; playfields scroll

	clr.l d0	; vertical scroll position
	clr.l d1	; horiz scroll position
	move.l cladr,a1
	move.l planeadr,a0

; wait on raster line 16 (for the exec interrupts)

wait:
	move.l vposr(a5),d2
	and.l #$0001ff00,d2
	cmp.l #$00001000,d2
	bne.s wait
	
; playfield 1 vertical scroll

	addq.b #2,d0
	cmp.w #$80,d0
	bne.s novover
	clr.l d0
novover:	move.l d0,d2
	lsr.w #2,d2
	mulu #52,d2
	add.l a0,d2
	add.l #planesize,d2
	move.w d2,14(a1)
	swap d2
	move.w d2,10(a1)
	
; playfield 2 horizontal scroll

	addq.b #1,d1
	cmp.w #$80,d1
	bne.s nohover
	clr.l d1
nohover:	move.l d1,d2
	lsr.w #2,d2
	move.l d2,d3
	and.w #$fff0,d2
	sub.w d2,d3
	move.w d4,bplcon1(a5)
	move.w d3,d4
	lsr.w #3,d2
	add.l a0,d2
	move.w d2,6(a1)
	swap d2
	move.w d2,2(a1)
	
	btst #6,ciaapra
	bne.s wait
	
; *** end program ***

; activate old copper list

	move.l #gfxname,a1
	clr.l d0
	jsr openlib(a6)
	move.l d0,a4
	move.l startlist(a4),cop1lc(a5)
	clr.w copjmp1(a5)
	move.w #$83e0,dmacon(a5)
	jsr permit(a6)
	
; free memory used by copper list

	move.l cladr,a1
	moveq #clsize,d0
	jsr freemem(a6)
	
; free memory used by bitplanes

freeplane:
	move.l planeadr,a1
	move.l #planesize*2,d0
	jsr freemem(a6)
	
ende:
	clr.l d0
	rts
	
; variables

cladr:	dc.l 0
planeadr:	dc.l 0
test:	dc.l 0

; constants

gfxname:	dc.b 'graphics.library',0
	even
	
