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

		section	runback,code

		incdir	include:
		include	os3_gst.i
		include	aga_regs.i

No	=	0
Yes	=	1

ResetVBR =	No
Vec_1	=	No
Vec_2	=	Yes
Vec_3	=	Yes
Vec_4	=	No
Vec_5	=	No
Vec_6	=	No
Vec_7	=	Yes

REPATCH:	MACRO
		lea	ExceptionTable(pc),a0
		IFNE	Vec_1
		lea	_NewVec64(pc),a1
		move.l	a1,$64(a0)		;patch level 1 interrupt
		ENDC
		IFNE	Vec_2
		lea	_NewVec68(pc),a1
		move.l	a1,$68(a0)		;patch level 2 interrupt
		ENDC
		IFNE	Vec_3
		lea	_NewVec6c(pc),a1
		move.l	a1,$6c(a0)		;patch level 3 interrupt
		ENDC
		IFNE	Vec_4
		lea	_NewVec70(pc),a1
		move.l	a1,$70(a0)		;patch level 4 interrupt
		ENDC
		IFNE	Vec_5
		lea	_NewVec74(pc),a1
		move.l	a1,$74(a0)		;patch level 5 interrupt
		ENDC
		IFNE	Vec_6
		lea	_NewVec78(pc),a1
		move.l	a1,$78(a0)		;patch level 6 interrupt
		ENDC
		IFNE	Vec_7
		lea	_NewVec7c(pc),a1
		move.l	a1,$7c(a0)		;patch level 7 interrupt
		ENDC
		ENDM
*******************************************************************************
rb_hunkstart:	suba.l	a1,a1
		move.l	4.w,a6
		jsr	-294(a6)		; findtask
		move.l	d0,a4
		moveq	#0,d0
		tst.l	$ac(a4)			; pr_cli
		bne.s	rb_fromcli
		lea	$5c(a4),a0		; pr_msgport
		jsr	-384(a6)		; waitport
		lea	$5c(a4),a0
		jsr	-372(a6)		; getmsg

rb_fromcli	move.l	d0,d7			; d7 = wbmsg
		lea	rb_dosname(pc),a1
		jsr	-408(a6)		; openlibrary
		tst.l	d0
		beq.s	rb_dexit
		move.l	d0,a5
		move.l	d0,dbase
		exg	a5,a6			; a6=dos
		move.l	$98(a4),d1		; pr_currentdir
		jsr	-96(a6)			; duplock
		lea	rb_currentdir,a0
		move.l	d0,(a0)
		exg	a5,a6			; a6=exec
		jsr	-132(a6)		; forbid
		lea	rb_progname,a0
		move.l	a0,d1
		moveq	#0,d2
		move.b	9(a4),d2		; priority
		lea	rb_hunkstart-4(pc),a0
		move.l	(a0),d3			; ptr to next segment
		clr.l	(a0)			; unlink next segment
		move.l	#4000,d4
		exg	a5,a6			; a6=dos
		jsr	-138(a6)		; createproc
		exg	a5,a6			; a6=exec
		jsr	-138(a6)		; permit
		move.l	a5,a1
		jsr	-414(a6)		; closelibrary
rb_dexit	tst.l	d7
		beq.s	rb_exit
		jsr	-132(a6)		; forbid
		move.l	d7,a1
		jsr	-378(a6)		; replymsg
rb_exit		moveq	#0,d0
		rts
*******************************************************************************
rb_dosname	dc.b	"dos.library",0
		even
*******************************************************************************

		section maincode,code

start:		suba.l	a1,a1
		move.l	4.w,a6
		jsr	-294(a6)		; findtask

		bsr.s	_ProgStart

		move.l	4.w,a6
		jsr	-132(a6)
		move.l	dbase(pc),a6
		move.l	rb_currentdir(pc),d1
		jsr	-90(a6)

		lea	start-4(pc),a0
		move.l	a0,d1
		moveq	#2,d0
		lsr.l	d0,d1
		jsr	-156(a6)
		moveq	#0,d0
		rts

dbase:		dc.l	0
rb_currentdir	dc.l	0
rb_progname	dc.b	"MFM Monitor v1.0b",0
		even
*******************************************************************************
*-------------- Init the interrupt for monitor activation!

_ProgStart:	movem.l	d0-d7/a0-a7,-(sp)

		move.l	4,a6
		move.l	#_RelocEnd-_RelocBeg,d0
		move.l	#MEMF_PUBLIC|MEMF_CLEAR,d1
		jsr	_LVOAllocMem(a6)
		tst.l	d0
		beq	_NoFreeRam
		move.l	d0,MemBlock
	
*-------------- Copy ALL code to allocated mem!

		lea	_RelocBeg(pc),a0
		move.l	d0,a1
.CopyMem:	move.b	(a0)+,(a1)+
		cmp.l	#_RelocEnd,a0
		bne.s	.CopyMem

		move.l	4,a6
		move.l	#_MonEnd-_MonBeg,d0
		move.l	#MEMF_CHIP|MEMF_CLEAR,d1
		jsr	_LVOAllocMem(a6)
		tst.l	d0
		beq	_NoFreeRam
		move.l	d0,MonMemBlock

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

		lea	_MonBeg(pc),a0
		move.l	d0,a1
.CopyMon:	move.b	(a0)+,(a1)+
		cmp.l	#_MonEnd,a0
		bne.s	.CopyMon

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

		move.l	MemBlock,a0
		jsr	(a0)

*-------------- After this interrupt levels 1-6 are patched with a k/b routine!

.Loop:		move.l	MemBlock,a0
		lea	_Exit-_RelocBeg(a0),a0
		tst.b	(a0)
		beq.s	.Loop

		move.w	CustomBase+intenar,d0

*-------------- Forbit multi tasking!

		move.l	$4,a6
		jsr	_LVOForbid(a6)

		move.l	MonMemBlock,a0
		jsr	$20(a0)		; skip .exe header

*-------------- Ok the last thing, permit the multi tasking!

		jsr	_LVOPermit(a6)


*-------------- We don't want the monitor so set the flag!

		move.l	MemBlock,a0
		lea	_Exit-_RelocBeg(a0),a0
		sf.b	(a0)

		bra.s	.Loop

*-------------- Forbit multi tasking!

		move.l	$4,a6
		jsr	_LVOForbid(a6)

*-------------- And free are memory block.

		lea	MemBlock(pc),a1
		move.l	(a1),a1
		move.l	#_RelocEnd-_RelocBeg,d0
		jsr	_LVOFreeMem(a6)

		lea	MemBlock(pc),a1
		clr.l	(a1)

*-------------- Ok the last thing, permit the multi tasking!

		jsr	_LVOPermit(a6)

		movem.l	(sp)+,d0-d7/a0-a7
		clr.l	d0
		rts			;exit to amigados

_NoFreeRam:	move.l	#103,d0
		movem.l	(sp)+,d0-d7/a0-a7
		rts
******************************************************************************
_RelocBeg:	lea	0,a0
		lea	ExceptionTable(pc),a1
		move.l	#256-1,d0
.Reloc_1:	move.l	(a0)+,(a1)+		;copy exception table to ram
		dbra	d0,.Reloc_1

		REPATCH

_DoPatch:	move.l	$80,-(sp)		;store trap 0 vector
		lea	_Patch_VBR(pc),a0
		move.l	a0,$80
		trap	#0			;execute patchvbr in supervisor mode
		move.l	(sp)+,$80		;restore trap 0 vector
		rts
*******************************************************************************
*-------------- Patch vector base register to new exception table.

_Patch_VBR:	lea	ExceptionTable(pc),a0
		movec	a0,VBR
		rte
*******************************************************************************
*-------------- New vectors to be called before old vectors at 0-3FF.

_NewVec64:	bsr	_CheckKey		; level 1
		move.l	$64,-(sp)
		rts
_NewVec68:	bsr	_CheckKey		; level 2
		move.l	$68,-(sp)
		rts
_NewVec6c:	bsr	_CheckRMB		; level 3
		move.l	$6c,-(sp)
		rts
_NewVec70:	bsr	_CheckKey		; level 4
		move.l	$70,-(sp)
		rts
_NewVec74:	bsr	_CheckKey		; level 5
		move.l	$74,-(sp)
		rts
_NewVec78:	bsr	_CheckKey		; level 6
		move.l	$78,-(sp)
		rts
_NewVec7c:	bsr	_CheckButton		; level 7
		move.l	$7c,-(sp)
		rts
*******************************************************************************
*-------------- Check 4 the level 7 button

_CheckButton:	movem.l	d0-d7/a0-a6,-(sp)

		lea	0,a0
		lea	ExceptionTable(pc),a1
		move.l	#256-1,d0
.Reloc:		move.l	(a0)+,(a1)+		;copy exception table to ram
		dbra	d0,.Reloc

		REPATCH

*-------------- We want the monitor so set the flag!

		lea	_Exit(pc),a0
		st.b	(a0)

*-------------- Reset the VBR

		IFNE	ResetVBR
		move.l	#0,d0
		movec	d0,VBR
		ENDC

		movem.l	(sp)+,d0-d7/a0-a6
		rts
*******************************************************************************
*-------------- Check the keyboard for the * key

_CheckKey:	movem.l	d0-d7/a0-a6,-(sp)
		lea	0,a0
		lea	ExceptionTable(pc),a1
		move.l	#256-1,d0
	
.Reloc_2:	move.l	(a0)+,(a1)+		;copy exception table to ram
		dbra	d0,.Reloc_2

		REPATCH

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

*-------------- We want the monitor so set the flag!

		lea	_Exit(pc),a0
		st.b	(a0)

*-------------- Reset the VBR

		IFNE	ResetVBR
		move.l	#0,d0
		movec	d0,VBR
		ENDC

.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

		movem.l	(sp)+,d0-d7/a0-a6
		rts
*******************************************************************************
_CheckRMB:	movem.l	d0-d7/a0-a6,-(sp)
		lea	0,a0
		lea	ExceptionTable(pc),a1
		move.l	#256-1,d0
	
.Reloc_3:	move.l	(a0)+,(a1)+		;copy exception table to ram
		dbra	d0,.Reloc_3

		REPATCH

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

*-------------- We want the monitor so set the flag!

		lea	_Exit(pc),a0
		st.b	(a0)

*-------------- Reset the VBR

		IFNE	ResetVBR
		move.l	#0,d0
		movec	d0,VBR
		ENDC

.Exit:		movem.l	(sp)+,d0-d7/a0-a6
		rts
******************************************************************************
_Exit:		ds.b	1
		even
dmastore:	ds.w	1
intstore:	ds.w	1
ExceptionTable:	ds.l	256
MemBlock:	ds.l	1
MonMemBlock:	ds.l	1

_RelocEnd:
******************************************************************************
_MonBeg:
_MonData:	incbin	hf:MFM-Mon/MFM.exe
_MonEnd:
	END

ARIV patches these vectors

2-12 = 8,c,10,14,18,1c,20,24,28,2c,30

24-48 = 60,64,68,6c,70,74,78,7c,80,84,88,8c,90,94,98,9c,a0,a4,a8,ac,b0,b4,b8,bc,c0
