 	
 	section ax,code_c
	opt c-,d+,w-,o+	
	
	include 'df1:insert/custom registers'
	
badd=(320*256)/8

	jsr bpinit
	move.l #table,karst
	move.l #table1,karst1
	move.l #table2,karst2
	move.l #table3,karst3
	move.l #table4,karst4
	move.l #table5,karst5
	lea $dff000,a4
	move.w #$87e0,dmacon(a4)
	jsr cop_init
 	move.l $6c,oldirq
 	move.l #newirq,$6c
mse:
 	cmp.b #$f0,$dff006
 	bne mse
 	
zog:
 	btst #6,$bfe001
	bne mse
	move.w #$0f,$dff096
 	move.l oldirq,$6c
 	move.l copaddr,a0
 	move.l oldcop,(a0)
nolib:	
	 rts	
newirq:  
	movem.l d0-d7/a0-a6,$75000
	cmpi.w #$0fff,colo
	beq fre
	cmpi.b #4,county
	bne fre
	move.b #0,county
	addi.w #$0111,colo
fre:
	jsr reader5
	jsr reader4
	jsr reader
	jsr reader1
	jsr reader2
	jsr reader3
	addi.b #1,county
	jsr bpmusic
	movem.l $75000,d0-d7/a0-a6
	dc.w $4ef9
	
oldirq: dc.l 0
karst:	dc.l 0
karst1: dc.l 0
karst2:	dc.l 0
karst3: dc.l 0
karst4: dc.l 0
karst5: dc.l 0
county:	dc.b 0


	even

cop_init:
 	
 	lea screen,a0
 	move.l a0,d0
 	move.w d0,lo1
 	swap d0
 	move.w d0,hi1
 	
 	lea chrset+640+2,a0
 	move.l a0,d0
 	move.w d0,elo1
 	swap d0
 	move.w d0,ehi1
 	
 	lea chrset+1280+2,a0
 	move.l a0,d0
 	move.w d0,clo1
 	swap d0
 	move.w d0,chi1
 	
 	lea chrset+1920+2,a0
 	move.l a0,d0
 	move.w d0,alo1
 	swap d0
 	move.w d0,ahi1
 	
 	lea chrset+2560+2,a0
 	move.l a0,d0
 	move.w d0,blo1
 	swap d0
 	move.w d0,bhi1
 	
 	lea chrset+3200+2,a0
 	move.l a0,d0
 	move.w d0,dlo1
 	swap d0
 	move.w d0,dhi1
 	
 	lea chrset+2,a0
 	move.l a0,d0
 	move.w d0,flo1
 	swap d0
 	move.w d0,fhi1
 	
 	moveq #$00,d0
 	lea grafname,a1
 	move.l $4,a6
 	jsr -132(A6)
 	move.l $4,A6
 	jsr -408(a6)
 	tst d0
 	beq nolib
 	move.l d0,grafbase
 	move.l d0,a6
 	add.l #$32,d0
 	move.l d0,a0
 	move.l a0,copaddr
 	move.l (a0),oldcop
 	move.l #newcop,(a0)
 	rts
 
	even
	  
grafname: dc.b "graphics.library",0
grafbase: dc.l 0
oldcop:   dc.l 0
copaddr:  dc.l 0
yaz:	  dc.w 0

	even
 
newcop:	 dc.w $0100,$1200
 	 dc.w $0102
boh: 	 dc.w $00ff
	 dc.w $0120
sp1l:	 dc.w $0000,$0122
sp1h: 	 dc.w $0000 
	 dc.w $0124
sp2l:	 dc.w $0000,$0126
sp2h: 	 dc.w $0000 
	 dc.w $0128
sp3l:	 dc.w $0000,$012a
sp3h: 	 dc.w $0000 
	 dc.w $012c
sp4l:	 dc.w $0000,$012e
sp4h: 	 dc.w $0000
	 dc.w $0130
sp5l:	 dc.w $0000,$132
sp5h: 	 dc.w $0000
 scr:  	 dc.w $2001,$fffe
 	 dc.w $00e0
 hi1:	 dc.w $0000
	 dc.w $00e2
 lo1:	 dc.w $0000
	 dc.w $008e,$3021 
	 dc.w $0090,$30f1
	 dc.w $180,$000
	 dc.w $182
colo:	 dc.w $000,$184,$fdb,$186,$db9
	 dc.w $188,$c97,$18a,$a75,$18c,$853,$18e,$033,$190,$c60
	 dc.w $192,$ba5,$194,$962,$196,$530,$198,$db4,$19a,$558
	 dc.w $19c,$447,$19e,$336,$1a0,$a40,$1a2,$f44,$1a4,$b00
	 dc.w $1a6,$222,$1a8,$333,$1aa,$444,$1ac,$555,$1ae,$666
	 dc.w $1b0,$777,$1b2,$888,$1b4,$999,$1b6,$aaa,$1b8,$ccc
	 dc.w $1ba,$ddd,$1bc,$eee,$1be,$fff

goob5:	 dc.w $8401,$fffe
	 dc.w $00e0
fhi1:	 dc.w $0000
	 dc.w $00e2
flo1:	 dc.w $0000 
goob4:	 dc.w $8c01,$fffe
	 dc.w $00e0
ehi1:	 dc.w $0000
	 dc.w $00e2
elo1:	 dc.w $0000 	
	 dc.w $0180,$0000
goob:	 dc.w $9401,$fffe
	 dc.w $0180,$0000
	 dc.w $00e0
chi1:	 dc.w $0000
	 dc.w $00e2
clo1:	 dc.w $0000
	
goob1:   dc.w $9c01,$fffe
	 dc.w $0180,$0000
	 dc.w $00e0
ahi1:	 dc.w $0000
	 dc.w $00e2
alo1:	 dc.w $0000 	
	
goob2:	 dc.w $a401,$fffe
	 dc.w $00e0
bhi1:	 dc.w $0000
	 dc.w $00e2
blo1:	 dc.w $0000 	
	
	 dc.w $0180,$0000
goob3:	 dc.w $ac01,$fffe
	 dc.w $00e0
dhi1:	 dc.w $0000
	 dc.w $00e2
dlo1:	 dc.w $0000 	
	
	 dc.w $0180,$0000

	 dc.w $ffff,$fffe
	 
	 even

screen:
	 ds.b 1*badd

chrset:
	 incbin 'df1:graphics/17bit.raw'

reader:
	move.l karst,a3
	move.w (a3)+,yaz
	cmpi.w #$0,yaz
	bne cdo
	move.l #table,karst
	rts
cdo:
	move.w yaz,goob
	move.l a3,karst
	rts
	
reader1:
	
	move.l karst1,a3
	move.w (a3)+,yaz
	cmpi.w #$0,yaz
	bne cdo1
	move.l #table1,karst1
	rts
cdo1:
	move.w yaz,goob1
	move.l a3,karst1
	rts
		
reader2:
	
	move.l karst2,a3
	move.w (a3)+,yaz
	cmpi.w #$0,yaz
	bne cdo2
	move.l #table2,karst2
	rts
cdo2:
	move.w yaz,goob2
	move.l a3,karst2
	rts

		
reader3:
	
	move.l karst3,a3
	move.w (a3)+,yaz
	cmpi.w #$0,yaz
	bne cdo3
	move.l #table3,karst3
	rts
cdo3:
	move.w yaz,goob3
	move.l a3,karst3
	rts	
	
		
reader4:
	
	move.l karst4,a3
	move.w (a3)+,yaz
	cmpi.w #$0,yaz
	bne cdo4
	move.l #table4,karst4
	rts
cdo4:
	move.w yaz,goob4
	move.l a3,karst4
	rts	
	
reader5:
	
	move.l karst5,a3
	move.w (a3)+,yaz
	cmpi.w #$0,yaz
	bne cdo5
	move.l #table5,karst5
	rts
cdo5:
	move.w yaz,goob5
	move.l a3,karst5
	rts	
	
table:
	dc.w $9401,$9001,$8c01,$8901,$8601,$8401,$8201,$8101
	dc.w $8001,$8001,$8001,$8001,$8001,$8101,$8201,$8401
	dc.w $8601,$8901,$8c01,$9001,$9401
	dc.w $0000
	
table1:
	dc.w $9c01,$9901,$9601,$9401,$9201,$9101,$9001,$9001
	dc.w $9001,$9001,$9001,$9001,$9001,$9001,$9001,$9101
	dc.w $9201,$9401,$9601,$9901,$9c01
	dc.w $0000
	
table2:
	dc.w $a401,$a201,$a001,$9f01,$9e01,$9e01,$9e01,$9e01
	dc.w $9e01,$9e01,$9e01,$9e01,$9e01,$9e01,$9e01,$9e01
	dc.w $9e01,$9f01,$a001,$a201,$a401
	dc.w $0000
	
table3:
	dc.w $ac01,$ab01,$aa01,$aa01,$aa01,$aa01,$aa01,$aa01
	dc.w $aa01,$aa01,$aa01,$aa01,$aa01,$aa01,$aa01,$aa01
	dc.w $aa01,$aa01,$aa01,$ab01,$ac01
	dc.w $0000
	
table4:
	dc.w $8c01,$8701,$8201,$7e01,$7a01,$7701,$7501,$7301
	dc.w $7101,$7001,$7001,$7001,$7101,$7301,$7501,$7701
	dc.w $7a01,$7e01,$8201,$8701,$8c01
	dc.w $0000
	
table5:
	dc.w $8401,$7e01,$7801,$7301,$6e01,$6a01,$6701,$6401
	dc.w $6201,$6101,$6101,$6101,$6201,$6401,$6701,$6a01,$6e01,$7301
	dc.w $7801,$7e01,$8401
	dc.w $0000
	
	even	

bpinit:		lea samples(pc),a0
		lea bpsong(pc),a1
		clr.b numtables
		cmpi.w #'V.',26(a1)
		bne.s bpnotv2
		cmpi.b #'2',28(a1)
		bne.s bpnotv2
		move.b 29(a1),numtables
bpnotv2:	move.l #512,d0
		move.w 30(a1),d1		;d1 now contains length in steps
		moveq.l #1,d2 			;1 is highest pattern number
		mulu #4,d1 			;4 voices per step
		subq.w #1,d1 			;correction for DBRA
findhighest:	cmp.w (a1,d0),d2		;Is it higher
		bge.s nothigher			;No
		move.w (a1,d0),d2		;Yes, so let D2 be highest
nothigher:	addq.l #4,d0 			;Next Voice
		dbra d1,findhighest		;And search
		move.w 30(a1),d1
		mulu #16,d1 			;16 bytes per step
		move.l #512,d0 			;header is 512 bytes
		mulu #48,d2 			;48 bytes per pattern
		add.l d2,d0
		add.l d1,d0 			;offset for samples
		add.l #bpsong,d0
		move.l d0,tables
		moveq.l #0,d1
		move.b numtables,d1		;Number of tables
		lsl.l #6,d1 			;x 64
		add.l d1,d0
		move.l #14,d1 			;15 samples
		add.l #32,a1
initloop:	move.l d0,(a0)+
		cmpi.b #$ff,(a1)
		beq.s bpissynth
		move.w 24(a1),d2
		mulu #2,d2 			;Length is in words
		add.l d2,d0 			;offset next sample
bpissynth:	add.l #32,a1 			;Length of Sample Part in header
		dbra d1,initloop
		rts
bpmusic:	bsr bpsynth
		subq.b #1,arpcount
		moveq.l #3,d0
		lea bpcurrent(pc),a0
		move.l #$dff0a0,a1
bploop1:	move.b 12(a0),d4
		ext.w d4
		add.w d4,(a0)
		tst.b $1e(a0)
		bne.s bplfo
		move.w (a0),6(a1)
bplfo:		move.l 4(a0),(a1)
		move.w 8(a0),4(a1)
		tst.b 11(a0)
		bne.s bpdoarp
		tst.b 13(a0)
		beq.s not2
bpdoarp:	tst.b arpcount
		bne.s not0
		move.b 11(a0),d3
		move.b 13(a0),d4
		and.w #240,d4
		and.w #240,d3
		lsr.w #4,d3
		lsr.w #4,d4
		add.w d3,d4
		add.b 10(a0),d4
		bsr bpplayarp
		bra.s not2
not0:		cmpi.b #1,arpcount 
		bne.s not1
		move.b 11(a0),d3
		move.b 13(a0),d4
		and.w #15,d3
		and.w #15,d4
		add.w d3,d4
		add.b 10(a0),d4
		bsr bpplayarp
		bra.s not2
not1:		move.b 10(a0),d4
		bsr bpplayarp
not2:		lea $10(a1),a1
		lea $20(a0),a0
		dbra d0,bploop1
		tst.b arpcount
		bne.s arpnotzero
		move.b #3,arpcount
arpnotzero:	subq.b #1,bpcount
		beq.s bpskip1
		rts
bpskip1:	move.b bpdelay,bpcount
bpplay:		bsr.s bpnext
		move.w dma,$dff096
		;move.l #$1f4,d0		;is this a waste ?????
bpxx:		;dbra d0,bpxx
		moveq.l #3,d0
		move.l #$dff0a0,a1
		moveq.w #1,d1
		lea bpcurrent(pc),a2
		lea bpbuffer(pc),a5
bploop2:	btst #15,(a2)
		beq.s bpskip7
		bsr bpplayit
bpskip7:	asl.w #1,d1
		lea $10(a1),a1
		lea $20(a2),a2
		lea $24(a5),a5
		dbra d0,bploop2
		rts
bpnext:		clr.w dma
		lea bpsong(pc),a0
		move.l #$dff0a0,a3
		moveq.l #3,d0
		moveq.w #1,d7
		lea bpcurrent(pc),a1
bploop3:	moveq.l #0,d1
		move.w bpstep,d1
		lsl.w #4,d1
		move.l d0,d2
		lsl.l #2,d2
		add.l d2,d1
		add.l #512,d1
		move.w (a0,d1),d2
		move.b 2(a0,d1),st
		move.b 3(a0,d1),tr
		subq.w #1,d2
		mulu #48,d2
		moveq.l #0,d3
		move.w 30(a0),d3
		lsl.w #4,d3
		add.l d2,d3
		move.l #$00000200,d4
		move.b bppatcount,d4
		add.l d3,d4
		move.l d4,a2
		add.l a0,a2
		moveq.l #0,d3 
		move.b (a2),d3
		tst.b d3
		bne.s bpskip4
		bra bpoptionals
bpskip4:	clr.w 12(a1)	  		;Clear autoslide/autoarpeggio
		move.b 1(a2),d4
		and.b #15,d4
		cmpi.b #10,d4    		;Option 10->transposes off
		bne.s bp_do1
		move.b 2(a2),d4
		and.b #240,d4	  		;Higher nibble=transpose
		bne.s bp_not1
bp_do1:		add.b tr,d3
		ext.w d3
bp_not1:	move.b d3,10(a1) 		; Voor Arpeggio's
		lea bpper(pc),a4
		lsl.w #1,d3
		move.w -2(a4,d3.w),(a1)
		bset #15,(a1)
		move.b #$ff,2(a1)
		moveq.w #0,d3
		move.b 1(a2),d3
		lsr.b #4,d3
		and.b #15,d3
		tst.b d3
		bne.s bpskip5
		move.b 3(a1),d3 
bpskip5: 	move.b 1(a2),d4
		and.b #15,d4
		cmpi.b #10,d4 			;option 10
		bne.s bp_do2
		move.b 2(a2),d4
		and.b #15,d4
		bne.s bp_not2
bp_do2:		add.b st,d3
bp_not2:	cmpi.w #1,8(a1)
		beq.s bpsamplechange
		cmp.b 3(a1),d3
		beq.s bpoptionals
bpsamplechange:	move.b d3,3(a1)
		or.w d7,dma
bpoptionals: 	moveq.l #0,d3
		moveq.l #0,d4
		move.b 1(a2),d3
		and.b #15,d3
		move.b 2(a2),d4
		cmpi.b #0,d3			; Optionals Here
		bne.s notopt0
		move.b d4,11(a1)
notopt0:	cmpi.b #1,d3
		bne.s bpskip3
		move.w d4,8(a3)
		move.b d4,2(a1) 		; Volume ook in BPCurrent
bpskip3:	cmpi.b #2,d3  			; Set Speed
		bne.s bpskip9
		move.b d4,bpcount
		move.b d4,bpdelay
bpskip9:	cmpi.b #3,d3 			; Filter = LED control
		bne.s bpskipa
		tst.b d4
		bne.s bpskipb
		bset #1,$bfe001
		bra.s bpskip2
bpskipb:	bclr #1,$bfe001
bpskipa:	cmpi.b #4,d3 			; PortUp
		bne.s noportup
		sub.w d4,(a1) 			; Slide data in BPCurrent
		clr.b 11(a1) 			; Arpeggio's uit
noportup:	cmpi.b #5,d3 			; PortDown
		bne.s noportdn
		add.w d4,(a1) 			; Slide down
		clr.b 11(a1)
noportdn:	cmpi.b #6,d3			; SetRepCount
		bne.s notopt6
		move.b d4,bprepcount
notopt6:	cmpi.b #7,d3			; DBRA repcount
		bne.s notopt7
		subq.b #1,bprepcount
		beq.s notopt7
		move.w d4,bpstep
notopt7:	cmpi.b #8,d3			;Set AutoSlide
		bne.s notopt8
		move.b d4,12(a1)
notopt8:	cmpi.b #9,d3			;Set AutoArpeggio
		bne.s notopt9
		move.b d4,13(a1)
notopt9:
bpskip2:	lea $10(a3),a3
		lea $20(a1),a1
		asl.w #1,d7
		dbra d0,bploop3   
		addq.b #3,bppatcount
		cmpi.b #48,bppatcount
		bne.s bpskip8
		move.b #0,bppatcount
		addq.w #1,bpstep
		lea bpsong(pc),a0
		move.w 30(a0),d1
		cmp.w bpstep,d1
		bne.s bpskip8
		move.w #0,bpstep
bpskip8:	rts
bpplayit:	bclr #15,(a2)
		tst.l (a5) 			;Was EG used
		beq.s noeg1 			;No ??
		moveq.w #0,d3 			;Well then copy
		move.l (a5),a4 			;Old waveform back
		moveq #7,d7 			;to waveform tables
eg1loop:	move.l 4(a5,d3.w),(a4)+		;Copy...
		addq.w #4,d3 			;Copy...
		dbra d7,eg1loop			;Copy...
noeg1:		move.w (a2),6(a1)		;Period from bpcurrent
		moveq.l #0,d7
		move.b 3(a2),d7			;Instrument number
		move.l d7,d6 			;Also in d6
		lsl.l #5,d7 			;Header offset	
		lea bpsong(pc),a3
		cmpi.b #$ff,(a3,d7.w)		;Is synthetic
		beq.s bpplaysynthetic		;Yes ??
		clr.l (a5) 			;EG Off
		clr.b $1a(a2) 			;Synthetic mode off
		clr.w $1e(a2) 			;Lfo Off
		add.l #24,d7 			;24 is name->ignore
		lsl.l #2,d6 			;x4 for sample offset
		move.l #samples,a4
		move.l -4(a4,d6),d4		;Fetch sample pointer
		beq.s bp_nosamp			;is zero->no sample
		move.l d4,(a1) 			;Sample pointer in hardware
		move.w (a3,d7),4(a1)		;length in hardware
		move.b 2(a2),9(a1)		;and volume from bpcurrent
		cmpi.b #$ff,2(a2)		;Use default volume
		bne.s skipxx 			;No ??
		move.w 6(a3,d7),8(a1)		;Default volume in hardware
skipxx: 	move.w 4(a3,d7),8(a2)		;Length in bpcurrent
		moveq.l #0,d6
		move.w 2(a3,d7),d6		;Calculate repeat
		add.l d6,d4
		move.l d4,4(a2)			;sample start in bpcurrent
		cmpi.w #1,8(a2)			;has sample repeat part
		bne.s bpskip6 			;Yes ??
bp_nosamp:	move.l #null,4(a2)		;Play no sample
		bra.s bpskip10
bpskip6:	move.w 8(a2),4(a1)		;Length to hardware
		move.l 4(a2),(a1)		;pointer to hardware
bpskip10:	or.w #$8000,d1			;Turn on DMA for this voice
		move.w d1,$dff096		;Yeah, do it
		rts
bpplaysynthetic:move.b #$1,$1a(a2)		;Synthetic mode on
		clr.w $e(a2) 			;EG Pointer restart
		clr.w $10(a2) 			;LFO Pointer restart
		clr.w $12(a2) 			;ADSR Pointer restart
		move.w 22(a3,d7.w),$14(a2)	;EG Delay
		addq.w #1,$14(a2)		;0 is nodelay
		move.w 14(a3,d7.w),$16(a2)	;LFO Delay
		addq.w #1,$16(a2)		;So I need correction
		move.w #1,$18(a2)		;ADSR Delay->Start immediate
		move.b 17(a3,d7.w),$1d(a2)	;EG OOC
		move.b 9(a3,d7.w),$1e(a2)	;LFO OOC
		move.b 4(a3,d7.w),$1f(a2)	;ADSR OOC
		move.b 19(a3,d7.w),$1c(a2)	;Current EG Value
		move.l tables,a4		; so far so good,now what ??
		moveq.l #0,d3			;Pointer to waveform tables
		move.b 1(a3,d7.w),d3		;Which waveform
		lsl.l #6,d3 			;x64 is length waveform table
		add.l d3,a4
		move.l a4,(a1) 			;Sample Pointer
		move.l a4,4(a2)			;In bpcurrent
		move.w 2(a3,d7.w),4(a1)		;Length in words
		move.w 2(a3,d7.w),8(a2)		;Length in bpcurrent
		tst.b 4(a3,d7.w)		;Is ADSR on
		beq.s bpadsroff			;No ??
		move.l tables,a4		;Tables
		moveq.l #0,d3
		move.b 5(a3,d7.w),d3		;ADSR table number
		lsl.l #6,d3 			;x64 for length
		add.l d3,a4 			;Add it
		moveq.w #0,d3
		move.b (a4),d3 			;Get table value
		add.b #128,d3 			;I want it from 0..255
		lsr.w #2,d3 			;Divide by 4->0..63
		cmpi.b #$ff,2(a2)
		bne.s bpskip99
		move.b 25(a3,d7.w),2(a2)
bpskip99:	moveq.w #0,d4
		move.b 2(a2),d4			;Default volume
		mulu d4,d3 			;default maal init volume
		lsr.w #6,d3 			;divide by 64
		move.w d3,8(a1)			;is new volume
		bra.s bpflipper
bpadsroff:	move.b 2(a2),9(a1)
		cmpi.b #$ff,2(a2)
		bne.s bpflipper			;No ADSR
		move.b 25(a3,d7.w),9(a1)	;So use default volume
bpflipper:	move.l 4(a2),a4			;Pointer on waveform
		move.l a4,(a5) 			;Save it
		moveq.w #0,d3 			;Save Old waveform
		moveq #7,d4 			;data in bpbuffer
eg2loop:	move.l (a4,d3.w),4(a5,d3.w)
		addq.w #4,d3 			;Copy 		
		dbra d4,eg2loop
		tst.b 17(a3,d7.w)		;EG off
		beq bpskip10			;Yes ??
		tst.b 19(a3,d7.w)		;Is there an init value for EG
		beq bpskip10			;No ??
		moveq.l #0,d3
		move.b 19(a3,d7.w),d3
		lsr.l #3,d3 			;Divide by 8 ->0..31
		move.b d3,$1c(a2)		;Current EG Value
		subq.l #1,d3 			;-1,DBRA correction
eg3loop:	neg.b (a4)+
		dbra d3,eg3loop
		bra bpskip10
bpplayarp:	lea bpper(pc),a4
		ext.w d4
		asl.w #1,d4
		move.w -2(a4,d4.w),6(a1)
		rts
bpsynth:	move.l #3,d0
		lea bpcurrent(pc),a2
		lea $dff0a0,a1
		lea bpsong(pc),a3
		lea bpbuffer(pc),a5
bpsynthloop:	tst.b $1a(a2) 			;Is synthetic sound
		beq.s bpnosynth			;No ??
		bsr.s bpyessynth			;Yes 		
bpnosynth:	lea $24(a5),a5
		lea $20(a2),a2
		lea $10(a1),a1
		dbra d0,bpsynthloop
		rts
bpyessynth:	moveq.w #0,d7
		move.b 3(a2),d7			;Which instr. was I playing
		lsl.w #5,d7 			;x32, is length of instr.
		tst.b $1f(a2) 			;ADSR off
		beq.s bpendadsr			;Yes ??
		subq.w #1,$18(a2)		;Delay,May I
		bne.s bpendadsr			;No ??
		moveq.l #0,d3
		move.b 8(a3,d7.w),d3
		move.w d3,$18(a2)		;Reset Delay Counter
		move.l tables,a4
		move.b 5(a3,d7.w),d3		;Which ADSR table
		lsl.l #6,d3 			;x64
		add.l d3,a4 			;This is my table
		move.w $12(a2),d3		;Get ADSR table pointer
		moveq.w #0,d4
		move.b (a4,d3.w),d4		;Value from table
		add.b #128,d4 			;Want it from 0..255
		lsr.w #2,d4 			;And now from 0..63
		moveq.w #0,d3
		move.b 2(a2),d3			;Current Volume
		mulu d3,d4 			;MultiPly with table volume
		lsr.w #6,d4 			;Divide by 64=New volume
		move.w d4,8(a1)			;Volume in hardware
		addq.w #1,$12(a2)		;Increment of ADSR pointer
		move.w 6(a3,d7.w),d4		;Length of adsr table
		cmp.w $12(a2),d4		;End of table reached
		bne.s bpendadsr			;No ??
		clr.w $12(a2) 			;Clear ADSR Pointer
		cmpi.b #1,$1f(a2)		;Once
		bne.s bpendadsr			;No ??
		clr.b $1f(a2) 			;ADSR off
bpendadsr:	tst.b $1e(a2) 			;LFO On
		beq.s bpendlfo			;No ??
		subq.w #1,$16(a2)		;LFO delay,May I
		bne.s bpendlfo			;No
		moveq.l #0,d3
		move.b 16(a3,d7.w),d3
		move.w d3,$16(a2)		;Set LFO Count
		move.l tables,a4
		move.b 10(a3,d7.w),d3		;Which LFO table
		lsl.l #6,d3 			;x64
		add.l d3,a4
		move.w $10(a2),d3		;LFO pointer
		moveq.l #0,d4
		move.b (a4,d3.w),d4		;That's my value
		ext.w d4 			;Make it a word
		ext.l d4 			;And a longword
		moveq.l #0,d5
		move.b 11(a3,d7.w),d5		;LFO depth
		tst.b d5
		beq.s bpnotx
		divs d5,d4 			;Calculate it
bpnotx:		move.w (a2),d5 			;Period
		add.w d4,d5 			;New Period
		move.w d5,6(a1)			;In hardware
		addq.w #1,$10(a2)		;Next position
		move.w 12(a3,d7.w),d3		;LFO table Length
		cmp.w $10(a2),d3		;End Reached
		bne.s bpendlfo			;NO ??
		clr.w $10(a2)		 	;Reset LFO Pointer
		cmpi.b #1,$1e(a2)		;Once LFO
		bne.s bpendlfo			;NO ??
		clr.b $1e(a2) 			;LFO Off
bpendlfo:	tst.b $1d(a2) 			;EG On
		beq bpendeg 			;No ??
		subq.w #1,$14(a2)		;EG delay,May I
		bne bpendeg 			;No
		tst.l (a5)
		beq.s bpendeg
		moveq.l #0,d3
		move.b 24(a3,d7.w),d3
		move.w d3,$14(a2)		;Set EG Count
		move.l tables,a4
		move.b 18(a3,d7.w),d3		;Which EG table
		lsl.l #6,d3 			;x64
		add.l d3,a4
		move.w $e(a2),d3		;EG pointer
		moveq.l #0,d4
		move.b (a4,d3.w),d4		;That's my value
		move.l (a5),a4 			;Pointer to waveform
		add.b #128,d4 			;0..255
		lsr.l #3,d4 			;0..31
		moveq.l #0,d3
		move.b $1c(a2),d3		;Old EG Value
		move.b d4,$1c(a2)
		add.l d3,a4 			;WaveForm Position
		move.l a5,a6 			;Buffer
		add.l d3,a6 			;Position
		addq.l #4,a6 			;For adress in buffer
		cmp.b d3,d4 			;Compare old with new value
		beq.s bpnexteg			;no change ??
		bgt bpishigh			;new value is higher
bpislow:	sub.l d4,d3 			;oldvalue-newvalue
		subq.l #1,d3 			;Correction for DBRA
bpegloop1a:	move.b -(a6),d4
		move.b d4,-(a4)
		dbra d3,bpegloop1a  
		bra.s bpnexteg
bpishigh:	sub.l d3,d4 			;Newvalue-oldvalue
		subq.l #1,d4 			;Correction for DBRA
bpegloop1b:	move.b (a6)+,d3
		neg.b d3
		move.b d3,(a4)+			;DoIt
		dbra d4,bpegloop1b
bpnexteg:	addq.w #1,$e(a2)		;Next position
		move.w 20(a3,d7.w),d3		;EG table Length
		cmp.w $e(a2),d3			;End Reached
		bne.s bpendeg 			;NO ??
		clr.w $e(a2) 			;Reset EG Pointer
		cmpi.b #1,$1d(a2)		;Once EG
		bne.s bpendeg 			;NO ??
		clr.b $1d(a2) 			;EG Off
bpendeg:	rts

null:		dc.w 0
bpcurrent:	dc.w 0,0			;periode,instrument =(volume.b,instr nr.b)
		dc.l null			;start
		dc.w 1				;length (words)
		dc.b 0,0,0,0 			;noot,arpeggio,autoslide,autoarpeggio
		dc.w 0,0,0			;EG,LFO,ADSR pointers
		dc.w 0,0,0			;EG,LFO,ADSR count
		dc.b 0,0			;Synthetic yes/no, Volume Slide
		dc.b 0,0			;Current EG value,EG OOC
		dc.b 0,0			;LFO OOC,ADSR OOC

		dc.w 0,0
		dc.l null
		dc.w 1,0,0
		dc.w 0,0,0,0,0,0,0,0,0

		dc.w 0,0
		dc.l null
		dc.w 1,0,0
		dc.w 0,0,0,0,0,0,0,0,0

		dc.w 0,0
		dc.l null
		dc.w 1,0,0
		dc.w 0,0,0,0,0,0,0,0,0

bpstep:		dc.w 0
bppatcount:	dc.b 0
st:		dc.b 0
tr:		dc.b 0
bpcount:	dc.b 1
bpdelay:	dc.b 6
arpcount:	dc.b 1
bprepcount:	dc.b 1
numtables:	dc.b 0

		even

dma:		dc.w 0
tables:		dc.l 0

bpbuffer:	dcb.b 144,0
		dc.w 6848,6464,6080,5760,5440,5120,4832,4576,4320,4064,3840,3616
		dc.w 3424,3232,3040,2880,2720,2560,2416,2288,2160,2032,1920,1808
		dc.w 1712,1616,1520,1440,1360,1280,1208,1144,1080,1016,0960,0904

bpper:		dc.w 0856,0808,0760,0720,0680,0640,0604,0572,0540,0508,0480,0452
		dc.w 0428,0404,0380,0360,0340,0320,0302,0286,0270,0254,0240,0226
		dc.w 0214,0202,0190,0180,0170,0160,0151,0143,0135,0127,0120,0113
		dc.w 0107,0101,0095,0090,0085,0080,0076,0072,0068,0064,0060,0057

samples:	dcb.l 15,0

bpsong:		incbin "df1:modules/bp.liverpool"

		

