home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 299.lha / MMU_Mon_v1.0 / MMU_MON.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-12-04  |  10.0 KB  |  440 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /*        MMU_MON.c written by Michael R. Mossman in Oct 89              */
  4. /*                and released to Public Domain.                         */
  5. /*                                                                       */
  6. /*************************************************************************/
  7.  
  8.  
  9. #include <exec/types.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <proto/exec.h>
  13.  
  14. #define PHY_MASK 0xffffff00
  15. #define LOG_MASK 0x000000ff
  16. #define VERSION  1
  17.  
  18. struct MMUM
  19.           {
  20.        ULONG LOGICAL_ADDRESS,*PHYSICAL_ADDRESS;
  21.       }; 
  22.  
  23.  
  24. struct MMU
  25.           {
  26.        struct MMUM myMMUM;
  27.        ULONG UCRP,LCRP,USRP,LSRP,UDRP,LDRP,TC;
  28.            UWORD AC,PSR,PCSR;
  29.        UBYTE CAL,VAL,SCC,BLANK;
  30.           }myMMU;
  31.  
  32. extern ULONG far MMU_PRESENT;
  33. extern USHORT far EXCEPTION;
  34.  
  35. ULONG WhatReg(),Read(),Table_Lookup(),Startup(),DisplayMem(),Read_MMU();
  36. ULONG Read_Table(),Write_MMU(),Translate();
  37. void MyExit(),CheckException(),Help(),EditMem();      
  38.  
  39. main()
  40.  
  41. {
  42.    UBYTE c;
  43.    ULONG error;
  44.    
  45.    error=Startup();
  46.    if(error==0 && MMU_PRESENT==0) exit(0L); 
  47.    printf("Command>");
  48.    while (c=getchar())
  49.          {
  50.            switch (c)
  51.           {
  52.         case'\n':
  53.              continue;
  54.         case 'r':
  55.         case 'R':
  56.                      error=Read();
  57.              if(error==0) MyExit();
  58.              break;
  59.         case 'd':
  60.         case 'D':
  61.              error=DisplayMem();
  62.              if(error==0) MyExit();
  63.              break;
  64.         case 'w':
  65.         case 'W':
  66.              error=WhatReg();
  67.              if(error==0) MyExit();
  68.              break;
  69.         case 'e':
  70.         case 'E':
  71.              EditMem();
  72.              break;
  73.         case 't':
  74.         case 'T':
  75.              error=Translate();
  76.              if(error==0) MyExit();
  77.              break;     
  78.         case 'x':
  79.         case 'X':
  80.              MyExit();
  81.         case 'h':
  82.         case 'H':
  83.         case '?':
  84.              Help();
  85.              break;
  86.         default:
  87.              printf("Unrecognized Character\n\n");
  88.              break;
  89.         }
  90.          printf("Command>");
  91.       }
  92. }    
  93.     
  94.  
  95. ULONG Read()
  96.  
  97. {
  98.     ULONG error;
  99.  
  100.         error=Read_MMU();
  101.     if (error==0) return(0L);
  102.     printf("\n\nCRP %08.X %08.X      ",myMMU.UCRP,myMMU.LCRP);
  103.     printf("SRP %08.X %08.X      ",myMMU.USRP,myMMU.LSRP);
  104.     printf("DRP %08.X %08.X\n\n",myMMU.UDRP,myMMU.LDRP);
  105.     printf("TC %08.X   ",myMMU.TC);
  106.     printf("AC %04.X   ",myMMU.AC);
  107.     printf("PSR %04.X   ",myMMU.PSR);
  108.     printf("PCSR %04.X   ",myMMU.PCSR);
  109.     printf("CAL %02.X   ",myMMU.CAL);
  110.     printf("VAL %02.X   ",myMMU.VAL);
  111.     printf("SCC %02.X\n\n",myMMU.SCC);
  112.     return(1L);
  113. }
  114.  
  115.  
  116.  
  117.  
  118. ULONG Table_Lookup()
  119.  
  120. {
  121.       ULONG error,paddress,laddress;
  122.       
  123.       error=Read_Table();
  124.       if(error==0 & MMU_PRESENT!=0)
  125.         {
  126.       printf("There is an MMU, but no address translation is being done.\n\n");
  127.           return (0L);
  128.     }
  129.       if(error==0 & MMU_PRESENT==0) return (0L);
  130.       laddress=myMMU.myMMUM.LOGICAL_ADDRESS & LOG_MASK;
  131.       paddress=(*myMMU.myMMUM.PHYSICAL_ADDRESS & PHY_MASK)+laddress;
  132.       printf("LOGICAL ADDRESS %08.X       IS MAPPED TO       PHYSICAL ADDRESS %08.X\n\n",myMMU.myMMUM.LOGICAL_ADDRESS,paddress);
  133.       return(1L);
  134.       
  135. }      
  136.  
  137.  
  138. ULONG Startup()
  139.  
  140. {
  141.     ULONG error;
  142.     
  143.     printf("\n\nMMU MON is a  monitor for the MC68851 Paged Memory Management Unit.\n\n");
  144.         printf("Select \"H\" for help.\n\n");
  145.     printf("Written by Michael R. Mossman in October 1989.\n");
  146.     printf("I can be contacted on CompuServe UI 76515,3017 or Punter Net node 94.\n");
  147.         error=Read();
  148.         if (error==0 & MMU_PRESENT==0)
  149.        {
  150.          printf("\n\nI can't help if no MMU is in your system!!!\n\n\n");
  151.          return(0L);
  152.        }
  153.         myMMU.myMMUM.LOGICAL_ADDRESS=0xfc0000;
  154.     error=Table_Lookup();
  155.     if (error==0) return (0L);
  156.     return(1L);
  157.     
  158. }    
  159.  
  160. ULONG WhatReg()
  161.  
  162. {
  163.   UBYTE buff[20];
  164.   ULONG num,error;
  165.   USHORT x=1,y=0;
  166.     
  167.   
  168.   while(x!=0)
  169.        {
  170.      printf("What register (eg.- CRP) ?\n>");
  171.          scanf("%5s",buff);
  172.     
  173.          if((x=strcmpi(buff,"CRP"))==0) 
  174.             {
  175.         y=1;
  176.                 continue;
  177.         }
  178.           if((x=strcmpi(buff,"SRP"))==0) 
  179.             {
  180.         y=2;
  181.                 continue;
  182.         }
  183.       if((x=strcmpi(buff,"DRP"))==0) 
  184.             {
  185.         y=3;
  186.                 continue;
  187.         }
  188.   
  189.           if((x=strcmpi(buff,"TC"))==0) 
  190.             {
  191.         y=4;
  192.                 continue;
  193.         }
  194.  
  195.           if((x=strcmpi(buff,"AC"))==0) 
  196.             {
  197.         y=5;
  198.                 continue;
  199.         }
  200.  
  201.           if((x=strcmpi(buff,"PSR"))==0) 
  202.             {
  203.         y=6;
  204.                 continue;
  205.         }
  206.           
  207.       if((x=strcmpi(buff,"PCSR"))==0) 
  208.             {
  209.         y=7;
  210.                 continue;
  211.         }
  212.           
  213.       if((x=strcmpi(buff,"CAL"))==0) 
  214.             {
  215.         y=8;
  216.                 continue;
  217.         }
  218.  
  219.           if((x=strcmpi(buff,"VAL"))==0) 
  220.             {
  221.         y=9;
  222.                 continue;
  223.         }
  224.           
  225.       if((x=strcmpi(buff,"SCC"))==0) 
  226.             {
  227.         y=10;
  228.                 continue;
  229.         }
  230.  
  231.          }
  232.      if(y!=7) printf("Hex value for ");
  233.      switch (y)
  234.           {
  235.         case 1:
  236.             printf("upper long word ?\n>");
  237.         scanf("%lx",&num);
  238.                 myMMU.UCRP=num;
  239.         printf("lower long word ?\n>");
  240.         scanf("%lx",&num);
  241.         myMMU.LCRP=num;
  242.                 error=Write_MMU(&myMMU.UCRP);
  243.                 break;
  244.             case 2:
  245.             printf("upper long word ?\n>");
  246.         scanf("%lx",&num);
  247.                 myMMU.USRP=num;
  248.         printf("lower long word ?\n>");
  249.         scanf("%lx",&num);
  250.         myMMU.LSRP=num;
  251.                 error=Write_MMU(&myMMU.USRP);
  252.                 break;
  253.  
  254.             case 3:
  255.             printf("upper long word ?\n>");
  256.         scanf("%lx",&num);
  257.                 myMMU.UDRP=num;
  258.         printf("lower long word ?\n>");
  259.         scanf("%lx",&num);
  260.         myMMU.LDRP=num;
  261.                 error=Write_MMU(&myMMU.UDRP);
  262.                 break;
  263.  
  264.             case 4:
  265.             printf("long word ?\n>");
  266.         scanf("%lx",&num);
  267.                 myMMU.TC=num;
  268.                 error=Write_MMU(&myMMU.TC);
  269.                 break;
  270.  
  271.             case 5:
  272.             printf("word ?\n>");
  273.         scanf("%lx",&num);
  274.         if(num>0xffff) printf("Number bigger then \"word\" size entered.\n");
  275.                 myMMU.AC=(USHORT)num;
  276.                 error=Write_MMU(&myMMU.AC);
  277.                 break;
  278.             
  279.         case 6:
  280.             printf("word ?\n>");
  281.         scanf("%lx",&num);
  282.         if(num>0xffff) printf("Number bigger then \"word\" size entered.\n");
  283.                 myMMU.PSR=(USHORT)num;
  284.                 error=Write_MMU(&myMMU.PSR);
  285.                 break;
  286.             
  287.         case 7:
  288.             printf("This is a read only register.\n");
  289.                 break;
  290.             
  291.         case 8:
  292.             printf("byte ?\n>");
  293.         scanf("%lx",&num);
  294.         if(num>0xff) printf("Number bigger then \"byte\" size entered.\n");
  295.                 myMMU.CAL=(UBYTE)num;
  296.                 error=Write_MMU(&myMMU.CAL);
  297.                 break;
  298.  
  299.             case 9:
  300.             printf("byte ?\n>");
  301.         scanf("%lx",&num);
  302.         if(num>0xff) printf("Number bigger then \"byte\" size entered.\n");
  303.                 myMMU.VAL=(UBYTE)num;
  304.                 error=Write_MMU(&myMMU.VAL);
  305.                 break;
  306.  
  307.             case 10:
  308.             printf("byte ?\n>");
  309.         scanf("%lx",&num);
  310.         if(num>0xff) printf("Number bigger then \"byte\" size entered.\n");
  311.                 myMMU.SCC=(BYTE)num;
  312.                 error=Write_MMU(&myMMU.SCC);
  313.                 break;
  314.       }
  315.     if(EXCEPTION!=0) CheckException();      
  316.     Read();
  317.     return(error);  
  318. }      
  319.       
  320. ULONG DisplayMem()
  321. {
  322.     
  323.    UBYTE *c,*d;
  324.    ULONG start,end;
  325.    
  326.    printf("Start address in hex ?\n>");
  327.    scanf("%lx",&start);
  328.    printf("End address in hex ?\n>");
  329.    scanf("%lx",&end);
  330.    if(end<=start) end=start+352;
  331.      
  332.        
  333.    
  334.    printf("\n");
  335.    
  336.    for(;start<end;)
  337.     {
  338.     c=(UBYTE *)start;
  339.     printf("%08X ",c);
  340.     
  341.     for(d=(UBYTE *)start+16;c<d;c++)
  342.        {
  343.         
  344.         printf("%02X ",*c);
  345.        
  346.        }
  347.     
  348.     printf(" \"");
  349.     
  350.     for(c=(UBYTE *)start,d=(UBYTE *)start+16;c<d;c++)
  351.        {
  352.          if(*c>31 & *c<127 | *c>159 & *c<=255)
  353.            {
  354.          printf("%c",*c);
  355.            }
  356.          else printf(".");
  357.         
  358.        }
  359.     
  360.     printf("\"\n");
  361.     start=start+16;
  362.     }
  363.    return(1L);     
  364. }
  365.  
  366.  
  367. ULONG Translate()
  368.  
  369. {
  370.    ULONG num,error;
  371.     
  372.    printf("Enter logical address (for logical to physical translation) ?\n>"); 
  373.    scanf("%lx",&num);
  374.    myMMU.myMMUM.LOGICAL_ADDRESS=num;
  375.    printf("\n");
  376.    error=Table_Lookup();
  377.    if(error==0) printf("TC register must be 0x80000000 or greater for mappings to take place!\n\n"); 
  378.    return(1L);
  379.    
  380. }   
  381.  
  382. void CheckException()
  383.  
  384. {
  385.   printf("\nYour last write operation caused a %#04.X exception error!!!!\n\n",EXCEPTION);
  386.   if(EXCEPTION==56) printf("Bad data was moved into the TC, CRP, SRP or DRP register.\n");
  387.   if(EXCEPTION==57) printf("PLoad instruction was executed with the enable bit of the TC register clear.\n");
  388.   if(EXCEPTION==58) printf("PValid instruction failed\n");
  389.   EXCEPTION=0L;
  390. }
  391.  
  392. void MyExit()
  393.  
  394. {
  395.     
  396.   End(); 
  397.   exit(0L);
  398.     
  399. }   
  400.  
  401.  
  402. void Help()
  403.  
  404. {
  405.    printf("\n\n\n\n                          MMU MON     HELP FILE\n\n\n\n\n\n");
  406.    printf(" R - Read the MMU registers.\n\n");
  407.    printf(" W - Write to one of the MMU registers.\n\n");
  408.    printf(" T - Translate a logical address to a physical address.\n\n");
  409.    printf(" D - Display memory in hex and ascii.\n\n");
  410.    printf(" E - Edit memory in hex.\n\n");
  411.    printf(" X - Exit this program.\n\n");
  412.    printf(" H - This help file. (I wonder how you got here?)\n\n");
  413.    
  414. }
  415.    
  416.  
  417. void EditMem()
  418.  
  419. {
  420.   ULONG start,num;
  421.   UBYTE *c,ch;
  422.  
  423.   printf("\n\n");
  424.   printf("Enter start address in hex for editing. Return to exit edit mode.\n>");
  425.   scanf("%lx",&start);
  426.   getchar();
  427.   printf("\n\n");
  428.   while (1)
  429.       {
  430.         c=(UBYTE *)start;
  431.         printf("%08X %02X     ",c,*c);
  432.     if((ch=getchar())!='\n') ungetc(ch,stdin);
  433.     if(ch=='\n') { printf("\n\n"); return; }
  434.         scanf("%lx",&num);
  435.     getchar();
  436.     *c=(UBYTE)num;
  437.     start++;
  438.       }
  439. }
  440.