*
* Change the vector base register on 68010+
*
* by Martin J. Laubach
*

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	incdir "inc:include.i"

	include "exec/execbase.i"
	include "exec/libraries.i"
	include "exec/tasks.i"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	xdef	main

	xref	_AbsExecBase

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

call	macro
	xref	_LVO\1
	jsr 	_LVO\1(a6)
	endm

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

stdout	equr	d7
DosBase	equr	a5
Task	equr	a4

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

main:	move.l	_AbsExecBase,a6
	lea	DosName,a1
	moveq	#0,d0
	call	OpenLibrary		; Open DOSBase to chat with user
	move.l	d0,DosBase
	tst.l	d0
	beq	exit

	move.l	d0,a6
	call	Output			; get stdout
	move.l	d0,stdout

	move.l	d0,d1
	move.l	#SignOn,d2
	moveq	#SignOnEnd-SignOn,d3
	call	Write			; say hello

	move.l	_AbsExecBase,a6
	cmp.w	#33,LIB_VERSION(a6)
	bls	noversion		; check for the right kickstart

	btst.b	#AFB_68010,AttnFlags+1(a6)
	beq	novbr			; and the right processor

	move.l	a5,a0
	lea	getvbr,a5
	call	Supervisor		; become SUPERMAN!

	tst.l	d4
	bne.s	already			; vbr already set

	move.l	#$400,d0
	moveq	#0,d1
	call	AllocMem		; get memory block for vectors
	move.l	d0,Memory
	beq.s	nomem

	sub.l	a0,a0
	move.l	Memory,a1
	move.l	#$400,d0
	call	CopyMem			; now copy the exception vectors

	move.l	Memory,d4
	move.l	a5,a0
	lea	setvbr,a5
	call	Supervisor

exit0:	move.l	DosBase,a1
	move.l	_AbsExecBase,a6
	call	CloseLibrary		; clean up

exit:	moveq	#0,d0
	rts

getvbr:	move.l	a0,a5
	movec.l	vbr,d4
	rte

setvbr:	move.l	a0,a5
	movec.l	d4,vbr
	rte

already:
	move.l	d4,d5

	moveq	#0,d4
	move.l	a5,a0
	lea	setvbr,a5
	call	Supervisor

	move.l	d5,a0
	sub.l	a1,a1
	move.l	#$400,d0
	call	CopyMem

	move.l	d5,a1
	move.l	#$400,d0
	call	FreeMem

	move.l	#Already,d2
	moveq	#AlreadyEnd-Already,d3
	bra.s	Panic

nomem:	move.l	#NoMem,d2
	moveq	#NoMemEnd-NoMem,d3
	bra.s	Panic

novbr:	move.l	#ProcErr,d2
	moveq	#ProcErrEnd-ProcErr,d3
	bra.s	Panic

noversion:
	move.l	#VerErr,d2
	moveq	#VerErrEnd-VerErr,d3
Panic:	move.l	DosBase,a6
	move.l	stdout,d1
	call	Write
	bra.s	exit0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Memory:		dc.l 0
OldTrap:	dc.l 0

DosName:	dc.b 'dos.library',0
SignOn:		dc.b 'cVBR (c) Copyright 1989 by Martin J. Laubach',10
SignOnEnd:
VerErr:		dc.b '*** You need at least version 1.2 of the OS '
		dc.b 'to use cVBR',10
VerErrEnd:
ProcErr:	dc.b '*** You really should have an 68010 to run cVBR',10
ProcErrEnd:
NoMem:		dc.b '*** You haven't got $400 more bytes...',10
NoMemEnd:
Already:	dc.b '*** VBR already changed, reverting to normal',10
AlreadyEnd:
