Root/Software/xc3sprog/bitfile.h

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#ifndef BITFILE_H
22#define BITFILE_H
23
24#include <stdio.h>
25#include <string>
26
27// ----------------------Xilinx .bit file format---------------------------
28
29// 00000000: 00 09 0f f0 0f f0 0f f0 0f f0 00 00 01 61 00 0a *.............a..*
30// 00000010: 78 66 6f 72 6d 2e 6e 63 64 00 62 00 0c 76 31 30 *xform.ncd.b..v10*
31// 00000020: 30 30 65 66 67 38 36 30 00 63 00 0b 32 30 30 31 *00efg860.c..2001*
32// 00000030: 2f 30 38 2f 31 30 00 64 00 09 30 36 3a 35 35 3a */08/10.d..06:55:*
33// 00000040: 30 34 00 65 00 0c 28 18 ff ff ff ff aa 99 55 66 *04.e..(.......Uf*
34/*
35Field 1
362 bytes length 0x0009 (big endian)
379 bytes some sort of header
382 bytes length 0x0001
39
40Field 2
411 byte key 0x61 (The letter "a")
422 bytes length 0x000a (value depends on file name length)
4310 bytes string design name "xform.ncd" (including a trailing 0x00)
44
45Field 3
461 byte key 0x62 (The letter "b")
472 bytes length 0x000c (value depends on part name length)
4812 bytes string part name "v1000efg860" (including a trailing 0x00)
49
50Field 4
511 byte key 0x63 (The letter "c")
522 bytes length 0x000b
5311 bytes string date "2001/08/10" (including a trailing 0x00)
54
55Field 5
561 byte key 0x64 (The letter "d")
572 bytes length 0x0009
589 bytes string time "06:55:04" (including a trailing 0x00)
59
60Field 6
611 byte key 0x65 (The letter "e")
624 bytes length 0x000c9090 (value depends on device type,
63                                           and maybe design details)
648233440 bytes raw bit stream starting with 0xffffffff aa995566 sync
65word. */
66
67// Modified to reflect parsing - Andrew Rogers
68// Reference: http://www.fpga-faq.com/FAQ_Pages/0026_Tell_me_about_bit_files.htm
69
70//--------------------------------------------------------------------------------
71
72typedef unsigned char byte;
73
74class BitFile
75{
76 private:
77  std::string ncdFilename; // key 'a'
78  std::string partName; // key 'b'
79  std::string date; // key 'c'
80  std::string time; // key 'd'
81  unsigned long length; // The length of the byte data that follows, multiply by 8 to get bitstream length.
82  byte *buffer; // Each byte is reversed, Xilinx does things MSB first and JTAG does things LSB first!
83  std::string filename;
84  byte bitRevTable[256]; // Bit reverse lookup table
85  bool Error;
86  std::string errorStr;
87  FILE *logfile;
88
89 private:
90  void initFlip();
91  void error(const std::string &str);
92  void readField(std::string &field, FILE *fp);
93  void processData(FILE *fp);
94 
95 public:
96  BitFile();
97  ~BitFile();
98  unsigned long load(const char *fname);
99  inline byte *getData(){return buffer;}
100  inline unsigned long getLength(){return length*8;} // Returns length of bitstream
101  inline const char *getError(){
102    if(!Error)return("");
103    Error=false;
104    return errorStr.c_str();
105  }
106  inline const char *getNCDFilename(){return ncdFilename.c_str();}
107  inline const char *getPartName(){return partName.c_str();}
108  inline const char *getDate(){return date.c_str();}
109  inline const char *getTime(){return time.c_str();}
110  unsigned long saveAsBin(const char *fname);
111};
112
113#endif //BITFILE_H
114
115

Archive Download this file

Branches:
master



interactive