; ===========================================================================
; Run a program with the VBR set to 0, (CLI, MC68010 & OS39+ only)
;
; Created: Tue/3/Nov/1998
;
; Copyright © 1998-1999 Andrew Bell
;
; e-mail: andrew@ab2000.freeserve.co.uk (Put 0VBRRun in the subject)
;
; params: 0VBRRun [File]
; ===========================================================================

		IFD	_PHXASS_

		MC68020
		SYMDEBUG
		OUTPUT	0VBRRun
		OPT	3

		ENDC

VERSION		MACRO
		dc.b	"1.0"
		ENDM

DATE		MACRO
		dc.b	"6.2.99"
		ENDM

YEAR		MACRO
		dc.b	"1999"
		ENDM

		INCDIR	AInc:
		INCLUDE	mymacros.i
		INCLUDE	system.i

; ===========================================================================

slibc		MACRO			; lib call and setup a6
		move.l	\1,a6
		jsr	_LVO\2(a6)
		ENDM

libc		MACRO			; lib call
		jsr	_LVO\1(a6)
		ENDM

; ===========================================================================

Base:		move.w	d0,d2
		move.l	a0,a2

		moveq	#20,d4			; return code

		move.l	(4).w,a6		; We need 010+ for VBR stuff
		move.w	AttnFlags(a6),d0
		btst.w	#AFB_68010,d0
		beq	.Exit2DOS

		lea	DOSName(pc),a1
		moveq	#39,d0
		libc	OpenLibrary
		move.l	d0,d7
		beq	.Exit2DOS

		moveq	#0,d4
		move.l	a6,d6

		lea.l	txt_Title(pc),a0
		suba.l	a1,a1
		bsr	Print

		cmp.b	#'?',(a2)
		beq	.PrintUsage

		subq.w	#1,d2
		bne	.ArgsOK

.PrintUsage	lea	txt_Usage(pc),a0
		suba.l	a1,a1
		bsr	Print
		bra	.ExitCloseDOS

.ArgsOK		clr.b	(a2,d2.w)		; Not OS friendly!

		bsr	ClearVBR
		move.l	d0,d5

		move.l	#TAG_DONE,-(sp)		; Run the program
		move.l	a2,d1
		move.l	sp,d2
		slibc	d7,SystemTagList
		addq.l	#4,sp
.FixVBR		move.l	d5,d1
		bsr	SetVBR

		tst.l	d0
		beq	.ExitCloseDOS

		moveq	#20,d4

		lea	txt_LaunchFail(pc),a0
		suba.l	a1,a1
		bsr	Print

.ExitCloseDOS	move.l	d7,a1
		slibc	d6,CloseLibrary

.Exit2DOS	move.l	d4,d0			; Back to system
		rts

ClearVBR	PUSH	d1/a5/a6		; d6=Execbase

		move.l	d6,a6
		lea.l	super_ClearVBR(pc),a5
		jsr	-30(a6)			; Supervisor

		PULL	d1/a5/a6
		rts

super_ClearVBR	movec	VBR,d0
		moveq	#0,d1
		movec	d1,VBR
		nop
		rte				; Return old VBR in d0

SetVBR		PUSH	a5/a6			; d1=VBR, d6=ExecBase
		move.l	d6,a6
		lea.l	super_SetVBR(pc),a5
		jsr	-30(a6)			; Supervisor
		PULL	a5/a6
		rts

super_SetVBR	movec	d1,VBR
		nop
		rte

Print		PUSH	a0-a1/d0-d2/a6	; A0=String, A1=Fmt, d7=DOSBase

		move.l	a0,d1
		move.l	a1,d2
		slibc	d7,VPrintf

		PULL	a0-a1/d0-d2/a6
		rts

; ===========================================================================

txt_VTag	dc.b	0,"$VER: 0VBRRun "
		VERSION
		dc.b	" ("
		DATE
		dc.b	")",0

txt_Title	dc.b	"0VBRRun "
		VERSION
		dc.b	" Copyright © "
		YEAR
		dc.b	" Andrew Bell\n",0

txt_LaunchFail	dc.b	"Failed to launch program!\n",0
txt_Usage	dc.b	"Usage: 0VBRRun [File]\n",0

DOSName		dc.b	"dos.library",0
