torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VirtexBitstream.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-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 
23 #include <boost/crc.hpp>
24 #include <stdio.h>
25 
26 using namespace torc::common;
27 
28 namespace torc {
29 namespace bitstream {
30 
31  void VirtexBitstream::readPackets(std::istream& inStream) {
32  uint32_t bitstreamWordLength = mBitstreamByteLength >> 2;
33  uint32_t cumulativeWordLength = 0;
34  while(cumulativeWordLength < bitstreamWordLength) {
35  push_back(VirtexPacket::read(inStream));
36  cumulativeWordLength += back().getWordSize();
37  }
38  }
39 
40  void VirtexBitstream::writePackets(std::ostream& inStream) {
41  const_iterator p = begin();
42  const_iterator e = end();
43  while(p < e) p++->write(inStream);
44  }
45 
46  void VirtexBitstream::updateFramePackets(EBitstreamType inBitstreamType,
47  EFrameInclude inFrameInclusion) {
48  // delete the existing frame packets
49  iterator position = deleteFramePackets();
50  // insert the new frame packets in place of the old ones
51  if(inBitstreamType == eBitstreamTypeFull) {
52  insert(position, generateFullBitstreamPackets());
53  } else {
54  insert(position, generatePartialBitstreamPackets(inFrameInclusion));
55  }
56  }
57 
58  void VirtexBitstream::generateBitstream(EBitstreamType inBitstreamType,
59  EFrameInclude inFrameInclusion) {
60  // discard all packets
61  clear();
62  if(inBitstreamType == eBitstreamTypeFull) {
63  // generate the prefix
64  append(generateFullBitstreamPrefix());
65  // generate the frame packets
66  append(generateFullBitstreamPackets());
67  // generate the suffix
68  append(generateFullBitstreamSuffix());
69  } else {
70  // generate the prefix
71  append(generatePartialBitstreamPrefix(inBitstreamType));
72  // generate the frame packets
73  append(generatePartialBitstreamPackets(inFrameInclusion));
74  // generate the suffix
75  append(generatePartialBitstreamSuffix(inBitstreamType));
76  }
77  }
78 
79  void VirtexBitstream::unimplemented(std::string inName) {
80  std::cerr << inName << " is not yet implemented for this architecture." << std::endl;
81  }
82 
83  void VirtexBitstream::readFramePackets(void) {
84  unimplemented("readFramePackets(void)");
85  }
86 
87  // template instantiations
88  template void VirtexBitstream::readFramePackets4567<Virtex7>(uint32_t inBlockFrameIndexBounds[],
89  std::map<Virtex7::FrameAddress, uint32_t>& inFrameAddressToIndex,
90  std::map<uint32_t, Virtex7::FrameAddress>& inFrameIndexToAddress);
91  template void VirtexBitstream::readFramePackets4567<Virtex6>(uint32_t inBlockFrameIndexBounds[],
92  std::map<Virtex6::FrameAddress, uint32_t>& inFrameAddressToIndex,
93  std::map<uint32_t, Virtex6::FrameAddress>& inFrameIndexToAddress);
94  template void VirtexBitstream::readFramePackets4567<Virtex5>(uint32_t inBlockFrameIndexBounds[],
95  std::map<Virtex5::FrameAddress, uint32_t>& inFrameAddressToIndex,
96  std::map<uint32_t, Virtex5::FrameAddress>& inFrameIndexToAddress);
97  template void VirtexBitstream::readFramePackets4567<Virtex4>(uint32_t inBlockFrameIndexBounds[],
98  std::map<Virtex4::FrameAddress, uint32_t>& inFrameAddressToIndex,
99  std::map<uint32_t, Virtex4::FrameAddress>& inFrameIndexToAddress);
100  // template definition
101  template <class ARCH> void VirtexBitstream::readFramePackets4567(
102  uint32_t inBlockFrameIndexBounds[],
103  std::map<typename ARCH::FrameAddress, uint32_t>& inFrameAddressToIndex,
104  std::map<uint32_t, typename ARCH::FrameAddress>& inFrameIndexToAddress) {
105  // clean up the template parameters
106  typedef typename ARCH::FrameAddress FrameAddress;
107  typedef std::map<FrameAddress, uint32_t> FrameAddressToIndex;
108  typedef std::map<uint32_t, FrameAddress> FrameIndexToAddress;
109  // declare a shared word array
110  boost::shared_array<uint32_t> frameWords;
111  // precalculate the start indexes of all blocks, and create all possible frames
112  uint32_t frameStart[eBlockTypeCount + 1];
113  frameStart[0] = 0;
114  VirtexFrame frame(ARCH::eFrameLength);
115  for(int i = 0; i < eBlockTypeCount; i++) {
116  frameStart[i + 1] = frameStart[i] + inBlockFrameIndexBounds[i];
117  // clear any existing frames
118  mFrameBlocks.mBlock[i].clear();
119  // populate the block with the correct number of empty frames
120  for(uint32_t j = 0; j < inBlockFrameIndexBounds[i]; j++) {
121  mFrameBlocks.mBlock[i].push_back(VirtexFrameSet::FrameSharedPtr
122  (new VirtexFrame(getFrameLength())));
123  mFrameBlocks.mBlock[i].back()->setUsed(false);
124  }
125  }
126  // walk the bitstream and extract all frames
127  const uint32_t frameLength = getFrameLength();
128  uint32_t frameIndex = 0;
129  FrameAddress frameAddress;
130  typename ARCH::ERegister lastAddress = typename ARCH::ERegister();
131  typename ARCH::iterator p = begin();
132  typename ARCH::iterator e = end();
133  while(p < e) {
134  const VirtexPacket& packet = *p++;
135  // process FAR write packets
136  if(packet.isWrite() && packet.getAddress() == ARCH::eRegisterFAR) {
137  // extract the new frame address
138  frameAddress = typename ARCH::FrameAddress(packet[1]);
139  // convert the frame address to the corresponding frame index
140  typename FrameAddressToIndex::iterator ip
141  = inFrameAddressToIndex.find(frameAddress);
142  if(ip != inFrameAddressToIndex.end()) frameIndex = ip->second;
143  }
144  // process FDRI write packets
145  else if(packet.isWrite()
146  && (
147  // this is a Type 2 packet and the prior Type 1 address was FDRI
148  (packet.isType2() && lastAddress == ARCH::eRegisterFDRI)
149  ||
150  // this is a non-empty Type 1 packet and its address is FDRI
151  (packet.isType1() && packet.getAddress() == ARCH::eRegisterFDRI
152  && packet.getWordCount() > 0)
153  )) {
154  // determine the number of frames in the packet and look up the frame words
155  const uint32_t numWords = packet.getWordCount();
156  boost::shared_array<uint32_t> frameWords(packet.getWords());
157  uint32_t position = 0;
158  // iterate over every frame in the packet
159  while(position + frameLength <= numWords) {
160  // look up the current block type and the frame index into that block type
161  typename ARCH::EFarBlockType blockType = frameAddress.mBlockType;
162  uint32_t index = frameIndex - frameStart[blockType];
163  mFrameBlocks.mBlock[blockType][index] = VirtexFrameSet::FrameSharedPtr
164  (new VirtexFrame(frameLength, &frameWords[position]));
165  mFrameBlocks.mBlock[blockType][index]->setUsed();
166  position += frameLength;
167  frameIndex++;
168  typename FrameIndexToAddress::iterator ap
169  = inFrameIndexToAddress.find(frameIndex);
170  if(ap != inFrameIndexToAddress.end()) frameAddress = ap->second;
171  // sanity escape exit (which also breaks out of the higher level loop)
172  if(position > numWords) {
173  std::cerr << "Overflowed expected frame counts for device." << std::endl;
174  break;
175  }
176  }
177  if(frameIndex != frameStart[frameAddress.mBlockType + 1]) {
178  // if we ended on a pad frame, where the current index has no corresponding
179  // frame address, we need to advance to the next valid frame address
180  if(inFrameIndexToAddress.find(frameIndex) == inFrameIndexToAddress.end())
181  frameIndex++;
182  if(inFrameIndexToAddress.find(frameIndex) == inFrameIndexToAddress.end())
183  frameIndex++;
184  // at this point we should again be on a valid frame
185  typename FrameIndexToAddress::iterator ap
186  = inFrameIndexToAddress.find(frameIndex);
187  if(ap != inFrameIndexToAddress.end()) frameAddress = ap->second;
188  else if(frameIndex == frameStart[frameAddress.mBlockType + 1]) /* at end */;
189  else {
190  std::cerr << "Failed to find the next valid frame address at the end of a "
191  "packet." << std::endl;
192  }
193  }
194  }
195  // remember the last register address for Type 2 packets
196  if(packet.isType1()) lastAddress = typename ARCH::ERegister(packet.getAddress());
197  // process MFWR write packets
198  /// \todo
199  }
200  }
201 
202  VirtexPacketVector::iterator VirtexBitstream::deleteFramePackets(void) {
203  unimplemented("deleteFramePackets()");
204  return end();
205  }
206 
207  // template instantiations
208  template VirtexPacketVector::iterator VirtexBitstream::deleteFramePackets4567<Virtex7>(void);
209  template VirtexPacketVector::iterator VirtexBitstream::deleteFramePackets4567<Virtex6>(void);
210  template VirtexPacketVector::iterator VirtexBitstream::deleteFramePackets4567<Virtex5>(void);
211  template VirtexPacketVector::iterator VirtexBitstream::deleteFramePackets4567<Virtex4>(void);
212  // template definition
213  template <class ARCH> VirtexPacketVector::iterator VirtexBitstream::deleteFramePackets4567(void)
214  {
215  // walk the bitstream and look for frame packet boundaries
216  // we begin with the first FAR write, and end before the first CRC write after an FDRI write
217  iterator b = begin();
218  iterator p = b;
219  iterator e = end();
220  iterator start = e;
221  iterator stop = b;
222  iterator fdri = b;
223  while(p < e) {
224  // look up the current packet
225  const VirtexPacket& packet = *p;
226  // start from the first FAR write
227  if(start == e && packet.isWrite() && packet.getAddress() == ARCH::eRegisterFAR) {
228  start = p;
229  }
230  // remember the last FDRI write (including a trailing Type 2 write if present)
231  if(packet.isWrite() && packet.getAddress() == ARCH::eRegisterFDRI) {
232  fdri = p;
233  if((p+1)->isWrite() && (p+1)->isType2()) fdri++;
234  }
235  // stop on the first CRC after the last FDRI write
236  // (Beware: the zero "address" of a Type 2 packet looks like the CRC register)
237  if(stop < fdri && packet.isWrite() && packet.isType1()
238  && packet.getAddress() == ARCH::eRegisterCRC) {
239  stop = p;
240  }
241  p++;
242  }
243  // look for anything unexpected
244  p = start;
245  while(p < stop) {
246  // look up the current packet
247  const VirtexPacket& packet = *p++;
248  // abort if we find anything unexpected
249  if(start < e && stop > b && !(
250  packet.isNop() || (
251  packet.isWrite() && (
252  packet.getAddress() == ARCH::eRegisterFAR ||
253  packet.getAddress() == ARCH::eRegisterLOUT ||
254  packet.getAddress() == ARCH::eRegisterFDRI ||
255  packet.getAddress() == ARCH::eRegisterCRC ||
256  (packet.getAddress() == ARCH::eRegisterCMD && packet[1] == ARCH::eCommandWCFG)
257  )))) {
258  /// \todo Throw some meaningful exception
259  std::cerr << "Unable to safely distinguish frame packets." << std::endl;
260  return e;
261  }
262  }
263  // if we found the expected FAR -> FDRI -> CRC structure, delete the frame packets
264  if(start < stop && b < stop) {
265  erase(start, stop);
266  return start;
267  }
268  // if we didn't find frame packets or delete anything, return the end iterator
269  return e;
270  }
271 
272  VirtexPacketVector VirtexBitstream::generateFullBitstreamPackets(void) {
273  unimplemented("generateFullBitstreamPackets()");
274  return VirtexPacketVector();
275  }
276 
277  // template instantiations
278  template VirtexPacketVector VirtexBitstream::generateFullBitstreamPackets4567<Virtex7>(
279  uint32_t inBlockFrameIndexBounds[]);
280  template VirtexPacketVector VirtexBitstream::generateFullBitstreamPackets4567<Virtex6>(
281  uint32_t inBlockFrameIndexBounds[]);
282  template VirtexPacketVector VirtexBitstream::generateFullBitstreamPackets4567<Virtex5>(
283  uint32_t inBlockFrameIndexBounds[]);
284  template VirtexPacketVector VirtexBitstream::generateFullBitstreamPackets4567<Virtex4>(
285  uint32_t inBlockFrameIndexBounds[]);
286  // template definition
287  template <class ARCH> VirtexPacketVector VirtexBitstream::generateFullBitstreamPackets4567(
288  uint32_t inBlockFrameIndexBounds[]) {
289 
290  // declare the packet vector and define a NOP packet
291  typedef VirtexFrame::word_t word_t;
292  VirtexPacketVector packets;
293  VirtexPacket nop(VirtexPacket::makeHeader(ePacketType1, eOpcodeNOP, 0, 0));
294 
295  // determine the total size of the frames to write
296  size_t size = 0;
297  for(int i = 0; i < eBlockTypeCount; i++)
298  size += inBlockFrameIndexBounds[i] * getFrameLength();
299  word_t* frameContents = new VirtexFrameSet::word_t[size];
300  word_t* pos = frameContents;
301  // gather the frames to write
302  for(int i = 0; i < eBlockTypeCount; i++) {
303  // look up the frame set for this block type
304  VirtexFrameSet& frameSet = mFrameBlocks.mBlock[i];
305  // iterate through the frames in the set and collect the frame words
306  VirtexFrameSet::iterator p = frameSet.begin();
307  VirtexFrameSet::iterator e = frameSet.end();
308  while(p < e) {
309  // look up the current frame
310  VirtexFrameSharedPtr framePtr = *p++;
311  // set up the pointers
312  const word_t* wp = framePtr->getWords();
313  const word_t* we = wp + framePtr->getLength();
314  // and copy the frame words into the new memory
315  if(wp) do { *pos++ = *wp++; } while(wp < we); // frames with words allocated
316  else do { *pos++ = 0; wp++; } while(wp < we); // frames with no words allocated
317  }
318  }
319  // write the starting frame address
320  packets.push_back(VirtexPacket::makeType1Write(ARCH::eRegisterFAR, 0));
321  // write the write configuration register command
322  packets.push_back(VirtexPacket::makeType1Write(ARCH::eRegisterCMD, ARCH::eCommandWCFG));
323  packets.push_back(nop);
324  // write 0 bytes to FDRI (in preparation for type 2 write packet)
325  packets.push_back(VirtexPacket::makeNullType1Write(ARCH::eRegisterFDRI));
326  // write all frames to FDRI
327  packets.push_back(VirtexPacket::makeType2Write(size, frameContents));
328 
329  // return the packet vector
330  return packets;
331  }
332 
333  VirtexPacketVector VirtexBitstream::generateFullBitstreamPrefix(void) {
334  unimplemented("generateFullBitstreamPrefix()");
335  return VirtexPacketVector();
336  }
337 
338  VirtexPacketVector VirtexBitstream::generateFullBitstreamSuffix(void) {
339  unimplemented("generateFullBitstreamSuffix()");
340  return VirtexPacketVector();
341  }
342 
343  VirtexPacketVector VirtexBitstream::generatePartialBitstreamPackets(
344  EFrameInclude inFrameInclusion) {
345  unimplemented("generatePartialBitstreamPackets()");
346  return VirtexPacketVector();
347  }
348 
349  // template instantiations
350  template VirtexPacketVector VirtexBitstream::generatePartialBitstreamPackets4567<Virtex7>(
351  EFrameInclude inFrameInclusion,
352  std::map<Virtex7::FrameAddress, uint32_t>& inFrameAddressToIndex,
353  std::map<uint32_t, Virtex7::FrameAddress>& inFrameIndexToAddress);
354  template VirtexPacketVector VirtexBitstream::generatePartialBitstreamPackets4567<Virtex6>(
355  EFrameInclude inFrameInclusion,
356  std::map<Virtex6::FrameAddress, uint32_t>& inFrameAddressToIndex,
357  std::map<uint32_t, Virtex6::FrameAddress>& inFrameIndexToAddress);
358  template VirtexPacketVector VirtexBitstream::generatePartialBitstreamPackets4567<Virtex5>(
359  EFrameInclude inFrameInclusion,
360  std::map<Virtex5::FrameAddress, uint32_t>& inFrameAddressToIndex,
361  std::map<uint32_t, Virtex5::FrameAddress>& inFrameIndexToAddress);
362  template VirtexPacketVector VirtexBitstream::generatePartialBitstreamPackets4567<Virtex4>(
363  EFrameInclude inFrameInclusion,
364  std::map<Virtex4::FrameAddress, uint32_t>& inFrameAddressToIndex,
365  std::map<uint32_t, Virtex4::FrameAddress>& inFrameIndexToAddress);
366  // template definition
367  template <class ARCH> VirtexPacketVector VirtexBitstream::generatePartialBitstreamPackets4567(
368  EFrameInclude inFrameInclusion,
369  std::map<typename ARCH::FrameAddress, uint32_t>& inFrameAddressToIndex,
370  std::map<uint32_t, typename ARCH::FrameAddress>& inFrameIndexToAddress) {
371  // clean up the template parameters
372  typedef typename ARCH::FrameAddress FrameAddress;
373  typedef std::map<FrameAddress, uint32_t> FrameAddressToIndex;
374  typedef std::map<uint32_t, FrameAddress> FrameIndexToAddress;
375 
376  // declare the packet vector and define a NOP packet
377  typedef VirtexFrame::word_t word_t;
378  VirtexPacketVector packets;
379  VirtexPacket nop(VirtexPacket::makeHeader(ePacketType1, eOpcodeNOP, 0, 0));
380 
381  // write the starting frame address
382  packets.push_back(VirtexPacket::makeType1Write(ARCH::eRegisterFAR, 0));
383  // write the write configuration register command
384  packets.push_back(VirtexPacket::makeType1Write(ARCH::eRegisterCMD, ARCH::eCommandWCFG));
385  packets.push_back(nop);
386 
387  // iterate through the frame blocks looking for groups of contiguous frames that are in use
388  bool empty = true;
389  uint32_t index = 0;
390  uint32_t blockStart = 0;
391  for(int i = 0; i < eBlockTypeCount; i++) {
392  blockStart += index;
393  index = 0;
394  bool started = false;
395  uint32_t startIndex = 0;
396  // look up the frame set for this block type
397  VirtexFrameSet& frameSet = mFrameBlocks.mBlock[i];
398  // iterate through the frames in the set and add up their lengths
399  VirtexFrameSet::iterator p = frameSet.begin();
400  VirtexFrameSet::iterator e = frameSet.end();
401  while(p < e) {
402  // look up the current frame
403  VirtexFrameSharedPtr framePtr = *p++;
404  // determine whether the frame fits inclusion criteria
405  // we include dirty frames, we include clean frames if permitted by the caller,
406  // and if we are collecting frames and we encounter pad frames, we include them,
407  // but we stop collecting if we reach the last frame in the set
408  /// \todo mFrameIndexToAddress.size() is too short because excludes pad frames
409  bool include = p < e
410  && (
411  framePtr->isDirty()
412  || (inFrameInclusion == eFrameIncludeAllUsedFrames && framePtr->isUsed())
413  || (started && blockStart + index < inFrameIndexToAddress.size() &&
414  inFrameIndexToAddress.find(blockStart + index)
415  == inFrameIndexToAddress.end())
416  );
417  // if we are accumulating frames and this frame doesn't meet the criteria, process
418  // the collection of frames and stop collecting
419  if((started && !include)) {
420  started = false;
421  uint32_t stopIndex = index + (p == e ? 1 : 0);
422  uint32_t currentIndex = startIndex;
423  // std::cerr << " stopped at: " << stopIndex << std::endl;
424  // include two trailing pad frames if appropriate
425  if(inFrameIndexToAddress.find(blockStart + stopIndex + 1)
426  == inFrameIndexToAddress.end()) {
427  stopIndex++;
428  if(inFrameIndexToAddress.find(blockStart + stopIndex + 1)
429  == inFrameIndexToAddress.end())
430  stopIndex++;
431  }
432  // determine the length of the contiguous block
433  size_t size = (stopIndex - startIndex) * getFrameLength();
434  // allocate a word array
435  word_t* frameContents = new VirtexFrameSet::word_t[size];
436  word_t* pos = frameContents;
437  while(currentIndex < stopIndex) {
438  // look up the current frame
439  VirtexFrameSharedPtr framePtr = mFrameBlocks.mBlock[i][currentIndex];
440  // set up the pointers
441  const word_t* wp = framePtr->getWords();
442  const word_t* we = wp + framePtr->getLength();
443  // and copy the frame words into the new memory
444  if(wp) do { *pos++ = *wp++; } while(wp < we); // frames with words
445  else do { *pos++ = 0; wp++; } while(wp < we); // frames with no words
446  currentIndex++;
447  }
448  // write the starting frame address
449  packets.push_back(VirtexPacket::makeType1Write(ARCH::eRegisterFAR,
450  inFrameIndexToAddress[blockStart + startIndex]));
451  packets.push_back(nop);
452  // if the size is more than 2048 words, we have to use a Type 2 write
453  if(size > 2048) {
454  // write 0 bytes to FDRI (in preparation for type 2 write packet)
455  packets.push_back(VirtexPacket::makeNullType1Write(ARCH::eRegisterFDRI));
456  // write all frames to FDRI
457  packets.push_back(VirtexPacket::makeType2Write(size, frameContents));
458  } else {
459  // write all frames to FDRI
460  packets.push_back(VirtexPacket::makeType1Write(ARCH::eRegisterFDRI, size,
461  frameContents));
462  }
463  if(size) empty = false;
464  // if we are not collecting, but we encounter a frame to include, begin collecting
465  } else if(!started && include) {
466  // std::cerr << " started at: " << index << std::endl;
467  startIndex = index;
468  started = true;
469  }
470  // increment the frame index
471  index++;
472  }
473  }
474 
475  // if we generated no frame contents, discard the wrapper packets
476  if(empty) packets.clear();
477  // return the packet vector
478  return packets;
479  }
480 
481  VirtexPacketVector VirtexBitstream::generatePartialBitstreamPrefix(
482  EBitstreamType inBitstreamType) {
483  unimplemented("generatePartialBitstreamPrefix()");
484  return VirtexPacketVector();
485  }
486 
487  VirtexPacketVector VirtexBitstream::generatePartialBitstreamSuffix(
488  EBitstreamType inBitstreamType) {
489  unimplemented("generatePartialBitstreamSuffix()");
490  return VirtexPacketVector();
491  }
492 
493  void VirtexBitstream::preflightPackets(void) {
494  // determine which architecture this is
495  DeviceDesignator deviceDesignator(getDeviceName());
496  DeviceDesignator::EFamily family = deviceDesignator.getFamily();
497  switch(family) {
500  updateCrc16(family); break;
504  case DeviceDesignator::eFamilyZynq7000: default:
505  updateCrc32(family); break;
506  }
507  }
508 
509  void VirtexBitstream::updateCrc16(DeviceDesignator::EFamily inFamily) {
510  // set up family-specific variables
511  uint32_t crcRegister = 0;
512  uint32_t cmdRegister = 0;
513  uint32_t fdriRegister = 0;
514  uint32_t rcrcCommand = 0;
515  uint32_t addressLength = 0;
516  bool autoCrc = false;
517  switch(inFamily) {
519  cmdRegister = Virtex::eRegisterCMD; rcrcCommand = Virtex::eCommandRCRC;
520  fdriRegister = Virtex::eRegisterFDRI; crcRegister = Virtex::eRegisterCRC;
521  addressLength = 4; autoCrc = false;
522  break;
524  cmdRegister = Virtex2::eRegisterCMD; rcrcCommand = Virtex2::eCommandRCRC;
525  fdriRegister = Virtex2::eRegisterFDRI; crcRegister = Virtex::eRegisterCRC;
526  addressLength = 5; autoCrc = true;
527  break;
528  default:
529  std::cerr << "Unsupported architecture in VirtexBitstream::updateCrc16()." << std::endl;
530  break;
531  }
532  // begin CRC calculation
533  uint32_t address = 0;
534  iterator p = begin();
535  iterator e = end();
536  // CRC-16-IBM polynomial for Spartan3/6 and Virtex/E/2/2P families
537  boost::crc_basic<16> crc16(0x8005, 0, 0, false, true);
538  while(p < e) {
539  // look up the current packet
540  const VirtexPacket& packet = *p++;
541  // only process write packets with non-zero payload length
542  if(!packet.isWrite()) continue;
543  if(packet.isType1()) address = packet.getAddress();
544  uint32_t wordCount = packet.getWordCount();
545  if(wordCount == 0) continue;
546  // CRC register write (this is what compares the expected and supplied CRC values)
547  if(address == crcRegister) {
548  //printf("Expected CRC16: %4.4x\n", packet[1]);
549  //printf("Calculated CRC16: %4.4x\n", crc16.checksum());
550  *(p-1) = VirtexPacket::makeType1Write(crcRegister, crc16.checksum());
551  crc16.reset();
552  // reset CRC command
553  } else if(address == cmdRegister && wordCount >= 1 && packet[1] == rcrcCommand) {
554  crc16.reset();
555  // process packet contents
556  } else {
557  uint32_t j;
558  uint32_t mask;
559  for(uint32_t i = 1; i <= wordCount; i++) {
560  uint32_t word = packet[i];
561  //printf("Address: %4.4x\n", address);
562  //printf("Word: %8.8x\n", word);
563  for(j = 0, mask = 1; j < 32; j++, mask <<= 1) {
564  crc16.process_bit((word & mask) ? 1 : 0);
565  }
566  for(j = 0, mask = 1; j < addressLength; j++, mask <<= 1) {
567  crc16.process_bit((address & mask) ? 1 : 0);
568  }
569  }
570  // process the Auto CRC on Virtex2 and Virtex2P
571  if(autoCrc && address == fdriRegister) {
572  //printf("Expected Auto CRC16: %4.4x\n", packet[1]);
573  //printf("Calculated Auto CRC16: %4.4x\n", crc16.checksum());
574  *p = VirtexPacket(crc16.checksum()); // current word is FDRI, next is Auto CRC
575  crc16.reset();
576  }
577  }
578  }
579  }
580 
581  void VirtexBitstream::updateCrc32(DeviceDesignator::EFamily inFamily) {
582  // set up family-specific variables
583  uint32_t crcRegister = 0;
584  uint32_t cmdRegister = 0;
585  uint32_t rcrcCommand = 0;
586  uint32_t addressLength = 5;
587  switch(inFamily) {
589  cmdRegister = Virtex4::eRegisterCMD; rcrcCommand = Virtex4::eCommandRCRC;
590  crcRegister = Virtex4::eRegisterCRC;
591  break;
593  cmdRegister = Virtex5::eRegisterCMD; rcrcCommand = Virtex5::eCommandRCRC;
594  crcRegister = Virtex5::eRegisterCRC;
595  break;
597  cmdRegister = Virtex6::eRegisterCMD; rcrcCommand = Virtex6::eCommandRCRC;
598  crcRegister = Virtex6::eRegisterCRC;
599  break;
602  cmdRegister = Virtex7::eRegisterCMD; rcrcCommand = Virtex7::eCommandRCRC;
603  crcRegister = Virtex7::eRegisterCRC;
604  break;
605  default:
606  std::cerr << "Unsupported architecture in VirtexBitstream::updateCrc32()." << std::endl;
607  break;
608  }
609  // begin CRC calculation
610  uint32_t address = 0;
611  iterator p = begin();
612  iterator e = end();
613  // CRC-32C (Castagnoli) polynomial for Virtex4/5/6/7 families
614  boost::crc_basic<32> crc32(0x1edc6f41, 0, 0, false, true);
615  while(p < e) {
616  // look up the current packet
617  const VirtexPacket& packet = *p++;
618  // only process write packets with non-zero payload length
619  if(!packet.isWrite()) continue;
620  if(packet.isType1()) address = packet.getAddress();
621  uint32_t wordCount = packet.getWordCount();
622  if(wordCount == 0) continue;
623  // CRC register write (this is what compares the expected and supplied CRC values)
624  if(address == crcRegister) {
625  //printf("Expected CRC32: %8.8x\n", packet[1]);
626  //printf("Calculated CRC32: %8.8x\n", crc32.checksum());
627  *(p-1) = VirtexPacket::makeType1Write(crcRegister, crc32.checksum());
628  crc32.reset();
629  // reset CRC command
630  } else if(address == cmdRegister && wordCount >= 1 && packet[1] == rcrcCommand) {
631  crc32.reset();
632  // process packet contents
633  } else {
634  uint32_t j;
635  uint32_t mask;
636  for(uint32_t i = 1; i <= wordCount; i++) {
637  uint32_t word = packet[i];
638  //printf("Address: %4.4x\n", address);
639  //printf("Word: %8.8x\n", word);
640  for(j = 0, mask = 1; j < 32; j++, mask <<= 1) {
641  crc32.process_bit((word & mask) ? 1 : 0);
642  }
643  for(j = 0, mask = 1; j < addressLength; j++, mask <<= 1) {
644  crc32.process_bit((address & mask) ? 1 : 0);
645  }
646  }
647  }
648  }
649  }
650 
651  void VirtexBitstream::updatePacketLength(void) {
652  uint32_t totalWordCount = 0;
653  iterator p = begin();
654  iterator e = end();
655  while(p < e) totalWordCount += (p++)->getWordSize();
656  mBitstreamByteLength = totalWordCount << 2;
657  }
658 
659 } // namespace bitstream
660 } // namespace torc
Header for the VirtexBitstream class.
Encapsulation of a device designator and its constituent elements.
Header for the Virtex4 class.
const EFamily & getFamily(void) const
Returns the device family.
Frame< uint32_t > VirtexFrame
Virtex frame type.
Definition: Frame.hpp:104
EFrameInclude
The frames to include in a partial bitstream. Use eFrameIncludeOnlyDirtyFrames to include only dirty...
Header for the Virtex6 class.
Header for the Virtex5 class.
uint32_t getWordCount(void) const
Returns the number of payload words in the packet, excluding the header word.
std::string string
boost::uint32_t uint32_t
Imported type name.
VirtexFrame::word_t word_t
FrameSet word type.
Definition: FrameSet.hpp:85
EBitstreamType
The bitstream type to generate. Use eBitstreamFull to fully reconfigure a device, eBitstreamTypePartialActive to partially reconfigure it while it continues to run, or eBitstreamTypePartialShutdown to partially recongifure it after shutting it down.
boost::shared_ptr< VirtexFrame > FrameSharedPtr
Shared pointer encapsulation of a Frame.
Definition: FrameSet.hpp:44
std::vector< VirtexPacket > VirtexPacketVector
Vector of Virtex packets.
Header for the Virtex class.
const WordSharedArray getWords(void) const
brief Returns the raw packet words, including the header word.
boost::shared_ptr< VirtexFrame > VirtexFrameSharedPtr
Virtex frame type.
Definition: Frame.hpp:108
VirtexFrame::word_t word_t
FrameSet word type.
Definition: FrameSet.hpp:42
Header for the Virtex2 class.
Bitstream packet for Virtex class architectures.
Bitstream frame.
Definition: Frame.hpp:31
WORD_TYPE word_t
Frame word type.
Definition: Frame.hpp:48
Header for the Virtex7 class.