home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 276_01 / az80.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-03  |  12.2 KB  |  432 lines

  1. /*
  2.     HEADER:        CUG276;
  3.     TITLE:        Z-80 Cross-Assembler (Portable);
  4.     FILENAME:    AZ80.H;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Z-80 and NSC800 families of
  10.             microprocessors.  The program is written in portable C
  11.             rather than BDS C.  All assembler features are
  12.             supported except relocation, linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Zilog, Z-80, NSC800;
  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 is written in as portable C as possible.
  22.             A port to BDS C would be extremely difficult.  A port
  23.             to Toolworks C is untried."
  24.  
  25.     AUTHORS:    William C. Colley III;
  26. */
  27.  
  28. /*
  29.               Z-80 Cross-Assembler in Portable C
  30.  
  31.         Copyright (c) 1986-1988 William C. Colley, III
  32.  
  33. Revision History:
  34.  
  35. Ver    Date        Description
  36.  
  37. 0.0    JUNE 1987    Derived from my S-6 cross-assembler.  WCC3.
  38.  
  39. 0.1    AUG 1988    Fixed a bug in the command line parser that puts it
  40.             into a VERY long loop if the user types a command line
  41.             like "AZ80 FILE.ASM -L".  WCC3 per Alex Cameron.
  42.  
  43. This header file contains the global constants and data type definitions for
  44. all modules of the cross-assembler.  This also seems a good place to put the
  45. compilation and linkage instructions for the animal.  This list currently
  46. includes the following compilers:
  47.  
  48.         Compiler Name        Op. Sys.    Processor
  49.  
  50.     1)  Aztec C86            CP/M-86        8086, 8088
  51.                     MSDOS/PCDOS
  52.  
  53.     2)  AZTEC C II            CP/M-80        8080, Z-80
  54.  
  55.     3)  Computer Innovations C86    MSDOS/PCDOS    8086, 8088
  56.  
  57.     4)  Eco-C            CP/M-80        Z-80
  58.  
  59.     5)  Eco-C88            MSDOS/PCDOS    8086, 8088
  60.  
  61.     6)  HP C            HP-UX        68000
  62.  
  63.     7)  Lattice C            MSDOS/PCDOS    8086, 8088
  64.  
  65.     8)  Microsoft C            MSDOS/PCDOS    8086, 8088
  66.  
  67.     9)  QNIX C            QNIX        8086, 8088
  68.  
  69. Further additions will be made to the list as users feed the information to
  70. me.  This particularly applies to UNIX and IBM-PC compilers.
  71.  
  72. Compile-assemble-link instructions for this program under various compilers
  73. and operating systems:
  74.  
  75.     1)    Aztec C86:
  76.  
  77.     A)  Uncomment out the "#define AZTEC_C 1" line and comment out all
  78.         other compiler names in AZ80.H.
  79.  
  80.     B)  Assuming that all files are on drive A:, run the following
  81.         sequence of command lines:
  82.  
  83.         A>cc az80
  84.         A>cc az80eval
  85.         A>cc az80util
  86.         A>ln az80.o az80eval.o az80util.o -lc
  87.         A>era az80*.o
  88.  
  89.     2)  Aztec CII (version 1.06B):
  90.  
  91.     A)  Uncomment out the "#define AZTEC_C 1" line and comment out all
  92.         other compiler names in AZ80.H.
  93.  
  94.     B)  Assuming the C compiler is called "CC.COM" and all files are
  95.         on drive A:, run the following sequence of command lines:
  96.  
  97.         A>cc az80
  98.         A>as -zap az80
  99.         A>cc az80eval
  100.         A>as -zap az80eval
  101.         A>cc az80util
  102.         A>as -zap az80util
  103.         A>ln az80.o az80eval.o az80util.o -lc
  104.         A>era az80*.o
  105.  
  106.     3)  Computer Innovations C86:
  107.  
  108.     A)  Uncomment out the "#define CI_C86 1" line and comment out all
  109.         other compiler names in AZ80.H.
  110.  
  111.     B)  Compile the files AZ80.C, AZ80EVAL.C, and AZ80UTIL.C.  Link
  112.         according to instructions that come with the compiler.
  113.  
  114.     4)  Eco-C (CP/M-80 version 3.10):
  115.  
  116.     A)  Uncomment out the "#define ECO_C 1" line and comment out all
  117.         other compiler names in AZ80.H.
  118.  
  119.     B)  Assuming all files are on drive A:, run the following sequence of
  120.         command lines:
  121.  
  122.         A>cp az80 -i -m
  123.         A>cp az80eval -i -m
  124.         A>cp az80util -i -m
  125.         A>l80 az80,az80eval,az80util,az80/n/e
  126.         A>era az80*.mac
  127.         A>era az80*.rel
  128.  
  129.     5)  Eco-C88:
  130.  
  131.     A)  Uncomment out the "#define ECO_C 1" line and comment out all
  132.         other compiler names in AZ80.H.
  133.  
  134.     B)  Compile the files AZ80.C, AZ80EVAL.C, and AZ80UTIL.C.  Link
  135.         according to instructions that come with the compiler.
  136.  
  137.     6)  HP-UX (a UNIX look-alike running on an HP-9000 Series 200/500,
  138.     68000-based machine):
  139.  
  140.     A)  Uncomment out the "#define HP_UX 1" line and comment out all
  141.         other compiler names in AZ80.H.
  142.  
  143.     B)  Run the following command line:
  144.  
  145.         . cc az80.c az80eval.c az80util.c
  146.  
  147.     7)  Lattice C:
  148.  
  149.     A)  Uncomment out the "#define LATTICE_C 1" line and comment out all
  150.         other compiler names in AZ80.H.
  151.  
  152.     B)  Compile the files AZ80.C, AZ80EVAL.C, and AZ80UTIL.C.  Link
  153.         according to instructions that come with the compiler.
  154.  
  155.     8)  Microsoft C (version 3.00):
  156.  
  157.     A)  Uncomment out the "#define MICROSOFT_C 1" line and comment out
  158.         all other compiler names in AZ80.H.
  159.  
  160.     B)  Run the following command line:
  161.  
  162.         C>cl az80.c az80eval.c az80util.c
  163.  
  164.     9)    QNIX C:
  165.  
  166.     A)  Uncomment out the "#define QNIX 1" line and comment out all other
  167.         compiler names in AZ80.H.
  168.  
  169.     B)  Run the following command line:
  170.  
  171.         . cc az80.c az80eval.c az80util.c
  172.  
  173. Note that, under CP/M-80, you can't re-execute a core image from a previous
  174. assembly run with the "@.COM" trick.  This technique is incompatible with the
  175. Aztec CII compiler, so I didn't bother to support it at all.
  176. */
  177.  
  178. #include <stdio.h>
  179.  
  180. /*  Comment out all but the line containing the name of your compiler:    */
  181.  
  182. #define        AZTEC_C        1
  183. /* #define    CI_C86        1                    */
  184. /* #define    ECO_C        1                    */
  185. /* #define    HP_UX        1                    */
  186. /* #define    LATTICE_C    1                    */
  187. /* #define    MICROSOFT_C    1                    */
  188. /* #define    QNIX        1                    */
  189.  
  190. /*  Compiler dependencies:                        */
  191.  
  192. #ifdef    AZTEC_C
  193. #define    getc(f)        agetc(f)
  194. #define    putc(c,f)    aputc(c,f)
  195. #endif
  196.  
  197. #ifndef    ECO_C
  198. #define    FALSE        0
  199. #define    TRUE        (!0)
  200. #endif
  201.  
  202. #ifdef    LATTICE_C
  203. #define    void        int
  204. #endif
  205.  
  206. #ifdef    QNIX
  207. #define    fprintf        tfprintf
  208. #define    printf        tprintf
  209. #endif
  210.  
  211. /*  On 8-bit machines, the static type is as efficient as the register    */
  212. /*  type and far more efficient than the auto type.  On larger machines    */
  213. /*  such as the 8086 family, this is not necessarily the case.  To    */
  214. /*  let you experiment to see what generates the fastest, smallest code    */
  215. /*  for your machine, I have declared internal scratch variables in    */
  216. /*  functions "SCRATCH int", "SCRATCH unsigned", etc.  A SCRATCH    */
  217. /*  variable is made static below, but you might want to try register    */
  218. /*  instead.                                */
  219.  
  220. #define    SCRATCH        static
  221.  
  222. /*  A slow, but portable way of cracking an unsigned into its various    */
  223. /*  component parts:                            */
  224.  
  225. #define    clamp(u)    ((u) &= 0xffff)
  226. #define    high(u)        (((u) >> 8) & 0xff)
  227. #define    low(u)        ((u) & 0xff)
  228. #define    word(u)        ((u) & 0xffff)
  229.  
  230. /*  The longest source line the assembler can hold without exploding:    */
  231.  
  232. #define    MAXLINE        255
  233.  
  234. /*  The maximum number of source files that can be open simultaneously:    */
  235.  
  236. #define    FILES        4
  237.  
  238. /*  The fatal error messages generated by the assembler:        */
  239.  
  240. #define    ASMOPEN        "Source File Did Not Open"
  241. #define    ASMREAD        "Error Reading Source File"
  242. #define    DSKFULL        "Disk or Directory Full"
  243. #define    FLOFLOW        "File Stack Overflow"
  244. #define    HEXOPEN        "Object File Did Not Open"
  245. #define    IFOFLOW        "If Stack Overflow"
  246. #define    LSTOPEN        "Listing File Did Not Open"
  247. #define    NOASM        "No Source File Specified"
  248. #define    SYMBOLS        "Too Many Symbols"
  249.  
  250. /*  The warning messages generated by the assembler:            */
  251.  
  252. #define    BADOPT        "Illegal Option Ignored"
  253. #define    NOHEX        "-o Option Ignored -- No File Name"
  254. #define    NOLST        "-l Option Ignored -- No File Name"
  255. #define    TWOASM        "Extra Source File Ignored"
  256. #define    TWOHEX        "Extra Object File Ignored"
  257. #define    TWOLST        "Extra Listing File Ignored"
  258.  
  259. /*  Line assembler (AZ80.C) constants:                    */
  260.  
  261. #define    BIGINST        4        /*  longest instruction length    */
  262. #define    IFDEPTH        16        /*  maximum IF nesting level    */
  263. #define    NOP        0x00        /*  processor's NOP opcode    */
  264. #define    ON        1        /*  assembly turned on        */
  265. #define    OFF        -1        /*  assembly turned off        */
  266.  
  267. /*  Line assembler (AZ80.C) opcode attribute word flag masks:        */
  268.  
  269. #define    PSEUDO        0400    /*  is pseudo op            */
  270. #define    ISIF        0200    /*  is IF, ELSE, or ENDIF        */
  271.  
  272. /*  Line assembler (AZ80.C) pseudo-op opcode token values:        */
  273.  
  274. #define    DB        1
  275. #define    DC        2
  276. #define    DS        3
  277. #define    DW        4
  278. #define    ELSE        5
  279. #define    END        6
  280. #define    ENDIF        7
  281. #define    EQU        8
  282. #define    IF        9
  283. #define    INCL        10
  284. #define    ORG        11
  285. #define    PAGE        12
  286. #define    TITLE        13
  287. #define    VAR        14
  288.  
  289. /*  Line assembler (AZ80.C) machine opcode attribute values:        */
  290.  
  291. #define    OPTYPE        0177    /*  Opcode type:            */
  292. #define    LD        0022    /*    LD                */
  293. #define    POP        0021    /*    POP, PUSH            */
  294. #define    EX        0020    /*    EX                */
  295. #define    ADD        0017    /*    ADD                */
  296. #define    ADC        0016    /*    ADC, SBC            */
  297. #define    CP        0015    /*    AND, CP, OR, SUB, XOR        */
  298. #define    DEC        0014    /*    DEC, INC            */
  299. #define    BIT        0013    /*    BIT, RES, SET            */
  300. #define    RLC        0012    /*    RL, RLC, RR, RRC, SLA, SRA, SRL    */
  301. #define    JR        0011    /*    JR                */
  302. #define    DJNZ        0010    /*    DJNZ                */
  303. #define    JP        0007    /*    JP                */
  304. #define    CALL        0006    /*    CALL                */
  305. #define    RET        0005    /*    RET                */
  306. #define    IN        0004    /*    IN                */
  307. #define    OUT        0003    /*    OUT                */
  308. #define    RST        0002    /*    RST                */
  309. #define    IM        0001    /*    IM                */
  310. #define    NO_ARGS        0000    /*    CCF, CPD, CPDR, CPI, CPIR, etc.    */
  311.  
  312. /*  Opcode argument field parser (AZ80EVAL.C) attribute values:        */
  313.  
  314. #define    B        000    /*  B                    */
  315. #define    C        001    /*  C                    */
  316. #define    D        002    /*  D                    */
  317. #define    E        003    /*  E                    */
  318. #define    H        004    /*  H                    */
  319. #define    L        005    /*  L                    */
  320. #define    HL_IND        006    /*  (HL)                */
  321. #define    A        007    /*  A                    */
  322. #define    NZ        010    /*  NZ                    */
  323. #define    Z        011    /*  Z                    */
  324. #define    NC        012    /*  NC                    */
  325. #define    CY        013    /*  C becomes CY on JP, CALL, etc.    */
  326. #define    PO        014    /*  PO                    */
  327. #define    PE        015    /*  PE                    */
  328. #define    P        016    /*  P                    */
  329. #define    M        017    /*  M                    */
  330. #define    BC        020    /*  BC                    */
  331. #define    DE        022    /*  DE                    */
  332. #define    HL        024    /*  HL                    */
  333. #define    AF        026    /*  AF                    */
  334. #define    IX        034    /*  IX                    */
  335. #define    IY        044    /*  IY                    */
  336. #define    SP        046    /*  SP                    */
  337. #define    BC_IND        050    /*  (BC)                */
  338. #define    DE_IND        052    /*  (DE)                */
  339. #define    IX_IND        056    /*  (IX+d)                */
  340. #define    IY_IND        066    /*  (IY+d)                */
  341. #define    SP_IND        076    /*  (SP)                */
  342. #define    I        070    /*  I                    */
  343. #define    R        071    /*  R                    */
  344. #define    C_IND        072    /*  (C)                    */
  345. #define    NUM        073    /*  nn                    */
  346. #define    NUM_IND        074    /*  (nn)                */
  347. #define    NONE        075    /*  nothing                */
  348.  
  349. /*  Lexical analyzer (AZ80EVAL.C) token buffer and stream pointer:    */
  350.  
  351. typedef struct {
  352.     unsigned attr;
  353.     unsigned valu;
  354.     char sval[MAXLINE + 1];
  355. } TOKEN;
  356.  
  357. /*  Lexical analyzer (AZ80EVAL.C) token attribute values:        */
  358.  
  359. #define    EOL        0    /*  end of line                */
  360. #define    SEP        1    /*  field separator            */
  361. #define    OPR        2    /*  operator                */
  362. #define    STR        3    /*  character string            */
  363. #define    VAL        4    /*  value                */
  364. #define    REG        5    /*  register tag            */
  365.  
  366. /*  Lexical analyzer (AZ80EVAL.C) token attribute word flag masks:    */
  367.  
  368. #define    BINARY        0x8000    /*  Operator:    is binary operator    */
  369. #define    UNARY        0x4000    /*        is unary operator    */
  370. #define    PREC        0x0f00    /*        precedence        */
  371.  
  372. #define    FORWD        0x8000    /*  Value:    is forward referenced    */
  373. #define    SOFT        0x4000    /*        is redefinable        */
  374.  
  375. #define    TYPE        0x000f    /*  All:    token type        */
  376.  
  377. /*  Lexical analyzer (AZ80EVAL.C) operator token values (unlisted ones    */
  378. /*  use ASCII characters):                        */
  379.  
  380. #define    AND        0
  381. #define    GE        1
  382. #define    HIGH        2
  383. #define    LE        3
  384. #define    LOW        4
  385. #define    MOD        5
  386. #define    NE        6
  387. #define    NOT        7
  388. #define    OR        8
  389. #define    SHR        9
  390. #define    SHL        10
  391. #define    XOR        11
  392.  
  393. /*  Lexical analyzer (AZ80EVAL.C) operator precedence values:        */
  394.  
  395. #define    UOP1        0x0000    /*  unary +, unary -            */
  396. #define    MULT        0x0100    /*  *, /, MOD, SHL, SHR            */
  397. #define    ADDIT        0x0200    /*  binary +, binary -            */
  398. #define    RELAT        0x0300    /*  >, >=, =, <=, <, <>            */
  399. #define    UOP2        0x0400    /*  NOT                    */
  400. #define    LOG1        0x0500    /*  AND                    */
  401. #define    LOG2        0x0600    /*  OR, XOR                */
  402. #define    UOP3        0x0700    /*  HIGH, LOW                */
  403. #define    RPREN        0x0800    /*  )                    */
  404. #define    LPREN        0x0900    /*  (                    */
  405. #define    ENDEX        0x0a00    /*  end of expression            */
  406. #define    START        0x0b00    /*  beginning of expression        */
  407.  
  408. /*  Utility package (AZ80UTIL.C) symbol table routines:            */
  409.  
  410. struct _symbol {
  411.     unsigned attr;
  412.     unsigned valu;
  413.     struct _symbol *left, *right;
  414.     char sname[1];
  415. };
  416.  
  417. typedef struct _symbol SYMBOL;
  418.  
  419. #define    SYMCOLS        4
  420.  
  421. /*  Utility package (AZ80UTIL.C) opcode/operator table routines:        */
  422.  
  423. typedef struct {
  424.     unsigned attr;
  425.     unsigned valu;
  426.     char oname[6];
  427. } OPCODE;
  428.  
  429. /*  Utility package (AZ80UTIL.C) hex file output routines:        */
  430.  
  431. #define    HEXSIZE        32
  432.