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

		incdir	include:
		include	macros.i
		include	aga_regs.i

No	=	0
Yes	=	1

Vec_2	=	Yes			; "*"
Vec_3	=	Yes			; rmb
Vec_7	=	Yes			; level 7 butt

*******************************************************************************
*-------------- 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:	IFNE	Vec_2
		bsr	_InitLevel2
		ENDC
		IFNE	Vec_3
		bsr	_InitLevel3
		ENDC
		IFNE	Vec_7
		bsr	_InitLevel7
		ENDC
		rts
******************************************************************************
_InitLevel2:	PUSH	a0/a1
		move.l	ExeptionBase(pc),a0
		lea	_NewVec68(pc),a1
		move.l	a1,$68(a0)		;patch level 2 interrupt
		PULL	a0/a1
		rts
_InitLevel3:	PUSH	a0/a1
		move.l	ExeptionBase(pc),a0
		lea	_NewVec6c(pc),a1
		move.l	a1,$6c(a0)		;patch level 3 interrupt
		PULL	a0/a1
		rts
_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
******************************************************************************
*-------------- New vectors to be called before old vectors at 0-3FF.

_NewVec68:	PUSHALL				; level 2
		lea	OldLevel2(pc),a0
		move.l	$68.w,(a0)
		lea	_Blank(pc),a0
		move.l	ExeptionBase(pc),a1
		move.l	a0,$68(a1)
		bsr	_CheckKey
		move.l	ExeptionBase(pc),a1
		move.l	OldLevel2(pc),$68(a1)
		bsr	_InitLevel2
		PULLALL
		rte
_NewVec6c:	PUSHALL				; level 3
		lea	OldLevel3(pc),a0
		move.l	$6c.w,(a0)
		lea	_Blank(pc),a0
		move.l	ExeptionBase(pc),a1
		move.l	a0,$6c(a1)
		bsr	_CheckRMB
		move.l	ExeptionBase(pc),a1
		move.l	OldLevel3(pc),$6c(a1)
		bsr	_InitLevel3
		PULLALL
		rte
_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
*******************************************************************************
*-------------- Check the keyboard for the * key

_CheckKey:	PUSHALL

		move.b	$bfec01,d0		; get key code
		bset	#6,$bfee01		; start ciaa
		cmpi.b	#$45,d0
		bne	.Exit

		bsr	_MonCode

.Exit:		lea	$dff006,a1
		moveq	#4-1,d2
.Key6:		move.b	(a1),d3
.Key1:		cmp.b	(a1),d3			; wait 4 rasters... before ciaa
		beq.b	.Key1			; stop.. ack`ing keyboard
		dbra	d2,.Key6

		bclr	#6,$bfee01.l		; stop ciaa - acknowlege kb

		PULLALL
		rts
*******************************************************************************
_CheckRMB:	PUSHALL

		btst	#$0a,$dff016
		bne.s	.Exit

		bsr	_MonCode

.Exit:		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

_MonCode:	move.w	$dff006,$dff180
		btst	#6,$bfe001
		bne.s	_MonCode
		rts


;	incbin	hf:MFM-Mon/MFM.exe

******************************************************************************
_RelocEnd:

RelocSize	=	(_RelocEnd-_RelocBeg)-1

	END

