;-------------------------------------------------------------------------------
*                                                                              *
* MFRHelp                                                                      *
*                                                                              *
*                                                                              *
*       Filename        mfrhelp.s                                              *
*       Author          Daniel Weber                                           *
*       Version         0.20                                                   *
*       Start           18.10.93                                               *
*                                                                              *
*       Last Revision   18.10.93                                               *
*                                                                              *
*                                                                              *
*       Written using the ProAsm assembler                                     *
*                                                                              *
;-------------------------------------------------------------------------------

	output	'ram:mfrhelp'

	opt	o+,q+,ow-,qw-			;optimize
	verbose
	super
	MC68040

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

	incdir	'include:'
	incdir	'dh1:routines'

	include	'libraries/asl.i'
	include	'exec/execbase.i'
	include	'exec/memory.i'

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

AbsExecBase			EQU	4

* exec library offsets
_LVOSupervisor			EQU	-30
_LVOForbid			EQU	-132
_LVOPermit			EQU	-138
_LVOAllocMem			EQU	-198
_LVOCloseLibrary		EQU	-414
_LVOSetFunction			EQU	-420
_LVOOpenLibrary			EQU	-552
_LVOCacheClearU			EQU	-636

* utility library offset
_LVOFindTagItem			EQU	-30

* asl library offset
_LVOAslRequest			EQU	-60


;-------------------------------------------------------------------------------
progbase:
	movem.l	d0-a6,-(a7)

	moveq	#36,d0				;open asl.library
	lea	AslName(pc),a1
	move.l	(AbsExecBase).w,a6
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,AslBase			;no asl.library => exit
	beq	.out

	move.l	#patchend-patch,d0		;allocate memory for patch
	move.l	#MEMF_PUBLIC,d1
	jsr	_LVOAllocMem(a6)
	move.l	d0,location			;no memory for patch => exit
	beq	.quit

	lea	patch(pc),a0			;copy patch...
	move.l	d0,a1
	move.w	#patchend-patch-1,d7
.copy:	move.b	(a0)+,(a1)+
	dbra	d7,.copy

;
; clear all caches if needed
;
.clearcaches:
	btst	#1,AttnFlags+1(a6)		;68000/10 installed?
	beq.s	.set
	cmp.w	#36,LIB_VERSION(a6)		;2.0 or higher?
	bcc.s	.exec
	lea	.clear(pc),a5
	jsr	_LVOSupervisor(a6)
	bra.s	.set

.clear:	movec	cacr,d1				;save cache control register
	move.w	#$8000,d2			;small 68040 processor test
	movec	d2,cacr
	movec	cacr,d2
	tst.w	d2				;d2.w=$8xxx => 68040 installed
	bmi.s	.68040
	or.w	#$808,d1			;data and instruction cache clear
	bra.s	.cc
.68040:	cpusha	bc				;68040: clear both caches
.cc:	movec	d1,cacr				;restore cache control register
	rte

.exec:	jsr	_LVOCacheClearU(a6)		;exec used for 2.0 of higher


;
; install patch
;
.set:	move.l	(AbsExecBase).w,a6
	jsr	_LVOForbid(a6)

	move.l	AslBase(pc),a1			;install patch
	lea	_LVOAslRequest,a0
	move.l	location(pc),a5
	move.l	a5,d0
	jsr	_LVOSetFunction(a6)
	move.l	d0,oldFunction-patch(a5)

	jsr	_LVOPermit(a6)

;
; done...
;
.quit:	move.l	AslBase(pc),a1
	jsr	_LVOCloseLibrary(a6)

.out:	movem.l	(a7)+,d0-a6
	moveq	#0,d0
	rts



;-------------------------------------------------------------------------------
*
* the patch
*
;-------------------------------------------------------------------------------

patch:	movem.l	a0/a1,-(a7)			;save structure pointer & taglist
	pea	patch2(pc)			;return address for old function
	move.l	oldFunction(pc),-(a7)	;old Function (returned by SetFunction)
	rts

;
; correct result of MFR's ASLRequest patch
;
patch2:	movem.l	(a7),a0/a1
	tst.l	d0				;no file selected
	beq	.out


	movem.l	d0-a6,-(a7)			;check for DOMULTISELECT
	moveq	#0,d5

	move.l	(AbsExecBase).w,a6		;check for CygnusEd3.5
	move.l	ThisTask(a6),a0
	move.l	LN_NAME(a0),d0			;pointer to name
	beq.s	2$
	move.l	d0,a0
	lea	cedname(pc),a2
.loop:	tst.b	(a2)				;compare task name
	beq.s	.ok
	cmp.b	(a0)+,(a2)+
	beq.s	.loop
	bra.s	2$				;not CygnusEd3.5

.ok:	moveq	#36,d0				;open utility.library
	lea	UtilityName(pc),a1
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,a6
	move.l	a6,d0
	beq.s	2$

	move.l	#ASLFR_Flags1,d0		;search for ASLFR_Flags1 tag
	move.l	_movembytes+4(a7),a0		;= 64(a7) (ced3.5's taglist)
	jsr	_LVOFindTagItem(a6)
	tst.l	d0
	beq.s	1$				;not found
	move.l	d0,a0
	move.l	ti_Data(a0),d0
	btst	#FRB_DOMULTISELECT,d0		;DOMULTISELECT bit set?
	sne	d5
1$:	move.l	#ASLFR_DoMultiSelect,d0		;search for ASLFR_DoMultiSelect
	move.l	_movembytes+4(a7),a0		;= 64(a7)
	jsr	_LVOFindTagItem(a6)
	tst.l	d0
	beq.s	11$				;not found
	move.l	d0,a0
	tst.l	ti_Data(a0)
	sne	d5

11$:	move.l	a6,a1				;close utility.library
	move.l	(AbsExecBase).w,a6
	jsr	_LVOCloseLibrary(a6)

2$:	tst.l	d5				;d5: (0: no... -: DOMULTISELECT)
	movem.l	(a7)+,d0-a6
	beq.s	.out

	tst.l	fr_NumArgs(a0)			;correct the ASL filerequester
	bne.s	.out				;structure...
	addq.l	#1,fr_NumArgs(a0)		;set fr_NumArgs to 1
	tst.l	fr_ArgList(a0)
	bne.s	.out
	mea	(a0),fr_ArgList(a0)		;HACK!!! (for CED) to simulate
.out:	movem.l	(a7)+,a0/a1			;a valid filelist with one entry
	rts

oldFunction:	dc.l	0

cedname:	dc.b	"CygnusEd ver. 3.5",0
UtilityName:	dc.b	"utility.library",0
		even
patchend:


;-------------------------------------------------------------------------------
*
* data
*
;-------------------------------------------------------------------------------

AslName:	dc.b	"asl.library",0
		even


AslBase:	dc.l	0
location:	dc.l	0

	END
