home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 529a.lha / A68k_v2.71 / Opcodes.c < prev    next >
C/C++ Source or Header  |  1991-07-02  |  9KB  |  287 lines

  1. /*------------------------------------------------------------------*/
  2. /*                                    */
  3. /*            MC68000 Cross Assembler                */
  4. /*                                    */
  5. /*                Copyright 1985 by Brian R. Anderson            */
  6. /*                                    */
  7. /*          Opcode table and scan routine - April 16, 1991        */
  8. /*                                    */
  9. /*   This program may be copied for personal, non-commercial use    */
  10. /*   only, provided that the above copyright notice is included        */
  11. /*   on all copies of the source code.  Copying for any other use   */
  12. /*   without the consent of the author is prohibited.            */
  13. /*                                    */
  14. /*------------------------------------------------------------------*/
  15. /*                                    */
  16. /*        Originally published (in Modula-2) in            */
  17. /*        Dr. Dobb's Journal, April, May, and June 1986.        */
  18. /*                                    */
  19. /*     AmigaDOS conversion copyright 1991 by Charlie Gibbs.        */
  20. /*                                    */
  21. /*------------------------------------------------------------------*/
  22.  
  23. #include "A68kdef.h"
  24. #include "A68kglb.h"
  25.  
  26. static int optabsize = 0;    /* Size of opcode table */
  27. static int oplimits['Z'-'A'+2];    /* Table limits by first letter */
  28.  
  29. /* Opcode table */
  30.  
  31. struct OpTab {
  32.     char Mnem[8];    /* Instruction mnemonic */
  33.     int  OpBits;    /* Op code bits */
  34.     int  AMA;        /* Address mode bits */
  35.     int  AMB;        /* More address mode bits */
  36. };
  37.  
  38. static struct OpTab MnemTab[] = {
  39.     "=",     0,      0xFFFF, Equ,
  40.     "ABCD",  0xC100, Rx911 | RegMem3 | Ry02, 0,
  41.     "ADD",   0xD000, OpM68D, EA05y,
  42.     "ADDA",  0xD000, OpM68A, EA05a,
  43.     "ADDI",  0x0600, 0, Size67 | EA05e | Exten,
  44.     "ADDQ",  0x5000, Data911, Size67 | EA05d,
  45.     "ADDX",  0xD100, Rx911 | RegMem3 | Ry02, Size67,
  46.     "AND",   0xC000, OpM68D, EA05x,
  47.     "ANDI",  0x0200, 0, Size67 | EA05e | Exten,
  48.     "ASL",   0xE100, CntR911, 0,
  49.     "ASR",   0xE000, CntR911, 0,
  50.     "BCC",   0x6400, Brnch, 0,
  51.     "BCHG",  0x0040, 0, EA05e | Exten | Bit811,
  52.     "BCLR",  0x0080, 0, EA05e | Exten | Bit811,
  53.     "BCS",   0x6500, Brnch, 0,
  54.     "BEQ",   0x6700, Brnch, 0,
  55.     "BGE",   0x6C00, Brnch, 0,
  56.     "BGT",   0x6E00, Brnch, 0,
  57.     "BHI",   0x6200, Brnch, 0,
  58.     "BLE",   0x6F00, Brnch, 0,
  59.     "BLS",   0x6300, Brnch, 0,
  60.     "BLT",   0x6D00, Brnch, 0,
  61.     "BMI",   0x6B00, Brnch, 0,
  62.     "BNE",   0x6600, Brnch, 0,
  63.     "BPL",   0x6A00, Brnch, 0,
  64.     "BRA",   0x6000, Brnch, 0,
  65.     "BSET",  0x00C0, 0, EA05e | Exten | Bit811,
  66.     "BSR",   0x6100, Brnch, 0,
  67.     "BSS",   0,      0xFFFF, BSS,
  68.     "BTST",  0x0000, 0, EA05c | Exten | Bit811,
  69.     "BVC",   0x6800, Brnch, 0,
  70.     "BVS",   0x6900, Brnch, 0,
  71.     "CHK",   0x4180, Rx911, EA05b,
  72.     "CLR",   0x4200, 0, Size67 | EA05e,
  73.     "CMP",   0xB000, OpM68C, EA05a,
  74.     "CMPA",  0xB000, OpM68A, EA05a,
  75.     "CMPI",  0x0C00, 0, Size67 | EA05e | Exten,
  76.     "CMPM",  0xB108, Rx911 | Ry02, Size67,
  77.     "CNOP",  0,      0xFFFF, Cnop,
  78.     "CODE",  0,      0xFFFF, CSeg,
  79.     "CSEG",  0,      0xFFFF, CSeg,
  80.     "DATA",  0,      0xFFFF, DSeg,
  81.     "DBCC",  0x54C8, DecBr, 0,
  82.     "DBCS",  0x55C8, DecBr, 0,
  83.     "DBEQ",  0x57C8, DecBr, 0,
  84.     "DBF",   0x51C8, DecBr, 0,
  85.     "DBGE",  0x5CC8, DecBr, 0,
  86.     "DBGT",  0x5EC8, DecBr, 0,
  87.     "DBHI",  0x52C8, DecBr, 0,
  88.     "DBLE",  0x5FC8, DecBr, 0,
  89.     "DBLS",  0x53C8, DecBr, 0,
  90.     "DBLT",  0x5DC8, DecBr, 0,
  91.     "DBMI",  0x5BC8, DecBr, 0,
  92.     "DBNE",  0x56C8, DecBr, 0,
  93.     "DBPL",  0x5AC8, DecBr, 0,
  94.     "DBRA",  0x51C8, DecBr, 0,
  95.     "DBT",   0x50C8, DecBr, 0,
  96.     "DBVC",  0x58C8, DecBr, 0,
  97.     "DBVS",  0x59C8, DecBr, 0,
  98.     "DC",    0,      0xFFFF, DC,
  99.     "DCB",   0,      0xFFFF, DCB,
  100.     "DIVS",  0x81C0, Rx911, EA05b,
  101.     "DIVU",  0x80C0, Rx911, EA05b,
  102.     "DS",    0,      0xFFFF, DS,
  103.     "DSEG",  0,      0xFFFF, DSeg,
  104.     "END",   0,      0xFFFF, End,
  105.     "ENDC",  0,      0xFFFF, EndC,
  106.     "ENDIF", 0,      0xFFFF, EndC,
  107.     "EOR",   0xB000, OpM68X, EA05e,
  108.     "EORI",  0x0A00, 0, Size67 | EA05e | Exten,
  109.     "EQU",   0,      0xFFFF, Equ,
  110.     "EQUR",  0,      0xFFFF, Equr,
  111.     "EVEN",  0,      0xFFFF, Even,
  112.     "EXG",   0xC100, OpM37, 0,
  113.     "EXT",   0x4800, OpM68S, 0,
  114.     "FAR",   0,      0xFFFF, Far,
  115.     "IDNT",  0,      0xFFFF, Idnt,
  116.     "IFC",   0,      0xFFFF, IfC,
  117.     "IFD",   0,      0xFFFF, IfD,
  118.     "IFEQ",  0,      0xFFFF, IfEQ,
  119.     "IFGE",  0,      0xFFFF, IfGE,
  120.     "IFGT",  0,      0xFFFF, IfGT,
  121.     "IFLE",  0,      0xFFFF, IfLE,
  122.     "IFLT",  0,      0xFFFF, IfLT,
  123.     "IFNC",  0,      0xFFFF, IfNC,
  124.     "IFND",  0,      0xFFFF, IfND,
  125.     "IFNE",  0,      0xFFFF, IfNE,
  126.     "ILLEGAL", 0x4AFC, 0, 0,
  127.     "INCBIN", 0,     0xFFFF, Incbin,
  128.     "INCLUDE", 0,    0xFFFF, Include,
  129.     "JMP",   0x4EC0, 0, EA05f,
  130.     "JSR",   0x4E80, 0, EA05f,
  131.     "LEA",   0x41C0, Rx911, EA05f,
  132.     "LINK",  0x4E50, Ry02, Exten,
  133.     "LIST",  0,      0xFFFF, DoList,
  134.     "LSL",   0xE308, CntR911, 0,
  135.     "LSR",   0xE208, CntR911, 0,
  136.     "MACRO", 0,      0xFFFF, Macro,
  137.     "MOVE",  0x0000, 0, Sz1213A | EA611,
  138.     "MOVEA", 0x0040, Rx911, Sz1213 | EA05a,
  139.     "MOVEM", 0x4880, 0, Size6 | EA05z | Exten,
  140.     "MOVEP", 0x0008, OpM68R, Exten,
  141.     "MOVEQ", 0x7000, Data07, 0,
  142.     "MULS",  0xC1C0, Rx911, EA05b,
  143.     "MULU",  0xC0C0, Rx911, EA05b,
  144.     "NBCD",  0x4800, 0, EA05e,
  145.     "NEAR",  0,      0xFFFF, Near,
  146.     "NEG",   0x4400, 0, Size67 | EA05e,
  147.     "NEGX",  0x4000, 0, Size67 | EA05e,
  148.     "NOL",   0,      0xFFFF, NoList,
  149.     "NOLIST",0,      0xFFFF, NoList,
  150.     "NOP",   0x4E71, 0, 0,
  151.     "NOT",   0x4600, 0, Size67 | EA05e,
  152.     "OR",    0x8000, OpM68D, EA05x,
  153.     "ORG",   0,      0xFFFF, Org,
  154.     "ORI",   0x0000, 0, Size67 | EA05e | Exten,
  155.     "PAGE",  0,      0xFFFF, Page,
  156.     "PEA",   0x4840, 0, EA05f,
  157.     "PUBLIC",0,      0xFFFF, Public,
  158.     "REG",   0,      0xFFFF, Reg,
  159.     "RESET", 0x4E70, 0, 0,
  160.     "ROL",   0xE718, CntR911, 0,
  161.     "ROR",   0xE618, CntR911, 0,
  162.     "RORG",  0,      0xFFFF, Org,
  163.     "ROXL",  0xE510, CntR911, 0,
  164.     "ROXR",  0xE410, CntR911, 0,
  165.     "RTE",   0x4E73, 0, 0,
  166.     "RTR",   0x4E77, 0, 0,
  167.     "RTS",   0x4E75, 0, 0,
  168.     "SBCD",  0x8100, Rx911 | RegMem3 | Ry02, 0,
  169.     "SCC",   0x54C0, 0, EA05e,
  170.     "SCS",   0x55C0, 0, EA05e,
  171.     "SECTION", 0,    0xFFFF, Section,
  172.     "SEQ",   0x57C0, 0, EA05e,
  173.     "SET",   0,      0xFFFF, Set,
  174.     "SF",    0x51C0, 0, EA05e,
  175.     "SGE",   0x5CC0, 0, EA05e,
  176.     "SGT",   0x5EC0, 0, EA05e,
  177.     "SHI",   0x52C0, 0, EA05e,
  178.     "SLE",   0x5FC0, 0, EA05e,
  179.     "SLS",   0x53C0, 0, EA05e,
  180.     "SLT",   0x5DC0, 0, EA05e,
  181.     "SMI",   0x5BC0, 0, EA05e,
  182.     "SNE",   0x56C0, 0, EA05e,
  183.     "SPC",   0,      0xFFFF, Space,
  184.     "SPL",   0x5AC0, 0, EA05e,
  185.     "ST",    0x50C0, 0, EA05e,
  186.     "STOP",  0x4E72, 0, Exten,
  187.     "SUB",   0x9000, OpM68D, EA05y,
  188.     "SUBA",  0x9000, OpM68A, EA05a,
  189.     "SUBI",  0x0400, 0, Size67 | EA05e | Exten,
  190.     "SUBQ",  0x5100, Data911, Size67 | EA05d,
  191.     "SUBX",  0x9100, Rx911 | RegMem3 | Ry02, Size67,
  192.     "SVC",   0x58C0, 0, EA05e,
  193.     "SVS",   0x59C0, 0, EA05e,
  194.     "SWAP",  0x4840, Ry02, 0,
  195.     "TAS",   0x4AC0, 0, EA05e,
  196.     "TITLE", 0,      0xFFFF, Title,
  197.     "TRAP",  0x4E40, Data03, 0,
  198.     "TRAPV", 0x4E76, 0, 0,
  199.     "TST",   0x4A00, 0, Size67 | EA05e,
  200.     "TTL",   0,      0xFFFF, Title,
  201.     "UNLK",  0x4E58, Ry02, 0,
  202.     "XDEF",  0,      0xFFFF, Xdef,
  203.     "XREF",  0,      0xFFFF, Xref,
  204.     "",0,0,0};        /* End-of-table flag */
  205.  
  206.  
  207.  
  208. int Instructions (loc) int loc;
  209. /* Looks up opcode and addressing mode bit patterns
  210.    If the opcode corresponds to an executable instruction,
  211.      returns TRUE with the following fields set up:
  212.     Op       - operation code bits
  213.     AdrModeA - addressing mode bits
  214.     AdrModeB - more addressing mode bits
  215.     Dir      - None
  216.    If the opcode corresponds to a directive (AdrModeA in the table
  217.      is 0xFFFF), returns TRUE with the following fields set up:
  218.     Op       - 0
  219.     AdrModeA - 0
  220.     AdrModeB - 0
  221.     Dir      - the appropriate directive value
  222.    If not found, returns FALSE with all the above fields set to zero.
  223.  
  224.    NOTE: The binary search doesn't use strcmp because this function
  225.     returns incorrect values under MS-DOS Lattice 2.12.              */
  226. {
  227.     register char *i, *j;
  228.     register int  lower, upper, mid;    /* Binary search controls */
  229.  
  230.  
  231.     if (optabsize == 0) {    /* Determine size of opcode table. */
  232.     while (MnemTab[optabsize].Mnem[0])
  233.         optabsize++;
  234.     oplimits[0] = 0;
  235.     oplimits['Z'-'A'+1] = optabsize;
  236.     mid = 0;
  237.     for (lower = 0; lower < optabsize; lower++) {
  238.         upper = (unsigned int) MnemTab[lower].Mnem[0] - 'A' + 1;
  239.         if (upper != mid) {
  240.         if (upper > 0) {    /* Start of the next letter */
  241.             mid++;
  242.             while (mid < upper)
  243.             oplimits[mid++] = lower;
  244.             oplimits[mid] = lower;
  245.         }
  246.         }
  247.     }
  248.     mid++;
  249.     while (mid < 'Z'-'A'+1) {
  250.         oplimits[mid++]=optabsize;    /* In case we didn't get to Z */
  251.     }
  252.     }
  253.     mid = (unsigned int) OpCode[0] - 'A' + 1;
  254.     if (mid < 0) {            /* This catches stuff like "=". */
  255.     lower = 0;
  256.     upper = oplimits[1];
  257.     } else if (mid > 'Z'-'A'+1) {
  258.     lower = upper = 0;        /* Reject this one. */
  259.     } else {
  260.     lower = oplimits[mid++];
  261.     upper = oplimits[mid];
  262.     }
  263.     while (lower < upper) {
  264.     mid = (lower + upper) / 2;    /* Search the opcode table. */
  265.     for (i = OpCode, j = MnemTab[mid].Mnem; *i == *j; i++, j++)
  266.         if (*i == '\0')
  267.         break;        /* Find the first non-match. */
  268.     if (*i < *j)
  269.         upper = mid;    /* Search lower half of table. */
  270.     else if (*i > *j)
  271.         lower = mid + 1;    /* Search upper half of table. */
  272.     else if (MnemTab[mid].AMA != 0xFFFF) {    /* Found it. */
  273.         Op = MnemTab[mid].OpBits;    /* Executable instruction */
  274.         AdrModeA = MnemTab[mid].AMA;
  275.         AdrModeB = MnemTab[mid].AMB;
  276.         Dir = None;
  277.         return (TRUE);
  278.     } else {
  279.         Op = AdrModeA = AdrModeB = 0;    /* Directive */
  280.         Dir = MnemTab[mid].AMB;
  281.         return (TRUE);
  282.     }
  283.     }
  284.     Op = AdrModeA = AdrModeB = Dir = 0;
  285.     return (FALSE);            /* We didn't find it. */
  286. }
  287.