;*************************************************************************
;** mmu.library                                                         **
;**                                                                     **
;** a system library for arbitration and control of the MC68K MMUs      **
;**                                                                     **
;** © 1998 THOR-Software, Thomas Richter                                **
;** No commercial use, reassembly, modification without prior, written  **
;** permission of the authors.                                          **
;** Including this library in any commercial software REQUIRES a        **
;** written permission and the payment of a small fee.                  **
;**                                                                     **
;** This is an internal header file, do not depend on anything here.    **
;** Use the official include files.                                     **
;** Distributed only for the mmu.library development group for private  **
;** use.                                                                **
;**                                                                     **
;**---------------------------------------------------------------------**
;** Block: The library base                                             **
;** Initializisation and basic jump vectors of the mmu.library          **
;*************************************************************************

;FOLD Includes
        include INC:macros.asm
        include mu_lib.i
        include mu_version.i
        include mu_alerts.i
        include mu_service.i

;we include just the offsets.
;I personally hate to write _LVO all the time, this is stripped
;in this file.
        include INC:exec_lib.asm
;ENDFOLD
;FOLD External References
        xref DetectMMUType
;ENDFOLD
        section main_code,code

;FOLD DOS Entry
;*************************************************
;** DOS Entry                                   **
;** Since this is not supposed to be executable,**
;** we return -1. This is an indicator for the  **
;** shell not to execute it.                    **
;*************************************************
DosEntry:
        moveq #-1,d0
        rts
;ENDFOLD
;FOLD Resident
;*************************************************
;** Resident                                    **
;** the resident structure of this library      **
;** used for the autoinit function              **
;*************************************************
Resident:
        dc.w $4afc                      ;MatchWord
        dc.l Resident                   ;Back pointer
        dc.l 0                          ;no endskip
        dc.b $80                        ;add to the system, do not jump in
        dc.b Version                    ;version nummer
        dc.b NT_LIBRARY                 ;this is a library
        dc.b 0                          ;priority
        dc.l MMUName                    ;name of the library
        dc.l IDString                   ;ID string
        dc.l Init                       ;pointer to the init data

        section main_data,data          ;we continue in the data
                                        ;segment for consitency
Init:
        dc.l mulib_len                  ;size of the library
        dc.l FuncInit                   ;jump vector init data, below
        dc.l 0                          ;no structinit. I can't stand this mess
        dc.l LibInit                    ;library init vector
;ENDFOLD
;FOLD FuncInit
;*************************************************
;** FuncInit                                    **
;** Library jump vector inits                   **
;*************************************************
FuncInit:
        dc.l OpenMULib
        dc.l CloseMULib
        dc.l ExpungeMULib
        dc.l ExtFunc                    ;Extended function, unused

                                        ;Library functions follow here
        dc.l AllocAligned
        dc.l -1
;ENDFOLD

        section main_code,code

;FOLD Stub routines, reserved routines etc.
        opt c+
        xdef ___stub
___stub:
        opt c-
ExtFunc:
ReservedVector:
        moveq #0,d0
        rts
;ENDFOLD

;FOLD MUAlert
;*************************************************
;** Alert routine                               **
;** Alerts with the alert ID on the stack       **
;** this long word is removed automagically     **
;*************************************************
        xdef MUAlert
MUAlert:
        saveregs d0-d1/d7/a0-a1/a5-a6
        move.l $20(a7),d7                       ;get alert code
        move.l mulib_CurrentContext(a6),a5      ;this as parameter
        move.l mulib_SysBase(a6),a6
        jsr Alert(a6)
        loadregs
        move.l (a7)+,(a7)
        rts
;ENDFOLD

;FOLD LibInit
;*************************************************
;** LibInit                                     **
;** the Library Init vector                     **
;** we're called like this:                     **
;** Library base in d0, ExecBase in a6          **
;** BPTR segment in a0                          **
;**                                             **
;** We must return the initialized library base **
;** in d0, or NULL in case of a failure         **
;*************************************************
LibInit:
        saveregs a4/a6

        move.l d0,a4

        move.l a6,mulib_SysBase(a4)             ;keep SysBase
        move.l a0,mulib_Segment(a4)

        move.b #NT_LIBRARY,mulib_type(a4)       ;type is library
        move.b #(1<<lib_changed)!(1<<lib_sumused),mulib_flags(a4)       ;library flags
        move.l #MMUName,mulib_name(a4)
        move.l #IDString,mulib_idstring(a4)
        move.l #PackedVersion,mulib_version(a4) ;Version, Revision at once

        lea mulib_ContextList(a4),a0
        NewList a0                              ;initialize the context list	

        lea UtilityName,a1
        moveq #37,d0                            ;Version 37 minimum
        jsr OpenLibrary(a6)
        move.l d0,mulib_UtilityBase(a4)
        beq.s .noutility                        ;exit on failure

        move.l a4,a6
        bsr DetectMMUType                       ;read MMU type
        move.b d0,mulib_MMUType(a4)

        bra.s .exit                             ;initialization done
.noutility:
        pea AN_NoUtility
        bsr MUAlert
.failure:
                                                ;in case of a failure,
                                                ;we've to cleanup ourselves
        move.l mulib_SysBase(a4),a6
        move.l a4,a1
        moveq #0,d0
        moveq #0,d1
        move.w mulib_negsize(a4),d0
        move.w mulib_possize(a4),d1
        sub.l d0,a1                             ;*Mem
        add.l d1,d0                             ;length
        jsr FreeMem(a6)                         ;release memory

        sub.l a4,a4                             ;tough luck
.exit:
        move.l a4,d0
        loadregs
        rts
;ENDFOLD
;FOLD OpenMULib
;*************************************************
;** OpenMULib                                   **
;** Open the MMU library                        **
;*************************************************
OpenMULib:
        bclr #lib_delexp,mulib_flags(a6)        ;clear the delayed expunge flag
        addq.w #1,mulib_opencnt(a6)             ;increment the open counter
        move.l a6,d0                            ;that's it
        rts
;ENDFOLD
;FOLD CloseMULib
;*************************************************
;** CloseMULib                                  **
;** Close the MMU library again                 **
;*************************************************
CloseMULib:
        tst.w mulib_opencnt(a6)
        beq.s .ignore
        subq.w #1,mulib_opencnt(a6)             ;Decrement open counter
        btst #lib_delexp,mulib_flags(a6)        ;Delayed expunge active ?
        bne.s ExpungeMULib
.ignore:
        moveq #0,d0                             ;do not quit
        rts
;ENDFOLD
;FOLD ExpungeMULib
;*************************************************
;** ExpungeMULib                                **
;** Remove the MMU library from memory, if      **
;** possible                                    **
;*************************************************
ExpungeMULib:
        bset #lib_delexp,mulib_flags(a6)        ;delay at least
        moveq #0,d0                             ;do not flush
        tst.w mulib_opencnt(a6)                 ;still in use?
        bne.s .exit
        btstm mmuf1_contextopen,mulib_flags1(a6)        ;still an open context?
        beq.s .flush
                                                ;here yes. Something went extremly wrong
        pea AN_ContextOpen
        bsr MUAlert                             ;Create a guru
        moveq #0,d0                             ;do not quit
        bra.s .exit
.flush:
        saveregs a4
        move.l a6,a4
                                                ;restore the vector base

        move.l a6,a1
        Remove                                  ;Remove the library from the system list

        move.l mulib_SysBase(a6),a6
        move.l mulib_UtilityBase(a4),d0
        beq.s .noutility
        move.l d0,a1
        jsr CloseLibrary(a6)
.noutility:

        move.l mulib_Segment(a4),-(a7)          ;Saveback the segment
        move.l a4,a1                            ;->Base Mem
        moveq #0,d0
        moveq #0,d1
        move.w mulib_negsize(a1),d0             ;NegSize->d0
        move.w mulib_possize(a1),d1             ;Possize->d1
        sub.l d0,a1                             ;*Mem
        add.l d1,d0                             ;mem total
        jsr FreeMem(a6)                         ;release

        move.l (a7)+,d0                         ;restore segment
        loadregs
.exit:
        rts
;ENDFOLD

        section main_data,data
;FOLD Strings
MMUName:        dc.b "mmu.library",0
UtilityName:    dc.b "utility.library",0
                dc.b "$VER: "
IDString:       dc.b "mmu "
                TextVersion
                dc.b " "
                TextDate
                dc.b " (c) 1998 The MMU.lib development group, THOR",$0a,$0d,0

;ENDFOLD
