summaryrefslogtreecommitdiffstats
path: root/fpga/xilinx/programmer/bit2svf/bitinfo-0.3/bitfile.c
blob: af4579dc162081024d6f08253b4fd63911f86de9 (plain)
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
/* bitfile.c
 *
 * Library routines for dealing with bit files, version 0.2
 *
 * Copyright 2001, 2002 by David Sullins
 *
 * This file is part of Bitinfo.
 * 
 * Bitinfo 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.
 * 
 * Bitinfo 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.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Bitinfo; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * You may contact the author at djs@naspa.net.
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "bitfile.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};

/* initbh
 *
 * Initialize the bithead struct
 */
void initbh(struct bithead *bh)
{
	bh->filename = NULL;
	bh->part = NULL;
	bh->date = NULL;
	bh->time = NULL;
	bh->length = 0;
}

/* freebh
 *
 * Free up memory allocated for a bithead struct.
 */
void freebh(struct bithead *bh)
{
	free(bh->filename);
	free(bh->part);
	free(bh->date);
	free(bh->time);
	initbh(bh);
}

/* readhead
 * 
 * Read the entire bit file header.  The file pointer will be advanced to
 * point to the beginning of the bitstream, and the bitfile header struct
 * will be filled with the appropriate data.
 *
 * Return -1 if an error occurs, 0 otherwise.
 */
int readhead(struct bithead *bh, FILE *f)
{
	int t;
	
	/* get first 13 bytes */
	t = readhead13(f);
	if (t) return t;
	
	/* get filename */
	t = readsecthead(NULL, f);
	if (-1 == t) return -1;
	bh->filename = malloc(t);
	t = readsection(bh->filename, t, f);
	
	/* get part name */
	t = readsecthead(NULL, f);
	if (-1 == t) return -1;
	bh->part = malloc(t);
	t = readsection(bh->part, t, f);
	
	/* get file creation date */
	t = readsecthead(NULL, f);
	if (-1 == t) return -1;
	bh->date = malloc(t);
	t = readsection(bh->date, t, f);
	
	/* get file creation time */
	t = readsecthead(NULL, f);
	if (-1 == t) return -1;
	bh->time = malloc(t);
	t = readsection(bh->time, t, f);
	
	/* get bitstream length */
	t = readlength(f);
	if (-1 == t) return -1;
	bh->length = t;
	
	return 0;
}

/* 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" and the length of the following section is 
 * returned.  If buf is NULL, the section letter is discarded.
 *
 * Return -1 if an error occurs, length of section otherwise.
 */
int readsecthead(char *buf, FILE *f)
{
	int t;
	char tbuf = 0;
	char lenbuf[2];
	
	/* if buf is NULL, use tbuf instead */
	if (NULL == buf)
	{
		buf = &tbuf;
	}

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

	/* convert 2-byte length to an int */
	return (((int)lenbuf[0]) <<8) | lenbuf[1];
}


/* readsection
 *
 * Read a section of a bit file.  The section contents are placed
 * in the contents buffer "buf."
 *
 * Return -1 if an error occurs, 0 otherwise.
 */
int readsection(char *buf, int length, FILE *f)
{
	int t;
	
	/* get section data */
	t = fread(buf, 1, length, f);
	if ((t != length) || (buf[length-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 section, make sure it's "e" */
	t = fread(&s, 1, 1, f);
	if ((t != 1) || (s != 'e'))
	{
		return -1;
	}
	
	/* 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;
}