torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Virtex7LibGen.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 "Virtex7LibGen.hpp"
22 
23 namespace torc {
24 namespace bitstream {
25 
26 const int Virtex7LibGen::sCrcWordIndex = 50;
28  "INT_R",
29  "INT_L",
30  "CLBLM_L",
31  "CLBLM_R",
32  "CLK_BUFG_BOT_R",
33  "CLK_BUFG_REBUF",
34  "CLK_BUFG_TOP_R",
35  "CLK_HROW_BOT_R",
36  "CLK_HROW_TOP_R",
37  "CLK_MTBF2",
38  "CLK_PMV",
39  "CLK_PMV2",
40  "HCLK_CMT",
41  "HCLK_IOI",
42  "HCLK_IOI3",
43  "HCLK_BRAM",
44  "HCLK_CLB",
45  "HCLK_GTX",
46  "HCLK_IOB",
47  "HCLK_VBRK",
48  "LAST_TILE"
49  };
50 
51 /// \details Architecture specific part of initializing the object
53 
56 }
57 
58 /// \details Returns true for the sites which are supported by library generation process
59 bool Virtex7LibGen::isSiteTypeSupported(const string &siteType) {
60  return (siteType.compare("SLICEM") == 0 ||
61  siteType.compare("SLICELO") == 0 || siteType.compare("SLICELE") == 0 ||
62  siteType.compare("*RAMB36E1") == 0 || siteType.compare("*DSP48E1") == 0 ||
63 // siteType.compare("BUFGCTRL") == 0 || siteType.compare("BUFDS") == 0 ||
64  siteType.compare("*BUFR") == 0 || siteType.compare("*BUFG") == 0)
65  ;
66 }
67 
68 /// \brief Returns list of supported tiles.
69 std::vector<std::string> Virtex7LibGen::getTilesSupportedForPips() {
70  uint32_t supportedTileIndex = 0;
71  std::vector<std::string> tilesSupported;
72  while(sTilesSupportedForPips[supportedTileIndex].compare("LAST_TILE") != 0) {
73  tilesSupported.push_back(sTilesSupportedForPips[supportedTileIndex++]);
74  }
75  return tilesSupported;
76 }
77 
78 /// \brief Compress given bitstream file against reference bitstream file
79 void Virtex7LibGen::compressBitFile(path primaryFile, path referenceFile) {
80 
81  bool debug = false;
82 
83  // Read the bitstream files
84  torc::bitstream::BitstreamSharedPtr referenceBitstreamPtr = Factory::newBitstreamPtr(referenceFile);
85  torc::bitstream::BitstreamSharedPtr primBitstreamPtr = Factory::newBitstreamPtr(primaryFile);
86 
87  // Check if both the bitstreams were read successfully.
88  if(referenceBitstreamPtr && primBitstreamPtr) {
89  std::cout << "Read the two bit files - " << primaryFile.filename() << " and " << referenceFile.filename() << " successfully" << std::endl;
90  } else {
91  std::cout << "Could not read the bit files " << primaryFile << " " << referenceFile << std::endl;
92  return;
93  }
94 
95  // dynamically cast the bitstream pointer into a Virtex7 bitstream pointer
96 
97  boost::shared_ptr<torc::bitstream::Virtex7> primVirtexBitstreamPtr = boost::dynamic_pointer_cast<torc::bitstream::Virtex7>(
98  primBitstreamPtr);
99  boost::shared_ptr<torc::bitstream::Virtex7> refVirtex7BitstreamPtr = boost::dynamic_pointer_cast<torc::bitstream::Virtex7>(
100  referenceBitstreamPtr);
101 
102  // initalize frame map
103  refVirtex7BitstreamPtr->initializeDeviceInfo(referenceBitstreamPtr->getDesignName());
104  refVirtex7BitstreamPtr->initializeFrameMaps();
105  refVirtex7BitstreamPtr->readFramePackets();
106 
107  primVirtexBitstreamPtr->initializeDeviceInfo(primBitstreamPtr->getDesignName());
108  primVirtexBitstreamPtr->initializeFrameMaps();
109  primVirtexBitstreamPtr->readFramePackets();
110 
111  uint32_t wordOffset = getWordOffsetFromXdlFile(primaryFile, primVirtexBitstreamPtr);
112 
113  // look up the frame blocks
114  torc::bitstream::VirtexFrameBlocks& refFrameBlocks = refVirtex7BitstreamPtr->getFrameBlocks();
115  torc::bitstream::VirtexFrameBlocks& primFrameBlocks = primVirtexBitstreamPtr->getFrameBlocks();
116  std::vector<boost::int32_t> combinedAddressVector;
117 
118 
119  // iterate over the block types
120  for(int blockIndex = 0; blockIndex < Bitstream::eBlockTypeCount; blockIndex++) {
121  // look up the frame set for this block
122  torc::bitstream::VirtexFrameSet& nullFrameSet = refFrameBlocks.mBlock[blockIndex];
123  torc::bitstream::VirtexFrameSet& primFrameSet = primFrameBlocks.mBlock[blockIndex];
124 
125  if(nullFrameSet.size() != primFrameSet.size()) {
126  std::cout << "Frame set sizes do not match" << std::endl;
127  return;
128  }
129 
130  // We expect the majorIndex to remain same for all the mismatching part of the bitstream.
131  uint32_t relativeFrameIndex = 0, currentMajorIndex = 0, previousMajorIndex = 0;
132  bool firstMismatch = true;
133 
134  // iterate over the frames
135  for(uint32_t frameIndex = 0; frameIndex < nullFrameSet.size(); frameIndex++) {
136  // look up the frame for this set
137  VirtexFrameSharedPtr nullFramePtr = nullFrameSet[frameIndex];
138  VirtexFrameSharedPtr primFramePtr = primFrameSet[frameIndex];
139 
140  const VirtexFrame::word_t* wordsReference = nullFramePtr->getWords();
141  const VirtexFrame::word_t* wordsPrim = primFramePtr->getWords();
142 
143  const VirtexFrame::word_t* pReferenceWords = wordsReference;
144  const VirtexFrame::word_t* pPrimWords = wordsPrim;
145  const VirtexFrame::word_t* eReferenceWords = pReferenceWords
146  + nullFramePtr->getLength();
147  const VirtexFrame::word_t* ePrimWords = pPrimWords + primFramePtr->getLength();
148 
149  if(nullFramePtr->getLength() != primFramePtr->getLength()) {
150  std::cout << "WARNING: Frames of different sizes " << nullFramePtr->getLength() << " "
151  << primFramePtr->getLength() << std::endl;
152  }
153 
154  // iterate over the frame words
155  bool found = false;
156  int wordIndex = 0;
157 
158  // Iterate over the frame words
159  while(pReferenceWords < eReferenceWords) {
160  // If the word is not zero and not same as that of reference bitstream and not crc word
161  if(*pPrimWords != 0 && *pReferenceWords != *pPrimWords && wordIndex != sCrcWordIndex) {
162 
163  boost::int32_t bitIndex = 0;
164  // ToDo: Uncomment after support is introduced
165  primVirtexBitstreamPtr->splitFrameIndex(frameIndex, currentMajorIndex, relativeFrameIndex);
166 
167  // If this is not the first mismatch, compare the major index with previous mismatching word's major index.
168  if(!firstMismatch) {
169  if(currentMajorIndex != previousMajorIndex) {
170  std::cout << "WARNING: Bitstream mismatch across major indices: " << currentMajorIndex << " and " << previousMajorIndex << std::endl;
171  //exit (-1);
172  return;
173  }
174  } else {
175  firstMismatch = false;
176  previousMajorIndex = currentMajorIndex;
177  }
178 
179  // Get the 1 bits in primary design which are zero in reference design
180  VirtexFrame::word_t tempWord = (*pPrimWords ^ *pReferenceWords) & *pPrimWords;
181  // Find all the 1s in the word
182  while(tempWord != 0) {
183  // check if the LSB is 1
184  if(tempWord & 1) {
185  // store the combined address
186  combinedAddressVector.push_back((relativeFrameIndex << 16) | ((wordIndex - wordOffset)
187  << 8) | bitIndex);
188 
189  }
190  tempWord = tempWord >> 1;
191  bitIndex++;
192  }
193  found = true;
194  }
195  pReferenceWords++;
196  pPrimWords++;
197  wordIndex++;
198  }
199  // if we found any non-zero words, display them. This if for debugging purpose only.
200  if(found) {
201  std::cout << "Found unmatching frame: block " << blockIndex << ", relative index "
202  << Hex32(relativeFrameIndex) << ", global index: " << Hex32(frameIndex)
203  << ", major index: " << Hex32(currentMajorIndex) << std::endl;
204  pReferenceWords = wordsReference;
205  if(debug) {
206  std::cout << " Referen: ";
207  while(pReferenceWords < eReferenceWords) {
208  std::cout << Hex32(*pReferenceWords++) << " ";
209  }
210  std::cout << std::endl;
211  pPrimWords = wordsPrim;
212  std::cout << " Primary: ";
213  while(pPrimWords < ePrimWords) {
214  std::cout << Hex32(*pPrimWords++) << " ";
215  }
216  std::cout << std::endl;
217  }
218  }
219  }
220  }
221 
222  // Store all the combined addresses in the sparse bit file.
223  boost::filesystem::path sparceBitstreamPath = primaryFile.replace_extension().string()
224  + ".cbit";
225 
226  std::ofstream sparceBitstreamFile;
227  sparceBitstreamFile.open(sparceBitstreamPath.string().c_str(), std::ios::binary | std::ios::out);
228  if(sparceBitstreamFile.good()) {
229  std::cout << " Opened library file to write " << sparceBitstreamPath.string().c_str()
230  << std::endl;
231  } else {
232  std::cerr << " Could not open library file to write - "
233  << sparceBitstreamPath.string().c_str() << std::endl;
234  exit(-1);
235  }
236 
237  boost::int32_t vectorSize = combinedAddressVector.size();
238  sparceBitstreamFile.write((char *) &vectorSize, 4);
239  std::cout << "Different words -" << std::endl;
240  std::cout << " ";
241  for(std::vector<boost::int32_t>::const_iterator iter = combinedAddressVector.begin(); iter
242  != combinedAddressVector.end(); iter++) {
243  sparceBitstreamFile.write((char *) (&*iter), 4);
244  std::cout << Hex32((*iter)) << " ";
245  }
246  std::cout << std::endl;
247 
248  std::cout << "Wrote data to the library file." << std::endl;
249 
250  sparceBitstreamFile.close();
251 
252 }
253 
255  uint32_t bitIndexBegin = 0, bitIndexEnd = 0;
256  boost::shared_ptr<Virtex7> virtex7Ptr = boost::dynamic_pointer_cast<Virtex7>(inBitstreamPtr);
257  // ToDo: Uncomment after support is introduced
258  uint32_t primaryXdlCol = virtex7Ptr->getPrimaryXdlColumn(tileRow, tileCol);
259  virtex7Ptr->getXdlFrames(tileRow, primaryXdlCol, bitIndexBegin,
261 
262  // Each frame covers the full height of clock region which contains many rows of tiles.
263  // The library contains configuration bits only for the first row. So the word index has to
264  // be offset to reach the correct row. The bit index remains same across rows.
265  uint32_t outWordOffset = 0;
266  if(bitIndexBegin != bitIndexEnd) {
267  outWordOffset = Virtex7::eFrameLength - (bitIndexEnd >> 5); // eFrameLength is number of words in a frame
268 // outWordOffset = bitIndexBegin >> 5;
269  }
270 
271  std::cout << "*******Word offset " << outWordOffset << std::endl;
272 
273  return outWordOffset;
274 
275 }
276 
277 /// \details Creates map from primitive name to primitive structure object pointer
279  PrimitiveStructuresSharedPtrMap primitiveStructures;
280  // look up the primitive def types
282  PrimitiveDefArray;
283  PrimitiveDefArray & primitiveDefs = mSites.getSiteTypes();
284  PrimitiveDefArray::const_iterator pPrimitiveDefs = primitiveDefs.begin();
285  PrimitiveDefArray::const_iterator ePrimitiveDefs = primitiveDefs.end();
286  // Store the corresponding primitive structures in a map.
287  while(pPrimitiveDefs < ePrimitiveDefs) {
288  // create a PrimitiveStructure object for this PrimitiveDef
289  torc::packer::PrimitiveStructureSharedPtr primitiveStructurePtr(
290  new torc::packer::Virtex7PrimitiveStructure(&*pPrimitiveDefs++));
291  const torc::architecture::PrimitiveDef *primitiveDefPtr =
292  primitiveStructurePtr->getPrimitiveDefPtr();
293  const std::string & primitiveDefName = primitiveDefPtr->getName();
294  // insert the PrimitiveStructure into the map
295  if(!isSlicelType(primitiveDefName)) {
296  primitiveStructures[primitiveDefName] = primitiveStructurePtr;
297  } else {
298  string sliceEven = primitiveDefName + "E";
299  string sliceOdd = primitiveDefName + "O";
300  primitiveStructures[sliceEven] = primitiveStructurePtr;
301  primitiveStructures[sliceOdd] = primitiveStructurePtr;
302  }
303  }
304  return primitiveStructures;
305 }
306 
308  // Get compressed bit files for LUTs in RAM/ROM mode.
309  path libraryMemFolder = mLibraryFolder / "memory";
310  string perlScript = libraryMemFolder.string() + "/extract-bits-for-luts.pl";
311  string llFile = getParentFamilyName() + "-CLBLM_L-SLICEM-X-ROM.ll";
312  path llFilePath = libraryMemFolder / llFile;
313  string systemCommand = "perl " + perlScript + " " + llFilePath.string();
314  int systemReturn = system(systemCommand.c_str());
315 
316  llFile = getParentFamilyName() + "-CLBLM_R-SLICEM-X-ROM.ll";
317  llFilePath = libraryMemFolder / llFile;
318  systemCommand = "perl " + perlScript + " " + llFilePath.string();
319  systemReturn = system(systemCommand.c_str());
320  string cpyCmd = "mv " + libraryMemFolder.string() + "/*.cbit " + mXdlGenerationFolder.string();
321 // ToDo: Uncomment line below
322  systemReturn = system(cpyCmd.c_str());
323  (void) systemReturn;
324 }
325 
326 
327 } //namespace bitstream
328 } //namespace torc
329 
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.
const Sites & mSites
Sites from the database object.
static const string sTilesSupportedForPips[]
Array of supported routing tiles.
virtual uint32_t getWordOffsetFromTileLocation(TileRow tileRow, TileCol tileCol, BitstreamSharedPtr inBitstreamPtr)
Get word offset from tile row and col.
virtual PrimitiveStructuresSharedPtrMap getPrimitiveNameToStructreMap()
Populate primitive name to primitive structure map.
Encapsulation of primitive site definition, with associated connections, elements, and pins.
Header for the Virtex7PrimitiveStructure class.
virtual string getParentFamilyName()
Get parent family name.
uint32_t getPrimaryXdlColumn(uint32_t inXdlRow, uint32_t inXdlCol)
Returns the primary column corresponding to the given tile. If the specified tile falls in an INT_L ...
Definition: Virtex7.cpp:1349
bool isSlicelType(const string &inSiteType)
Return true if site type is SLICEL.
void initialize()
Do architecture specific part of initialization.
virtual bool isSiteTypeSupported(const string &inSiteType)
Returns true if site type is supported.
std::string string
uint32_t getWordOffsetFromXdlFile(path inBitstreamPath, BitstreamSharedPtr inBitstreamPtr)
Get word offset in frame for the site/pip in corresponding Xdl.
const Array< const PrimitiveDef > & getSiteTypes(void) const
Returns the site types for this family.
Definition: Sites.hpp:125
static const int sCrcWordIndex
This have to be changed for different Family.
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.
Subclass of PrimitiveStructure for Virtex7.
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 BitstreamSharedPtr newBitstreamPtr(const boost::filesystem::path &inPath)
Virtex7 bitstream.
Definition: Virtex7.hpp:40
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.
std::map< string, string > getReferenceConfigMap()
Get a map from config setting to reference value.
static CompoundSettingMap getCompoundSettingsMap()
Returns map of coumpound settings.
virtual std::vector< std::string > getTilesSupportedForPips()
Returns list of supported tiles. Assumes last tile is "LAST_TILE".
boost::shared_ptr< Bitstream > BitstreamSharedPtr
torc::bitstream::BitstreamSharedPtr BitstreamSharedPtr
Imported type name.
virtual void compressBitFile(path primaryFile, path referenceFile)
Compress given bitstream file against reference bitstream file.
virtual void generateMemoryMicroBitstream()
Generate micro-bitstreams for memory elements like LUT in RAM mode.
boost::filesystem::path path
Imported type name.
WORD_TYPE word_t
Frame word type.
Definition: Frame.hpp:48
Header for the Virtex7 class.
virtual void initializeDeviceInfo(const std::string &inDeviceName)
Initialize the Device Information.
Definition: Virtex7.cpp:416
const string & getName(void) const
Returns the name of the primitive.