*******************************************************************************
*-------------- Allocate our Variables (RS.) Area

		incdir	include:
		include	macros.i
		include	aga_regs.i


*******************************************************************************
*-------------- Init the interrupt for monitor activation!

_ProgStart:	PUSHALL

*-------------- Allocmem 4 the monitor code.

		move.l	4,a6
		move.l	#RelocSize+1,d0
		move.l	#1,d1
		jsr	-198(a6)
		tst.l	d0
		beq	_NoFreeRam
		move.l	d0,MemBlock

		bsr	_GetVBR

*-------------- Copy Monitor code!

		lea	_RelocBeg(pc),a0
		move.l	MemBlock,a1
		move.w	#RelocSize,d7
.CopyMon:	move.b	(a0)+,(a1)+
		dbra	d7,.CopyMon

*-------------- Call the first routine (Patching the interrupts)

		move.l	MemBlock(pc),a0
		jsr	(a0)

		PULLALL
		clr.l	d0
		rts			;exit to amigados

_NoFreeRam:	move.l	#103,d0
		movem.l	(sp)+,d0-d7/a0-a7
		rts
******************************************************************************
_GetVBR:	PUSH	d0/d1/a0/a1/a5/a6
		move.l	4.w,a6
		jsr	-150(a6)			; Supervisor
		lea	Old_SR(pc),a0
		move.l	d0,(a0)
		lea	IllegalPtr(pc),a0
		move.l	$10.w,(a0)
		lea	.Illegal(pc),a0
		move.l	a0,$10.w
		dc.l	$4e7a0801			; movec	vbr,d0
		lea	ExeptionBase(pc),a0
		move.l	d0,(a0)
		bra.s	.Standard
.Illegal:	move.w	#0,(a7)+
		move.l	#0,(a7)+
.Standard:	move.l	IllegalPtr(pc),$10.w
		tst.l	Old_SR
		beq.s	.InSuperstate
;		move.l	4.w,a6
		move.l	Old_SR(pc),d0
;		jsr	-156(a6)			; User
		bsr.w	.UserState
.InSuperstate:	PULL	d0/d1/a0/a1/a5/a6
		rts
.UserState:	move.l	(a7)+,a0
		move.l	a7,usp
		move.l	d0,a7
		andi.w	#$dfff,sr
		jmp	(a0)
******************************************************************************
_RelocBeg:
******************************************************************************
_InitLevel7:	PUSH	a0/a1
		move.l	ExeptionBase(pc),a0
		lea	_NewVec7c(pc),a1
		move.l	a1,$7c(a0)		;patch level 7 interrupt
		PULL	a0/a1
		rts
_NewVec7c:	PUSHALL
		lea	OldLevel7(pc),a0
		move.l	$7c.w,(a0)
		lea	_Blank(pc),a0
		move.l	ExeptionBase(pc),a1
		move.l	a0,$7c(a1)
		bsr	_CheckButton
		move.l	ExeptionBase(pc),a1
		move.l	OldLevel7(pc),$7c(a1)
		bsr	_InitLevel7
		PULLALL
_Blank:		rte
*******************************************************************************
*-------------- Check 4 the level 7 button

_CheckButton:	PUSHALL
		bsr	_MonCode
		PULLALL
		rts
******************************************************************************
MemBlock:	ds.l	1

OldLevel2:	ds.l	1
OldLevel3:	ds.l	1
OldLevel7:	ds.l	1
ExeptionBase:	ds.l	1
IllegalPtr:	ds.l	1
Old_SR:		ds.l	1

		cnop	0,4
_MonCode:	move.w	$dff006,$dff180
		btst	#6,$bfe001
		bne.s	_MonCode
		rts
******************************************************************************
_RelocEnd:

RelocSize	=	(_RelocEnd-_RelocBeg)-1

	END

