home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CROSSASM / 68ASMSIM.ZIP / asmsrc / codegen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-09  |  4.9 KB  |  211 lines

  1. /***********************************************************************
  2.  *
  3.  *        CODE.C
  4.  *        Code Generation Routines for 68000 Assembler
  5.  *
  6.  *    Function: output()
  7.  *        Places the data whose size and value are specified onto
  8.  *        the output stream at the current location contained in
  9.  *        global varible loc. That is, if a listing is being
  10.  *        produced, it calls listObj() to print the data in the
  11.  *        object code field of the current listing line; if an
  12.  *        object file is being produced, it calls outputObj() to
  13.  *        output the data in the form of S-records. 
  14.  *
  15.  *        effAddr()
  16.  *        Computes the 6-bit effective address code used by the
  17.  *        68000 in most cases to specify address modes. This code
  18.  *        is returned as the value of effAddr(). The desired
  19.  *        addressing mode is determined by the field of the
  20.  *        opDescriptor which is pointed to by the operand
  21.  *        argument. The lower 3 bits of the output contain the
  22.  *        register code and the upper 3 bits the mode code. 
  23.  *
  24.  *        extWords()
  25.  *        Computes and outputs (using output()) the extension 
  26.  *        words for the specified operand. The generated
  27.  *        extension words are determined from the data contained
  28.  *        in the opDescriptor pointed to by the op argument and
  29.  *        from the size code of the instruction, passed in 
  30.  *        the size argument. The errorPtr argument is used to
  31.  *        return an error code by the standard mechanism. 
  32.  *
  33.  *     Usage: output(data, size)
  34.  *        int data, size;
  35.  *
  36.  *        effAddr(operand)
  37.  *        opDescriptor *operand;
  38.  *
  39.  *        extWords(op, size, errorPtr)
  40.  *        opDescriptor *op;
  41.  *        int size, *errorPtr;
  42.  *
  43.  *      Author: Paul McKee
  44.  *        ECE492    North Carolina State University
  45.  *
  46.  *        Date:    12/13/86
  47.  *
  48.  ************************************************************************/
  49.  
  50.  
  51. #include <stdio.h>
  52. #include "asm.h"
  53.  
  54. extern long    loc;
  55. extern char pass2;
  56. extern FILE *listFile;
  57.  
  58. extern char listFlag;    /* True if a listing is desired */
  59. extern char objFlag;    /* True if an object code file is desired */
  60.  
  61.  
  62. int    output(data, size)
  63. long    data;
  64. int    size;
  65. {
  66.  
  67.     if (listFlag)
  68.         listObj(data, size);
  69.     if (objFlag)
  70.         outputObj(loc, data, size);
  71.  
  72.     return NORMAL;
  73.  
  74. }
  75.  
  76.  
  77. int    effAddr(operand)
  78. opDescriptor *operand;
  79. {
  80.     if (operand->mode == DnDirect)
  81.         return 0x00 | operand->reg;
  82.  
  83.     if (operand->mode == AnDirect)
  84.         return 0x08 | operand->reg;
  85.  
  86.     if (operand->mode == AnInd      )
  87.         return 0x10 | operand->reg;
  88.  
  89.     if (operand->mode == AnIndPost)
  90.         return 0x18 | operand->reg;
  91.  
  92.     if (operand->mode == AnIndPre)
  93.         return 0x20 | operand->reg;
  94.  
  95.     if (operand->mode == AnIndDisp)
  96.         return 0x28 | operand->reg;
  97.  
  98.     if (operand->mode == AnIndIndex)
  99.         return 0x30 | operand->reg;
  100.  
  101.     if (operand->mode == AbsShort)
  102.         return 0x38;
  103.  
  104.     if (operand->mode == AbsLong)
  105.         return 0x39;
  106.  
  107.     if (operand->mode == PCDisp)
  108.         return 0x3A;
  109.  
  110.     if (operand->mode == PCIndex)
  111.         return 0x3B;
  112.  
  113.     if (operand->mode == Immediate)
  114.         return 0x3C;
  115.  
  116.     printf("INVALID EFFECTIVE ADDRESSING MODE!\N");
  117.     exit (0);
  118.  
  119. }
  120.  
  121.  
  122. int    extWords(op, size, errorPtr)
  123. opDescriptor *op;
  124. int size, *errorPtr;
  125. {
  126. long    disp;
  127.  
  128.     if (op->mode == DnDirect ||
  129.          op->mode == AnDirect ||
  130.          op->mode == AnInd ||
  131.          op->mode == AnIndPost ||
  132.          op->mode == AnIndPre)
  133.              { ; }
  134.     else if (op->mode == AnIndDisp ||
  135.          op->mode == PCDisp) {
  136.                 if (pass2) {
  137.                     disp = op->data;
  138.                     if (op->mode == PCDisp)
  139.                         disp -= loc;
  140.                     output(disp & 0xFFFF, WORD);
  141.                     if (disp < -32768 || disp > 32767)
  142.                         NEWERROR(*errorPtr, INV_DISP);
  143.                     }
  144.                   loc += 2;
  145.          }
  146.     else if (op->mode == AnIndIndex ||
  147.          op->mode == PCIndex) {
  148.                 if (pass2) {
  149.                     disp = op->data;
  150.                     if (op->mode == PCIndex)
  151.                         disp -= loc;
  152.                     output((( (int) (op->size) == LONG) ? 0x800 : 0)
  153.                            | (op->index << 12) | (disp & 0xFF), WORD);
  154.                     if (disp < -128 || disp > 127)
  155.                         NEWERROR(*errorPtr, INV_DISP);
  156.                     }
  157.                   loc += 2;
  158.          }
  159.     else if (op->mode == AbsShort) {
  160.                 if (pass2) {
  161.                     output(op->data & 0xFFFF, WORD);
  162.                     if (op->data < -32768 || op->data > 32767)
  163.                         NEWERROR(*errorPtr, INV_ABS_ADDRESS);
  164.                     }
  165.                   loc += 2;
  166.         }
  167.     else if (op->mode == AbsLong) {
  168.                 if (pass2)
  169.                     output(op->data, LONG);
  170.                   loc += 4;
  171.         }
  172.     else if (op->mode == Immediate) {
  173.                 if (!size || size == WORD) {
  174.                     if (pass2) {
  175.                         output(op->data & 0xFFFF, WORD);
  176.  
  177. /*
  178.                         if (op->data < -32768 || op->data > 32767)
  179.                             NEWERROR(*errorPtr, INV_16_BIT_DATA);
  180. */
  181.                         if (op->data > 0xffff)
  182.                             NEWERROR(*errorPtr, INV_16_BIT_DATA);
  183.                         }
  184.                     loc += 2;
  185.                     }
  186.                   else if (size == BYTE) {
  187.                     if (pass2) {
  188.                         output(op->data & 0xFF, WORD);
  189.                         if (op->data < -32768 || op->data > 32767)
  190.                             NEWERROR(*errorPtr, INV_8_BIT_DATA);
  191.                         }
  192.                     loc += 2;
  193.                     }
  194.                   else if (size == LONG) {
  195.                     if (pass2)
  196.                         output(op->data, LONG);
  197.                     loc += 4;
  198.                     }
  199.         }
  200.     else {
  201.         printf("INVALID EFFECTIVE ADDRESSING MODE!\n");
  202.         exit(0);
  203.         }
  204.  
  205.     return NORMAL;
  206.  
  207. }
  208.  
  209.  
  210.  
  211.