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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [tools/] [bit2mcs/] [bit2mcs.c] - Blame information for rev 260

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

Line No. Rev Author Line
1 234 hellwig
/*
2
 * bit2mcs.c -- convert Xilinx bitfile data to Intel hex 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
    exit(1);
98
  }
99
  loadAddr = strtoul(argv[1], &endptr, 16);
100
  if (*endptr != '\0') {
101
    error("illegal load address %s", argv[1]);
102
  }
103
  infile = fopen(argv[2], "rb");
104
  if (infile == NULL) {
105
    error("cannot open input file %s", argv[2]);
106
  }
107
  outfile = fopen(argv[3], "wt");
108
  if (outfile == NULL) {
109
    error("cannot open output file %s", argv[3]);
110
  }
111
  /* 13 bytes header */
112
  for (i = 0; i < 13; i++) {
113
    if (fgetc(infile) != bitHeader[i]) {
114
      error("input file header is not a '.bit' header");
115
    }
116
  }
117
  /* section 'a' */
118
  if (fgetc(infile) != 'a') {
119
    error("section 'a' not found");
120
  }
121
  i = getCount2(infile);
122
  show("design name:\t\t", infile, i);
123
  /* section 'b' */
124
  if (fgetc(infile) != 'b') {
125
    error("section 'b' not found");
126
  }
127
  i = getCount2(infile);
128
  show("part name:\t\t", infile, i);
129
  /* section 'c' */
130
  if (fgetc(infile) != 'c') {
131
    error("section 'c' not found");
132
  }
133
  i = getCount2(infile);
134
  show("creation date:\t\t", infile, i);
135
  /* section 'd' */
136
  if (fgetc(infile) != 'd') {
137
    error("section 'd' not found");
138
  }
139
  i = getCount2(infile);
140
  show("creation time:\t\t", infile, i);
141
  /* section 'e' */
142
  if (fgetc(infile) != 'e') {
143
    error("section 'e' not found");
144
  }
145
  i = getCount4(infile);
146
  printf("bit stream size:\t0x%08X\n", i);
147
  totalBytes = 0;
148
  while (1) {
149
    if ((loadAddr & 0xFFFF) == 0) {
150
      fprintf(outfile, ":02000004");
151
      fprintf(outfile, "%04X", loadAddr >> 16);
152
      chksum = 0x02 + 0x04 +
153
               ((loadAddr >> 24) & 0xFF) +
154
               ((loadAddr >> 16) & 0xFF);
155
      fprintf(outfile, "%02X\n", (-chksum) & 0xFF);
156
    }
157
    chksum = 0;
158
    for (numBytes = 0; numBytes < 16; numBytes++) {
159
      c = fgetc(infile);
160
      if (c == EOF) {
161
        break;
162
      }
163
      c = mirror(c & 0xFF);
164
      lineData[numBytes] = c;
165
      chksum += c;
166
    }
167
    if (numBytes == 0) {
168
      break;
169
    }
170
    totalBytes += numBytes;
171
    fprintf(outfile, ":%02X%04X00", numBytes, loadAddr & 0xFFFF);
172
    for (i = 0; i < numBytes; i++) {
173
      fprintf(outfile, "%02X", lineData[i]);
174
    }
175
    chksum += numBytes;
176
    chksum += ((loadAddr >> 8) & 0xFF) +
177
              ((loadAddr >> 0) & 0xFF);
178
    fprintf(outfile, "%02X\n", (-chksum) & 0xFF);
179
    loadAddr += numBytes;
180
    if (c == EOF) {
181
      break;
182
    }
183
  }
184
  fprintf(outfile, ":00000001FF\n");
185
  fclose(infile);
186
  fclose(outfile);
187
  printf("bytes converted:\t0x%08X\n", totalBytes);
188
  return 0;
189
}

powered by: WebSVN 2.1.0

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