OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [tools/] [bin2exo/] [bin2exo.c] - Blame information for rev 38

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 hellwig
/*
2
 * bin2exo.c -- convert binary data to Motorola S-records
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
 
11
 
12
void error(char *fmt, ...) {
13
  va_list ap;
14
 
15
  va_start(ap, fmt);
16
  printf("Error: ");
17
  vprintf(fmt, ap);
18
  printf("\n");
19
  va_end(ap);
20
  exit(1);
21
}
22
 
23
 
24
int main(int argc, char *argv[]) {
25
  char *endptr;
26
  unsigned int loadAddr;
27
  FILE *infile;
28
  FILE *outfile;
29
  int numBytes, i;
30
  int c;
31
  unsigned char lineData[16];
32
  unsigned int chksum;
33
 
34
  if (argc != 4) {
35
    printf("Usage: %s <load addr, hex> <input file> <output file>\n",
36
           argv[0]);
37
    exit(1);
38
  }
39
  loadAddr = strtoul(argv[1], &endptr, 16);
40
  if (*endptr != '\0') {
41
    error("illegal load address %s", argv[1]);
42
  }
43
  infile = fopen(argv[2], "rb");
44
  if (infile == NULL) {
45
    error("cannot open input file %s", argv[2]);
46
  }
47
  outfile = fopen(argv[3], "wt");
48
  if (outfile == NULL) {
49
    error("cannot open output file %s", argv[3]);
50
  }
51
  while (1) {
52
    chksum = 0;
53
    for (numBytes = 0; numBytes < 16; numBytes++) {
54
      c = fgetc(infile);
55
      if (c == EOF) {
56
        break;
57
      }
58
      lineData[numBytes] = c;
59
      chksum += c;
60
    }
61
    if (numBytes == 0) {
62
      break;
63
    }
64
    fprintf(outfile, "S2%02X%06X", numBytes + 4, loadAddr);
65
    for (i = 0; i < numBytes; i++) {
66
      fprintf(outfile, "%02X", lineData[i]);
67
    }
68
    chksum += numBytes + 4;
69
    chksum += ((loadAddr >>  0) & 0xFF) +
70
              ((loadAddr >>  8) & 0xFF) +
71
              ((loadAddr >> 16) & 0xFF);
72
    fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
73
    loadAddr += numBytes;
74
    if (c == EOF) {
75
      break;
76
    }
77
  }
78
  fprintf(outfile, "S804000000FB\n");
79
  fclose(infile);
80
  fclose(outfile);
81
  return 0;
82
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.