torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Spartan3EUnitTest.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 
16 /// \file
17 /// \brief Unit test for the Spartan3E class.
18 
19 #include <boost/test/unit_test.hpp>
22 #include "torc/common/Devices.hpp"
28 #include <fstream>
29 #include <iostream>
30 #include <boost/filesystem.hpp>
31 
32 namespace torc {
33 namespace bitstream {
34 
35 BOOST_AUTO_TEST_SUITE(bitstream)
36 
37 
38 /// \brief Unit test for the Spartan3E class.
39 BOOST_AUTO_TEST_CASE(Spartan3EUnitTest) {
40 
41  // enums tested:
42  // EPacket
43  // EFar
44  boost::uint32_t mask;
45  // type 1 packet subfield masks
49  BOOST_CHECK_EQUAL(mask, 0xFFFFFFFFu);
50  // type 2 packet subfield masks
53  BOOST_CHECK_EQUAL(mask, 0xFFFFFFFFu);
54  // frame address register subfield masks
56  BOOST_CHECK_EQUAL(mask, 0x07FFFE00u);
57 
58  // members tested:
59  // Spartan3E::sPacketTypeName and EPacketTypeName
60  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[0], "[UNKNOWN TYPE 0]");
61  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[Spartan3E::ePacketType1], "TYPE1");
62  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[Spartan3E::ePacketType2], "TYPE2");
63  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[3], "[UNKNOWN TYPE 3]");
64  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[4], "[UNKNOWN TYPE 4]");
65  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[5], "[UNKNOWN TYPE 5]");
66  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[6], "[UNKNOWN TYPE 6]");
67  BOOST_CHECK_EQUAL(Spartan3E::sPacketTypeName[7], "[UNKNOWN TYPE 7]");
68 
69  // members tested:
70  // Spartan3E::sOpcodeName and EOpcode
71  BOOST_CHECK_EQUAL(Spartan3E::sOpcodeName[Spartan3E::eOpcodeNOP], "NOP");
72  BOOST_CHECK_EQUAL(Spartan3E::sOpcodeName[Spartan3E::eOpcodeRead], "READ");
73  BOOST_CHECK_EQUAL(Spartan3E::sOpcodeName[Spartan3E::eOpcodeWrite], "WRITE");
74  BOOST_CHECK_EQUAL(Spartan3E::sOpcodeName[Spartan3E::eOpcodeReserved], "RESERVED");
75 
76  // members tested:
77  // Spartan3E::sRegisterName and ERegister
78  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterCRC], "CRC");
79  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterFAR], "FAR");
80  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterFDRI], "FDRI");
81  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterFDRO], "FDRO");
82  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterCMD], "CMD");
83  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterCTL], "CTL");
84  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterMASK], "MASK");
85  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterSTAT], "STAT");
86  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterLOUT], "LOUT");
87  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterCOR], "COR");
88  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterMFWR], "MFWR");
89  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterFLR], "FLR");
90  BOOST_CHECK_EQUAL(Spartan3E::sRegisterName[Spartan3E::eRegisterIDCODE], "IDCODE");
91 
92  // members tested:
93  // Spartan3E::sCommandName and ECommand
94  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandWCFG], "WCFG");
95  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandMFWR], "MFWR");
96  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandLFRM], "LFRM");
97  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandRCFG], "RCFG");
98  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandSTART], "START");
99  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandRCAP], "RCAP");
100  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandRCRC], "RCRC");
101  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandAGHIGH], "AGHIGH");
102  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandSWITCH], "SWITCH");
103  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandGRESTORE], "GRESTORE");
104  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandSHUTDOWN], "SHUTDOWN");
105  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandGCAPTURE], "GCAPTURE");
106  BOOST_CHECK_EQUAL(Spartan3E::sCommandName[Spartan3E::eCommandDESYNCH], "DESYNCH");
107 
108  // build the file paths
110  / "torc" / "bitstream" / "Spartan3EUnitTest.reference.bit";
112  / "regression" / "Spartan3EUnitTest.generated.bit";
113 
114  // read the bitstream
115  std::fstream fileStream(referencePath.string().c_str(), std::ios::binary | std::ios::in);
116  BOOST_REQUIRE(fileStream.good());
117  Spartan3E bitstream;
118  bitstream.read(fileStream, false);
119  // write the bitstream digest to the console
120  std::cout << bitstream << std::endl;
121 
122  std::string designName = bitstream.getDesignName();
123  std::string deviceName = bitstream.getDeviceName();
124  std::string designDate = bitstream.getDesignDate();
125  std::string designTime = bitstream.getDesignTime();
126  torc::common::DeviceDesignator deviceDesignator(deviceName);
127  std::cout << "family of " << deviceName << " is " << deviceDesignator.getFamily() << std::endl;
128 
129  // write the bitstream back out
130  std::fstream outputStream(generatedPath.string().c_str(), std::ios::binary | std::ios::out);
131  BOOST_REQUIRE(outputStream.good());
132  bitstream.write(outputStream);
133  outputStream.flush();
134 
135  // compare the reference and generated XDL
136  BOOST_CHECK(torc::common::fileContentsAreEqual(generatedPath, referencePath));
137 }
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 void testSpartan3EDevice(const std::string& inDeviceName, const boost::filesystem::path& inWorkingPath);
178 
179 /// \brief Unit test for the Spartan3E class Frame Address Register mapping.
180 BOOST_AUTO_TEST_CASE(Spartan3EFarUnitTest) {
181 
182  // look up the command line arguments
183  int& argc = boost::unit_test::framework::master_test_suite().argc;
184  char**& argv = boost::unit_test::framework::master_test_suite().argv;
185  // make sure that we at least have the name under which we were invoked
186  BOOST_REQUIRE(argc >= 1);
187  // resolve symbolic links if applicable
188  torc::common::DirectoryTree directoryTree(argv[0]);
189 
190  // iterate over the devices
192  torc::common::DeviceVector::const_iterator dp = devices.begin();
193  torc::common::DeviceVector::const_iterator de = devices.end();
194  while(dp < de) {
195  const std::string& device = *dp++;
196  if(device.empty()) break;
197 //std::cout << "device " << ": " << device << std::endl;
199  }
200 }
201 
202 /*
203  class TileTypeWidths {
204  public:
205  uint32_t mWidth[8];
206  TileTypeWidths(uint32_t in0 = 0, uint32_t in1 = 0, uint32_t in2 = 0, uint32_t in3 = 0,
207  uint32_t in4 = 0, uint32_t in5 = 0, uint32_t in6 = 0, uint32_t in7 = 0) {
208  int i = 0;
209  mWidth[i++] = in0; mWidth[i++] = in1; mWidth[i++] = in2; mWidth[i++] = in3;
210  mWidth[i++] = in4; mWidth[i++] = in5; mWidth[i++] = in6; mWidth[i++] = in7;
211  }
212  void clear(void) { for(int i = 0; i < 8; i++) mWidth[i] = 0; }
213  uint32_t operator[] (int inIndex) const { return mWidth[inIndex]; }
214  };
215 */
216 
217 void testSpartan3EDevice(const std::string& inDeviceName, const boost::filesystem::path& inWorkingPath) {
218 
219  // build the file paths
220  boost::filesystem::path debugBitstreamPath = inWorkingPath / "torc" / "bitstream" / "regression";
221  //boost::filesystem::path generatedPath = debugBitstreamPath / (inDeviceName + ".debug.bit");
222  boost::filesystem::path referencePath = debugBitstreamPath / (inDeviceName + ".debug.bit");
223 std::cerr << "TRYING TO FIND " << referencePath << std::endl;
224 
225  // read the bitstream
226  std::fstream fileStream(referencePath.string().c_str(), std::ios::binary | std::ios::in);
227  std::cerr << "Trying to read: " << referencePath << std::endl;
228  BOOST_REQUIRE(fileStream.good());
229  Spartan3E bitstream;
230  bitstream.read(fileStream, false);
231  // write the bitstream digest to the console
232  //std::cout << bitstream << std::endl;
233 
234 // // initialize the bitstream frame maps
235 // boost::filesystem::path deviceColumnsPath = inWorkingPath / "regression"
236 // / (inDeviceName + ".cpp");
237 // std::fstream deviceColumnsStream(deviceColumnsPath.string().c_str(), std::ios::out);
238  bitstream.initializeDeviceInfo(inDeviceName);
239  bitstream.initializeFrameMaps();
240 
241  // iterate through the packets, and extract all of the FARs
242  Spartan3E::FrameAddressToIndex farRemaining = bitstream.mFrameAddressToIndex;
244  {
245  bool first = true;
246  Spartan3E::const_iterator p = bitstream.begin();
247  Spartan3E::const_iterator e = bitstream.end();
250  while(p < e) {
251  const SpartanPacket& packet = *p++;
252  if(packet.getHeader() != header) continue;
253  if(first) { first = false; continue; }
254  Spartan3E::FrameAddress far = packet[1];
255  //std::cout << std::endl << "Debug Far Address: " << Hex32(packet[1]) << std::endl;
256  farVisited[far] = 0;
257  Spartan3E::FrameAddressToIndex::iterator found = farRemaining.find(far);
258  if(found != farRemaining.end()) {
259  farRemaining.erase(found);
260  } else {
261  std::cerr << "missing " << far << " ";
262  }
263  }
264  }
265  {
266  std::cerr << std::endl;
267  Spartan3E::FrameAddressToIndex::const_iterator p = farRemaining.begin();
268  Spartan3E::FrameAddressToIndex::const_iterator e = farRemaining.end();
269  while(p != e) {
270  std::cerr << "remaining " << p->first << " ";
271  p++;
272  }
273  std::cerr << std::endl;
274  }
275  // verify that we have visited all of the expected FARs and no others
276  std::cout << "Device: " << inDeviceName << std::endl;
277  std::cout << "Size of farRemaining: " << farRemaining.size() << std::endl;
278  std::cout << "Size of farVisited: " << farVisited.size() << std::endl;
279  BOOST_REQUIRE_EQUAL(bitstream.mFrameAddressToIndex.size(), farVisited.size());
280  BOOST_REQUIRE_EQUAL(farRemaining.size(), 0u);
281 
282 return;
283 }
284 
285 BOOST_AUTO_TEST_SUITE_END()
286 
287 } // namespace bitstream
288 } // namespace torc
static const char * sOpcodeName[eOpcodeCount]
Packet opcode names.
Definition: Spartan3E.hpp:87
Header for torc::bitstream output stream helpers.
static const char * sRegisterName[eRegisterCount]
Configuration register names.
Definition: Spartan3E.hpp:89
Encapsulation of a device designator and its constituent elements.
Header for the DeviceInfoHelper class.
const EFamily & getFamily(void) const
Returns the device family.
std::map< Spartan3E::FrameAddress, uint32_t > FrameAddressToIndex
Map from frame address to frame index.
Definition: Spartan3E.hpp:187
static const char * sCommandName[eCommandCount]
Configuration command names.
Definition: Spartan3E.hpp:91
Header for the DirectoryTree class.
static const char * sPacketTypeName[ePacketTypeCount]
Packet type names.
Definition: Spartan3E.hpp:85
Header for the DeviceDesignator class.
std::string string
Header for Boost.Test helper functions.
static uint32_t makeHeader(EPacketType inType, EOpcode inOpcode, uint32_t inAddress, uint32_t inCount)
Construct a packet header.
Encapsulation of filesystem paths that are used by the library.
static const DeviceVector & getSpartan3EDevices(void)
Returns the Spartan3E devices.
Definition: Devices.hpp:203
Header for the Devices class.
boost::filesystem::path path
uint32_t getHeader(void) const
static const boost::filesystem::path & getWorkingPath(void)
Returns the absolute path to the working directory.
Header for the DDB class.
Header for the Spartan3E class.
bool fileContentsAreEqual(const boost::filesystem::path &inA, const boost::filesystem::path &inB)
Compare the raw contents of two files to determine whether they are identical.
Spartan3E bitstream.
Definition: Spartan3E.hpp:38
Bitstream packet for Spartan class architectures.
BOOST_AUTO_TEST_CASE(hexCharacterToDec)
std::vector< std::string > DeviceVector
Vector of device names.
Definition: Devices.hpp:119
virtual void read(std::istream &inStream, bool inCleanDateAndTime=true)
Read the bitstream header and packets from a stream.
static const boost::filesystem::path & getExecutablePath(void)
Returns the absolute path to the executable directory.
void testSpartan3EDevice(const std::string &inDeviceName, const boost::filesystem::path &inWorkingPath)