00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "torc/bitstream/SpartanBitstream.hpp"
00017 #include <boost/crc.hpp>
00018 #include <stdio.h>
00019
00020 namespace torc {
00021 namespace bitstream {
00022
00023 void SpartanBitstream::readPackets(std::istream& inStream) {
00024 uint32_t bitstreamWordLength = mBitstreamByteLength >> 2;
00025 uint32_t cumulativeWordLength = 0;
00026 while(cumulativeWordLength < bitstreamWordLength) {
00027 push_back(SpartanPacket::read(inStream));
00028 cumulativeWordLength += back().getWordSize();
00029 }
00030 }
00031
00032 void SpartanBitstream::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 SpartanBitstream::preflightPackets(void) {
00039 uint32_t crc;
00040 uint32_t address = 0;
00041 const_iterator p = begin();
00042 const_iterator e = end();
00043 boost::crc_basic<16> crc16(0x8005, 0, 0, false, true);
00044 while(p < e) {
00045
00046 const SpartanPacket& packet = *p++;
00047 if(!packet.isWrite()) continue;
00048 if(packet.isType1()) address = packet.getAddress();
00049 uint32_t wordCount = packet.getWordCount();
00050 if(wordCount == 0) continue;
00051
00052 if(address == 0) {
00053 printf("Expected CRC: %x\n", packet[1]);
00054 crc = crc16.checksum();
00055 printf("Calculated CRC16: %x\n", crc);;
00056 crc = 0;
00057 crc16.reset();
00058 }
00059
00060 else if(address == 4 && wordCount >= 1 && packet[1] == 7) {
00061 crc = 0;
00062 crc16.reset();
00063 }
00064 else {
00065 for(uint32_t i = 1; i <= wordCount; i++) {
00066 uint32_t word = packet[i];
00067
00068
00069 uint32_t j;
00070 uint32_t mask;
00071 for(j = 0, mask = 1; j < 32; j++, mask <<= 1) {
00072 crc16.process_bits((word & mask) ? 1 : 0, 1);
00073 }
00074 for(j = 0, mask = 1; j < 5; j++, mask <<= 1) {
00075 crc16.process_bits((address & mask) ? 1 : 0, 1);
00076 }
00077
00078 }
00079
00080 if (packet.isType2()){
00081 crc = crc16.checksum();
00082 printf("Calculated Auto CRC16: %x\n", crc);
00083 crc = 0;
00084 crc16.reset();
00085 }
00086 }
00087 }
00088 }
00089
00090 void SpartanBitstream::updatePacketLength(void) {
00091 uint32_t totalWordCount = 0;
00092 iterator p = begin();
00093 iterator e = end();
00094 while(p < e) totalWordCount += (p++)->getWordSize();
00095 mBitstreamByteLength = totalWordCount << 2;
00096 }
00097
00098 }
00099 }