torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Virtex5LibGen.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \brief Class to encapsulate micro-bitstream library generation code
17 /// \author Ritesh Kumar Soni
18 
19 #include "Virtex5LibGen.hpp"
21 
22 namespace torc {
23 namespace bitstream {
24 
25 const int Virtex5LibGen::sCrcWordIndex = 20;
27  "INT",
28  "CLBLL",
29  "CLBLM",
30  "CLK_HROW",
31  "HCLK",
32  "CLK_BUFGMUX",
33  "CFG_CENTER",
34  "CFG_HCLK_INTERFACE",
35  "CLKV",
36  "CLKV_MC",
37  "CLK_CMT_BOT",
38  "CLK_CMT_BOT_MGT",
39  "CLK_CMT_TOP",
40  "CLK_CMT_TOP_MGT",
41  "CLK_HROW_MGT",
42  "CLK_IOB_B",
43  "CLK_IOB_T",
44  "CLK_MGT_BOT",
45  "CLK_MGT_BOT_MGT",
46  "CLK_MGT_TOP",
47  "CLK_MGT_TOP_MGT",
48  "CLK_TERM_BOT",
49  "CLK_TERM_TOP",
50  "HCLK_BRAM",
51  "HCLK_BRAM_MGT",
52  "HCLK_BRAM_MGT_LEFT",
53  "HCLK_CLB",
54  "HCLK_CMT_CMT",
55  "HCLK_CMT_CMT_MGT",
56  "HCLK_CMT_IOI",
57  "HCLK_DSP",
58  "HCLK_GT3",
59  "HCLK_GTX",
60  "HCLK_GTX_LEFT",
61  "HCLK_IOB",
62  "HCLK_IOB_CMT_BOT",
63  "HCLK_IOB_CMT_BOT_MGT",
64  "HCLK_IOB_CMT_MID",
65  "HCLK_IOB_CMT_MID_MGT",
66  "HCLK_IOB_CMT_TOP",
67  "HCLK_IOB_CMT_TOP_MGT",
68  "HCLK_IOI",
69  "HCLK_IOI_BOTCEN",
70  "HCLK_IOI_BOTCEN_MGT",
71  "HCLK_IOI_CENTER",
72  "HCLK_IOI_CMT",
73  "HCLK_IOI_CMT_MGT",
74  "HCLK_IOI_TOPCEN",
75  "HCLK_IOI_TOPCEN_MGT",
76  "HCLK_LIOB",
77  "HCLK_PCIE_BRAM",
78  "HCLK_PPC",
79  "HCLK_PPC_TERM",
80  "HCLK_TERM_L",
81  "HCLK_TERM_R",
82  "HCLK_VBRK",
83  "INT_HCLK_BUFS",
84  "INT_BUFS_L",
85  "INT_BUFS_R",
86  "IOI",
87  "LAST_TILE"
88  };
89 
90 /// \details Architecture specific part of initializing the object
92 
95 }
96 
97 /// \details Returns true for the sites which are supported by library generation process
98 bool Virtex5LibGen::isSiteTypeSupported(const string &siteType) {
99  return (siteType.compare("SLICEM") == 0 ||
100  siteType.compare("SLICELO") == 0 || siteType.compare("SLICELE") == 0 ||
101  siteType.compare("RAMB36_EXP") == 0 || siteType.compare("DSP48E") == 0 ||
102 // siteType.compare("BUFGCTRL") == 0 || siteType.compare("BUFDS") == 0 ||
103  siteType.compare("*BUFR") == 0 || siteType.compare("*BUFG") == 0);
104 }
105 
106 /// \brief Returns list of supported tiles.
107 std::vector<std::string> Virtex5LibGen::getTilesSupportedForPips() {
108  uint32_t supportedTileIndex = 0;
109  std::vector<std::string> tilesSupported;
110  while(sTilesSupportedForPips[supportedTileIndex].compare("LAST_TILE") != 0) {
111  tilesSupported.push_back(sTilesSupportedForPips[supportedTileIndex++]);
112  }
113  return tilesSupported;
114 }
115 
116 /// \brief Compress given bitstream file against reference bitstream file
117 void Virtex5LibGen::compressBitFile(path primaryFile, path referenceFile) {
118 
119  bool debug = false;
120 
121  // Read the bitstream files
122  torc::bitstream::BitstreamSharedPtr referenceBitstreamPtr = Factory::newBitstreamPtr(referenceFile);
123  torc::bitstream::BitstreamSharedPtr primBitstreamPtr = Factory::newBitstreamPtr(primaryFile);
124 
125  // Check if both the bitstreams were read successfully.
126  if(referenceBitstreamPtr && primBitstreamPtr) {
127  std::cout << "Read the two bit files - " << primaryFile.filename() << " and " << referenceFile.filename() << " successfully" << std::endl;
128  } else {
129  std::cout << "Could not read the bit files " << primaryFile << " " << referenceFile << std::endl;
130  return;
131  }
132 
133  // dynamically cast the bitstream pointer into a Virtex5 bitstream pointer
134  // ToDo: This part should move to architecture specific derived class
135  boost::shared_ptr<torc::bitstream::Virtex5> refVirtex5BitstreamPtr = boost::dynamic_pointer_cast<torc::bitstream::Virtex5>(
136  referenceBitstreamPtr);
137 
138  boost::shared_ptr<torc::bitstream::Virtex5> primVirtexBitstreamPtr = boost::dynamic_pointer_cast<torc::bitstream::Virtex5>(
139  primBitstreamPtr);
140 
141  // initialize frame map
142  refVirtex5BitstreamPtr->initializeDeviceInfo(referenceBitstreamPtr->getDesignName());
143  refVirtex5BitstreamPtr->initializeFrameMaps();
144  refVirtex5BitstreamPtr->readFramePackets();
145 
146  primVirtexBitstreamPtr->initializeDeviceInfo(primBitstreamPtr->getDesignName());
147  primVirtexBitstreamPtr->initializeFrameMaps();
148  primVirtexBitstreamPtr->readFramePackets();
149 
150  uint32_t wordOffset = getWordOffsetFromXdlFile(primaryFile, primVirtexBitstreamPtr);
151 
152  // look up the frame blocks
153  torc::bitstream::VirtexFrameBlocks& refFrameBlocks = refVirtex5BitstreamPtr->getFrameBlocks();
154  torc::bitstream::VirtexFrameBlocks& primFrameBlocks = primVirtexBitstreamPtr->getFrameBlocks();
155  std::vector<boost::int32_t> combinedAddressVector;
156 
157 
158  // iterate over the block types
159  for(int blockIndex = 0; blockIndex < Bitstream::eBlockTypeCount; blockIndex++) {
160  // look up the frame set for this block
161  torc::bitstream::VirtexFrameSet& nullFrameSet = refFrameBlocks.mBlock[blockIndex];
162  torc::bitstream::VirtexFrameSet& primFrameSet = primFrameBlocks.mBlock[blockIndex];
163 
164  if(nullFrameSet.size() != primFrameSet.size()) {
165  std::cout << "Frame set sizes do not match" << std::endl;
166  return;
167  }
168 
169  // We expect the majorIndex to remain same for all the mismatching part of the bitstream.
170  uint32_t relativeFrameIndex = 0, currentMajorIndex = 0, previousMajorIndex = 0;
171  bool firstMismatch = true;
172 
173  // iterate over the frames
174  for(uint32_t frameIndex = 0; frameIndex < nullFrameSet.size(); frameIndex++) {
175  // look up the frame for this set
176  VirtexFrameSharedPtr nullFramePtr = nullFrameSet[frameIndex];
177  VirtexFrameSharedPtr primFramePtr = primFrameSet[frameIndex];
178 
179  const VirtexFrame::word_t* wordsReference = nullFramePtr->getWords();
180  const VirtexFrame::word_t* wordsPrim = primFramePtr->getWords();
181 
182  const VirtexFrame::word_t* pReferenceWords = wordsReference;
183  const VirtexFrame::word_t* pPrimWords = wordsPrim;
184  const VirtexFrame::word_t* eReferenceWords = pReferenceWords
185  + nullFramePtr->getLength();
186  const VirtexFrame::word_t* ePrimWords = pPrimWords + primFramePtr->getLength();
187 
188  if(nullFramePtr->getLength() != primFramePtr->getLength()) {
189  std::cout << "WARNING: Frames of different sizes " << nullFramePtr->getLength() << " "
190  << primFramePtr->getLength() << std::endl;
191  }
192 
193  // iterate over the frame words
194  bool found = false;
195  int wordIndex = 0;
196 
197  // Iterate over the frame words
198  while(pReferenceWords < eReferenceWords) {
199  // If the word is not zero and not same as that of reference bitstream and not crc word
200  if(*pPrimWords != 0 && *pReferenceWords != *pPrimWords && wordIndex != sCrcWordIndex) {
201 
202  boost::int32_t bitIndex = 0;
203 
204  primVirtexBitstreamPtr->splitFrameIndex(frameIndex, currentMajorIndex,
205  relativeFrameIndex);
206 
207  // If this is not the first mismatch, compare the major index with previous mismatching word's major index.
208  if(!firstMismatch) {
209  if(currentMajorIndex != previousMajorIndex) {
210  std::cout << "WARNING: Bitstream mismatch across major indices: " << currentMajorIndex << " and " << previousMajorIndex << std::endl;
211  //exit (-1);
212  return;
213  }
214  } else {
215  firstMismatch = false;
216  previousMajorIndex = currentMajorIndex;
217  }
218 
219  // Get the 1 bits in primary design which are zero in reference design
220  VirtexFrame::word_t tempWord = (*pPrimWords ^ *pReferenceWords) & *pPrimWords;
221  // Find all the 1s in the word
222  while(tempWord != 0) {
223  // check if the LSB is 1
224  if(tempWord & 1) {
225  // store the combined address
226  combinedAddressVector.push_back((relativeFrameIndex << 16) | ((wordIndex - wordOffset)
227  << 8) | bitIndex);
228 
229  }
230  tempWord = tempWord >> 1;
231  bitIndex++;
232  }
233  found = true;
234  }
235  pReferenceWords++;
236  pPrimWords++;
237  wordIndex++;
238  }
239  // if we found any non-zero words, display them. This if for debugging purpose only.
240  if(found) {
241  std::cout << "Found unmatching frame: block " << blockIndex << ", relative index "
242  << Hex32(relativeFrameIndex) << ", global index: " << Hex32(frameIndex)
243  << ", major index: " << Hex32(currentMajorIndex) << std::endl;
244  pReferenceWords = wordsReference;
245  if(debug) {
246  std::cout << " Referen: ";
247  while(pReferenceWords < eReferenceWords) {
248  std::cout << Hex32(*pReferenceWords++) << " ";
249  }
250  std::cout << std::endl;
251  pPrimWords = wordsPrim;
252  std::cout << " Primary: ";
253  while(pPrimWords < ePrimWords) {
254  std::cout << Hex32(*pPrimWords++) << " ";
255  }
256  std::cout << std::endl;
257  }
258  }
259  }
260  }
261 
262  // Store all the combined addresses in the sparse bit file.
263  boost::filesystem::path sparceBitstreamPath = primaryFile.replace_extension().string()
264  + ".cbit";
265 
266  std::ofstream sparceBitstreamFile;
267  sparceBitstreamFile.open(sparceBitstreamPath.string().c_str(), std::ios::binary | std::ios::out);
268  if(sparceBitstreamFile.good()) {
269  std::cout << " Opened library file to write " << sparceBitstreamPath.string().c_str()
270  << std::endl;
271  } else {
272  std::cerr << " Could not open library file to write - "
273  << sparceBitstreamPath.string().c_str() << std::endl;
274  exit(-1);
275  }
276 
277  boost::int32_t vectorSize = combinedAddressVector.size();
278  sparceBitstreamFile.write((char *) &vectorSize, 4);
279  std::cout << "Different words -" << std::endl;
280  std::cout << " ";
281  for(std::vector<boost::int32_t>::const_iterator iter = combinedAddressVector.begin(); iter
282  != combinedAddressVector.end(); iter++) {
283  sparceBitstreamFile.write((char *) (&*iter), 4);
284  std::cout << Hex32((*iter)) << " ";
285  }
286  std::cout << std::endl;
287 
288  std::cout << "Wrote data to the library file." << std::endl;
289 
290  sparceBitstreamFile.close();
291 
292 }
293 
295  uint32_t bitIndexBegin = 0, bitIndexEnd = 0;
296  boost::shared_ptr<Virtex5> virtex5Ptr = boost::dynamic_pointer_cast<Virtex5>(inBitstreamPtr);
297  uint32_t primaryXdlCol = virtex5Ptr->getPrimaryXdlColumn(tileCol);
298  virtex5Ptr->getXdlFrames(tileRow, primaryXdlCol,
299  bitIndexBegin, bitIndexEnd);
300 
301  // Each frame covers the full height of clock region which contains many rows of tiles.
302  // The library contains configuration bits only for the first row. So the word index has to
303  // be offset to reach the correct row. The bit index remains same across rows.
304  uint32_t outWordOffset = 0;
305  if(bitIndexBegin != bitIndexEnd) {
306  outWordOffset = Virtex5::eFrameLength - (bitIndexEnd >> 5); // eFrameLength is number of words in a frame
307  }
308 
309 // std::cout << "*******Word offset " << outWordOffset << std::endl;
310 
311  return outWordOffset;
312 }
313 
314 /// \details Creates map from primitive name to primitive structure object pointer
316  PrimitiveStructuresSharedPtrMap primitiveStructures;
317  // look up the primitive def types
319  PrimitiveDefArray;
320  PrimitiveDefArray & primitiveDefs = mSites.getSiteTypes();
321  PrimitiveDefArray::const_iterator pPrimitiveDefs = primitiveDefs.begin();
322  PrimitiveDefArray::const_iterator ePrimitiveDefs = primitiveDefs.end();
323  // Store the corresponding primitive structures in a map.
324  while(pPrimitiveDefs < ePrimitiveDefs) {
325  // create a PrimitiveStructure object for this PrimitiveDef
326  torc::packer::PrimitiveStructureSharedPtr primitiveStructurePtr(
327  new torc::packer::Virtex5PrimitiveStructure(&*pPrimitiveDefs++));
328  const torc::architecture::PrimitiveDef *primitiveDefPtr =
329  primitiveStructurePtr->getPrimitiveDefPtr();
330  const std::string & primitiveDefName = primitiveDefPtr->getName();
331  // insert the PrimitiveStructure into the map
332  if(!isSlicelType(primitiveDefName)) {
333  primitiveStructures[primitiveDefName] = primitiveStructurePtr;
334  } else {
335  string sliceEven = primitiveDefName + "E";
336  string sliceOdd = primitiveDefName + "O";
337  primitiveStructures[sliceEven] = primitiveStructurePtr;
338  primitiveStructures[sliceOdd] = primitiveStructurePtr;
339  }
340  }
341  return primitiveStructures;
342 }
343 
345  // Get compressed bit files for LUTs in RAM/ROM mode.
346  path libraryMemFolder = mLibraryFolder / "memory";
347  string perlScript = libraryMemFolder.string() + "/extract-bits-for-luts.pl";
348  string llFile = mDB.getFamilyName() + "-CLBLM-SLICEM-X-ROM.ll";
349  path llFilePath = libraryMemFolder / llFile;
350  string systemCommand = "perl " + perlScript + " " + llFilePath.string();
351  int systemReturn = system(systemCommand.c_str());
352  string cpyCmd = "mv " + libraryMemFolder.string() + "/*.cbit " + mXdlGenerationFolder.string();
353  systemReturn = system(cpyCmd.c_str());
354  (void) systemReturn;
355 
356 }
357 
358 } //namespace bitstream
359 } //namespace torc
360 
Encapsulation of a tile row in an unsigned 16-bit integer.
path mXdlGenerationFolder
Folder in which Xdls will be generated.
Encapsulation of a tile column in an unsigned 16-bit integer.
std::map< string, string > getReferenceConfigMap()
Get a map from config setting to reference value.
const string & getFamilyName(void) const
Returns the family name.
Definition: DDB.hpp:134
const Sites & mSites
Sites from the database object.
Header for the Virtex5PrimitiveStructure class.
Encapsulation of primitive site definition, with associated connections, elements, and pins.
bool isSlicelType(const string &inSiteType)
Return true if site type is SLICEL.
static CompoundSettingMap getCompoundSettingsMap()
Returns map of coumpound settings.
virtual void compressBitFile(path primaryFile, path referenceFile)
Compress given bitstream file against reference bitstream file.
std::string string
uint32_t getWordOffsetFromXdlFile(path inBitstreamPath, BitstreamSharedPtr inBitstreamPtr)
Get word offset in frame for the site/pip in corresponding Xdl.
Virtex5 bitstream.
Definition: Virtex5.hpp:41
const Array< const PrimitiveDef > & getSiteTypes(void) const
Returns the site types for this family.
Definition: Sites.hpp:125
virtual PrimitiveStructuresSharedPtrMap getPrimitiveNameToStructreMap()
Populate primitive name to primitive structure map.
std::map< string, string > mReferenceConfigMap
Map from config setting to reference config value.
CompoundSettingMap mCompoundSettingMap
Map from one compound setting to vector of other related settings.
FrameSet< FRAME_TYPE > mBlock[Bitstream::eBlockTypeCount]
FrameSets for each of the eight block types.
Definition: FrameSet.hpp:88
boost::filesystem::path path
std::map< std::string, PrimitiveStructureSharedPtr > PrimitiveStructuresSharedPtrMap
Typedef of map from primitive name to primitive structure shared pointer.
static const int sCrcWordIndex
This have to be changed for different Family.
virtual void generateMemoryMicroBitstream()
Generate micro-bitstreams for memory elements like LUT in RAM mode.
static BitstreamSharedPtr newBitstreamPtr(const boost::filesystem::path &inPath)
virtual bool isSiteTypeSupported(const string &inSiteType)
Returns true if site type is supported.
virtual std::vector< std::string > getTilesSupportedForPips()
Returns list of supported tiles.
boost::shared_ptr< VirtexFrame > VirtexFrameSharedPtr
Virtex frame type.
Definition: Frame.hpp:108
path mLibraryFolder
Path to library folder.
boost::shared_ptr< PrimitiveStructure > PrimitiveStructureSharedPtr
Shared pointer encapsulation of a PrimitiveStructure.
void initialize()
Do architecture specific part of initialization.
virtual uint32_t getWordOffsetFromTileLocation(TileRow tileRow, TileCol tileCol, BitstreamSharedPtr inBitstreamPtr)
Get word offset from tile row and col.
static const string sTilesSupportedForPips[]
Array of supported routing tiles.
uint32_t getPrimaryXdlColumn(uint32_t inXdlCol)
Returns the primary column corresponding to the given column. If immediately to the left of a BRAM...
Definition: Virtex5.cpp:1176
boost::shared_ptr< Bitstream > BitstreamSharedPtr
torc::bitstream::BitstreamSharedPtr BitstreamSharedPtr
Imported type name.
Subclass of PrimitiveStructure for Virtex5.
boost::filesystem::path path
Imported type name.
WORD_TYPE word_t
Frame word type.
Definition: Frame.hpp:48
virtual void initializeDeviceInfo(const std::string &inDeviceName)
Initialize the device information.
Definition: Virtex5.cpp:412
const string & getName(void) const
Returns the name of the primitive.