Root/Software/xc3sprog/bitfile.cpp

1/* Xilinx .bit file parser
2
3Copyright (C) 2004 Andrew Rogers
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
19
20
21#include "bitfile.h"
22
23using namespace std;
24
25BitFile::BitFile()
26{
27  Error=false;
28  logfile=stderr;
29  initFlip();
30  buffer=0;
31  length=0;
32}
33
34unsigned long BitFile::load(const char *fname)
35{
36  FILE *fp=fopen(fname,"rb");
37  if(fp==0){
38    string err="Cannot open file '";
39    err+=fname;
40    err+="'";
41    error(err);
42    return 0;
43  }
44  filename=fname;
45  
46  // Parse the header
47  char hdr[13];
48  fread(hdr,1,13,fp); // 13 byte header
49  char key;
50  do{
51    fread(&key,1,1,fp);
52    if(key=='a')readField(ncdFilename,fp);
53    if(key=='b')readField(partName,fp);
54    if(key=='c')readField(date,fp);
55    if(key=='d')readField(time,fp);
56  }while(key!='e'&&!feof(fp));
57  if(key=='e')processData(fp); // This is the data
58  else{
59    error("Unexpected end of file");
60    fclose(fp);
61    return 0;
62  }
63  fclose(fp);
64  return getLength();
65}
66
67void BitFile::processData(FILE *fp)
68{
69  byte t[4];
70  fread(t,1,4,fp);
71  length=(t[0]<<24)+(t[1]<<16)+(t[2]<<8)+t[3];
72  if(buffer) delete [] buffer;
73  buffer=new byte[length];
74  for(int i=0; i<length&&!feof(fp); i++){
75    byte b;
76    fread(&b,1,1,fp);
77    buffer[i]=bitRevTable[b]; // Reverse the bit order.
78  }
79  if(feof(fp)){
80    error("Unexpected end of file");
81    length=0;
82  }
83  fread(t,1,1,fp);
84  if(!feof(fp))error("Ignoring extra data at end of file");
85}
86
87unsigned long BitFile::saveAsBin(const char *fname)
88{
89  if(length<=0)return length;
90  FILE *fptr=fopen(fname,"wb");
91  if(fptr==0){
92    string err="Cannot open file'";
93    err+=fname;
94    err+="'";
95    error(err);
96    return 0;
97  }
98  for(int i=0; i<length; i++){
99    byte b=bitRevTable[buffer[i]]; // Reverse bit order
100    fwrite(&b,1,1,fptr);
101  }
102  fclose(fptr);
103  return length;
104}
105
106void BitFile::error(const string &str)
107{
108  errorStr=str;
109  Error=true;
110  fprintf(logfile,"%s\n",str.c_str());
111}
112
113void BitFile::readField(string &field, FILE *fp)
114{
115  byte t[2];
116  fread(t,1,2,fp);
117  unsigned short len=(t[0]<<8)+t[1];
118  for(int i=0; i<len; i++){
119    byte b;
120    fread(&b,1,1,fp);
121    field+=(char)b;
122  }
123}
124
125void BitFile::initFlip()
126{
127  for(int i=0; i<256; i++){
128    int num=i;
129    int fnum=0;
130    for(int k=0; k<8; k++){
131      int bit=num&1;
132      num=num>>1;
133      fnum=(fnum<<1)+bit;
134    }
135    bitRevTable[i]=fnum;
136  }
137}
138
139
140BitFile::~BitFile()
141{
142  if(buffer) delete [] buffer;
143}
144

Archive Download this file

Branches:
master



interactive