home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Simulation / PDP-8 Simulator / Source Code / Assembler / CodeHelp.c copy < prev    next >
Encoding:
Text File  |  1992-03-13  |  3.5 KB  |  173 lines  |  [TEXT/KAHL]

  1. /************************************************************
  2. *
  3. *
  4. *    Unit containing instruction code helping routines.
  5. *
  6. *    by Adrian Bool in cooperation with Graham Cox.
  7. *
  8. *    copyright © phantasm coding 1992.
  9. *
  10. *
  11. ************************************************************/
  12.  
  13. #include "Global.h"
  14. #include "Code.h"
  15.  
  16.  
  17. void assembleOpcode(pdpTableRef opCode,short pass,rBlock theDestination,short *size,pHandle theProgram)
  18.     {
  19.     (pdp8[opCode]).itsFunction(pass,theDestination,size,theProgram);
  20.     }
  21.     
  22.  
  23. pdpTableRef getOpcode(char *theText)
  24.     {
  25.     short x;
  26.     short found;
  27.     
  28.     found = false;
  29.     
  30.     for (x=1 ; x <= noOfOpcodes ; x++) 
  31.     if (strcmp(theText,pdp8[x].mnemonic) == 0) 
  32.         {
  33.         found = true;
  34.         break;
  35.         } 
  36.     
  37.     if (found)
  38.         return(x);
  39.     else
  40.         {
  41.         AsmError = NoSuchOpcode;
  42.         return(0);
  43.         }
  44.     }
  45.     
  46.  
  47. short getDataSection(char *theData , short *position , str255 result)
  48.     {
  49.     short x = 0;
  50.     
  51.     while(theData[*position] == ',') (*position)++;    /* get to next data item */
  52.     
  53.     while(isalnum(theData[*position]))                /* read in whole of item */ 
  54.         {
  55.         result[x] = theData[*position];
  56.         (*position)++; x++;
  57.         }
  58.         
  59.     result[x] = '\0';                                /* terminate string */
  60.     
  61.     if (result[0] == '\0') return(false);
  62.     else return(true);
  63.     }
  64.             
  65.  
  66. void makeMemoryInstruction(pdpTableRef theInstruction,rBlock theResult,pHandle theProgram)
  67.     {
  68.     short anAddress;
  69.     
  70.     theResult[0] = pdp8[theInstruction].iNumber << 9;        /* enter instuction code in top 4 bits */
  71.     anAddress = getAddress(theProgram);
  72.     theResult[0] |= anAddress;                                /* and put the address in bottom 7 bits */
  73.     }
  74.  
  75.  
  76. addressType getAddress(pHandle theProgram)
  77.     {
  78.     char x;
  79.     str255 aSegment;
  80.     addressType    anAddress,
  81.                 theAddress;
  82.     
  83.     sHandle sourceCode;
  84.     
  85.     sourceCode = (*theProgram)->sourceCode;
  86.     
  87.     getSection(sourceCode,aSegment);
  88.     
  89.     theAddress = 0;
  90.     
  91.     if (aSegment[0] == '*') 
  92.         {
  93.         theAddress |= (1 << 8);                    /* set indirection bit */
  94.         x = 1;
  95.         }
  96.     else x = 0;         
  97.     
  98.     anAddress = decodeNumber(theProgram,aSegment,x);
  99.     
  100.     if (anAddress > 4095) 
  101.         AsmError = AddressOutOfBounds;
  102.     else {
  103.         if (anAddress < 128)
  104.             { /* its zero page */
  105.             theAddress |= (anAddress & 127);          /* allow only bottom 6 bits through */
  106.             }
  107.         else
  108.             {    
  109.             theAddress |= (1 << 7);                    /* set present-page bit */
  110.             if (pageOf(anAddress) == thisPage(theProgram)) 
  111.                 theAddress = theAddress | (anAddress & 127);
  112.             else
  113.                 AsmError = AddressOutOfRange;
  114.             }
  115.         return(theAddress);
  116.         }
  117.     return(0);
  118.     }
  119.     
  120.  
  121. addressType decodeNumber(pHandle Program , char *aSegment , short offset)
  122.     {
  123.     wordType    theValue;
  124.     
  125.     lHandle labelHeader;
  126.     
  127.     labelHeader = (*Program)->labelHeader;
  128.          
  129.      if (isalpha(aSegment[offset]))
  130.          theValue = getLabelValue(labelHeader,&aSegment[offset]);
  131.     
  132.     if (isdigit(aSegment[offset]))
  133.         theValue = atoi(&aSegment[offset]);
  134.     
  135.     if (!(isalpha(aSegment[offset]) || isdigit(aSegment[offset])))
  136.          AsmError = SyntaxError;
  137.     
  138.     if (theValue >4095) 
  139.         {
  140.         AsmError = BadNumber;
  141.         return(0xFFFF);
  142.         }
  143.     else 
  144.         return(theValue);
  145.     }
  146.  
  147. int checkAddress(pHandle theProgram , addressType theAddress)
  148.     {
  149.     if ((theAddress < 4096) && 
  150.             (pageOf(theAddress) == 0) || (pageOf(theAddress) == thisPage(theProgram)))
  151.                     return(true);
  152.     else 
  153.         {
  154.         AsmError = AddressOutOfBounds;
  155.         return(false);
  156.         }
  157.     }
  158.     
  159. short thisPage(pHandle theProgram)
  160.     {
  161.     return(pageOf((*(*theProgram)->objectCode)->address));
  162.     }
  163.  
  164.  
  165. short pageOf(addressType anAddress)
  166.     {
  167.     short page;
  168.     
  169.     page = anAddress & 0xf80;                   /* mask out all except page info */
  170.     page >>= 7;                                  /* move to begining of the integer */
  171.     return(page);
  172.     }
  173.