home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / m / motorola / !AsRef / Sources / c / do5 < prev    next >
Encoding:
Text File  |  1993-07-18  |  6.6 KB  |  222 lines

  1. #define HC05
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. #include "mselect.h"    /*external selection of microprocessor symbol table*/
  8. #include "proto.h"
  9. #include "as.h"
  10. #include "extvars.h"
  11. #include "structs.h"
  12. /*
  13.  * MC6805 specific processing
  14.  */
  15.  
  16. /* addressing modes */
  17. #define IMMED   0               /* immediate */
  18. #define IND     1               /* indexed */
  19. #define OTHER   2               /* NOTA */
  20.  
  21. /*
  22.  * localinit --- machine specific initialization
  23.  */
  24. void localinit(void)
  25. {
  26. }
  27.  
  28. /*
  29.  * do_op --- process mnemonic
  30.  * 
  31.  * Called with the base opcode and it's class. Optr points to the beginning of
  32.  * the operand field.
  33.  */
  34. void 
  35. do_op(int opcode, int class)
  36. /* mnemonic class */
  37. {
  38.         int             dist;   /* relative branch distance */
  39.         int             amode;  /* indicated addressing mode */
  40.         char           *peek;
  41.  
  42.         /* guess at addressing mode */
  43.         peek = Optr;
  44.         amode = OTHER;
  45.         while (!delim(*peek) && *peek != EOS)   /* check for comma in operand
  46.                                                  * field */
  47.                 if (*peek++ == ',') {
  48.                         amode = IND;
  49.                         break;
  50.                 }
  51.         if (*Optr == '#')
  52.                 amode = IMMED;
  53.  
  54.         switch (class) {
  55.         case INH:               /* inherent addressing */
  56.                 emit(opcode);
  57.                 return;
  58.         case GEN:               /* general addressing */
  59.                 do_gen(opcode, amode);
  60.                 return;
  61.         case REL:               /* short relative branches */
  62.                 eval();
  63.                 dist = Result - (Pc + 2);
  64.                 emit(opcode);
  65.                 if ((dist > 127 || dist < -128) && Pass == 2) {
  66.                         error("Branch out of Range");
  67.                         emit(lobyte(-2));
  68.                         return;
  69.                 }
  70.                 emit(lobyte(dist));
  71.                 return;
  72.         case NOIMM:
  73.                 if (amode == IMMED) {
  74.                         error("Immediate Addressing Illegal");
  75.                         return;
  76.                 }
  77.                 do_gen(opcode, amode);
  78.                 return;
  79.         case GRP2:
  80.                 if (amode == IND) {
  81.                         do_indexed(opcode + 0x20);
  82.                         return;
  83.                 }
  84.                 eval();
  85.                 Cycles += 2;
  86.                 if (Force_byte) {
  87.                         emit(opcode);
  88.                         emit(lobyte(Result));
  89.                         return;
  90.                 }
  91.                 if (Result >= 0 && Result <= 0xFF) {
  92.                         emit(opcode);
  93.                         emit(lobyte(Result));
  94.                         return;
  95.                 }
  96.                 error("Extended Addressing not allowed");
  97.                 return;
  98.         case SETCLR:
  99.         case BTB:
  100.                 eval();
  101.                 if (Result < 0 || Result > 7) {
  102.                         error("Bit Number must be 0-7");
  103.                         return;
  104.                 }
  105.                 emit(opcode | (Result << 1));
  106.                 if (*Optr++ != ',')
  107.                         error("SYNTAX");
  108.                 eval();
  109.                 emit(lobyte(Result));
  110.                 if (class == SETCLR)
  111.                         return;
  112.                 /* else it's bit test and branch */
  113.                 if (*Optr++ != ',')
  114.                         error("SYNTAX");
  115.                 eval();
  116.                 dist = Result - (Old_pc + 3);
  117.                 if ((dist > 127 || dist < -128) && Pass == 2) {
  118.                         error("Branch out of Range");
  119.                         emit(lobyte(-3));
  120.                         return;
  121.                 }
  122.                 emit(lobyte(dist));
  123.                 return;
  124.         default:
  125.                 fatal("Error in Mnemonic table");
  126.         }
  127. }
  128.  
  129. /*
  130.  * do_gen --- process general addressing
  131.  */
  132. void do_gen(int op, int mode)
  133. {
  134.         if (mode == IMMED) {
  135.                 Optr++;
  136.                 emit(op);
  137.                 eval();
  138.                 emit(lobyte(Result));
  139.                 return;
  140.         } else if (mode == IND) {
  141.                 do_indexed(op + 0x30);
  142.                 return;
  143.         } else if (mode == OTHER) {     /* direct or extended addressing */
  144.                 eval();
  145.                 if (Force_word) {
  146.                         emit(op + 0x20);
  147.                         eword(Result);
  148.                         Cycles += 3;
  149.                         return;
  150.                 }
  151.                 if (Force_byte) {
  152.                         emit(op + 0x10);
  153.                         emit(lobyte(Result));
  154.                         Cycles += 2;
  155.                         return;
  156.                 }
  157.                 if (Result >= 0 && Result <= 0xFF) {
  158.                         emit(op + 0x10);
  159.                         emit(lobyte(Result));
  160.                         Cycles += 2;
  161.                         return;
  162.                 } else {
  163.                         emit(op + 0x20);
  164.                         eword(Result);
  165.                         Cycles += 3;
  166.                         return;
  167.                 }
  168.         } else {
  169.                 error("Unknown Addressing Mode");
  170.                 return;
  171.         }
  172. }
  173.  
  174. /*
  175.  * do_indexed --- handle all wierd stuff for indexed addressing
  176.  */
  177. void do_indexed(int op)
  178. {
  179.         eval();
  180.         if (!(*Optr++ == ',' && (*Optr == 'x' || *Optr == 'X')))
  181.                 warn("Indexed Addressing Assumed");
  182.         if (Force_word) {
  183.                 if (op < 0x80) {/* group 2, no extended addressing */
  184.                         emit(op + 0x10);        /* default to one byte
  185.                                                  * indexed */
  186.                         emit(lobyte(Result));
  187.                         Cycles += 3;
  188.                         return;
  189.                 }
  190.                 emit(op);
  191.                 eword(Result);
  192.                 Cycles += 4;
  193.                 return;
  194.         }
  195.         Cycles += 3;            /* assume 1 byte indexing */
  196.         if (Force_byte) {
  197.                 emit(op + 0x10);
  198.                 emit(lobyte(Result));
  199.                 return;
  200.         }
  201.         if (Result == 0) {
  202.                 emit(op + 0x20);
  203.                 Cycles--;       /* ,x slightly faster */
  204.                 return;
  205.         }
  206.         if (Result > 0 && Result <= 0xFF) {
  207.                 emit(op + 0x10);
  208.                 emit(lobyte(Result));
  209.                 return;
  210.         }
  211.         if (op < 0x80) {
  212.                 warn("Value Truncated");
  213.                 emit(op + 0x10);
  214.                 emit(lobyte(Result));
  215.                 return;
  216.         }
  217.         emit(op);
  218.         eword(Result);
  219.         Cycles++;               /* 2 byte slightly slower */
  220.         return;
  221. }
  222.