Exemplo n.º 1
0
int main( int argc, char *argv[] )
{
  extern char *optarg;
  extern int optind;
  int c;
  int usage = 0;
  int memory_dump = 0;
  int dos_newlines = 0;
  char *pc;
  int  cmd_processor = 0;
  char *processor_name = NULL;

  state.i_memory = i_memory_create();
  
  state.pass = 0;
  state.quiet = 0;

  #ifdef PARSE_DEBUG
  {
    extern int yydebug;
    yydebug = 1; /* enable parse debug */
  }
  #endif

  #ifdef USE_GPASM_HEADER_PATH
    /* add the header path to the include paths list */
    #ifndef __MSDOS__
      include_paths[n_include_paths++] = GPASM_HEADER_PATH;
    #else
      include_paths[n_include_paths++] = "c:\\gputils\\header";    
    #endif
  #endif

  while ((c = GETOPT_FUNC) != EOF) {
    switch (c) {
    case '?':
    case 'h':
      usage = 1;
      break;
    case 'a':
      select_hexformat(optarg);
      state.cmd_line.hex_format = 1;
      break;
    case 'c':
      state.case_insensitive = 1;
      if (state.stDefines != NULL) {
        printf("Warning: The -c option must be called before the -d option.\n");
      }
      break;
    case 'D':
    case 'd':
      if ((optarg != NULL) && (strlen(optarg) > 0)) {
	struct symbol *sym;
	char *lhs, *rhs;

        /* the Defines symbol table is not yet defined*/
        if (state.stDefines == NULL) {
          state.stDefines = push_symbol_table(NULL, state.case_insensitive);
        }

	lhs = strdup(optarg);
	rhs = strchr(lhs, '=');
	if (rhs != NULL) {
	  *rhs = '\0';	/* Terminate the left-hand side */
	  rhs++;	/* right-hand side begins after the '=' */
	}

	sym = get_symbol(state.stDefines, lhs);
	if (sym == NULL)
	  sym = add_symbol(state.stDefines, lhs);
	if (rhs)
	  annotate_symbol(sym, rhs);
      }
      break;
    case 'e':
      select_expand(optarg);
      state.cmd_line.macro_expand = 1;
      break;
    case 'I':
       if(n_include_paths < MAX_INCLUDE_PATHS) {
 	 include_paths[n_include_paths++] = optarg;
       } else {
 	 fprintf(stderr, "too many -I paths\n");
 	 exit(1);
       }
       break;    
 
    case 'l':
      dump_processor_list();
      exit(0);
      break;
    case 'm':
      memory_dump = 1;
      break;
    case 'n':
      #ifndef __MSDOS__
        dos_newlines = 1;
      #endif
      break;
    case 'o':
	    strcpy(state.hexfilename, optarg);
	    strcpy(state.basefilename, optarg);
	    pc = strrchr(state.basefilename, '.');
	    if (pc)
		   *pc = 0;
      break;
    case 'p':
      cmd_processor = 1;
      processor_name = optarg;
      break;
    case 'q':
      state.quiet = 1;
      break;
    case 'r':
      select_radix(optarg);
      state.cmd_line.radix = 1;
      break;
    case 'w':
      select_errorlevel(atoi(optarg));
      state.cmd_line.error_level = 1;
      break;
    case 'v':
      fprintf(stderr, "%s\n", GPASM_VERSION_STRING);
      exit(0);
      
    }
    if (usage)
      break;
  }
  
  if (optind < argc)
    state.srcfilename = argv[optind];
  else
    usage = 1;

  if (usage) {
    show_usage();
  }

  if(state.basefilename[0] == '\0') {
	  strcpy(state.basefilename, state.srcfilename);
	  pc = strrchr(state.basefilename, '.');
	  if (pc)
		  *pc = 0;
  }

  /* the Defines symbol table is not yet defined*/
  if (state.stDefines == NULL) {
    state.stDefines = push_symbol_table(NULL, state.case_insensitive);
  }

  /* Builtins are always case insensitive */
  state.stBuiltin = push_symbol_table(NULL, 1);
  state.stDirective = state.stBuiltin;
  state.stMacros = push_symbol_table(NULL, state.case_insensitive);
  state.stTop = 
    state.stGlobal = push_symbol_table(NULL, state.case_insensitive);

  opcode_init(0);

  /* the tables are built, select the processor if -p was used */
  if (cmd_processor) {
    select_processor(processor_name);
    state.cmd_line.processor = 1;
  }

  state.maxram = (MAX_RAM - 1);

  open_src(state.srcfilename, 0);
  state.pass = 1;
  yyparse();
 
  open_src(state.srcfilename, 0);
  state.pass++;
  state.org = 0;
  state.cblock = 0;
  if (state.cmd_line.radix != 1)
    state.radix = 16;
  cod_init();
  lst_init();
  yyparse();

  assert(state.pass == 2);
  
  pop_symbol_table(state.stBuiltin);
  
  if (check_writehex(state.i_memory, state.hex_format)) {
    gperror(GPE_IHEX,NULL); 
  } else {
    int byte_words;
    
    if (state.device.core_size > 0xff) {
      byte_words = 0;
    } else {
      byte_words = 1;
      if (state.hex_format != inhx8m) {
        gpwarning(GPW_UNKNOWN,"Must use inhx8m format for EEPROM8");
        state.hex_format = inhx8m;
      }
    }
    
    if (writehex(state.basefilename, state.i_memory, 
                 state.hex_format, state.num.errors,
                 byte_words, dos_newlines)) {
      gperror(GPE_UNKNOWN,"Error generating hex file");
    }
  }

  if(memory_dump)
    print_i_memory(state.i_memory);

  /* Maybe produce a symbol table */
  if (state.lst.symboltable) {
    lst_throw(); /* Start symbol table on a fresh page */
    lst_symbol_table(state.stGlobal);
    lst_defines_table(state.stDefines);
  }

  /* Maybe produce a memory map */
  if (state.lst.memorymap) {
    lst_memory_map(state.i_memory);
  }
  
  /* Finish off the listing and symbol files*/
  lst_close();
  if (state.processor_info)
    cod_close_file();
  free_files();

  if (state.num.errors > 0)
    return 1;
  else
    return 0;
}
Exemplo n.º 2
0
int nascom_exec(char *target)
{
    char    filename[FILENAME_MAX+1];   
    FILE   *fpin, *fpout;
    int        c;
    int        i;
    int        len;

    if ( help )
        return -1;

    if ( binname == NULL ) {
        return -1;
    }

    if ( outfile == NULL ) {
        strcpy(filename,binname);
        suffix_change(filename,".nas");
    } else {
        strcpy(filename,outfile);
    }

    if ( origin == -1 ) {
        origin = 0xc80;
    }


    if ( (fpin=fopen(binname,"rb") ) == NULL ) {
        fprintf(stderr,"Can't open input file %s\n",binname);
        myexit(NULL,1);
    }


/*
 *        Now we try to determine the size of the file
 *        to be converted
 */
    if (fseek(fpin,0,SEEK_END)) {
        fprintf(stderr,"Couldn't determine size of file\n");
        fclose(fpin);
        myexit(NULL,1);
    }

    len=ftell(fpin);

    fseek(fpin,0L,SEEK_SET);




    if ( (fpout=fopen(filename,"wb") ) == NULL ) {
        myexit("Can't open output file\n",1);
        exit(1);
    }

    writehexword(origin,fpout);

    for (i=0; i<len;i++) {
        if ((i>0) && ((i%8)==0)) {
            fprintf(fpout, "%c%c\n",8,8);
            writehexword(origin,fpout);
        }
        c=getc(fpin);
        fputc(' ',fpout);
        writehex(c,fpout);
        origin++;
    }

    /* Padding the last 8 bytes block*/
    if ( (i % 8) != 0 ) {
        while ( (i % 8) != 0 ) {
            fprintf(fpout, " 00");
            i++;
        }
        fprintf(fpout, "%c%c\n",8,8);
    }
    fprintf(fpout, ".\n");

    fclose(fpin);
    fclose(fpout);
    return 0;
}
Exemplo n.º 3
0
static int
processor(uint8_t *f, int fd, size_t l)
{
	uint8_t	frag[33];
	size_t	start, off;
	ssize_t	scan;
	int	rc = EXIT_SUCCESS;
	int	stop = 0;
	char	ch = 0;

	if (stdin_nobuf()) {
		return EXIT_FAILURE;
	}

	while (1) {
		if (0xa != ch) {
			printf("\n> ");
		}

		ch = fgetc(stdin);
		printf("%c", ch);

		switch (ch) {
		case '+':
			if (readnum(&start)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			printf("%lx\n", off+start);
			break;
		case '-':
			if (readnum(&start)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			printf("%lx\n", start-off);
			break;
		case 'e':
			if (readnum(&off)) {
				printf("\nINVALID OFFSET\n");
				break;
			}

			if (-1 == expand(&f, fd, l, off)) {
				stop = 1;
				rc = EXIT_FAILURE;
				break;
			}

			l += off;
			printf("\nEXTENDED %lu BYTES\n", (long unsigned)off);
			break;
		case 'f':
			if (readnum(&start) || (start >= l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			if (-1 == read_fragment(frag, off)) {
				printf("\nINVALID FRAGMENT\n");
				break;
			}

			printf("\nFIND FRAG FROM %lx SIZE %lu\n", start, off);
			find_frag(f+start, l-start, start, frag, off);
			break;
		case 'i':
			if (readnum(&start) || (start >= l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (-1 == expand(&f, fd, l, 1)) {
				stop = 1;
				rc = EXIT_FAILURE;
				break;
			}
			l++;

			if (-1 == shift_by(f+start, l-start)) {
				stop = 1;
				rc = EXIT_FAILURE;
				break;
			}
			break;
		case 'l':
			printf(" %lx\n", l);
			break;
		case 0x4:
		case 'q':
			printf("\nQUIT\n");
			stop++;
			break;

		case 'r':
			if (readnum(&start) || (start >=l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off) || ((start+off) > l)) {
				printf("\nINVALID OFFSET\n");
				rc = EXIT_FAILURE;
				break;
			}

			if (0 == off) {
				off = (l - start);
			}

			printhex(f+start, off, start);
			break;
		case 's':
			if (readnum(&start) || (start >= l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			if (-1 == read_fragment(frag, off)) {
				printf("\nINVALID FRAGMENT\n");
				break;
			}

			printf("\nSCAN FRAG FROM %lx SIZE %lu\n", start, off);
			while (-1 != (scan = find_frag(f+start, l-start,
					    start, frag, off))) {
				start += scan + 1;
			}
			break;
		case 'w':
			if (readnum(&start) || (start >=l)) {
				printf("\nINVALID START\n");
				break;
			}

			writehex(f+start, l-start);
			break;
		}


		if (stop) {
			break;
		}
	}

	return rc;
}
Exemplo n.º 4
0
void writehexword(unsigned int i,FILE *fp)
{
    writehex(i/256,fp);
    writehex(i%256,fp);
}