home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 348_01 / z80a.h < prev    next >
C/C++ Source or Header  |  1991-05-03  |  13KB  |  460 lines

  1. /*
  2.     HEADER:        ;
  3.     TITLE:        Z80 Cross-Assembler (Portable);
  4.     FILENAME:    Z80A.H;
  5.     VERSION:    0.0;
  6.     DATE:        10/11/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Intel 8080/Z80 family of 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.             Intel, Zilog, Z80;
  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 Toolworks C is untried."
  24.  
  25.     AUTHORS:    William C. Colley III, Michael G. Panas;
  26. */
  27.  
  28. /*
  29.               z80 Cross-Assembler in Portable C
  30.  
  31.         Portions Copyright (c) 1985,1987 William C. Colley, III
  32.         Portions Copyright (c) 1988  Michael G. Panas
  33.  
  34. Revision History:
  35.  
  36. Ver    Date        Description
  37.  
  38. 0.0    SEPT 1988    Adapted from version 0.0 of Michael Panas portable
  39.             QBAL Z80 cross-assembler by Michael G. Panas.
  40.  
  41. This header file contains the global constants and data type definitions for
  42. all modules of the cross-assembler.  This also seems a good place to put the
  43. compilation and linkage instructions for the animal.  This list currently
  44. includes the following compilers:
  45.  
  46.         Compiler Name        Op. Sys.    Processor
  47.  
  48.     1)  Aztec C86            CP/M-86        8086, 8088
  49.                     MSDOS/PCDOS
  50.  
  51.     2)  AZTEC C II            CP/M-80        8080, Z-80
  52.  
  53.     3)  Computer Innovations C86    MSDOS/PCDOS    8086, 8088
  54.  
  55.     4)  Eco-C            CP/M-80        Z-80
  56.  
  57.     5)  Eco-C88            MSDOS/PCDOS    8086, 8088
  58.  
  59.     6)  HP C            HP-UX        68000
  60.  
  61.     7)  Lattice C            MSDOS/PCDOS    8086, 8088
  62.  
  63.     8)  Microsoft C            MSDOS/PCDOS    8086, 8088
  64.  
  65.     9)  QNIX C            QNIX        8086, 8088
  66.  
  67. Further additions will be made to the list as users feed the information to
  68. me.  This particularly applies to UNIX and IBM-PC compilers.
  69.  
  70. Compile-assemble-link instructions for this program under various compilers
  71. and operating systems:
  72.  
  73.     1)    Aztec C86:
  74.  
  75.     A)  Uncomment out the "#define AZTEC_C" line and comment out all
  76.         other compiler names in Z80A.H.
  77.  
  78.     B)  Assuming that all files are on drive A:, run the following sequence
  79.         of command lines:
  80.  
  81.         A>cc Z80A
  82.         A>cc Z80Aeval
  83.         A>cc Z80Autil
  84.         A>ln Z80A.o Z80Aeval.o Z80Autil.o -lc
  85.         A>era Z80A*.o
  86.  
  87.     2)  Aztec CII (version 1.06B):
  88.  
  89.     A)  Uncomment out the "#define AZTEC_C" line and comment out all
  90.         other compiler names in Z80A.H.
  91.  
  92.     B)  Assuming the C compiler is called "CC.COM" and all files are
  93.         on drive A:, run the following sequence of command lines:
  94.  
  95.         A>cc Z80A
  96.         A>as -zap Z80A
  97.         A>cc Z80Aeval
  98.         A>as -zap Z80Aeval
  99.         A>cc Z80Autil
  100.         A>as -zap Z80Autil
  101.         A>ln Z80A.o Z80Aeval.o Z80Autil.o -lc
  102.         A>era Z80A*.o
  103.  
  104.     3)  Computer Innovations C86:
  105.  
  106.     A)  Uncomment out the "#define CI_C86" line and comment out all
  107.         other compiler names in Z80A.H.
  108.  
  109.     B)  Compile the files Z80A.C, Z80AEVAL.C, and Z80AUTIL.C.  Link
  110.         according to instructions that come with the compiler.
  111.  
  112.     4)  Eco-C (CP/M-80 version 3.10):
  113.  
  114.     A)  Uncomment out the "#define ECO_C" line and comment out all
  115.         other compiler names in Z80A.H.
  116.  
  117.     B)  Assuming all files are on drive A:, run the following sequence of
  118.         command lines:
  119.  
  120.         A>cp Z80A -i -m
  121.         A>cp Z80Aeval -i -m
  122.         A>cp Z80Autil -i -m
  123.         A>l80 Z80A,Z80Aeval,Z80Autil,Z80A/n/e
  124.         A>era Z80A*.mac
  125.         A>era Z80A*.rel
  126.  
  127.     5)  Eco-C88:
  128.  
  129.     A)  Uncomment out the "#define ECO_C" line and comment out all
  130.         other compiler names in Z80A.H.
  131.  
  132.     B)  Compile the files Z80A.C, Z80AEVAL.C, and Z80AUTIL.C.  Link
  133.         according to instructions that come with the compiler.
  134.  
  135.     6)  HP-UX (a UNIX look-alike running on an HP-9000 Series 200/500,
  136.     68000-based machine):
  137.  
  138.     A)  Uncomment out the "#define HP_UX" line and comment out all
  139.         other compiler names in Z80A.H.
  140.  
  141.     B)  Run the following command line:
  142.  
  143.         . cc Z80A.c Z80Aeval.c Z80Autil.c
  144.  
  145.     7)  Lattice C:
  146.  
  147.     A)  Uncomment out the "#define LATTICE_C" line and comment out all
  148.         other compiler names in Z80A.H.
  149.  
  150.     B)  Compile the files Z80A.C, Z80AEVAL.C, and Z80AUTIL.C.  Link
  151.         according to instructions that come with the compiler.
  152.  
  153.     8)  Microsoft C (version 3.00 --> version 5.1):
  154.  
  155.     A)  Uncomment out the "#define MICROSOFT_C" line and comment out all
  156.         other compiler names in A68.H.
  157.  
  158.     B)  Run the following command line:
  159.  
  160.         C>cl Z80A.c Z80Aeval.c Z80Autil.c
  161.  
  162.     9)    QNIX C:
  163.  
  164.     A)  Uncomment out the "#define QNIX" line and comment out all other
  165.         compiler names in Z80A.H.
  166.  
  167.     B)  Run the following command line:
  168.  
  169.         . cc Z80A.c Z80Aeval.c Z80Autil.c
  170.  
  171. Note that, under CP/M-80, you can't re-execute a core image from a previous
  172. assembly run with the "@.COM" trick.  This technique is incompatible with the
  173. Aztec CII compiler, so I didn't bother to support it at all.
  174. */
  175.  
  176. #include <stdio.h>
  177.  
  178. /*  Comment out all but the line containing the name of your compiler:    */
  179.  
  180. /* #define AZTEC_C    /*                    */
  181. /* #define CI_C86    /*                    */
  182. /* #define ECO_C    /*                    */
  183. /* #define HP_UX    /* All UNIX's use this            */
  184. /* #define LATTICE_C    /*                    */
  185. #define MICROSOFT_C    /* 3.0 4.0 5.1                 */
  186. /* #define QNIX        /*                    */
  187.  
  188. /*  Compiler dependencies:                        */
  189.  
  190. #ifdef    AZTEC_C
  191. #define    getc(f)        agetc(f)
  192. #define    putc(c,f)    aputc(c,f)
  193. #endif
  194.  
  195. #ifndef    ECO_C
  196. #define    FALSE        0
  197. #define    TRUE        (!0)
  198. #endif
  199.  
  200. #ifdef    LATTICE_C
  201. #define    void        int
  202. #endif
  203.  
  204. #ifdef    QNIX
  205. #define    fprintf        tfprintf
  206. #define    printf        tprintf
  207. #endif
  208.  
  209. /*  On 8-bit machines, the static type is as efficient as the register    */
  210. /*  type and far more efficient than the auto type.  On larger machines    */
  211. /*  such as the 8086 family, this is not necessarily the case.  To    */
  212. /*  let you experiment to see what generates the fastest, smallest code    */
  213. /*  for your machine, I have declared internal scratch variables in    */
  214. /*  functions "SCRATCH int", "SCRATCH unsigned", etc.  A SCRATCH    */
  215. /*  varible is made static below, but you might want to try register    */
  216. /*  instead.                                */
  217.  
  218. #define    SCRATCH        static
  219.  
  220. /*  A slow, but portable way of cracking an unsigned into its various    */
  221. /*  component parts:                            */
  222.  
  223. #define    clamp(u)    ((u) &= 0xffff)
  224. #define    high(u)        (((u) >> 8) & 0xff)
  225. #define    low(u)        ((u) & 0xff)
  226. #define    word(u)        ((u) & 0xffff)
  227.  
  228. /*  The longest source line the assembler can hold without exploding:    */
  229.  
  230. #define    MAXLINE        255
  231.  
  232. /*  The maximum number of source files that can be open simultaneously:    */
  233.  
  234. #define    FILES        4
  235.  
  236. /*  The fatal error messages generated by the assembler:        */
  237.  
  238. #define    ASMOPEN        "Source File Did Not Open"
  239. #define    ASMREAD        "Error Reading Source File"
  240. #define    DSKFULL        "Disk or Directory Full"
  241. #define    FLOFLOW        "File Stack Overflow"
  242. #define    HEXOPEN        "Object File Did Not Open"
  243. #define    IFOFLOW        "If Stack Overflow"
  244. #define    LSTOPEN        "Listing File Did Not Open"
  245. #define    NOASM        "No Source File Specified"
  246. #define    SYMBOLS        "Too Many Symbols"
  247.  
  248. /*  The warning messages generated by the assembler:            */
  249.  
  250. #define    BADOPT        "Illegal Option Ignored"
  251. #define    NOHEX        "-o Option Ignored -- No File Name"
  252. #define    NOLST        "-l Option Ignored -- No File Name"
  253. #define    TWOASM        "Extra Source File Ignored"
  254. #define    TWOHEX        "Extra Object File Ignored"
  255. #define    TWOLST        "Extra Listing File Ignored"
  256.  
  257. /*  Line assembler (Z80A.C) constants:                    */
  258.  
  259. #define    BIGINST        4        /* longest instruction length    */
  260. #define    IFDEPTH        16        /* maximum IF nesting level    */
  261. #define    NOP        0x00        /* processor's NOP opcode    */
  262. #define    ON        1        /* assembly turned on        */
  263. #define    OFF        -1        /* assembly turned off        */
  264.  
  265. /*  Line assembler (Z80A.C) opcode attribute word flag masks:        */
  266.  
  267. #define    PSEUDO        0x8000        /* is pseudo-op            */
  268. #define    ISIF        0x4000        /* is IF, ELSE, or ENDI        */
  269.  
  270. #define    SHIFT        0x0f00        /* operand shift left count    */
  271. #define    OPTYPE        0x001f        /* opcode type mask        */
  272.  
  273. /*  Line assembler (Z80A.C) opcode types:                */
  274.  
  275. #define    NO_ARG        0        /*    no arg inst        */
  276. #define    ONE_ARG        1        /*    one (8 bit) arg        */
  277.                     /*    all 8 bit immediate inst*/
  278. #define    JUMP        2        /*    jump            */
  279. #define    CALL        3        /*    call             */
  280. #define    JREL        4        /*    jump relative        */
  281. #define    RETURN        5        /*    return            */
  282. #define    L_ARG        6        /*    Long (16 bit) arg    */
  283.                     /*      LDI16            */
  284. #define    L_ARG2        7        /*    Long (16 bit) arg type 2*/
  285.                     /*                  */
  286. #define    L_OP        8        /*    long operation        */
  287.                     /*      ADDHL, DEC16, INC16,    */
  288.                     /*      PUSH, POP        */
  289. #define    MOV        10        /*    LD instruction        */
  290. #define    RESTART        11        /*    restart instruction    */
  291. #define    S_OP        12        /*    short operation        */
  292.                     /*      ADD, AND, CMP, DEC,    */
  293.                     /*      INC, OR, SBC, SUB,    */
  294.                     /*      XOR ... needs SHIFT    */
  295.                     /*      value            */
  296. #define    NO_ARG_L    13        /* opcode is 2 bytes long    */
  297. #define    ONE_ARG_L    14        /* opcode is 2 bytes with arg    */
  298.                     /*    JUMPI, LDSP        */
  299. #define    ONE_ARG_L2    15        /* opcode is 2 bytes w/arg type 2*/
  300.                     /*    ADCHL, ADDIX, ADDIY,     */
  301.                     /*    SBCHL            */
  302. #define    ROTATE        16        /*     all extened rotate inst    */
  303. #define    XIO        17        /*    all extened I/O inst    */
  304. #define    BIT        18        /*    all bit type inst    */
  305. #define    L_ARG_L        19        /* opcode is 2 bytes w/ 2 byte arg */
  306.                     /*    LD16            */
  307. #define    L_ARG_L2    20        /* opcode 2bytes w/2byte arg #2 */
  308.                     /*    ST16            */
  309.  
  310. /*  Line assembler (Z80A.C) pseudo-op opcode token values:        */
  311.  
  312. #define    DB        0
  313. #define    DS        1
  314. #define    DW        2
  315. #define    ELSE        3
  316. #define    END        4
  317. #define    ENDIF        5
  318. #define    EQU        6
  319. #define    IF        7
  320. #define    INCL        8
  321. #define    ORG        9
  322. #define    PAGE        10
  323. #define    SET        11
  324. #define    TITL        12
  325. #define    PAGT        13
  326. #define    CODE        14
  327. #define    NCODE        15
  328. #define    CSECT        16
  329. #define    LIST        17
  330. #define    ULIST        18
  331. #define    SKP        19
  332. #define    TYP        20
  333. #define    URLC        21
  334. #define    RLC        22
  335. #define    MACRO        23
  336. #define    ENDM        24
  337.  
  338. /*  Lexical analyzer (Z80AEVAL.C) token buffer and stream pointer:    */
  339.  
  340. typedef struct {
  341.     unsigned attr;
  342.     unsigned valu;
  343.     char sval[MAXLINE + 1];
  344. } TOKEN;
  345.  
  346. /*  Lexical analyzer (Z80AEVAL.C) token attribute values:        */
  347.  
  348. #define    EOL        0    /*  end of line                */
  349. #define    SEP        1    /*  field separator            */
  350. #define    OPR        2    /*  operator                */
  351. #define    STR        3    /*  character string            */
  352. #define    VAL        4    /*  value                */
  353. #define    REG        5    /*  register or other tag        */
  354. #define    IMM        6    /*  immediate mode flag         */
  355. #define    CON        7    /*  conditional flag            */
  356. #define    MIX        8    /*  mixed - register or conditional    */
  357.  
  358. /*  Lexical analyzer (Z80AEVAL.C) token attribute word flag masks:    */
  359.  
  360. #define    BINARY        0x8000    /*  Operator:    is binary operator    */
  361. #define    UNARY        0x4000    /*        is unary operator    */
  362. #define    INDEX        0x1000    /*         is index reg        */
  363. #define    PREC        0x0f00    /*        precedence        */
  364.  
  365. #define    FORWD        0x8000    /*  Value:    is forward referenced    */
  366. #define    SOFT        0x4000    /*        is redefinable        */
  367. #define    PIN        0x2000    /*        is PIN addressed    */
  368. #define    PIN_VAL        0x1000    /*        is PIN valued        */
  369.  
  370. #define    TYPE        0x000f    /*  All:    token type        */
  371.  
  372. /*  Lexical analyzer (Z80AEVAL.C) register token values:        */
  373. #define    A        0x07    /*  A                    */
  374. #define    B        0x00    /*  B                    */
  375. #define    C        0x01    /*  C reg or C carry jump        */
  376. #define    D        0x02    /*  D                    */
  377. #define    E        0x03    /*  E                    */
  378. #define    H        0x04    /*  H                    */
  379. #define    L        0x05    /*  L                    */
  380. #define    M        0x06    /*  M reg or M minus jump        */
  381.  
  382. #define    X        0xdd    /*  IX                    */
  383. #define    Y        0xfd    /*  IY                    */
  384.  
  385. #define    AF        0x08    /*  AF (PSW)                */
  386. #define    SP        0x09    /*  SP                    */
  387. #define    BC        0x0a    /*  BC                    */
  388. #define    DE        0x0b    /*  DE                    */
  389. #define    HL        0x0c    /*  HL                    */
  390.  
  391. /*  Lexical analyzer (Z80AEVAL.C) conditional token values (+$10)    */
  392.  
  393. #define    NZ        0x10    /*  NZ non-zero jump            */
  394. #define    Z        0x11    /*  Z zero jump                */
  395. #define    NC        0x12    /*  NC no-carry jump (C already used)    */
  396.                 /*  C  carry ( 0x03)            */
  397. #define    PO        0x14    /*  PO parity odd jump            */
  398. #define    PE        0x15    /*  PE parity even jump            */
  399. #define    P        0x16    /*  P  plus jump (M already used)    */
  400.                 /*  M  minus ( 0x07)            */
  401.                 
  402. /*  Lexical analyzer (Z80AEVAL.C) operator token values (unlisted ones    */
  403. /*  use ASCII characters):                        */
  404.  
  405. #define    AND        0
  406. #define    GE        1
  407. #define    HIGH        2
  408. #define    LE        3
  409. #define    LOW        4
  410. #define    MOD        5
  411. #define    NE        6
  412. #define    NOT        7
  413. #define    OR        8
  414. #define    SHR        9
  415. #define    SHL        10
  416. #define    XOR        11
  417. #define    SIZE        12
  418. #define    LENOF        13
  419.  
  420. /*  Lexical analyzer (Z80AEVAL.C) operator precedence values:        */
  421.  
  422. #define    UOP1        0x0000    /*  unary +, unary -            */
  423. #define    MULT        0x0100    /*  *, /, MOD, SHL, SHR            */
  424. #define    ADDIT        0x0200    /*  binary +, binary -            */
  425. #define    RELAT        0x0300    /*  >, >=, =, <=, <, <>            */
  426. #define    UOP2        0x0400    /*  NOT                    */
  427. #define    LOG1        0x0500    /*  AND                    */
  428. #define    LOG2        0x0600    /*  OR, XOR                */
  429. #define    UOP3        0x0700    /*  HIGH, LOW, .            */
  430. #define    RPREN        0x0800    /*  )                    */
  431. #define    LPREN        0x0900    /*  (                    */
  432. #define    ENDEX        0x0a00    /*  end of expression            */
  433. #define    START        0x0b00    /*  beginning of expression        */
  434.  
  435. /*  Utility package (Z80AUTIL.C) symbol table routines:            */
  436.  
  437. struct _symbol {
  438.     unsigned attr;
  439.     unsigned valu;
  440.     unsigned len;
  441.     struct _symbol *left, *right;
  442.     char sname[1];
  443. };
  444.  
  445. typedef struct _symbol SYMBOL;
  446.  
  447. #define    SYMCOLS        4
  448.  
  449. /*  Utility package (Z80AUTIL.C) opcode/operator table routines:        */
  450.  
  451. typedef struct {
  452.     unsigned attr;
  453.     unsigned valu;
  454.     char oname[8];
  455. } OPCODE;
  456.  
  457. /*  Utility package (Z80AUTIL.C) hex file output routines:        */
  458.  
  459. #define    HEXSIZE        32
  460.