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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [tools/] [bit2exo/] [bit2exo.c] - Blame information for rev 86

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

Line No. Rev Author Line
1 14 hellwig
/*
2
 * bit2exo.c -- convert Xilinx bitfile 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
unsigned char bitHeader[13] = {
13
  0x00, 0x09, 0x0F, 0xF0,
14
  0x0F, 0xF0, 0x0F, 0xF0,
15
  0x0F, 0xF0, 0x00, 0x00,
16
  0x01
17
};
18
 
19
 
20
void error(char *fmt, ...) {
21
  va_list ap;
22
 
23
  va_start(ap, fmt);
24
  printf("Error: ");
25
  vprintf(fmt, ap);
26
  printf("\n");
27
  va_end(ap);
28
  exit(1);
29
}
30
 
31
 
32
unsigned int getCount2(FILE *infile) {
33
  unsigned char b1, b2;
34
 
35
  b1 = fgetc(infile);
36
  b2 = fgetc(infile);
37
  return ((unsigned int) b1 << 8) | (unsigned int) b2;
38
}
39
 
40
 
41
unsigned int getCount4(FILE *infile) {
42
  unsigned char b1, b2, b3, b4;
43
 
44
  b1 = fgetc(infile);
45
  b2 = fgetc(infile);
46
  b3 = fgetc(infile);
47
  b4 = fgetc(infile);
48
  return ((unsigned int) b1 << 24) | ((unsigned int) b2 << 16) |
49
         ((unsigned int) b3 <<  8) | ((unsigned int) b4 <<  0);
50
}
51
 
52
 
53
void show(char *name, FILE *infile, int count) {
54
  int c;
55
 
56
  printf("%s", name);
57
  while (count--) {
58
    c = fgetc(infile);
59
    if (c >= 0x20 && c <= 0x7E) {
60
      printf("%c", c);
61
    }
62
  }
63
  printf("\n");
64
}
65
 
66
 
67
unsigned char mirror(unsigned char n) {
68
  unsigned char m;
69
  int i;
70
 
71
  m = 0;
72
  for (i = 0; i < 8; i++) {
73
    m <<= 1;
74
    if (n & 1) {
75
      m |= 1;
76
    }
77
    n >>= 1;
78
  }
79
  return m;
80
}
81
 
82
 
83
int main(int argc, char *argv[]) {
84
  char *endptr;
85
  unsigned int loadAddr;
86
  FILE *infile;
87
  FILE *outfile;
88
  int numBytes, i;
89
  int c;
90
  unsigned char lineData[16];
91
  unsigned int chksum;
92
  int totalBytes;
93
 
94
  if (argc != 4) {
95
    printf("Usage: %s <load addr, hex> <input file> <output file>\n",
96
           argv[0]);
97
    printf("    ROM quadrant 0: load addr = 0x000000\n");
98
    printf("    ROM quadrant 1: load addr = 0x080000\n");
99
    printf("    ROM quadrant 2: load addr = 0x100000\n");
100
    printf("    ROM quadrant 3: load addr = 0x180000\n");
101
    exit(1);
102
  }
103
  loadAddr = strtoul(argv[1], &endptr, 16);
104
  if (*endptr != '\0') {
105
    error("illegal load address %s", argv[1]);
106
  }
107
  infile = fopen(argv[2], "rb");
108
  if (infile == NULL) {
109
    error("cannot open input file %s", argv[2]);
110
  }
111
  outfile = fopen(argv[3], "wt");
112
  if (outfile == NULL) {
113
    error("cannot open output file %s", argv[3]);
114
  }
115
  /* 13 bytes header */
116
  for (i = 0; i < 13; i++) {
117
    if (fgetc(infile) != bitHeader[i]) {
118
      error("input file header is not a '.bit' header");
119
    }
120
  }
121
  /* section 'a' */
122
  if (fgetc(infile) != 'a') {
123
    error("section 'a' not found");
124
  }
125
  i = getCount2(infile);
126
  show("design name:\t\t", infile, i);
127
  /* section 'b' */
128
  if (fgetc(infile) != 'b') {
129
    error("section 'b' not found");
130
  }
131
  i = getCount2(infile);
132
  show("part name:\t\t", infile, i);
133
  /* section 'c' */
134
  if (fgetc(infile) != 'c') {
135
    error("section 'c' not found");
136
  }
137
  i = getCount2(infile);
138
  show("creation date:\t\t", infile, i);
139
  /* section 'd' */
140
  if (fgetc(infile) != 'd') {
141
    error("section 'd' not found");
142
  }
143
  i = getCount2(infile);
144
  show("creation time:\t\t", infile, i);
145
  /* section 'e' */
146
  if (fgetc(infile) != 'e') {
147
    error("section 'e' not found");
148
  }
149
  i = getCount4(infile);
150
  printf("bit stream size:\t0x%08X\n", i);
151
  totalBytes = 0;
152
  while (1) {
153
    chksum = 0;
154
    for (numBytes = 0; numBytes < 16; numBytes++) {
155
      c = fgetc(infile);
156
      if (c == EOF) {
157
        break;
158
      }
159
      c = mirror(c & 0xFF);
160
      lineData[numBytes] = c;
161
      chksum += c;
162
    }
163
    if (numBytes == 0) {
164
      break;
165
    }
166
    totalBytes += numBytes;
167
    fprintf(outfile, "S2%02X%06X", numBytes + 4, loadAddr);
168
    for (i = 0; i < numBytes; i++) {
169
      fprintf(outfile, "%02X", lineData[i]);
170
    }
171
    chksum += numBytes + 4;
172
    chksum += ((loadAddr >>  0) & 0xFF) +
173
              ((loadAddr >>  8) & 0xFF) +
174
              ((loadAddr >> 16) & 0xFF);
175
    fprintf(outfile, "%02X\n", 0xFF - (chksum & 0xFF));
176
    loadAddr += numBytes;
177
    if (c == EOF) {
178
      break;
179
    }
180
  }
181
  fprintf(outfile, "S804000000FB\n");
182
  fclose(infile);
183
  fclose(outfile);
184
  printf("bytes converted:\t0x%08X\n", totalBytes);
185
  return 0;
186
}

powered by: WebSVN 2.1.0

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