;*---------------------------------------------------------------------------
;  :Modul.	dbffix.s
;  :Contents.	routine to fix empty dbf loops
;		the dbf loop will be replaced by a wait based on the vertical
;		raster position
;  :Version.	$Id: dbffix.s 1.2 1999/05/09 20:03:47 jah Exp jah $
;  :History.	19.03.99 written based on the stfix routine
;  :Requires.	-
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly V1.131
;  :To Do.
;---------------------------------------------------------------------------*
;
; this will patch sequences of the following form:
;		move.w	#$xxxx,dn	;3x3c xxxx
;	.loop	dbf	dn,.loop	;51cx fffe
;
; if a label PATCHCOUNT does exist, the amount of patches will be count in
; the memory region specified in A2
;
; IN:	A0 = APTR start of memory to patch
;	A1 = APTR end of memory to patch
;	A2 = APTR space for patch routine MUST be < $8000 !!!
;		  required only if PATCHCOUNT is used
; OUT:	D0-D1/A0-A1 unused
;	A2 = APTR points to the end of patch routine

_dbffix		movem.l	d0-d1/a0-a1,-(a7)

	IFD PATCHCOUNT
		clr.l	(a2)+			;counters = 0
		clr.l	(a2)+			;counters = 0
	ENDC

		subq.l	#6,a1
.loop		cmp.w	#$fffe,(6,a0)		;loop distance
		bne	.next
		move.w	(a0),d0
		and.w	#$f1ff,d0
		cmp.w	#$303c,d0		;move.w #$xxxx,d0
		bne	.next
		move.w	(a0),d0
		and.w	#$0e00,d0		;register number
		rol.w	#7,d0
		or.w	#$51c8,d0		;dbf d0,_xxx
		cmp.w	(4,a0),d0
		bne	.next

	IFD PATCHCOUNT
		and.w	#7,d0			;register number
		addq.b	#1,(-8,a2,d0.w)
	ENDC

		moveq	#0,d1			;clear high word
		move.w	(2,a0),d1		;loop counter
		divu	#34,d1
		move.w	#$4eb9,(a0)+		;JSR (xxxxxxxx.L)
		pea	(.wait,pc)
		move.l	(a7)+,(a0)+
		move.w	d1,(a0)

.next		addq.l	#2,a0
		cmp.l	a0,a1
		bhi	.loop

		movem.l	(a7)+,d0-d1/a0-a1
		rts

.wait		movem.l	d0-d1/a0,-(a7)
		move.l	(12,a7),a0		;return PC
		move.w	(a0)+,d1		;loop counter
		move.l	a0,(12,a7)		;return PC
.1		move.b	$dff006,d0
.2		cmp.b	$dff006,d0
		beq	.2
		dbf	d1,.1
		movem.l	(a7)+,d0-d1/a0
		rts
