;no odd frame or non-laced=don't add - else "sync"
VB_ILACEFIX:MACRO
	cmp.w #buffs,Frame-R(A5)
	bne.s .no1st
	tst.w 4-C(A6)
	bmi.s .no1st
	subq.w #1,Frame-R(A5)
	subq.w #1,OldFrame-R(A5)
	subq.w #4,FrameOffs-R(A5)
.no1st:
	ENDM

LOADREGS_R_C:MACRO
	lea R(PC),a5
	lea CUSTOM+C,a6
	ENDM

WAITVB:	MACRO
.wvb\@:	btst #5,$1f-C(A6)
	beq.s .wvb\@
	ENDM
;a0,a1=ptr into BufL,non-shown buf. Backup in (a5), ie. R(PC).
GETDRAWBUF:MACRO
	lea BufL+buffs*4(PC),a0
	add.w BufLoffs(PC),a0
	add.w FrameOffs(PC),a0
	move.l (a0),a1
	move.l a1,(a5)
	ENDM
		;1=loopaddr
CHECKEXIT:MACRO
	tst.w NoExit2
	bne \1
	ENDM
		;loads testvalues, always .l, into Dn, but not d0(=0)
EXITREGS:MACRO
	IF FINAL=0
	IF CRUNCHED=0
	move.l \1,d1
	IFNB \2
	move.l \2,d2
	ENDC
	IFNB \3
	move.l \3,d3
	ENDC
	IFNB \4
	move.l \4,d4
	ENDC
	IFNB \5
	move.l \5,d5
	ENDC
	IFNB \6
	move.l \6,d6
	ENDC
	IFNB \7
	move.l \7,d7
	ENDC
	ENDC
	ENDC
	ENDM
GETRAS:MACRO
	IF FINAL=0
	move.l $4-C(A6),Raster-R(A5)
	ENDC
	ENDM
DIFFRAS:MACRO
	IF FINAL=0
	move.l $4-C(A6),d0
	sub.l Raster(PC),d0
	move.l d0,Raster-R(A5)
	cmp.l MaxRas(PC),d0
	blt.s .nomax\@
	move.l d0,MaxRas-R(A5)
.nomax\@:
	ENDC
	ENDM
BG:MACRO
	if final=0
	move.w #\1,$180-C(A6)
	endc
	ENDM



;BLIT #10<<6+5,#$0dfc0000	;a0->a1 #bsize,bltcon[,admod=0,amask=-1,cbdat]
;BLIT #10<<6+5,#$0dfc0000,#-2,#$7ffffffe,#5555aaaa
;@need to differentiate between reg and constant. how? 16x string compare?
;@no, just require mask param for C_AD (?) blit.
BLIT:	MACRO
	BSR WB2
	BLITn \1,\2,\3,\4,\5
	ENDM
	
BLITn:	MACRO	;a0,a1=src/dst. Params bltsize,bltcon,[a/dmod],[mask],[c/bdat]
	move.l \2,$40-C(A6)
	IFNB \5
	move.l \5,$70-C(A6)
	ENDC
	IFNB \4
	move.l a1,$4c-C(A6)
	ifb \3
	clr.l $60-C(A6)
	else
	move.l \3,$60-C(A6)
	endc
	ENDC
	movem.l a0-a1,$50-C(A6)
	ifb \3
	clr.l $64-C(A6)
	else
	move.l \3,$64-C(A6)
	endc
	IFB \4
	move.l #-1,$44-C(A6)
	ELSE
	move.l \4,$44-C(A6)
	ENDC
	move.w \1,$58-C(A6)
	ENDM


;BLIT3 #10<<6+5,$0dfc0000 ;a2+a1+a0->a1 #bltsize,bltcon,cbmod[,admod=0,amask=-1]
;BLIT3 #10<<6+5,$0dfc0000,#-2,#$7ffffffe,#5555aaaa
;like BLIT, only with non-optional cbmod. (an extra srcC)
;cbmod is swapped if C points to the dest.
BLIT3:	MACRO
	BSR WB2
	BLIT3n \1,\2,\3,\4,\5
	ENDM
;NOTE! removed # before \2! May affect DragonBobs!
BLIT3n:	MACRO	;a0,a1=src/dst. Params bltsize,bltcon,cbmod,[a/dmod],[mask]
	move.l \2,$40-C(A6)
	move.l a1,$48-C(A6)
	move.l a2,$4c-C(A6)
	ifb \3
	clr.l $60-C(A6)
	else
	move.l \3,$60-C(A6)
	endc
	movem.l a0-a1,$50-C(A6)
	ifb \4
	clr.l $64-C(A6)
	else
	move.l \4,$64-C(A6)
	endc
	IFB \5
	move.l #-1,$44-C(A6)
	ELSE
	move.l \4,$44-C(A6)
	ENDC
	move.w \1,$58-C(A6)
	ENDM

;like blit3, but all 4 channels different ptrs.
;a0-a3=Apt,Dpt,Bpt,Cpt
;bob blit: A,B,C,D=mask,bob,screen,screen.$fca, A & B shifted same.
BLIT4:	MACRO
	BSR WB2
	BLIT4n \1,\2,\3,\4,\5
	ENDM
;NOTE! removed # before \2! May affect DragonBobs!
BLIT4n:	MACRO	;a0,a1=src/dst. Params bltsize,bltcon,cbmod,[a/dmod],[mask]
	BSR WB2
	move.l \2,$40-C(A6)
	move.l a3,$48-C(A6)
	move.l a2,$4c-C(A6)
	ifb \3
	clr.l $60-C(A6)
	else
	move.l \3,$60-C(A6)
	endc
	movem.l a0-a1,$50-C(A6)
	ifb \4
	clr.l $64-C(A6)
	else
	move.l \4,$64-C(A6)
	endc
	IFB \5
	move.l #-1,$44-C(A6)
	ELSE
	move.l \4,$44-C(A6)
	ENDC
	move.w \1,$58-C(A6)
	ENDM



	;d0/a0-a1=cnt-1,src,dst. [reptcnt] [src,dst] [src,dst,reptcnt]
	ifnd WaitBlit2
WaitBlit2:MACRO
	btst #6,(a6)
	btst #6,(a6)
	bne.s *-4
	ENDM
	endc
WAITRAS:MACRO
	move.w #\1,d0
	bsr WRAS
	ENDM
LOADREGS:MACRO
	ifnb \1
	ifnb \2
	move.l \2,\1
	ENDC
	ENDC
	ifnb \3
	ifnb \4
	move.l \4,\3
	ENDC
	ENDC
	ifnb \5
	ifnb \6
	move.l \6,\5
	ENDC
	ENDC
	ifnb \7
	ifnb \8
	move.l \8,\7
	ENDC
	ENDC
	ENDM
SETCOP:	MACRO	;copper (0 or 1),addr/reg
	IF DEBUG=0
	move.l \2,$80+\1*4-C(A6)
	ENDC
	ENDM

;P61_Init, destroys all but a6, d0=0 if ok, optional \1,\2 -> a0,a4 (was a3)
INITMUSIC:MACRO
	IF MUSIC=1
	IFNB \1
	move.l \1,a0
	ENDC
	IFNB \2
	move.l \2,a4
	ENDC
	sub.l a1,a1
	if cia=1
	moveq #0,d0
	endc
	bsr P61_Init
	lea R(PC),a5
	ENDC
	ENDM

;P61_End, trashes all but a6
EXITMUSIC:MACRO
	IF MUSIC=1
	bsr P61_End
	ENDC
	lea R(PC),a5
	ENDM

;P61_Music, trashes all, a6 set to $dff000+C at exit.
PLAYMUSIC:MACRO
	IF MUSIC=1
	bsr P61_Music
	lea R(PC),a5
	ENDC
	ENDM

;Call before P61_Music to hear next frame. Trashes d0-d1/a0-a1/a3.
JUMPMUSIC:MACRO
	moveq #\1,d0
	bsr P61_SetPosition
	ENDM
;dbf-count in d0, or dbf-count,srcaddrmode,destaddrmode. Commas not supported.
COPYB:	MACRO
	IFB \1
.clb\@:	move.b (a0)+,(a1)+
	dbf d0,.clb\@
	ELSE
	moveq #\1,d0
.clb\@:	move.b \2,\3
	dbf d0,.clb\@
	ENDC
	ENDM

COPYW:	MACRO
	IFB \1
.clw\@:	move.w (a0)+,(a1)+
	dbf d0,.clw\@
	ELSE
	moveq #\1,d0
.clw\@:	move.w \2,\3
	dbf d0,.clw\@
	ENDC
	ENDM

COPYL:	MACRO
	IFB \1
.cll\@:	move.l (a0)+,(a1)+
	dbf d0,.cll\@
	ELSE
	moveq #\1,d0
.cll\@:	move.l \2,\3
	dbf d0,.cll\@
	ENDC
	ENDM
;rept not allowed in macro.
BPLIST:	MACRO
	IF buffs=1
	dc.l \1
	dc.l \1
	dc.l \1
	ENDC
	IF buffs=2
	dc.l \1
	dc.l \2
	dc.l \1
	dc.l \2
	dc.l \1
	dc.l \2
	ENDC
	IF buffs=3
	dc.l \1
	dc.l \2
	dc.l \3
	dc.l \1
	dc.l \2
	dc.l \3
	dc.l \1
	dc.l \2
	dc.l \3
	ENDC
	ENDM
			;1=mouseblock (or earlier to handle sth else)
VB_BUFFERING:MACRO
	lea \1,a0
	subq.w #1,(a0)+
	addq.w #1,(a0)+
	move.w (a0)+,d7
	move.w (a0),d2
	move.w d7,(a0)+

	move.w (a0),d0
	cmp.w d2,d7
	beq.s .noswp
	addq.w #4,d0
	cmp.w #buffs*4,d0
	blt.s .nowr
	clr.w d0
.nowr:	move.w d0,(a0)
	ENDM
;current vblank custom regs list --> a0.

;auto-handles event 15=exitflag. d0=frameoffs?
VB_PREP_EVENTS:MACRO
	lea BufL+buffs*4(PC,d0.w),a4
	add.w BufLoffs(PC),a4
	move.l (a4),d3
	move.l Cop0L-BufL(a4),a2
	IF DUALCOP=1
	move.l Cop1L-BufL(a4),a3
	ENDC
	move.l RegL-BufL(a4),a0

	move.l CurrEvP(PC),a1
	move.l (a1)+,d5
	move.w Vblanks(PC),d0
	cmp.w d0,d5
	bne.s .noev
	move.l d5,LastEv-R(A5)
	move.l a1,CurrEvP-R(A5)
.noev:	move.w LastEv(PC),d5
	bpl.s .noev15
	addq.w #1,ExitFlag-R(A5)
.noev15:
	ENDM
			;this eventframe handled, wait for next
FLOOP:	MACRO			;if not blocked, read mousebutton.
	tst.w ExitFlag-R(A5)
	bne.s .fexit
	tst.w MouseBlock-R(A5)
	bpl.w .floop
	btst #6,$bfe001
	BNE.W .floop
.fexit:
	ENDM

