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

Subversion Repositories eco32

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

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 39 hellwig
  fprintf(outfile, "S00600004844521B\n");
84 14 hellwig
  while (1) {
85
    chksum = 0;
86
    for (numBytes = 0; numBytes < 16; numBytes++) {
87
      c = fgetc(infile);
88
      if (c == EOF) {
89
        break;
90
      }
91
      lineData[numBytes] = c;
92
      chksum += c;
93
    }
94
    if (numBytes == 0) {
95
      break;
96
    }
97 39 hellwig
    switch (type) {
98
      case S1:
99
        fprintf(outfile, "S1%02X%04X", numBytes + 3, loadAddr);
100
        break;
101
      case S2:
102
        fprintf(outfile, "S2%02X%06X", numBytes + 4, loadAddr);
103
        break;
104
      case S3:
105
        fprintf(outfile, "S3%02X%08X", numBytes + 5, loadAddr);
106
        break;
107
    }
108 14 hellwig
    for (i = 0; i < numBytes; i++) {
109
      fprintf(outfile, "%02X", lineData[i]);
110
    }
111 39 hellwig
    switch (type) {
112
      case S1:
113
        chksum += numBytes + 3 +
114
                  ((loadAddr >>  0) & 0xFF) +
115
                  ((loadAddr >>  8) & 0xFF);
116
        break;
117
      case S2:
118
        chksum += numBytes + 4 +
119
                  ((loadAddr >>  0) & 0xFF) +
120
                  ((loadAddr >>  8) & 0xFF) +
121
                  ((loadAddr >> 16) & 0xFF);
122
        break;
123
      case S3:
124
        chksum += numBytes + 5 +
125
                  ((loadAddr >>  0) & 0xFF) +
126
                  ((loadAddr >>  8) & 0xFF) +
127
                  ((loadAddr >> 16) & 0xFF) +
128
                  ((loadAddr >> 24) & 0xFF);
129
        break;
130
    }
131 14 hellwig
    fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
132
    loadAddr += numBytes;
133
    if (c == EOF) {
134
      break;
135
    }
136
  }
137 39 hellwig
  switch (type) {
138
    case S1:
139
      fprintf(outfile, "S903%04X", startAddr);
140
      chksum = 3 +
141
               ((startAddr >>  0) & 0xFF) +
142
               ((startAddr >>  8) & 0xFF);
143
      break;
144
    case S2:
145
      fprintf(outfile, "S804%06X", startAddr);
146
      chksum = 4 +
147
               ((startAddr >>  0) & 0xFF) +
148
               ((startAddr >>  8) & 0xFF) +
149
               ((startAddr >> 16) & 0xFF);
150
      break;
151
    case S3:
152
      fprintf(outfile, "S705%08X", startAddr);
153
      chksum = 5 +
154
               ((startAddr >>  0) & 0xFF) +
155
               ((startAddr >>  8) & 0xFF) +
156
               ((startAddr >> 16) & 0xFF) +
157
               ((startAddr >> 24) & 0xFF);
158
      break;
159
  }
160
  fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
161 14 hellwig
  fclose(infile);
162
  fclose(outfile);
163
  return 0;
164
}

powered by: WebSVN 2.1.0

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