Bitstream Interpretation Library (BIL)  0.1
bitreverse.cpp
Go to the documentation of this file.
1 
8 #include <cstdlib>
9 #include <cstring>
10 #include <fstream>
11 #include <iostream>
14 #include <bitfile/BitFileData.hpp>
18 #include <cfgdecode/CfgDecoder.hpp>
19 #include <cfgdecode/CfgPrint.hpp>
28 #include <xdlrc/model/Device.hpp>
29 
30 using namespace bil;
31 
32 
33 int main(int argc, char** argv)
34 {
35  try
36  {
37  //======================================================================
38  // process given command line
39  std::cout << SPLASH_MSG;
40  parseCommandLine(argc, argv);
41 
42 
43  //======================================================================
44  // get configuration data from bitstream
45  std::string deviceName;
46  V5AddressLayoutRegistry addressLayoutRegistry;
47  V5PacketProcessor packetProcessor(addressLayoutRegistry);
48  V5Configuration& configuration = packetProcessor.configuration();
49  {
50  // load bitfile
51  std::cout << LOADING_BITFILE_MSG;
52  BitFileData bfd;
53  std::ifstream bitfileStream(bitFileName.c_str(), std::ios::binary);
54  readBitfile(bfd, bitfileStream);
55 
56  // construct bitstream from bitfile raw data
57  std::cout << DECODING_BITSTREAM_MSG;
58  Bitstream bs;
60  bfd.bitstreamWordCount(0);
61 
62  // read in available devices
63  std::cout << LOADING_DEVICELIST_MSG;
64  std::string deviceListFileName = dataPathName + V5DEVICES_FILENAME;
65  std::ifstream lstFileStream(deviceListFileName.c_str(), std::ios::binary);
66  DeviceRegistry deviceRegistry;
67  readBinary(deviceRegistry, lstFileStream);
68 
69  // detect target device type
70  std::cout << DETECTING_TARGETDEVICE_MSG;
71  DeviceID::ID_t deviceID = detectV5DeviceType(bs);
72  deviceName = deviceRegistry.lookup(deviceID);
73  if (deviceName.empty()) throw Exception();
74 
75  // load address layout
76  std::cout << LOADING_ADDRESSLAYOUT1_MSG << deviceName << LOADING_ADDRESSLAYOUT2_MSG;
77  V5AddressLayout addressLayout;
78  std::string calFileName = dataPathName + deviceName + CAL_FILE_EXT;
79  std::ifstream calFileStream(calFileName.c_str(), std::ios::binary);
80  readBinary(addressLayout, calFileStream);
81  addressLayoutRegistry.insert(deviceID, addressLayout);
82 
83  // execute bitstream
84  std::cout << EXECUTING_BITSTREAM_MSG;
85  bs.runVisitor(packetProcessor);
86  }
87 
88 
89  //======================================================================
90  // load device description and configuration tile map
91  std::string devicePackageName = deviceName + packageName;
92  Device device;
93  V5CfgTileMap cfgTileMap;
94  {
95  std::cout << LOADING_DEVICE1_MSG << devicePackageName << LOADING_DEVICE2_MSG;
96  std::string deviceFileName = dataPathName + devicePackageName + DEVICE_FILE_EXT;
97  std::ifstream deviceInputStream(deviceFileName.c_str(), std::ios::binary);
98  readBinary(device, deviceInputStream);
99 
100  std::cout << LOADING_CFGTILEMAP_MSG;
101  std::string mapFileName = dataPathName + devicePackageName + MAP_FILE_EXT;
102  std::ifstream mapInputStream(mapFileName.c_str(), std::ios::binary);
103  readBinary(cfgTileMap, mapInputStream);
104  }
105 
106 
107  //======================================================================
108  // load configuration database
109  DeviceCfgDb cfgDatabase;
110  {
111  std::cout << LOADING_DATABASE_MSG;
112  std::string dbFilename = dataPathName + devicePackageName + CFGDB_FILE_EXT;
113  std::ifstream dbFileStream(dbFilename.c_str(), std::ios::binary);
114  readBinary(cfgDatabase, dbFileStream);
115  }
116 
117 
118  //======================================================================
119  // do reversal
120  CfgDecoder decoder;
121  {
122  std::cout << DOING_REVERSAL_MSG;
123  V5CfgExtractor cfgExtractor(configuration, cfgTileMap);
124  decoder.decode(cfgExtractor, cfgDatabase, device);
125 
126  std::ofstream xdlFileStream(xdlFileName.c_str());
127  printPIPs(decoder.pipRefs(), device, xdlFileStream);
128  }
129 
130 
131  //======================================================================
132  // finished
133  std::cout << FINISHED_MSG;
134  }
135  catch (const CommandLineException& e)
136  {
137  std::cout << ERROR_MSG << e.what() << INFO_MSG;
138  return EXIT_FAILURE;
139  }
140  catch (...)
141  {
142  std::cout << ERROR_UNKNOWN_MSG;
143  return EXIT_FAILURE;
144  }
145  return EXIT_SUCCESS;
146 }