00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "torc/bitstream/VirtexBitstream.hpp"
00017 #include <boost/crc.hpp>
00018 #include <stdio.h>
00019
00020 namespace torc {
00021 namespace bitstream {
00022
00023 void VirtexBitstream::readPackets(std::istream& inStream) {
00024 uint32_t bitstreamWordLength = mBitstreamByteLength >> 2;
00025 uint32_t cumulativeWordLength = 0;
00026 while(cumulativeWordLength < bitstreamWordLength) {
00027 push_back(VirtexPacket::read(inStream));
00028 cumulativeWordLength += back().getWordSize();
00029 }
00030 }
00031
00032 void VirtexBitstream::writePackets(std::ostream& inStream) {
00033 const_iterator p = begin();
00034 const_iterator e = end();
00035 while(p < e) p++->write(inStream);
00036 }
00037
00038 void VirtexBitstream::preflightPackets(void) {
00039 uint32_t crc;
00040 uint32_t address = 0;
00041 uint32_t addressLength = 0;
00042 bool crctype;
00043 string DeviceName = getDeviceName();
00044 switch (DeviceName[0])
00045 {
00046
00047 case '2':
00048 addressLength = 5;
00049 crctype = false;
00050 break;
00051
00052 case '4':
00053 case '5':
00054 case '6':
00055 addressLength = 5;
00056 crctype = true;
00057 break;
00058
00059 case 'v':
00060 addressLength = 4;
00061 crctype = false;
00062 break;
00063 default:
00064 addressLength = 5;
00065 crctype = true;
00066 break;
00067 }
00068 const_iterator p = begin();
00069 const_iterator e = end();
00070 boost::crc_basic<16> crc16(0x8005, 0, 0, false, true);
00071 boost::crc_basic<32> crc32(0x1EDC6F41, 0, 0, false, true);
00072 while(p < e) {
00073
00074 const VirtexPacket& packet = *p++;
00075 if(!packet.isWrite()) continue;
00076 if(packet.isType1()) address = packet.getAddress();
00077 uint32_t wordCount = packet.getWordCount();
00078 if(wordCount == 0) continue;
00079
00080 if(address == 0) {
00081 printf("Expected CRC: %x\n", packet[1]);
00082 if (crctype) {
00083 crc = crc32.checksum();
00084 printf("Calculated CRC32: %x\n", crc);
00085 }
00086 else {
00087 crc = crc16.checksum();
00088
00089 }
00090 crc = 0;
00091 crc16.reset();
00092 crc32.reset();
00093 }
00094
00095 else if(address == 4 && wordCount >= 1 && packet[1] == 7) {
00096 crc = 0;
00097 crc16.reset();
00098 crc32.reset();
00099 }
00100 else {
00101 uint32_t i;
00102 for(i = 1; i <= wordCount; i++) {
00103 uint32_t word = packet[i];
00104
00105
00106 uint32_t j;
00107 uint32_t mask;
00108 for(j = 0, mask = 1; j < 32; j++, mask <<= 1) {
00109 crc16.process_bits((word & mask) ? 1 : 0, 1);
00110
00111 crc32.process_bits((word & mask) ? 1 : 0, 1);
00112
00113 }
00114 for(j = 0, mask = 1; j < addressLength; j++, mask <<= 1) {
00115 crc16.process_bits((address & mask) ? 1 : 0, 1);
00116
00117 crc32.process_bits((address & mask) ? 1 : 0, 1);
00118
00119 }
00120
00121 }
00122
00123 if (packet.isType2() && DeviceName[0] == '2') {
00124 crc = crc16.checksum();
00125 printf("Calculated Auto CRC16: %x\n", crc);
00126 crc = 0;
00127 crc16.reset();
00128 }
00129 }
00130 }
00131 }
00132
00133 void VirtexBitstream::updatePacketLength(void) {
00134 uint32_t totalWordCount = 0;
00135 iterator p = begin();
00136 iterator e = end();
00137 while(p < e) totalWordCount += (p++)->getWordSize();
00138 mBitstreamByteLength = totalWordCount << 2;
00139 }
00140
00141 }
00142 }