File size: 5,952 Bytes
c7436b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* promgen.c

 *

 * This file converts a Xilinx bitfile into a promfile. 

 *

 * Copyright 2009 by Gideon Zweijtzer

 * 

 * Some routines from this file have been taken from bitinfo.c, created by:

 *

 * Copyright 2001, 2002 by David Sullins

 *

 * Promgen is free software; you can redistribute it and/or modify it under the

 * terms of the GNU General Public License as published by the Free Software

 * Foundation, version 2 of the License.

 * 

 * Promgen is distributed in the hope that it will be useful, but WITHOUT ANY

 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS

 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more

 * details.

 * 

 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#ifndef uchar
#define uchar unsigned char
#endif

/* first 13 bytes of a bit file */
static uchar head13[] = {0, 9, 15, 240, 15, 240, 15, 240, 15, 240, 0, 0, 1};

/* readhead13

 *

 * Read the first 13 bytes of the bit file.  Discards the 13 bytes but

 * verifies that they are correct.

 *

 * Return -1 if an error occurs, 0 otherwise.

 */
int readhead13 (FILE *f)
{
	int t;
	uchar buf[13];

	/* read header */
	t = fread(buf, 1, 13, f);
	if (t != 13)
	{
		return -1;
	}
	
	/* verify header is correct */
	t = memcmp(buf, head13, 13);
	if (t)
	{
		return -1;
	}
	
	return 0;
}

/* readsecthead

 *

 * Read the header of a bit file section.  The section letter is placed in

 * section buffer "buf".

 *

 * Return -1 if an error occurs, '0' otherwise.

 */
int readsecthead(char *buf, FILE *f)
{
	int t;

	/* get section letter */
	t = fread(buf, 1, 1, f);
	if (t != 1)
	{
		return -1;
	}
	return 0;
} 

/* readstring

 *

 * Read a string from the bit file header. The string is placed in "buf".

 *

 * Return -1 if an error occurs, '0' otherwise.

 */
int readstring(char *buf, int bufsize, FILE *f)
{
	char lenbuf[2];
    int len, t;
    
	/* read length */
	t = fread(lenbuf, 1, 2, f);
	if (t != 2) 
	{
		return -1;
	}

	/* convert 2-byte length to an int */
	len = (((int)lenbuf[0]) <<8) | lenbuf[1];
	
    if(len > bufsize) {
        return -1;
    }
    t = fread(buf, 1, len, f);

	if ((t != len) || (buf[len-1] != 0))
	{
		return -1;
	}
    return 0;
}

/* readlength

 *

 * Read in the bitstream length.  The section letter "e" is discarded

 * and the length is returned.

 *

 * Return -1 if an error occurs, length otherwise.

 */
int readlength(FILE *f)
{
	char s = 0;
	uchar buf[4];
	int length;
	int t;
	
	/* get length */
	t = fread(buf, 1, 4, f);
	if (t != 4)
	{
		return -1;
	}
	
	/* convert 4-byte length to an int */
	length = (((int)buf[0]) <<24) | (((int)buf[1]) <<16) 
	         | (((int)buf[2]) <<8) | buf[3];
	
	return length;
}

/* readhead

 * 

 * Read the entire bit file header.  The file pointer will be advanced to

 * point to the beginning of the bitstream, and the length of the bitstream

 * will be returned.

 *

 * Return -1 if an error occurs, length of bitstream otherwise.

 */
int readhead(FILE *f)
{
#define BUFSIZE 500

	int t, len;
	uchar typ;
    uchar buffer[BUFSIZE];
    	
	/* get first 13 bytes */
	t = readhead13(f);
	if (t) return t;
	
	/* get other fields */
    do {
    	if(readsecthead(&typ, f) == -1)
    	    return -1;

        switch(typ) {
            case 0x61:
            case 0x62:
            case 0x63:
            case 0x64:
    	        t = readstring(buffer, BUFSIZE, f);
            	if (t) return -1;
                break;
            case 0x65:
                len = readlength(f);
                break;
            default:
                printf("Unknown header field '%c'.\n", typ);
                return -1;
        }
        switch(typ) {
            case 0x61:
                printf("Design name:    %s\n", buffer);
                break;
            case 0x62:
                printf("Target device:  xc%s\n", buffer);
                break;
            case 0x63:
                printf("Bitfile date:   %s\n", buffer);
                break;
            case 0x64:
                printf("Bitfile time:   %s\n", buffer);
                break;
            case 0x65:
                printf("Bitfile length: %08x\n", len);
                return len;
        }
        
    }
    while(1);
    return -1;
}


int main(int argc, char **argv)
{
	unsigned char rotated[256];
	unsigned char buffer[1024];
    unsigned char r, t, b;
    
	int length, i, bytesread;

    FILE *fi, *fo;

    if(argc < 3) {
        printf("Usage: promgen [-r] <infile> <outfile>\n");
        exit(1);
    }

    int rotate = 1;
    int ar = 1;
    if(strcmp(argv[1], "-r")==0) {
        rotate = 0;
        ar++;
    }
    
    fi = fopen(argv[ar++], "rb");
    if(!fi) {
	    printf("Couldn't open file '%s' for reading.\n", argv[1]);
	    exit(2);
	}

    fo = fopen(argv[ar++], "wb");
    if(!fo) {
	    printf("Couldn't open file '%s' for writing.\n", argv[2]);
        fclose(fi);
	    exit(3);
	}

    // create rotate table
    for(i=0;i<256;i++) {
        r = 0;
        t = (unsigned char) i;
        for(b=0;b<8;b++) {
            r <<= 1;
            r |= (t & 1);
            t >>= 1;
        }
        rotated[i] = r;
    }

    // parse header of bitfile
    if((length = readhead(fi)) < 0) {
        printf("Parsing of header unsuccessful. Invalid bitfile?\n");
        exit(11);        
    }

    do {
        bytesread = fread(buffer, 1, 1024, fi);
        if(rotate) {
            for(i=0;i<bytesread;i++) {
                buffer[i] = rotated[buffer[i]];
            }
        }
        fwrite(buffer, 1, bytesread, fo);
        length -= bytesread;
    } while(length && bytesread);

    fclose(fo);
    fclose(fi);
    
    return 0;
}