
	incdir	include:
	include	exec/execbase.i

;in:
* a6	ExecBase

	XDEF	_CallVBLANKServers

_CallVBLANKServers:
* Save some more registers
*        MOVE.L  A2,-(A7)
	movem.l	a2/a5/a6,-(sp)

;set up registers
	move.w	#1<<5,d1		;INTF_VERTB
	lea	$dff000,a0
	move.l	IVVERTB+IV_DATA(a6),a1

        MOVEA.L A1,A2
        MOVE.L  A1,-(A7)
L0000ED:
        MOVEA.L (A2),A2
        MOVE.L  (A2),D0
        BEQ.B   L0000EE
        MOVEM.L $000E(A2),A1/A5
        JSR     (A5)
        BEQ.B   L0000ED
L0000EE:
        MOVEA.L (A7)+,A1
* Skip the interrupt flag reset
*        MOVE.W  $000E(A1),$00DFF09C

*        MOVEA.L (A7)+,A2
	movem.l	(sp)+,a2/a5/a6
        RTS


;in:
* d0	scratch
* d1	INTENAR & INTREQR
* a0	$DFF000
* a1	IS_DATA
* a5	IS_CODE
* a6	ExecBase

	XDEF	@NewVBLANKHandler
	XREF	_NewVBLANKdata

@NewVBLANKHandler:
	move.l	_NewVBLANKdata,a5	;faster than (pc)...
	tst.l	(a5)+			;Test if we should call the old VBLANKHandler
	beq.b	1$
	move.l	(a5),a5			;Yes
	jmp	(a5)
1$
	move.w	$000E(A1),$00DFF09C
	rts


;in:
* a0	Pointer to VGA registers
;out:
* d0	Current E-Clock (only 16 bits), used for VBeamPos() calculation.

	XDEF	_WaitVBLANK

_WaitVBLANK:
	move.w	#$4000,$dff09a		;Disable all interrupts
1$
	nop
	move.b	$03da(a0),d0		;Read Input Status #1 Register
	nop
	and.b	#$08,d0			;Vertical Retrace in progress if set
	beq.b	1$			;Loop if no vblank
	move.w	#$C000,$dff09a		;Enable all interrupts

	XDEF	_myReadEClock
_myReadEClock:
	moveq	#0,d0
	move.b	$bfe701,d0
	move.b	$bfe601,d1
	cmp.b	$bfe701,d0
	bne.b	_myReadEClock
	lsl.w	#8,d0
	move.b	d1,d0
	rts
