add ucsim z80 test case
[fw/sdcc] / as / xa51 / xa_main.c
index c38221d45c611417a3958b2eb372442a343d8199..91c70944865e0317f3d4396041260291d38d6014 100644 (file)
@@ -21,6 +21,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
+
+#define printf(x...) fprintf(stderr,x)
 
 #include "xa_main.h"
 
@@ -31,7 +34,7 @@ extern int yyparse();
 
 /* global variables */
 
-FILE *fhex, *fmem, *list_fp;
+FILE *fhex, *fmem, *list_fp, *sym_fp;
 extern FILE *yyin;
 extern char *yytext;
 extern char last_line_text[];
@@ -46,6 +49,25 @@ struct area_struct area[NUM_AREAS];
 int current_area=AREA_CSEG;
 
 
+char *areaToString (int area) {
+  switch (area) 
+    {
+    case AREA_CSEG: return "CSEG";
+    case AREA_DSEG: return "DSEG";
+      //case AREA_OSEG: return "OSEG";
+      //case AREA_ISEG: return "ISEG";
+    case AREA_BSEG: return "BSEG";
+    case AREA_XSEG: return "XSEG";
+    case AREA_XISEG: return "XISEG";
+    case AREA_XINIT: return "XINIT";
+    case AREA_GSINIT: return "GSINIT";
+    case AREA_GSFINAL: return "GSFINAL";
+    case AREA_HOME: return "HOME";
+    case AREA_SSEG: return "SSEG";
+    }
+  return ("UNKNOW");
+}
+
 /* "mem" is replaced by area[current_area].alloc_position */
 /* int mem=0; */   /* mem is location in memory */
 
@@ -68,6 +90,7 @@ struct symbol * build_sym_list(char *thename)
        new->isbit = 0;
        new->isreg = 0;
        new->line_def = lineno - 1;
+       new->area = current_area;
        new->next = NULL;
        if (sym_list == NULL) return (sym_list = new);
        p = sym_list;
@@ -109,6 +132,22 @@ int mk_bit(char *thename)
         exit(1);
 }
 
+int mk_sfr(char *thename)
+{
+        struct symbol *p;
+
+        p = sym_list;
+        while (p != NULL) {
+                if (!(strcasecmp(thename, p->name))) {
+                        p->issfr = 1;
+                        return (0);
+                }
+                p = p->next;
+        }
+        fprintf(stderr, "Internal Error!  Couldn't find symbol\n");
+        exit(1);
+}
+
 
 int mk_reg(char *thename)
 {
@@ -180,19 +219,32 @@ void flag_targets()
 
 void print_symbol_table()
 {
-       struct symbol *p;
-       p = sym_list;
-       while (p != NULL) {
-               printf("Sym:%12s = %5d (%04X)  Def:", \
-                 p->name, p->value, p->value);
-               if (p->isdef) printf("Yes"); else printf("No ");
-               printf("  Bit:");
-               if (p->isbit) printf("Yes"); else printf("No ");
-               printf("  Target:");
-               if (p->istarget) printf("Yes"); else printf("No ");
-               printf(" Line %d\n", p->line_def);
-               p = p->next;
-       }
+  struct symbol *p;
+  p = sym_list;
+  while (p != NULL) {
+#if 0
+    fprintf(sym_fp, "Sym in %-5s: %s\n", areaToString(p->area), p->name);
+    fprintf(sym_fp, "  at: 0x%04X (%5d)", p->value, p->value);
+    fprintf(sym_fp, " Def:%s", p->isdef ? "Yes" : "No ");
+    fprintf(sym_fp, " Bit:%s", p->isbit ? "Yes" : "No ");
+    fprintf(sym_fp, " Target:%s", p->istarget ? "Yes" : "No ");
+    fprintf(sym_fp, " Line %d\n", p->line_def);
+#else
+    if (p->issfr) {
+      fprintf (sym_fp, "%-5s", "SFR");
+    } else if (p->isbit && !p->area) {
+      fprintf (sym_fp, "%-5s", "SBIT");
+    } else {
+      fprintf (sym_fp, "%-5s", areaToString(p->area));
+    }
+    fprintf (sym_fp, " 0x%04x (%5d)", p->value, p->value);
+    fprintf (sym_fp, " %s", p->isdef ? "D" : "-");
+    fprintf (sym_fp, "%s", p->isbit ? "B" : "-");
+    fprintf (sym_fp, "%s", p->istarget ? "T" : "-");
+    fprintf (sym_fp, " %s\n", p->name);
+#endif
+    p = p->next;
+  }
 }
 
 /* check that every symbol is in the table only once */
@@ -274,17 +326,19 @@ void out(int *byte_list, int num)
        if (num > 0) fprintf(list_fp, "%06X: ", MEM_POS);
        else fprintf(list_fp, "\t");
 
-       for (i=0; i<num; i++) {
-               hexout(byte_list[i], MEM_POS + i, 0);
-               if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
-               fprintf(list_fp, "%02X", byte_list[i]);
-               if ((i+1) % 4 == 0) {
-                       if (first) fprintf(list_fp, "\t%s\n", last_line_text);
-                       else fprintf(list_fp, "\n");
-                       first = 0;
-               } else {
-                       if (i<num-1) fprintf(list_fp, " ");
-               }
+       if (current_area==AREA_CSEG || current_area==AREA_XINIT) {
+         for (i=0; i<num; i++) {
+           hexout(byte_list[i], MEM_POS + i, 0);
+           if (!first && (i % 4) == 0) fprintf(list_fp, "\t");
+           fprintf(list_fp, "%02X", byte_list[i]);
+           if ((i+1) % 4 == 0) {
+             if (first) fprintf(list_fp, "\t%s\n", last_line_text);
+             else fprintf(list_fp, "\n");
+             first = 0;
+           } else {
+             if (i<num-1) fprintf(list_fp, " ");
+           }
+         }
        }
        if (first) {
                if (num < 3) fprintf(list_fp, "\t");
@@ -352,7 +406,7 @@ int binary2int(char *str)
        return (sum);
 }
 
-void print_usage();
+void print_usage(int);
 
 
 /* todo: someday this will allow the user to control where the */
@@ -363,18 +417,99 @@ void print_usage();
 
 void init_areas(void)
 {
-       area[AREA_CSEG].alloc_position = 0;
-       area[AREA_DSEG].alloc_position = 0x30;
-       area[AREA_OSEG].alloc_position = 0x80;
-       area[AREA_ISEG].alloc_position = 0;
-       area[AREA_BSEG].alloc_position = 0;
-       area[AREA_XSEG].alloc_position = 0;
-       area[AREA_XISEG].alloc_position = 0;
-       area[AREA_GSINIT].alloc_position = 0;
-       area[AREA_GSFINAL].alloc_position = 0;
-       area[AREA_HOME].alloc_position = 0;
+  area[AREA_CSEG].start=area[AREA_CSEG].alloc_position = 0;
+  area[AREA_DSEG].start=area[AREA_DSEG].alloc_position = 0x30;
+  area[AREA_BSEG].start=area[AREA_BSEG].alloc_position = 0;
+  area[AREA_XSEG].start=area[AREA_XSEG].alloc_position = 0;
+  area[AREA_XISEG].start=area[AREA_XISEG].alloc_position = 0;
+  area[AREA_XINIT].start=area[AREA_XINIT].alloc_position = 0;
+  area[AREA_GSINIT].start=area[AREA_GSINIT].alloc_position = 0;
+  area[AREA_GSFINAL].start=area[AREA_GSFINAL].alloc_position = 0;
+  area[AREA_HOME].start=area[AREA_HOME].alloc_position = 0;
 }
 
+void addAreaSymbols() {
+  char buffer[132];
+  int i;
+  for (i=0; i<NUM_AREAS; i++) {
+    current_area=i;
+    sprintf (buffer, "s_%s", areaToString(i));
+    build_sym_list (buffer);
+    assign_value (buffer, area[i].start);
+    buffer[0]='l';
+    build_sym_list (buffer);
+    assign_value (buffer, area[i].alloc_position-area[i].start);
+  }
+}
+
+void printVersion() {
+  printf("\nPaul's XA51 Assembler\n");
+  printf("Copyright 1997,2002 Paul Stoffregen\n\n");
+  printf("This program is free software; you can redistribute it\n");
+  printf("and/or modify it under the terms of the GNU General Public\n");
+  printf("License, Version 2, published by the Free Software Foundation\n\n");
+  printf("This program is distributed in the hope that it will be useful,\n");
+  printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
+  printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
+}
+
+char infilename[PATH_MAX];
+char outfilename[PATH_MAX];
+char listfilename[PATH_MAX];
+char symfilename[PATH_MAX];
+//char mapfilename[PATH_MAX];
+
+int verbose=0, createSymbolFile=0;
+
+void process_args(int argc, char **argv) 
+{
+  int i=0;
+
+  if (argc < 2) print_usage(1);
+  
+  while (++i<argc && *argv[i]=='-') {
+    if (strcmp(argv[i], "--version")==0) {
+      printVersion();
+      exit (0);
+    }
+    if (strcmp(argv[i], "--help")==0) {
+      print_usage(0);
+    }
+    if (strcmp(argv[i], "-v")==0) {
+      verbose++;
+      continue;
+    }
+    if (strcmp(argv[i], "-s")==0) {
+      createSymbolFile++;
+      continue;
+    }
+    print_usage(1);
+  }
+
+  if (i!=argc-1) {
+    // only 1 source file for now
+    print_usage(1);
+  }
+
+  strcpy(infilename, argv[i]);
+
+  if (strncasecmp(infilename+strlen(infilename)-3, ".xa", 3)) {
+    fprintf (stderr, "unrecognized input file: \"%s\"\n", argv[i]);
+    print_usage(1);
+  }
+
+  strcpy(outfilename, infilename);
+  outfilename[strlen(outfilename)-3] = '\0';
+  strcpy(listfilename, outfilename);
+  if (createSymbolFile) {
+    strcpy(symfilename, outfilename);
+    strcat(symfilename, ".sym");
+  }
+  //strcpy(mapfilename, outfilename);
+  strcat(outfilename, ".hex");
+  strcat(listfilename, ".lst");
+  //strcat(mapfilename, ".map");
+}
 
 /* pass #1 (p1=1) find all symbol defs and branch target names */
 /* pass #2 (p2=1) align branch targets, evaluate all symbols */
@@ -382,19 +517,8 @@ void init_areas(void)
 
 int main(int argc, char **argv)
 {
-       char infilename[200], outfilename[200], listfilename[200];
-
-       if (argc < 2) print_usage();
-       strcpy(infilename, argv[1]);
-       if(strlen(infilename) > 3) {
-               if (strncasecmp(infilename+strlen(infilename)-3, ".xa", 3))
-                       strcat(infilename, ".xa");
-       } else strcat(infilename, ".xa");
-       strcpy(outfilename, infilename);
-       outfilename[strlen(outfilename)-3] = '\0';
-       strcpy(listfilename, outfilename);
-       strcat(outfilename, ".hex");
-       strcat(listfilename, ".lst");
+       process_args (argc, argv);
+
        yyin = fopen(infilename, "r");
        if (yyin == NULL) {
                fprintf(stderr, "Can't open file '%s'.\n", infilename);
@@ -410,56 +534,67 @@ int main(int argc, char **argv)
                fprintf(stderr, "Can't write file '%s'.\n", listfilename);
                exit(1);
        }
+       if (createSymbolFile) {
+         sym_fp = fopen(symfilename, "w");
+         if (sym_fp == NULL) {
+           fprintf(stderr, "Can't write file '%s'.\n", symfilename);
+           exit(1);
+         }
+       }
 
-       /* todo: add a command line option to supress verbose messages */
-       printf("\nPaul's XA51 Assembler\n");
-       printf("Copyright 1997,2002 Paul Stoffregen\n\n");
-       printf("This program is free software; you can redistribute it\n");
-       printf("and/or modify it under the terms of the GNU General Public\n");
-       printf("License, Version 2, published by the Free Software Foundation\n\n");
-       printf("This program is distributed in the hope that it will be useful,\n");
-       printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
-       printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
-
-
-
-       printf("    Building Symbol Table:\n");
+       if (verbose) printf("Pass 1: Building Symbol Table:\n");
        p1 = 1;
-       //mem = 0;
        init_areas();
        yyparse();
        flag_targets();
-       print_symbol_table();
        check_redefine();
+
+       if (verbose) printf("Pass 2: Aligning Branch Targets:\n");
        p1 = 0;
        p2 = 1;
        rewind(yyin);
        yyrestart(yyin);
        lineno = 1;
-       printf("    Aligning Branch Targets:\n");
-       //mem = 0;
        init_areas();
        yyparse();
-       // print_symbol_table();
+
+       addAreaSymbols();
+       if (createSymbolFile) print_symbol_table();
+
+       if (verbose) printf("Pass 3: Generating Object Code:\n");
        p2 = 0;
        p3 = 1;
        rewind(yyin);
        yyrestart(yyin);
        lineno = 1;
-       printf("    Generating Object Code:\n");
-       //mem = 0;
        init_areas();
        yyparse();
+
        fclose(yyin);
        hexout(0, 0, 1);  /* flush and close intel hex file output */
        return 0;
 }
 
 
-void print_usage()
+void print_usage(int fatal)
 {
-       fprintf(stderr, "Usage: xa_asm file\n");
-       fprintf(stderr, "   or  xa_asm file.asm\n");
-       exit(1);
+  FILE *out = fatal ? stderr : stdout;
+
+  fprintf (out, "Usage: xa_asm [-s] [-v] file.xa\n");
+  fprintf (out, "  -v            verbose: show progress\n");
+  fprintf (out, "  -s            create symbol file\n");
+  fprintf (out, "  --version     show version/copyright info and exit\n");
+  fprintf (out, "  --help        show this and exit\n");
+#if 0
+  // some usefull options I can think of.
+  fprintf (out, "  -m            create map file\n");
+  fprintf (out, "  -ss           create symbol file sorted by symbol\n");
+  fprintf (out, "  -sa           create symbol file sorted by segment/address\n");
+  fprintf (out, "  --no-temps    supress temp symbols in map and sym file\n");
+  fprintf (out, "  --code-loc=#  sets the start address of the code\n");
+  fprintf (out, "  --xdata-loc=# sets the start address of the external data\n");
+  fprintf (out, "  --stack-loc=# sets the start address of the stack\n");
+#endif
+  exit(fatal);
 }