home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 149_01 / a685util.c < prev    next >
Text File  |  1989-01-13  |  15KB  |  529 lines

  1. /*
  2.     HEADER:        CUG149;
  3.     TITLE:        6805 Cross-Assembler (Portable);
  4.     FILENAME:    A685UTIL.C;
  5.     VERSION:    0.3;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Motorola 6805 family microprocessors.
  10.             The program is written in portable C rather than BDS
  11.             C.  All    assembler features are supported except
  12.             relocation linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Motorola, MC6805;
  16.  
  17.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  18.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  19.             Lattice C, Microsoft C,    QNIX C;
  20.  
  21.     WARNINGS:    "This program has compiled successfully on 2 UNIX
  22.             compilers, 5 MSDOS compilers, and 2 CP/M compilers.
  23.             A port to BDS C would be extremely difficult, but see
  24.             volume CUG113.  A port to Toolworks C is untried."
  25.  
  26.     AUTHORS:    William C. Colley III;
  27. */
  28.  
  29. /*
  30.               6805 Cross-Assembler in Portable C
  31.  
  32.            Copyright (c) 1985 William C. Colley, III
  33.  
  34. Revision History:
  35.  
  36. Ver    Date        Description
  37.  
  38. 0.0    SEP 1985    Adapted from version 3.2 of the portable 6801 cross-
  39.             assembler.  WCC3.
  40.  
  41. 0.1    JUL 1986    Added compilation instructions and tweaks for CI-C86,
  42.             Eco-C88, and Lattice C.  WCC3.
  43.  
  44. 0.2    JAN 1987    Fixed bug that made "FCB 0," legal syntax.  WCC3.
  45.  
  46. 0.3    AUG 1988    Fixed a bug in the command line parser that puts it
  47.             into a VERY long loop if the user types a command line
  48.             like "A685 FILE.ASM -L".  WCC3 per Alex Cameron.
  49.  
  50. This module contains the following utility packages:
  51.  
  52.     1)  symbol table building and searching
  53.  
  54.     2)  opcode and operator table searching
  55.  
  56.     3)  listing file output
  57.  
  58.     4)  hex file output
  59.  
  60.     5)  error flagging
  61. */
  62.  
  63. /*  Get global goodies:  */
  64.  
  65. #include "a685.h"
  66.  
  67. /*  Make sure that MSDOS compilers using the large memory model know    */
  68. /*  that calloc() returns pointer to char as an MSDOS far pointer is    */
  69. /*  NOT compatible with the int type as is usually the case.        */
  70.  
  71. char *calloc();
  72.  
  73. /*  Get access to global mailboxes defined in A68.C:            */
  74.  
  75. extern char errcode, line[], title[];
  76. extern int eject, listhex;
  77. extern unsigned address, bytes, errors, listleft, obj[], pagelen;
  78.  
  79. /*  The symbol table is a binary tree of variable-length blocks drawn    */
  80. /*  from the heap with the calloc() function.  The root pointer lives    */
  81. /*  here:                                */
  82.  
  83. static SYMBOL *sroot = NULL;
  84.  
  85. /*  Add new symbol to symbol table.  Returns pointer to symbol even if    */
  86. /*  the symbol already exists.  If there's not enough memory to store    */
  87. /*  the new symbol, a fatal error occurs.                */
  88.  
  89. SYMBOL *new_symbol(nam)
  90. char *nam;
  91. {
  92.     SCRATCH int i;
  93.     SCRATCH SYMBOL **p, *q;
  94.     void fatal_error();
  95.  
  96.     for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
  97.     p = i < 0 ? &(q -> left) : &(q -> right);
  98.     if (!q) {
  99.     if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
  100.         fatal_error(SYMBOLS);
  101.     strcpy(q -> sname,nam);
  102.     }
  103.     return q;
  104. }
  105.  
  106. /*  Look up symbol in symbol table.  Returns pointer to symbol or NULL    */
  107. /*  if symbol not found.                        */
  108.  
  109. SYMBOL *find_symbol(nam)
  110. char *nam;
  111. {
  112.     SCRATCH int i;
  113.     SCRATCH SYMBOL *p;
  114.  
  115.     for (p = sroot; p && (i = strcmp(nam,p -> sname));
  116.     p = i < 0 ? p -> left : p -> right);
  117.     return p;
  118. }
  119.  
  120. /*  Opcode table search routine.  This routine pats down the opcode    */
  121. /*  table for a given opcode and returns either a pointer to it or    */
  122. /*  NULL if the opcode doesn't exist.                    */
  123.  
  124. OPCODE *find_code(nam)
  125. char *nam;
  126. {
  127.     OPCODE *bsearch();
  128.  
  129.     static OPCODE opctbl[] = {
  130.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb9,    "ADC"    },
  131.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xbb,    "ADD"    },
  132.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb4,    "AND"    },
  133.     { DIR + IDX + 2,                0x38,    "ASL"    },
  134.     { NULL + 1,                    0x48,    "ASLA"    },
  135.     { NULL + 1,                    0x58,    "ASLX"    },
  136.     { DIR + IDX + 2,                0x37,    "ASR"    },
  137.     { NULL + 1,                    0x47,    "ASRA"    },
  138.     { NULL + 1,                    0x57,    "ASRX"    },
  139.     { REL + 2,                    0x24,    "BCC"    },
  140.     { BIT + DIR + 2,                0x11,    "BCLR"    },
  141.     { REL + 2,                    0x25,    "BCS"    },
  142.     { REL + 2,                    0x27,    "BEQ"    },
  143.     { REL + 2,                    0x28,    "BHCC"    },
  144.     { REL + 2,                    0x29,    "BHCS"    },
  145.     { REL + 2,                    0x22,    "BHI"    },
  146.     { REL + 2,                    0x24,    "BHS"    },
  147.     { REL + 2,                    0x2f,    "BIH"    },
  148.     { REL + 2,                    0x2e,    "BIL"    },
  149.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb5,    "BIT"    },
  150.     { REL + 2,                    0x25,    "BLO"    },
  151.     { REL + 2,                    0x23,    "BLS"    },
  152.     { REL + 2,                    0x2c,    "BMC"    },
  153.     { REL + 2,                    0x2b,    "BMI"    },
  154.     { REL + 2,                    0x2d,    "BMS"    },
  155.     { REL + 2,                    0x26,    "BNE"    },
  156.     { REL + 2,                    0x2a,    "BPL"    },
  157.     { REL + 2,                    0x20,    "BRA"    },
  158.     { BIT + REL + DIR + 3,                0x01,    "BRCLR"    },
  159.     { REL + 2,                    0x21,    "BRN"    },
  160.     { BIT + REL + DIR + 3,                0x00,    "BRSET"    },
  161.     { BIT + DIR + 2,                0x10,    "BSET"    },
  162.     { REL + 2,                    0xad,    "BSR"    },
  163.     { NULL + 1,                    0x98,    "CLC"    },
  164.     { NULL + 1,                    0x9a,    "CLI"    },
  165.     { DIR + IDX + 2,                0x3f,    "CLR"    },
  166.     { NULL + 1,                    0x4f,    "CLRA"    },
  167.     { NULL + 1,                    0x5f,    "CLRX"    },
  168.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb1,    "CMP"    },
  169.     { DIR + IDX + 2,                0x33,    "COM"    },
  170.     { NULL + 1,                    0x43,    "COMA"    },
  171.     { NULL + 1,                    0x53,    "COMX"    },
  172.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb3,    "CPX"    },
  173.     { DIR + IDX + 2,                0x3a,    "DEC"    },
  174.     { NULL + 1,                    0x4a,    "DECA"    },
  175.     { NULL + 1,                    0x5a,    "DECX"    },
  176.     { NULL + 1,                    0x5a,    "DEX"    },
  177.     { PSEUDO + ISIF,                ELSE,    "ELSE"    },
  178.     { PSEUDO,                    END,    "END"    },
  179.     { PSEUDO + ISIF,                ENDIF,    "ENDIF"    },
  180.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb8,    "EOR"    },
  181.     { PSEUDO,                    EQU,    "EQU"    },
  182.     { PSEUDO,                    FCB,    "FCB"    },
  183.     { PSEUDO,                    FCC,    "FCC"    },
  184.     { PSEUDO,                    FDB,    "FDB"    },
  185.     { PSEUDO + ISIF,                IF,    "IF"    },
  186.     { DIR  + IDX + 2,                0x3c,    "INC"    },
  187.     { NULL + 1,                    0x4c,    "INCA"    },
  188.     { PSEUDO,                    INCL,    "INCL"    },
  189.     { NULL + 1,                    0x5c,    "INCX"    },
  190.     { NULL + 1,                    0x5c,    "INX"    },
  191.     { DIR + EXT + IDX + EXT_IDX + 3,        0xbc,    "JMP"    },
  192.     { DIR + EXT + IDX + EXT_IDX + 3,        0xbd,    "JSR"    },
  193.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb6,    "LDA"    },
  194.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xbe,    "LDX"    },
  195.     { DIR + IDX + 2,                0x38,    "LSL"    },
  196.     { NULL + 1,                    0x48,    "LSLA"    },
  197.     { NULL + 1,                    0x58,    "LSLX"    },
  198.     { DIR + IDX + 2,                0x34,    "LSR"    },
  199.     { NULL + 1,                    0x44,    "LSRA"    },
  200.     { NULL + 1,                    0x54,    "LSRX"    },
  201.     { NULL + 1,                    0x42,    "MUL"    },
  202.     { DIR + IDX + 2,                0x30,    "NEG"    },
  203.     { NULL + 1,                    0x40,    "NEGA"    },
  204.     { NULL + 1,                    0x50,    "NEGX"    },
  205.     { NULL + 1,                    0x9d,    "NOP"    },
  206.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xba,    "ORA"    },
  207.     { PSEUDO,                    ORG,    "ORG"    },
  208.     { PSEUDO,                    PAGE,    "PAGE"    },
  209.     { PSEUDO,                    RMB,    "RMB"    },
  210.     { DIR + IDX + 2,                0x39,    "ROL"    },
  211.     { NULL + 1,                    0x49,    "ROLA"    },
  212.     { NULL + 1,                    0x59,    "ROLX"    },
  213.     { DIR + IDX + 2,                0x36,    "ROR"    },
  214.     { NULL + 1,                    0x46,    "RORA"    },
  215.     { NULL + 1,                    0x56,    "RORX"    },
  216.     { NULL + 1,                    0x9c,    "RSP"    },
  217.     { NULL + 1,                    0x80,    "RTI"    },
  218.     { NULL + 1,                    0x81,    "RTS"    },
  219.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb2,    "SBC"    },
  220.     { NULL + 1,                    0x99,    "SEC"    },
  221.     { NULL + 1,                    0x9b,    "SEI"    },
  222.     { PSEUDO,                    SET,    "SET"    },
  223.     { DIR + EXT + IDX + EXT_IDX + 3,        0xb7,    "STA"    },
  224.     { NULL + 1,                    0x8e,    "STOP"    },
  225.     { DIR + EXT + IDX + EXT_IDX + 3,        0xbf,    "STX"    },
  226.     { IMMED + DIR + EXT + IDX + EXT_IDX + 3,    0xb0,    "SUB"    },
  227.     { NULL + 1,                    0x83,    "SWI"    },
  228.     { NULL + 1,                    0x97,    "TAX"    },
  229.     { PSEUDO,                    TITLE,    "TITLE"    },
  230.     { DIR + IDX + 2,                0x3d,    "TST"    },
  231.     { NULL + 1,                    0x4d,    "TSTA"    },
  232.     { NULL + 1,                    0x5d,    "TSTX"    },
  233.     { NULL + 1,                    0x9f,    "TXA"    },
  234.     { NULL + 1,                    0x8f,    "WAIT"    }
  235.     };
  236.  
  237.     return bsearch(opctbl,opctbl + (sizeof(opctbl) / sizeof(OPCODE)),nam);
  238. }
  239.  
  240. /*  Operator table search routine.  This routine pats down the        */
  241. /*  operator table for a given operator and returns either a pointer    */
  242. /*  to it or NULL if the opcode doesn't exist.                */
  243.  
  244. OPCODE *find_operator(nam)
  245. char *nam;
  246. {
  247.     OPCODE *bsearch();
  248.  
  249.     static OPCODE oprtbl[] = {
  250.     { BINARY + LOG1  + OPR,        AND,        "AND"    },
  251.     { BINARY + RELAT + OPR,        '=',        "EQ"    },
  252.     { BINARY + RELAT + OPR,        GE,        "GE"    },
  253.     { BINARY + RELAT + OPR,        '>',        "GT"    },
  254.     { UNARY  + UOP3  + OPR,        HIGH,        "HIGH"    },
  255.     { BINARY + RELAT + OPR,        LE,        "LE"    },
  256.     { UNARY  + UOP3  + OPR,        LOW,        "LOW"    },
  257.     { BINARY + RELAT + OPR,        '<',        "LT"    },
  258.     { BINARY + MULT  + OPR,        MOD,        "MOD"    },
  259.     { BINARY + RELAT + OPR,        NE,        "NE"    },
  260.     { UNARY  + UOP2  + OPR,        NOT,        "NOT"    },
  261.     { BINARY + LOG2  + OPR,        OR,        "OR"    },
  262.     { BINARY + MULT  + OPR,        SHL,        "SHL"    },
  263.     { BINARY + MULT  + OPR,        SHR,        "SHR"    },
  264.     { REG,                'X',        "X"    },
  265.     { BINARY + LOG2  + OPR,        XOR,        "XOR"    }
  266.     };
  267.  
  268.     return bsearch(oprtbl,oprtbl + (sizeof(oprtbl) / sizeof(OPCODE)),nam);
  269. }
  270.  
  271. static OPCODE *bsearch(lo,hi,nam)
  272. OPCODE *lo, *hi;
  273. char *nam;
  274. {
  275.     SCRATCH int i;
  276.     SCRATCH OPCODE *chk;
  277.  
  278.     for (;;) {
  279.     chk = lo + (hi - lo) / 2;
  280.     if (!(i = ustrcmp(chk -> oname,nam))) return chk;
  281.     if (chk == lo) return NULL;
  282.     if (i < 0) lo = chk;
  283.     else hi = chk;
  284.     }
  285. }
  286.  
  287. static int ustrcmp(s,t)
  288. char *s, *t;
  289. {
  290.     SCRATCH int i;
  291.  
  292.     while (!(i = toupper(*s++) - toupper(*t)) && *t++);
  293.     return i;
  294. }
  295.  
  296. /*  Buffer storage for line listing routine.  This allows the listing    */
  297. /*  output routines to do all operations without the main routine    */
  298. /*  having to fool with it.                        */
  299.  
  300. static FILE *list = NULL;
  301.  
  302. /*  Listing file open routine.  If a listing file is already open, a    */
  303. /*  warning occurs.  If the listing file doesn't open correctly, a    */
  304. /*  fatal error occurs.  If no listing file is open, all calls to    */
  305. /*  lputs() and lclose() have no effect.                */
  306.  
  307. void lopen(nam)
  308. char *nam;
  309. {
  310.     FILE *fopen();
  311.     void fatal_error(), warning();
  312.  
  313.     if (list) warning(TWOLST);
  314.     else if (!(list = fopen(nam,"w"))) fatal_error(LSTOPEN);
  315.     return;
  316. }
  317.  
  318. /*  Listing file line output routine.  This routine processes the    */
  319. /*  source line saved by popc() and the output of the line assembler in    */
  320. /*  buffer obj into a line of the listing.  If the disk fills up, a    */
  321. /*  fatal error occurs.                            */
  322.  
  323. void lputs()
  324. {
  325.     SCRATCH int i, j;
  326.     SCRATCH unsigned *o;
  327.     void check_page(), fatal_error();
  328.  
  329.     if (list) {
  330.     i = bytes;  o = obj;
  331.     do {
  332.         fprintf(list,"%c  ",errcode);
  333.         if (listhex) {
  334.         fprintf(list,"%04x  ",address);
  335.         for (j = 4; j; --j) {
  336.             if (i) { --i;  ++address;  fprintf(list," %02x",*o++); }
  337.             else fprintf(list,"   ");
  338.         }
  339.         }
  340.         else fprintf(list,"%18s","");
  341.         fprintf(list,"   %s",line);  strcpy(line,"\n");
  342.         check_page();
  343.         if (ferror(list)) fatal_error(DSKFULL);
  344.     } while (listhex && i);
  345.     }
  346.     return;
  347. }
  348.  
  349. /*  Listing file close routine.  The symbol table is appended to the    */
  350. /*  listing in alphabetic order by symbol name, and the listing file is    */
  351. /*  closed.  If the disk fills up, a fatal error occurs.        */
  352.  
  353. static int col = 0;
  354.  
  355. void lclose()
  356. {
  357.     void fatal_error(), list_sym();
  358.  
  359.     if (list) {
  360.     if (sroot) {
  361.         list_sym(sroot);
  362.         if (col) fprintf(list,"\n");
  363.     }
  364.     fprintf(list,"\f");
  365.     if (ferror(list) || fclose(list) == EOF) fatal_error(DSKFULL);
  366.     }
  367.     return;
  368. }
  369.  
  370. static void list_sym(sp)
  371. SYMBOL *sp;
  372. {
  373.     void check_page();
  374.  
  375.     if (sp) {
  376.     list_sym(sp -> left);
  377.     fprintf(list,"%04x  %-10s",sp -> valu,sp -> sname);
  378.     if (col = ++col % SYMCOLS) fprintf(list,"    ");
  379.     else {
  380.         fprintf(list,"\n");
  381.         if (sp -> right) check_page();
  382.     }
  383.     list_sym(sp -> right);
  384.     }
  385.     return;
  386. }
  387.  
  388. static void check_page()
  389. {
  390.     if (pagelen && !--listleft) eject = TRUE;
  391.     if (eject) {
  392.     eject = FALSE;  listleft = pagelen;  fprintf(list,"\f");
  393.     if (title[0]) { listleft -= 2;  fprintf(list,"%s\n\n",title); }
  394.     }
  395.     return;
  396. }
  397.  
  398. /*  Buffer storage for hex output file.  This allows the hex file    */
  399. /*  output routines to do all of the required buffering and record    */
  400. /*  forming without the    main routine having to fool with it.        */
  401.  
  402. static FILE *hex = NULL;
  403. static unsigned cnt = 0;
  404. static unsigned addr = 0;
  405. static unsigned sum = 0;
  406. static unsigned buf[HEXSIZE];
  407.  
  408. /*  Hex file open routine.  If a hex file is already open, a warning    */
  409. /*  occurs.  If the hex file doesn't open correctly, a fatal error    */
  410. /*  occurs.  If no hex file is open, all calls to hputc(), hseek(), and    */
  411. /*  hclose() have no effect.                        */
  412.  
  413. void hopen(nam)
  414. char *nam;
  415. {
  416.     FILE *fopen();
  417.     void fatal_error(), warning();
  418.  
  419.     if (hex) warning(TWOHEX);
  420.     else if (!(hex = fopen(nam,"w"))) fatal_error(HEXOPEN);
  421.     return;
  422. }
  423.  
  424. /*  Hex file write routine.  The data byte is appended to the current    */
  425. /*  record.  If the record fills up, it gets written to disk.  If the    */
  426. /*  disk fills up, a fatal error occurs.                */
  427.  
  428. void hputc(c)
  429. unsigned c;
  430. {
  431.     void record();
  432.  
  433.     if (hex) {
  434.     buf[cnt++] = c;
  435.     if (cnt == HEXSIZE) record(0);
  436.     }
  437.     return;
  438. }
  439.  
  440. /*  Hex file address set routine.  The specified address becomes the    */
  441. /*  load address of the next record.  If a record is currently open,    */
  442. /*  it gets written to disk.  If the disk fills up, a fatal error    */
  443. /*  occurs.                                */
  444.  
  445. void hseek(a)
  446. unsigned a;
  447. {
  448.     void record();
  449.  
  450.     if (hex) {
  451.     if (cnt) record(0);
  452.     addr = a;
  453.     }
  454.     return;
  455. }
  456.  
  457. /*  Hex file close routine.  Any open record is written to disk, the    */
  458. /*  EOF record is added, and file is closed.  If the disk fills up, a    */
  459. /*  fatal error occurs.                            */
  460.  
  461. void hclose()
  462. {
  463.     void fatal_error(), record();
  464.  
  465.     if (hex) {
  466.     if (cnt) record(0);
  467.     record(1);
  468.     if (fclose(hex) == EOF) fatal_error(DSKFULL);
  469.     }
  470.     return;
  471. }
  472.  
  473. static void record(typ)
  474. unsigned typ;
  475. {
  476.     SCRATCH unsigned i;
  477.     void fatal_error(), putb();
  478.  
  479.     putc(':',hex);  putb(cnt);  putb(high(addr));
  480.     putb(low(addr));  putb(typ);
  481.     for (i = 0; i < cnt; ++i) putb(buf[i]);
  482.     putb(low(-sum));  putc('\n',hex);
  483.  
  484.     addr += cnt;  cnt = 0;
  485.  
  486.     if (ferror(hex)) fatal_error(DSKFULL);
  487.     return;
  488. }
  489.  
  490. static void putb(b)
  491. unsigned b;
  492. {
  493.     static char digit[] = "0123456789ABCDEF";
  494.  
  495.     putc(digit[b >> 4],hex);  putc(digit[b & 0x0f],hex);
  496.     sum += b;  return;
  497. }
  498.  
  499. /*  Error handler routine.  If the current error code is non-blank,    */
  500. /*  the error code is filled in and the    number of lines with errors    */
  501. /*  is adjusted.                            */
  502.  
  503. void error(code)
  504. char code;
  505. {
  506.     if (errcode == ' ') { errcode = code;  ++errors; }
  507.     return;
  508. }
  509.  
  510. /*  Fatal error handler routine.  A message gets printed on the stderr    */
  511. /*  device, and the program bombs.                    */
  512.  
  513. void fatal_error(msg)
  514. char *msg;
  515. {
  516.     printf("Fatal Error -- %s\n",msg);
  517.     exit(-1);
  518. }
  519.  
  520. /*  Non-fatal error handler routine.  A message gets printed on the    */
  521. /*  stderr device, and the routine returns.                */
  522.  
  523. void warning(msg)
  524. char *msg;
  525. {
  526.     printf("Warning -- %s\n",msg);
  527.     return;
  528. }
  529.