/*************************************************************************
 ** 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 sample source demonstrates how to print out the mapping of the **
 ** MMU library. It's here done for the default context                 **
 *************************************************************************/

#include <exec/types.h>
#include <proto/exec.h>
#include <proto/mmu.h>

#include <mmu/mmubase.h>
#include <mmu/context.h>
#include <mmu/config.h>

#include <stdio.h>

struct MMUBase *MMUBase;

char version[]="$VER: MMUScan 0.02 (19.9.98) © THOR";

int main(int argc,char **argv)
{
struct MinList *list;
struct MappingNode *mn;
struct MMUConfig *mcf;

        if (MMUBase=(struct MMUBase *)OpenLibrary(MMU_NAME,MMU_VERSION)) {
                /* Read the MMU type */
                switch (GetMMUType()) {
                        case MUTYPE_NONE:
                                printf("No MMU available.\n");
                                break;
                        case MUTYPE_68851:
                                printf("68851 MMU detected.\n");
                                break;
                        case MUTYPE_68030:
                                printf("68030 MMU detected.\n");
                                break;
                        case MUTYPE_68040:
                                printf("68040 MMU detected.\n");
                                break;
                        case MUTYPE_68060:
                                printf("68060 MMU detected.\n");
                                break;
                };

                /* Read now the page size of the MMU for the default
                   (public) context */
                printf("MMU page size is 0x%lx bytes.\n",GetPageSize(NULL));

                /* Read the MMU configuration. There's usually little reason
                   to do that except for debugging. */
                if (mcf=GetMMUConfig()) {
                        printf("User root pointer: 0x%08lx%08lx\n",mcf->mcf_UserRootPointer[0],
                                                                 mcf->mcf_UserRootPointer[1]);
                        printf("Supervisor root pointer: 0x%08lx%08lx\n",mcf->mcf_SupervisorRootPointer[0],
                                                                       mcf->mcf_SupervisorRootPointer[1]);
                        printf("Translation Control           : 0x%08lx\n",mcf->mcf_TranslationControl);
                        printf("Data Transparent translation 0: 0x%08lx\n",mcf->mcf_DTT0);
                        printf("Data Transparent translation 1: 0x%08lx\n",mcf->mcf_DTT1);
                        printf("Inst Transparent translation 0: 0x%08lx\n",mcf->mcf_ITT0);
                        printf("Inst Transparent translation 1: 0x%08lx\n",mcf->mcf_ITT1);

                        ReleaseMMUConfig(mcf);
                }

                /* Get the mapping of the default context */
                list=GetMapping(NULL);
                /* next, we print it out. Usually, you should
                   make a copy first since the context is now
                   blocked, i.e. nobody else will be allowed to
                   make changes to it. */
                for(mn=(struct MappingNode *)(list->mlh_Head);mn->map_succ;mn=mn->map_succ) {

                        printf("Memory region: %08lx to %08lx ",
                                        mn->map_Lower,mn->map_Higher);

                        if (mn->map_Properties & MAPP_WRITEPROTECTED)
                                printf("write protected ");

                        if (mn->map_Properties & MAPP_TRANSLATED)
                                printf("transparently translated ");

                        if (mn->map_Properties & MAPP_ROM)
                                printf("read only (writes tolerated) ");

                        if (mn->map_Properties & MAPP_USERPAGE0)
                                printf("user page attribute 0");

                        if (mn->map_Properties & MAPP_USERPAGE1)
                                printf("user page attribute 1");

                        if (mn->map_Properties & MAPP_CACHEINHIBIT)
                                printf("cache inhibited ");

                        if (mn->map_Properties & MAPP_IMPRECISE)
                                printf("imprecise exception ");

                        if (mn->map_Properties & MAPP_NONSERIALIZED)
                                printf("non-serialized access ");

                        if (mn->map_Properties & MAPP_COPYBACK)
                                printf("copyback cache mode ");

                        if (mn->map_Properties & MAPP_SUPERVISORONLY)
                                printf("supervisor only ");

                        if (mn->map_Properties & MAPP_INVALID)
                                printf("invalid, ID %08lx ",mn->map_un.map_UserData);

                        if (mn->map_Properties & MAPP_SWAPPED)
                                printf("swapped out, ID %08lx ",mn->map_un.map_UserData);

                        if (mn->map_Properties & MAPP_INDIRECT)
                                printf("remapped to %08lx ",mn->map_un.map_Delta+mn->map_Lower);

                        if (mn->map_Properties & MAPP_BLANK)
                                printf("blank space ");

                        if (mn->map_Properties & MAPP_SHARED)
                                printf("shared with public context ");

                        printf("\n");
                }

                /* now release it */
                ReleaseMapping(NULL);

                CloseLibrary((struct Library *)MMUBase);
        } else  printf("Can't open mmu.library.\n");

        return 0;
}

