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

  1. /*
  2.     HEADER:        CUG149;
  3.     TITLE:        1805A Cross-Assembler (Portable);
  4.     FILENAME:    A18.C;
  5.     VERSION:    2.5;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the RCA 1802, 1804, 1805, 1805A, 1806, and
  10.             1806A microprocessors.  The program is written in
  11.             portable C rather than BDS C.  All assembler features
  12.             are supported except relocation, linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             RCA, CDP1802, CDP1805A;
  16.  
  17.     SEE-ALSO:    CUG113, 1802 Cross-Assembler;
  18.  
  19.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  20.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  21.             Lattice C, Microsoft C,    QNIX C;
  22.  
  23.     WARNINGS:    "This program has compiled successfully on 2 UNIX
  24.             compilers, 5 MSDOS compilers, and 2 CP/M compilers.
  25.             A port to BDS C would be extremely difficult, but see
  26.             volume CUG113.  A port to Toolworks C is untried."
  27.  
  28.     AUTHORS:    William C. Colley III;
  29. */
  30.  
  31. /*
  32.               1805A Cross-Assembler in Portable C
  33.  
  34.            Copyright (c) 1985 William C. Colley, III
  35.  
  36. Revision History:
  37.  
  38. Ver    Date        Description
  39.  
  40. 2.0    APR 1985    Recoded from BDS C version 1.1.  WCC3.
  41.  
  42. 2.1    AUG 1985    Greatly shortened the routines find_symbol() and
  43.             new_symbol().  Fixed bugs in expression evaluator.
  44.             Added compilation instructions for Aztec C86,
  45.             Microsoft C, and QNIX C.  Added optional optimizations
  46.             for 16-bit machines.  Adjusted structure members for
  47.             fussy compilers.  WCC3.
  48.  
  49. 2.2    SEP 1985    Added the INCL pseudo-op and associated stuff.  WCC3.
  50.  
  51. 2.3    JUL 1986    Added compilation instructions and tweaks for CI-C86,
  52.             Eco-C88, and Lattice C.  WCC3.
  53.  
  54. 2.4    JAN 1987    Fixed bug that made "BYTE 0," legal syntax.  WCC3.
  55.  
  56. 2.5    AUG 1988    Fixed a bug in the command line parser that puts it
  57.             into a VERY long loop if the user types a command line
  58.             like "A18 FILE.ASM -L".  WCC3 per Alex Cameron.
  59.  
  60. This file contains the main program and line assembly routines for the
  61. assembler.  The main program parses the command line, feeds the source lines to
  62. the line assembly routine, and sends the results to the listing and object file
  63. output routines.  It also coordinates the activities of everything.  The line
  64. assembly routine uses the expression analyzer and the lexical analyzer to
  65. parse the source line convert it into the object bytes that it represents.
  66. */
  67.  
  68. /*  Get global goodies:  */
  69.  
  70. #include "a18.h"
  71.  
  72. /*  Define global mailboxes for all modules:                */
  73.  
  74. char errcode, line[MAXLINE + 1], title[MAXLINE];
  75. int pass = 0;
  76. int eject, filesp, forwd, listhex;
  77. unsigned address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
  78. FILE *filestk[FILES], *source;
  79. TOKEN token;
  80.  
  81. /*  Mainline routine.  This routine parses the command line, sets up    */
  82. /*  the assembler at the beginning of each pass, feeds the source text    */
  83. /*  to the line assembler, feeds the result to the listing and hex file    */
  84. /*  drivers, and cleans everything up at the end of the run.        */
  85.  
  86. static int done, extend, ifsp, off;
  87.  
  88. void main(argc,argv)
  89. int argc;
  90. char **argv;
  91. {
  92.     SCRATCH unsigned *o;
  93.     int newline();
  94.     void asm_line();
  95.     void lclose(), lopen(), lputs();
  96.     void hclose(), hopen(), hputc();
  97.     void error(), fatal_error(), warning();
  98.  
  99.     printf("1802/1805A Cross-Assembler (Portable) Ver 2.5\n");
  100.     printf("Copyright (c) 1985 William C. Colley, III\n\n");
  101.  
  102.     while (--argc > 0) {
  103.     if (**++argv == '-') {
  104.         switch (toupper(*++*argv)) {
  105.         case 'L':   if (!*++*argv) {
  106.                 if (!--argc) { warning(NOLST);  break; }
  107.                 else ++argv;
  108.                 }
  109.                 lopen(*argv);
  110.                 break;
  111.  
  112.         case 'O':   if (!*++*argv) {
  113.                 if (!--argc) { warning(NOHEX);  break; }
  114.                 else ++argv;
  115.                 }
  116.                 hopen(*argv);
  117.                 break;
  118.  
  119.         default:    warning(BADOPT);
  120.         }
  121.     }
  122.     else if (filestk[0]) warning(TWOASM);
  123.     else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
  124.     }
  125.     if (!filestk[0]) fatal_error(NOASM);
  126.  
  127.     while (++pass < 3) {
  128.     fseek(source = filestk[0],0L,0);  done = extend = off = FALSE;
  129.     errors = filesp = ifsp = pagelen = pc = 0;  title[0] = '\0';
  130.     while (!done) {
  131.         errcode = ' ';
  132.         if (newline()) {
  133.         error('*');
  134.         strcpy(line,"\tEND\n");
  135.         done = eject = TRUE;  listhex = FALSE;
  136.         bytes = 0;
  137.         }
  138.         else asm_line();
  139.         pc = word(pc + bytes);
  140.         if (pass == 2) {
  141.         lputs();
  142.         for (o = obj; bytes--; hputc(*o++));
  143.         }
  144.     }
  145.     }
  146.  
  147.     fclose(filestk[0]);  lclose();  hclose();
  148.  
  149.     if (errors) printf("%d Error(s)\n",errors);
  150.     else printf("No Errors\n");
  151.  
  152.     exit(errors);
  153. }
  154.  
  155. /*  Line assembly routine.  This routine gets expressions and tokens    */
  156. /*  from the source file using the expression evaluator and lexical    */
  157. /*  analyzer, respectively.  It fills a buffer with the machine code    */
  158. /*  bytes and returns nothing.                        */
  159.  
  160. static char label[MAXLINE];
  161. static int ifstack[IFDEPTH] = { ON };
  162.  
  163. static OPCODE *opcod;
  164.  
  165. void asm_line()
  166. {
  167.     SCRATCH int i;
  168.     int isalph(), popc();
  169.     OPCODE *find_code(), *find_operator();
  170.     void do_label(), flush(), normal_op(), pseudo_op();
  171.     void error(), pops(), pushc(), trash();
  172.  
  173.     address = pc;  bytes = 0;  eject = forwd = listhex = FALSE;
  174.     for (i = 0; i < BIGINST; obj[i++] = NOP);
  175.  
  176.     label[0] = '\0';
  177.     if ((i = popc()) != ' ' && i != '\n') {
  178.     if (isalph(i)) {
  179.         pushc(i);  pops(label);
  180.         if (find_operator(label)) { label[0] = '\0';  error('L'); }
  181.     }
  182.     else {
  183.         error('L');
  184.         while ((i = popc()) != ' ' && i != '\n');
  185.     }
  186.     }
  187.  
  188.     trash(); opcod = NULL;
  189.     if ((i = popc()) != '\n') {
  190.     if (!isalph(i)) error('S');
  191.     else {
  192.         pushc(i);  pops(token.sval);
  193.         if (!(opcod = find_code(token.sval))) error('O');
  194.     }
  195.     if (!opcod) { listhex = TRUE;  bytes = BIGINST; }
  196.     }
  197.  
  198.     if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
  199.     else if (off) { listhex = FALSE;  flush();  return; }
  200.  
  201.     if (!opcod) { do_label();  flush(); }
  202.     else {
  203.     listhex = TRUE;
  204.     if (opcod -> attr & PSEUDO) pseudo_op();
  205.     else normal_op();
  206.     while ((i = popc()) != '\n') if (i != ' ') error('T');
  207.     }
  208.     source = filestk[filesp];
  209.     return;
  210. }
  211.  
  212. static void flush()
  213. {
  214.     while (popc() != '\n');
  215. }
  216.  
  217. static void do_label()
  218. {
  219.     SCRATCH SYMBOL *l;
  220.     SYMBOL *find_symbol(), *new_symbol();
  221.     void error();
  222.  
  223.     if (label[0]) {
  224.     listhex = TRUE;
  225.     if (pass == 1) {
  226.         if (!((l = new_symbol(label)) -> attr)) {
  227.         l -> attr = FORWD + VAL;
  228.         l -> valu = pc;
  229.         }
  230.     }
  231.     else {
  232.         if (l = find_symbol(label)) {
  233.         l -> attr = VAL;
  234.         if (l -> valu != pc) error('M');
  235.         }
  236.         else error('P');
  237.     }
  238.     }
  239. }
  240.  
  241. static void normal_op()
  242. {
  243.     SCRATCH unsigned attrib, *objp, operand;
  244.     unsigned expr();
  245.     TOKEN *lex();
  246.     void do_label(), error(), unlex();
  247.  
  248.     do_label();
  249.     bytes = (attrib = opcod -> attr) & BYTES;
  250.     if (pass == 1) return;
  251.  
  252.     objp = obj;
  253.     if (attrib & IS1805) {
  254.     *objp++ = PREBYTE;
  255.     if (!extend) error('O');
  256.     }
  257.     *objp++ = opcod -> valu;  objp[0] = objp[1] = 0;
  258.     
  259.     while (attrib & (REGTYP + NUMTYP)) {
  260.     operand = expr();
  261.     switch (attrib & REGTYP) {
  262.         case IOPORT:    if (operand > 7) { error('R');  return; }
  263.  
  264.         case NOT_R0:    if (!operand) { error('R');  return; }
  265.  
  266.         case ANY:        if (operand > 15) { error('R');  return; }
  267.                 *(objp - 1) += operand;  attrib &= ~(REGTYP);
  268.                 break;
  269.  
  270.         case NULL:        switch (attrib & NUMTYP) {
  271.                 case SIXTN:    *objp++ = high(operand);
  272.                         *objp = low(operand);
  273.                         break;
  274.  
  275.                 case BRANCH:    if (high(operand) !=
  276.                             high(pc + bytes - 1)) {
  277.                             error('B');  return;
  278.                         }
  279.                         *objp = low(operand);
  280.                         break;
  281.  
  282.                 case IMMED:    if (operand > 0xff &&
  283.                             operand < 0xff80) {
  284.                             error('V');  return;
  285.                         }
  286.                         *objp = low(operand);
  287.                 }
  288.                 attrib &= ~NUMTYP;
  289.                 break;
  290.     }
  291.     }
  292. }
  293.  
  294. static void pseudo_op()
  295. {
  296.     SCRATCH char *s;
  297.     SCRATCH unsigned *o, u;
  298.     SCRATCH SYMBOL *l;
  299.     unsigned expr();
  300.     SYMBOL *find_symbol(), *new_symbol();
  301.     TOKEN *lex();
  302.     void do_label(), error(), fatal_error(), hseek(), unlex();
  303.  
  304.     o = obj;
  305.     switch (opcod -> valu) {
  306.     case BLK:   do_label();
  307.             u = word(pc + expr());
  308.             if (forwd) error('P');
  309.             else {
  310.             pc = u;
  311.             if (pass == 2) hseek(pc);
  312.             }
  313.             break;
  314.  
  315.     case BYTE:  do_label();
  316.             do {
  317.             if ((lex() -> attr & TYPE) == SEP) u = 0;
  318.             else {
  319.                 unlex();
  320.                 if ((u = expr()) > 0xff && u < 0xff80) {
  321.                 u = 0;  error('V');
  322.                 }
  323.             }
  324.             *o++ = low(u);  ++bytes;
  325.             } while ((token.attr & TYPE) == SEP);
  326.             break;
  327.  
  328.     case CPU:   listhex = FALSE;  do_label();
  329.             u = expr();
  330.             if (forwd) error('P');
  331.             else if (u != 1802 && u != 1805) error('V');
  332.             else extend = u == 1805;
  333.             break;
  334.  
  335.     case EJCT:  listhex = FALSE;  do_label();
  336.             if ((lex() -> attr & TYPE) != EOL) {
  337.             unlex();  pagelen = expr();
  338.             if (pagelen > 0 && pagelen < 3) {
  339.                 pagelen = 0;  error('V');
  340.             }
  341.             }
  342.             eject = TRUE;
  343.             break;
  344.  
  345.     case ELSE:  listhex = FALSE;
  346.             if (ifsp) off = (ifstack[ifsp] = -ifstack[ifsp]) != ON;
  347.             else error('I');
  348.             break;
  349.  
  350.     case END:   do_label();
  351.             if (filesp) { listhex = FALSE;  error('*'); }
  352.             else {
  353.             done = eject = TRUE;
  354.             if (pass == 2 && (lex() -> attr & TYPE) != EOL) {
  355.                 unlex();  hseek(address = expr());
  356.             }
  357.             if (ifsp) error('I');
  358.             }
  359.             break;
  360.  
  361.     case ENDI:  listhex = FALSE;
  362.             if (ifsp) off = ifstack[--ifsp] != ON;
  363.             else error('I');
  364.             break;
  365.  
  366.     case EQU:   if (label[0]) {
  367.             if (pass == 1) {
  368.                 if (!((l = new_symbol(label)) -> attr)) {
  369.                 l -> attr = FORWD + VAL;
  370.                 address = expr();
  371.                 if (!forwd) l -> valu = address;
  372.                 }
  373.             }
  374.             else {
  375.                 if (l = find_symbol(label)) {
  376.                 l -> attr = VAL;
  377.                 address = expr();
  378.                 if (forwd) error('P');
  379.                 if (l -> valu != address) error('M');
  380.                 }
  381.                 else error('P');
  382.             }
  383.             }
  384.             else error('L');
  385.             break;
  386.  
  387.     case IF:    if (++ifsp == IFDEPTH) fatal_error(IFOFLOW);
  388.             address = expr();
  389.             if (forwd) { error('P');  address = TRUE; }
  390.             if (off) { listhex = FALSE;  ifstack[ifsp] = NULL; }
  391.             else {
  392.             ifstack[ifsp] = address ? ON : OFF;
  393.             if (!address) off = TRUE;
  394.             }
  395.             break;
  396.  
  397.     case LOAD:  do_label();  bytes = 6;
  398.             obj[0] = obj[3] = LDI;
  399.             if ((u = expr()) > 15) { u = 0;  error('R'); }
  400.             obj[2] = PHI + u;  obj[5] = PLO + u;
  401.             u = expr();
  402.             obj[1] = high(u);  obj[4] = low(u);
  403.             break;
  404.  
  405.     case INCL:  listhex = FALSE;  do_label();
  406.             if ((lex() -> attr & TYPE) == STR) {
  407.             if (++filesp == FILES) fatal_error(FLOFLOW);
  408.             if (!(filestk[filesp] = fopen(token.sval,"r"))) {
  409.                 --filesp;  error('V');
  410.             }
  411.             }
  412.             else error('S');
  413.             break;
  414.  
  415.     case ORG:   u = expr();
  416.             if (forwd) error('P');
  417.             else {
  418.             pc = address = u;
  419.             if (pass == 2) hseek(pc);
  420.             }
  421.             do_label();
  422.             break;
  423.  
  424.     case PAGE:  address = pc = (pc + 255) & 0xff00;
  425.             if (pass == 2) hseek(pc);
  426.             do_label();
  427.             break;
  428.  
  429.     case SET:   if (label[0]) {
  430.             if (pass == 1) {
  431.                 if (!((l = new_symbol(label)) -> attr)
  432.                 || (l -> attr & SOFT)) {
  433.                 l -> attr = FORWD + SOFT + VAL;
  434.                 address = expr();
  435.                 if (!forwd) l -> valu = address;
  436.                 }
  437.             }
  438.             else {
  439.                 if (l = find_symbol(label)) {
  440.                 address = expr();
  441.                 if (forwd) error('P');
  442.                 else if (l -> attr & SOFT) {
  443.                     l -> attr = SOFT + VAL;
  444.                     l -> valu = address;
  445.                 }
  446.                 else error('M');
  447.                 }
  448.                 else error('P');
  449.             }
  450.             }
  451.             else error('L');
  452.             break;
  453.  
  454.     case TEXT:  do_label();
  455.             while ((lex() -> attr & TYPE) != EOL) {
  456.             if ((token.attr & TYPE) == STR) {
  457.                 for (s = token.sval; *s; *o++ = *s++)
  458.                 ++bytes;
  459.                 if ((lex() -> attr & TYPE) != SEP) unlex();
  460.             }
  461.             else error('S');
  462.             }
  463.             break;
  464.  
  465.     case TITL:  listhex = FALSE;  do_label();
  466.             if ((lex() -> attr & TYPE) == EOL) title[0] = '\0';
  467.             else if ((token.attr & TYPE) != STR) error('S');
  468.             else strcpy(title,token.sval);
  469.             break;
  470.  
  471.     case WORD:  do_label();
  472.             do {
  473.             if ((lex() -> attr & TYPE) == SEP) u = 0;
  474.             else { unlex();  u = expr(); }
  475.             *o++ = high(u);  *o++ = low(u);
  476.             bytes += 2;
  477.             } while ((token.attr & TYPE) == SEP);
  478.             break;
  479.     }
  480.     return;
  481. }
  482.