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

Subversion Repositories eco32

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

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 39 hellwig
#define S1      1
13
#define S2      2
14
#define S3      3
15
 
16
 
17 14 hellwig
void error(char *fmt, ...) {
18
  va_list ap;
19
 
20
  va_start(ap, fmt);
21
  printf("Error: ");
22
  vprintf(fmt, ap);
23
  printf("\n");
24
  va_end(ap);
25
  exit(1);
26
}
27
 
28
 
29
int main(int argc, char *argv[]) {
30 39 hellwig
  int type;
31
  unsigned int loadAddrMsk;
32
  char *loadAddrStr;
33
  char *infileStr;
34
  char *outfileStr;
35 14 hellwig
  char *endptr;
36
  unsigned int loadAddr;
37 39 hellwig
  unsigned int startAddr;
38 14 hellwig
  FILE *infile;
39
  FILE *outfile;
40
  int numBytes, i;
41
  int c;
42
  unsigned char lineData[16];
43
  unsigned int chksum;
44
 
45 39 hellwig
  if (argc != 5) {
46
    printf("Usage: %s -S1|-S2|-S3 <load addr, hex> ", argv[0]);
47
    printf("<input file> <output file>\n");
48 14 hellwig
    exit(1);
49
  }
50 39 hellwig
  if (strcmp(argv[1], "-S1") == 0) {
51
    type = S1;
52
    loadAddrMsk = 0x0000FFFF;
53
  } else
54
  if (strcmp(argv[1], "-S2") == 0) {
55
    type = S2;
56
    loadAddrMsk = 0x00FFFFFF;
57
  } else
58
  if (strcmp(argv[1], "-S3") == 0) {
59
    type = S3;
60
    loadAddrMsk = 0xFFFFFFFF;
61
  } else {
62
    error("exactly one of -S1, -S2, or -S3 must be specified");
63
  }
64
  loadAddrStr = argv[2];
65
  infileStr = argv[3];
66
  outfileStr = argv[4];
67
  loadAddr = strtoul(loadAddrStr, &endptr, 16);
68 14 hellwig
  if (*endptr != '\0') {
69 39 hellwig
    error("illegal load address %s", loadAddrStr);
70 14 hellwig
  }
71 39 hellwig
  if (loadAddr & ~loadAddrMsk) {
72
    error("load address too big");
73
  }
74
  startAddr = loadAddr;
75
  infile = fopen(infileStr, "rb");
76 14 hellwig
  if (infile == NULL) {
77 39 hellwig
    error("cannot open input file %s", infileStr);
78 14 hellwig
  }
79 39 hellwig
  outfile = fopen(outfileStr, "wt");
80 14 hellwig
  if (outfile == NULL) {
81 39 hellwig
    error("cannot open output file %s", outfileStr);
82 14 hellwig
  }
83
  while (1) {
84
    chksum = 0;
85
    for (numBytes = 0; numBytes < 16; numBytes++) {
86
      c = fgetc(infile);
87
      if (c == EOF) {
88
        break;
89
      }
90
      lineData[numBytes] = c;
91
      chksum += c;
92
    }
93
    if (numBytes == 0) {
94
      break;
95
    }
96 39 hellwig
    switch (type) {
97
      case S1:
98
        fprintf(outfile, "S1%02X%04X", numBytes + 3, loadAddr);
99
        break;
100
      case S2:
101
        fprintf(outfile, "S2%02X%06X", numBytes + 4, loadAddr);
102
        break;
103
      case S3:
104
        fprintf(outfile, "S3%02X%08X", numBytes + 5, loadAddr);
105
        break;
106
    }
107 14 hellwig
    for (i = 0; i < numBytes; i++) {
108
      fprintf(outfile, "%02X", lineData[i]);
109
    }
110 39 hellwig
    switch (type) {
111
      case S1:
112
        chksum += numBytes + 3 +
113
                  ((loadAddr >>  0) & 0xFF) +
114
                  ((loadAddr >>  8) & 0xFF);
115
        break;
116
      case S2:
117
        chksum += numBytes + 4 +
118
                  ((loadAddr >>  0) & 0xFF) +
119
                  ((loadAddr >>  8) & 0xFF) +
120
                  ((loadAddr >> 16) & 0xFF);
121
        break;
122
      case S3:
123
        chksum += numBytes + 5 +
124
                  ((loadAddr >>  0) & 0xFF) +
125
                  ((loadAddr >>  8) & 0xFF) +
126
                  ((loadAddr >> 16) & 0xFF) +
127
                  ((loadAddr >> 24) & 0xFF);
128
        break;
129
    }
130 14 hellwig
    fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
131
    loadAddr += numBytes;
132
    if (c == EOF) {
133
      break;
134
    }
135
  }
136 39 hellwig
  switch (type) {
137
    case S1:
138
      fprintf(outfile, "S903%04X", startAddr);
139
      chksum = 3 +
140
               ((startAddr >>  0) & 0xFF) +
141
               ((startAddr >>  8) & 0xFF);
142
      break;
143
    case S2:
144
      fprintf(outfile, "S804%06X", startAddr);
145
      chksum = 4 +
146
               ((startAddr >>  0) & 0xFF) +
147
               ((startAddr >>  8) & 0xFF) +
148
               ((startAddr >> 16) & 0xFF);
149
      break;
150
    case S3:
151
      fprintf(outfile, "S705%08X", startAddr);
152
      chksum = 5 +
153
               ((startAddr >>  0) & 0xFF) +
154
               ((startAddr >>  8) & 0xFF) +
155
               ((startAddr >> 16) & 0xFF) +
156
               ((startAddr >> 24) & 0xFF);
157
      break;
158
  }
159
  fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
160 14 hellwig
  fclose(infile);
161
  fclose(outfile);
162
  return 0;
163
}

powered by: WebSVN 2.1.0

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