

;Based on FastVBR 2.0 by Michaî i Piotr Kolodziejczykowie.
;V2.1 (by Tadek Knapik) new features:
; - more sophisticated processor checking
; - error code 5 is returned if sth is wrong (and not 20!)
;
;V2.2 new features:
; - 'Chip' option - bring VBR back to Chip RAM at $00000000!
;   ("chip" or "slow" in the command line)
; - 'eXchange' option - switches VBR chip/fast every time ran.
;   ("x" in the command line). No memory freeing!
;
;V2.3 new features:
; - memory freeing when capitalized letter (S, C, X)
;
;V2.4 new features:
; - Can be run from Workbench
;
;V2.5
; - bug removed (freeing when old vectors were at $00000000)
;
;----------
; - chip at $00000000 isn't allocated of course
; - fast is freed with FreeMem, not FreeVec (because FastVBR does AllocMem)
;
;

	incdir	Includes:
	include	exec/execbase.i
	include	exec/memory.i
	include	libraries/dosextens.i
	include	offsets/exec_lib.i

VECTABLE_SIZE	equ	512

Begin:
	bra	Start

	dc.b	'$VER: FastVBR v2.5 (07.07.96)',0

Start:
	move.l	$4,a6
	move.l	a0,a4
	move.l	#0,d5

	sub.l	a1,a1
	jsr	_LVOFindTask(a6)
	move.l	d0,a0
	tst.l	pr_CLI(a0)
	bne	CLIStuff
	lea	pr_MsgPort(a0),a0
	move.l	a0,a2
	jsr	_LVOWaitPort(a6)
	move.l	a2,a0
	jsr	_LVOGetMsg(a6)
	move.l	d0,d5

CLIStuff:
	moveq	#0,d0
	move.w	AttnFlags(a6),d0
	andi.w	#AFF_68010!AFF_68020!AFF_68030!AFF_68040!AFF_68060,d0
	beq	Error

	cmpi.b	#'?',(a4)
	beq	Quit

	lea	GetVBR(pc),a5
	jsr	_LVOSupervisor(a6)

	move.l	d0,d6
	moveq	#0,d7

	cmpi.b	#'C',(a4)
	beq	SlowForce
	cmpi.b	#'c',(a4)
	beq	Slow
	cmpi.b	#'S',(a4)
	beq	SlowForce
	cmpi.b	#'s',(a4)
	beq	Slow

	cmpi.b	#'X',(a4)
	beq	SwitchForce
	cmpi.b	#'x',(a4)
	beq	Switch

	bra	Fast

SwitchForce:
	moveq	#1,d7
Switch:
	tst.l	d0
	bne	Slow

Fast:
	tst.l	d0
	bne	Quit
	

	move.l	#VECTABLE_SIZE,d0
	move.l	#MEMF_PUBLIC!MEMF_FAST,d1
	jsr	_LVOAllocMem(a6)
	move.l	d0,d2
	beq	Error

	move.l	d0,a1
	sub.l	a0,a0
	bra	CopyVectors

SlowForce:
	moveq	#1,d7
Slow:
	tst.l	d0
	beq	Quit

	move.l	d0,a0
	sub.l	a1,a1
	moveq	#0,d2

CopyVectors:

	jsr	_LVOForbid(a6)

	move.l	#VECTABLE_SIZE,d0
	jsr	_LVOCopyMemQuick(a6)

	move.l	d2,d0
	lea	SetVBR(pc),a5
	jsr	_LVOSupervisor(a6)

	tst.l	d7
	beq	Quit

	tst.l	d6
	beq	Quit

	jsr	_LVOPermit(a6)
	move.l	d6,a1
	move.l	#VECTABLE_SIZE,d0
	jsr	_LVOFreeMem(a6)
	jsr	_LVOForbid(a6)

Quit:
	tst.l	d5
	beq	QuitNow

	move.l	d5,a1
	jsr	_LVOReplyMsg(a6)

QuitNow:
	moveq	#0,d0
	rts

Error:
	tst.l	d5
	beq	ErrorNow

	move.l	d5,a1
	jsr	_LVOReplyMsg(a6)

ErrorNow:
	moveq	#5,d0
	rts

GetVBR:
	movec	VBR,d0
	rte

SetVBR:
	movec	d0,VBR
	rte

