Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | /* Xilinx .bit file parser |
| 2 | |
| 3 | Copyright (C) 2004 Andrew Rogers |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, 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 | /* |
| 35 | Field 1 |
| 36 | 2 bytes length 0x0009 (big endian) |
| 37 | 9 bytes some sort of header |
| 38 | 2 bytes length 0x0001 |
| 39 | |
| 40 | Field 2 |
| 41 | 1 byte key 0x61 (The letter "a") |
| 42 | 2 bytes length 0x000a (value depends on file name length) |
| 43 | 10 bytes string design name "xform.ncd" (including a trailing 0x00) |
| 44 | |
| 45 | Field 3 |
| 46 | 1 byte key 0x62 (The letter "b") |
| 47 | 2 bytes length 0x000c (value depends on part name length) |
| 48 | 12 bytes string part name "v1000efg860" (including a trailing 0x00) |
| 49 | |
| 50 | Field 4 |
| 51 | 1 byte key 0x63 (The letter "c") |
| 52 | 2 bytes length 0x000b |
| 53 | 11 bytes string date "2001/08/10" (including a trailing 0x00) |
| 54 | |
| 55 | Field 5 |
| 56 | 1 byte key 0x64 (The letter "d") |
| 57 | 2 bytes length 0x0009 |
| 58 | 9 bytes string time "06:55:04" (including a trailing 0x00) |
| 59 | |
| 60 | Field 6 |
| 61 | 1 byte key 0x65 (The letter "e") |
| 62 | 4 bytes length 0x000c9090 (value depends on device type, |
| 63 | and maybe design details) |
| 64 | 8233440 bytes raw bit stream starting with 0xffffffff aa995566 sync |
| 65 | word. */ |
| 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 | |
| 72 | typedef unsigned char byte; |
| 73 | |
| 74 | class 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 |
Branches:
master
