	section StartupRoutines,code

	include "Include:lvo3.0/exec_lib.i"
	include "Include:lvo3.0/dos_lib.i"

_SaveSystem:
	OpenLib		DosName,DosBase
	OpenLib		GfxName,GfxBase

	bsr	_GetVBR

	IFD	KillSystem

	move.l	4.w,a6
	CALL	Disable(a6)

	bsr	_SaveDMA_Interrupt
	ENDIF

	IFD	NewCopperList
	bsr	_RemoveSystemScreen
	ENDIF

	rts


_RestoreSystem:
	IFD	KillSystem

	BlitterWait
	bsr	_RestoreDMA_Interrupt

	move.l	4.w,a6
	CALL	Enable(a6)

	ENDIF

	IFD	NewCopperList
	bsr	_RestoreSystemScreen
	ENDIF
	
	CloseLib	DosBase
	CloseLib	GfxBase

	moveq	#0,d0
	rts


;-----Misc routines--------------------------------------------------

_GetVBR:
     	moveq	#0,d0			; clear
     	move.l	4.w,a6
     	btst.b	#0,296+1(a6)		; are we at least a 68010?
     	beq.b	.Just_68000		; nope.
     	lea.l	.VBR_Exception(pc),a5	; addr of function to get VBR
     	CALL	Supervisor(a6)		; supervisor state
.Just_68000:
     	move.l	d0,VectorBase		; save it for later.
	rts
.VBR_Exception:
     	movec   vbr,d0
     	rte

;------------------------------------------------

_SaveInterrupts:
	move.l	VectorBase,a0

	move.l	$64(a0),_OldLevel1
	move.l	$68(a0),_OldLevel2
	move.l	$6c(a0),_OldLevel3
	move.l	$70(a0),_OldLevel4
	move.l	$74(a0),_OldLevel5
	move.l	$78(a0),_OldLevel6
	move.l	$7c(a0),_OldLevel7

	rts
;------------------------------------------------

_RestoreInterrupts:
	move.l	VectorBase,a0

	move.l	_OldLevel1,$64(a0)
	move.l	_OldLevel2,$68(a0)
	move.l	_OldLevel3,$6c(a0)
	move.l	_OldLevel4,$70(a0)
	move.l	_OldLevel5,$74(a0)
	move.l	_OldLevel6,$78(a0)
	move.l	_OldLevel7,$7c(a0)

	rts
;------------------------------------------------

_RemoveSystemScreen:
	move.l	GfxBase,a6

	move.l	34(a6),_WBView
	sub.l	a1,a1
	CALL	LoadView(a6)
	CALL	WaitTOF(a6)
	CALL	WaitTOF(a6)
	rts

;------------------------------------------------

_RestoreSystemScreen:
	move.l	GfxBase,a6
	move.l	_WBView,a1
	CALL	LoadView(a6)
	CALL	WaitTOF(a6)
	CALL	WaitTOF(a6)
	move.l	38(a6),$dff080
	move.w	#0,$dff088
	rts

;------------------------------------------------
_SaveDMA_Interrupt:
	move.w	$dff01c,_INTENAR	;SAVE READ INTERRUPT ENABLE;
	or.w	#$C000,_INTENAR		;or two last bit til INTENAR
	move.w	$dff002,_DMAdata	;
	or.w	#$8100,_DMAdata		;or it off!!!

	IFD	SaveInterrupts
	bsr	_SaveInterrupts
	ENDIF

	rts

;------------------------------------------------

_RestoreDMA_Interrupt:

	move.l	#$7fff7fff,$dff096;
	move.w	#$7fff,$dff09a;

	IFD	SaveInterrupts
	bsr	_RestoreInterrupts
	ENDIF

	move.w	_DMAdata,$dff096
	move.w	_INTENAR,$dff09A;
	rts
	
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------

	
;-----Data Storage---------------------------------------------------

DosBase:	dc.l	0
GfxBase:	dc.l	0

DosName:	dc.b	"dos.library",0
GfxName:	dc.b	"graphics.library",0
		even

VectorBase:    	dc.l	0

;-----Internal Data----------------------------------------------------

_WBView:	dc.l	0
_INTENAR:	dc.w	0
_DMAdata:	dc.w	0

_OldLevel1:	dc.l	0
_OldLevel2:	dc.l	0
_OldLevel3:	dc.l	0
_OldLevel4:	dc.l	0
_OldLevel5:	dc.l	0
_OldLevel6:	dc.l	0
_OldLevel7:	dc.l	0


