home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / dev / cross / caz / src / caz.c < prev    next >
C/C++ Source or Header  |  1994-01-08  |  63KB  |  2,609 lines

  1. #define _STRICT_ANSI
  2.  
  3. #include "defs.h"
  4. #include "cazerrors.h"
  5.  
  6. /*#define DEBUG2 */    /* internal use for more debugging (print out coded MNE) */
  7. /*#define DEBUG_STRTOINT*/  /* debug strtoint() funktion */
  8.  
  9.  
  10. #define MODE_ALL    1        /* Verbose Mode: Print out all source text */
  11. #define MODE_MNE    2        /* Verbose Mode: Print out only MNE text */
  12. #define MODE_OPTION 4        /* if Verbose Mode is set via Commandline Option */
  13.  
  14. #define ACT_OBJ_ADDR(pobj) (((u_char *)(pobj->objbuffer+(pobj->actbyte-pobj->firstbyte))))
  15.  
  16. #define COPYRIGHT " ©1992-94 by Carsten Rose\n"
  17. const char *vers = "\0$VER: Z80_Assenbler 1.26ß (7 Januar 94)";
  18.  
  19. extern char *directives[];        /* declared in File 'cazmne.c' */
  20. extern struct command cmd[];    /* declared in File 'cazmne.c' */
  21.  
  22. static int        init                (struct listheader *plabel,struct objsize *pobj);
  23. static void    symboltable        (struct listheader *plabel);
  24. static int     writesymboltable    (struct listheader *plabel,char *filename);
  25. static char   *ibtoh            (u_char byte,char *pbuf);
  26. static char   *iwtoh            (unsigned short word,char *pbuf);
  27. static void    decodearg        (char *str,u_long pa);
  28. static int        strcmp_lany_rupper(char *pany,char *pupper);
  29. static void    cutend            (char *pos);
  30. static char   *skipspace        (char *pos);
  31. static char    validquote        (char quote);
  32. static void    killcomment        (char *pos);
  33. static int        expandlist        (struct listheader *plist);
  34. static struct labelitem 
  35.               *insertlabel        (struct listheader *plabel,char *name,int type,u_short value,BOOL valid);
  36. static int        dolabel            (struct listheader *plabel,struct objsize *pobjcode,char   *lname);
  37. static int        finddirective    (char *pos);
  38. static struct command
  39.               *findmne            (struct command *pcmd,char *pmne);
  40. static u_long  argtype            (struct listheader *plabel,char *pos,u_short *pshort,int mode);
  41. static int        doobjadr            (struct objsize *pobj,int n,int mode);
  42. static u_char  codebits            (u_long mask);
  43. static int        writemnedata        (struct objsize *pobj,u_long maskarg,u_short val,u_long maskcmd,u_char pos);
  44. static int        fillihbuffer        (int addr,int nbytes,u_char *pbuf);
  45. static int        dointelhex        (int addr,int size,u_char *data);
  46. static int        makecode            (struct objsize *pobj,struct command *pcmd,int mode,u_long type1,u_short val1,u_long type2,u_short val2);
  47. static int        domne            (struct listheader *plabel,struct objsize *pobj,char *ptok,int mode,u_short *pcycle);
  48. static int       pushfile            (struct item_file **pstack,char *p_filenamenew,FILE **p_fp);
  49. static int       popfile            (struct item_file **pstack,FILE **p_fp);
  50. static int        setdefaultbase    (char *pbase);
  51. static int        dodirective        (struct listheader *plabel,struct objsize *pobj,char *ptok,int direc,int mode,int *orgflag);
  52. static int        parseline        (struct listheader *plabel,struct objsize *pobj,char *pwork,int mode);
  53. static int        readsrc            (struct listheader *plabel,struct objsize *pobj,char *p_actname);
  54.  
  55. int            main             (int argc,char *argv[]);
  56.             
  57. int     lnr=0,errors=0,
  58.         debugv=FALSE,            /* debug values, set via Commandline option */ 
  59.         debugf=FALSE,            /* debug functions, set via Commandline option */ 
  60.         verbose=FALSE,            /* set via Commandline option */
  61.         intelhex=FALSE,            /* set via Commandline option */
  62.         clockcycle=FALSE,        /* set via Commandline option */
  63.         showsymbols=FALSE;        /* set via Commandline option */
  64.         default_num_base;        /* change via DEFNUM directive */
  65. char    errortext[BUFSIZ],        /* Common Buffer for Error Messages */
  66.         line[BUFSIZ],            /* Actual read Source text line (original) */
  67.         work[BUFSIZ],            /* Actual read Source text line (truncated,modified) */
  68.         actfilename[FILENAME_MAX]; /* Actual Filename of assembled Source File */
  69.  
  70. struct    listheader  ih_head;
  71. struct    listheader label;
  72. struct    objsize    object;
  73. struct  item_file    *file_stack=NULL;
  74.  
  75. BOOL    endflag=FALSE,            /* Set if END is found */
  76.         assembleflag=TRUE;        /* Normal TRUE, only if 'COND xx' xx==0 then FALSE */
  77. FILE     *fpin=NULL,*fpout=NULL;
  78.  
  79.  
  80. static void showerror(char *errpos, char *fmt , ...)
  81. {
  82.     /*
  83.      * Print parsing error:
  84.      *
  85.      *   xxxx:........................
  86.      *              ^
  87.      *   'Error Description'
  88.      */
  89.  
  90.         int i;
  91.         va_list pa;
  92.         char buffer[BUFSIZ];
  93.  
  94.     if(debugf)  puts("showerror");
  95.  
  96.         /* If there are more than 1 source files - print Filenames */
  97.     if(file_stack)
  98.         fprintf(stderr,"Error in File:%s\n",actfilename);
  99.  
  100.     fprintf(stderr,"%4d:%s",lnr,line);        /* line number and Source code line */
  101.  
  102.     i = strlen(line);
  103.  
  104.     if(!i)                /* not really needed */
  105.         return;
  106.  
  107.     if(line[i-1]!='\n')
  108.         fputs("\n",stderr);
  109.  
  110.     if(!errpos)
  111.         errpos=work;
  112.  
  113.     i = (int)(errpos - work);
  114.  
  115.     buffer[i]='\0';
  116.  
  117.     while(i--)
  118.     {
  119.         if(line[i]=='\t')
  120.             buffer[i]='\t';
  121.         else
  122.             buffer[i]=' ';
  123.     }
  124.  
  125.     fprintf(stderr,"     %s^ ",buffer);        /* mark position */
  126.  
  127.     va_start(pa,fmt);
  128.     vfprintf(stderr,fmt,pa);                /* Print Errormessage */
  129.     va_end(pa);
  130.     fputs("\n",stderr);
  131.     errors++;                        /* count errors */
  132. }
  133.  
  134. static int init(struct listheader *plabel,struct objsize *pobj)
  135. {
  136.     /*
  137.      * Init listheader
  138.      *
  139.      * RC : NULL if all went fine 
  140.      */
  141.      
  142.  
  143.     if(debugf)  puts("init");
  144.  
  145.     plabel->nitem = 0;
  146.     plabel->actitem = 0;
  147.     plabel->sizeitem = sizeof(struct labelitem);
  148.     plabel->newnitem = NEWITEMOFF;
  149.     plabel->userdata = ERROR;
  150.     plabel->list = NULL;
  151.  
  152.     ih_head.nitem = 0;
  153.     ih_head.actitem = 0;
  154.     ih_head.sizeitem = sizeof(u_char);
  155.     ih_head.newnitem = 4096;
  156.     ih_head.userdata = ERROR;
  157.     ih_head.list = NULL;
  158.  
  159.     pobj->firstbyte = ERROR;
  160.     pobj->actbyte   = ERROR;
  161.     pobj->lastbyte  = ERROR;
  162.     pobj->objbuffer = NULL;
  163.  
  164.     return(NULL);
  165. }
  166.  
  167. static void symboltable(struct listheader *plabel)
  168. {
  169.     int i;
  170.     struct labelitem *lst;
  171.     char *pltyp;
  172.  
  173.     if(debugf)  puts("symboltable");
  174.  
  175.     lst = plabel->list;
  176.     i   = plabel->actitem;
  177.     puts("Lable's:");
  178.  
  179.     while(i--)
  180.     {
  181.         switch(lst->type)
  182.         {
  183.             case L_EQU:        pltyp = "EQU";
  184.                             break;
  185.             case L_DEFL:    pltyp = "DEFL";
  186.                             break;
  187.             case L_POSITION:
  188.                             pltyp = "POSITION";
  189.                             break;
  190.             deafult:        pltyp = "unknown";
  191.                             INTERNAL_ERROR
  192.                             break;
  193.         }
  194.         
  195.         printf("Name:%-32s  Type:%-8s  Value:0x%X\n",lst->name,pltyp,lst->value);
  196.         lst++;
  197.     }
  198. }
  199.  
  200. static int writesymboltable(struct listheader *plabel,char *filename)
  201. {
  202.     /* Write all Labels as EQU definition's with associated values into
  203.      * 'filename'
  204.      *
  205.      * RC: NULL if all went fine else
  206.      *     ERROR
  207.      */
  208.  
  209.     int i;
  210.     struct labelitem *lst;
  211.     FILE   *fplabel;
  212.  
  213.     if(debugf)  puts("writesymboltable");
  214.  
  215.     lst = plabel->list;
  216.     i   = plabel->actitem;
  217.  
  218.     if(!(fplabel = fopen(filename,"wb")))    /* open file where to write Label's */
  219.     {
  220.         perror("Can't open file:");
  221.         return(ERROR);
  222.     }
  223.  
  224.     while(i--)
  225.     {
  226.         if(fprintf(fplabel,"%-32s EQU 0x%X\n",lst->name,lst->value)<0)
  227.         {
  228.             perror("Writing Labelfile failed");
  229.             fclose(fplabel);
  230.             return(ERROR);
  231.         }
  232.         lst++;
  233.     }
  234.  
  235.     fclose(fplabel);
  236.  
  237.     return(NULL);
  238. }
  239.  
  240.  
  241. static char *ibtoh(unsigned char byte,char *pbuf)
  242. {
  243.     /* Convert 'byte' in ASCII Hex Notation.
  244.      * 
  245.      * Write ASCII Hex Code (2 Chars) at '*pbuf'.
  246.      *
  247.      * RC: pbuf+2    (points to next free char)
  248.      */  
  249.  
  250.     unsigned char     low,    /* low 4 Bits of 'byte' */
  251.             high;    /* high 4 Bits of 'byte' (normalized) */
  252.  
  253.     low = 0x0F&byte;
  254.     high = (0xF0&byte)>>4;
  255.  
  256.     /* convert Integer to ASCII Hex */
  257.     *pbuf++= (high<=9) ? high+'0' : (high-0x0A)+'A';
  258.     *pbuf++= (low<=9) ? low+'0' : (low-0x0A)+'A';
  259.  
  260.     return(pbuf);
  261. }
  262.  
  263. static char *iwtoh(u_short word,char *pbuf)
  264. {
  265.     /* Convert 'word' in ASCII Hex Notation.
  266.      * 
  267.      * Write ASCII Hex Code (4 Chars) at '*pbuf'.
  268.      *
  269.      * RC: pbuf+4    (points to next free char)
  270.      */  
  271.  
  272.     char *pos;
  273.     u_char lowb,highb;
  274.  
  275.     lowb = (u_char)(word & 0xFF);
  276.     highb = (u_char)(word >> 8);
  277.  
  278.     pos=ibtoh(highb,pbuf);
  279.     pos=ibtoh(lowb,pos);
  280.  
  281.     return(pos);
  282. }
  283.  
  284. static void decodearg(char *str,u_long pa)
  285. {
  286.     char pos[256];
  287.  
  288.     if(debugf)  puts("decodearg");
  289.  
  290.     pos[0]='\0';
  291.  
  292.     if( (pa^BRA)&BRA)
  293.     {
  294.         if(!( (pa®8)^REG8))
  295.             strcat(pos,"REG8   ");
  296.         else if(!( (pa®16)^REG16))
  297.             strcat(pos,"REG16  ");
  298.         else
  299.         {
  300.             if(pa®_A)  strcat(pos,"REG_A  ");
  301.             if(pa®_B)  strcat(pos,"REG_B  ");
  302.             if(pa®_C)  strcat(pos,"REG_C  ");
  303.             if(pa®_D)  strcat(pos,"REG_D  ");
  304.             if(pa®_F)  strcat(pos,"REG_F  ");
  305.             if(pa®_H)  strcat(pos,"REG_H  ");
  306.             if(pa®_I)  strcat(pos,"REG_I  ");
  307.             if(pa®_L)  strc