torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SpartanBitstreamUnitTest.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 SpartanBitstream class.
18 
19 #include <boost/test/unit_test.hpp>
28 #include <fstream>
29 #include <iostream>
30 
31 namespace torc {
32 namespace bitstream {
33 
34 BOOST_AUTO_TEST_SUITE(bitstream)
35 
36 /// \brief Unit test for the SpartanBitstream class.
37 BOOST_AUTO_TEST_CASE(SpartanBitstreamCrcUnitTest) {
38  // check the CRC generation
40  / "torc" / "bitstream" / "Spartan3EUnitTest.reference.bit";
41  BOOST_CHECK(checkCRC<Spartan3E>(regressionPath, true));
42 }
43 
44 /// \brief Unit test for the SpartanBitstream class.
45 BOOST_AUTO_TEST_CASE(SpartanBitstreamUnitTest) {
46 
47  // build the file paths
49  / "torc" / "bitstream" / "Spartan3EUnitTest.reference.bit";
51  / "regression" / "SpartanBitstreamUnitTest.generated.bit";
52 
53 /*
54  // read the reference bitstream
55  std::fstream fileStream1(referencePath.string().c_str(), std::ios::binary | std::ios::in);
56  BOOST_REQUIRE(fileStream1.good());
57  Spartan3E bitstream1;
58  bitstream1.read(fileStream1, false);
59  // find the type 2 write packet
60  SpartanPacket frameContents;
61  Spartan3E::const_iterator p = bitstream1.begin();
62  Spartan3E::const_iterator e = bitstream1.end();
63  while(p < e) {
64  if(p->isType2() && p->isWrite()) { frameContents = *p; break; }
65  p++;
66  }
67  {
68  uint32_t length = frameContents.getWordCount();
69  for(uint32_t i = 1; i < length; i++) {
70  if(frameContents[i] != 0) printf("buffer[0x%8.8x] = 0x%8.8x; // byte address 0x%8.8x\n", i - 1, frameContents[i], (i - 1) << 2);
71  }
72  }
73 */
74 
75  // declare a bitstream of unspecified architectures
76  SpartanBitstream bitstream;
77  // prepare the bitstream header
78  bitstream.setDesignName("Spartan3EUnitTest.reference.ncd");
79  bitstream.setDeviceName("3s500ecp132");
80  bitstream.setDesignDate("2011/ 2/ 1");
81  bitstream.setDesignTime(" 9: 6:48");
82 
83  // construct and add packets to the bitstream
86  0, 0));
87  // dummy words
88  bitstream.insert(bitstream.end(), 1, dummy);
89  // sync
90  bitstream.push_back(SpartanPacket(Spartan3E::eSynchronizationSync));
91  //bitstream.push_back(nop);
92  // reset CRC command
94  // FLR command
95  bitstream.push_back(SpartanPacket::makeType1Write(Spartan3E::eRegisterFLR, 0x00000060));
96  // configuration options register
101  Spartan3E::makeSubfield(Spartan3E::eRegisterCOR, "Capture", "Continuous") |
102  Spartan3E::makeSubfield(Spartan3E::eRegisterCOR, "ConfigRate", "[UNKNOWN 0]") |
103  Spartan3E::makeSubfield(Spartan3E::eRegisterCOR, "StartupClk", "Cclk") |
105  Spartan3E::makeSubfield(Spartan3E::eRegisterCOR, "Match_cycle", "1") |
106  Spartan3E::makeSubfield(Spartan3E::eRegisterCOR, "LCK_cycle", "NoWait") |
109  0));
110  // write the ID code
111  bitstream.push_back(SpartanPacket::makeType1Write(Spartan3E::eRegisterIDCODE, 0x01c22093));
112  // control register mask
114  Spartan3E::makeSubfield(Spartan3E::eRegisterMASK, "Security", "Protected") |
115  Spartan3E::makeSubfield(Spartan3E::eRegisterMASK, "Persist", "Protected") |
116  Spartan3E::makeSubfield(Spartan3E::eRegisterMASK, "GTS_USER_B", "Protected") |
117  0x00000000 | // no documentation available for this subfield
118  0));
119  // switch command
121  // frame address register
123  // enable configuration write command
125  // write 0 bytes to FDRI (in preparation for type 2 write packet)
127  // write 0x0001149a words to FDRI
128  //bitstream.push_back(SpartanPacket::makeType2Write(Spartan3E::eRegisterFDRI,0x0001149a));
129  // note that we do not release the buffer as it will be taken over by the packet
130  size_t length = 0x0001149a;
131  uint32_t* buffer = new uint32_t[length];
132  uint32_t* ptr = buffer + length - 1;
133  while(ptr >= buffer) *ptr-- = 0;
134  // kludge: the "null" reference bitstream in question isn't completely empty
135  buffer[0x00000124] = 0x05500000; // byte address 0x00000490
136  buffer[0x000001e3] = 0x00002a80; // byte address 0x0000078c
137  buffer[0x000001e4] = 0x18000000; // byte address 0x00000790
138  buffer[0x0000cbda] = 0x0000000a; // byte address 0x00032f68
139  buffer[0x0000cbdb] = 0xa0000000; // byte address 0x00032f6c
140  buffer[0x0000cc39] = 0x00140000; // byte address 0x000330e4
141  buffer[0x0000cc3a] = 0x05000000; // byte address 0x000330e8
142  bitstream.push_back(SpartanPacket::makeType2Write(0x0001149a, buffer));
143  // NOP
144  bitstream.push_back(nop);
145  // restore command
148  // last frame command
151  // 97 NOPs
152  bitstream.insert(bitstream.end(), 97, nop);
153  // start command
156  // control register
158  Spartan3E::makeSubfield(Spartan3E::eRegisterCTL, "Security", "None") |
160  Spartan3E::makeSubfield(Spartan3E::eRegisterCTL, "GTS_USER_B", "IoDisabled") |
161  0x00000000 | // no documentation available for this subfield
162  0));
163  // write the CRC value
164  bitstream.push_back(SpartanPacket::makeType1Write(Spartan3E::eRegisterCRC, 0x00005f57));
165  // desynch command
168  bitstream.insert(bitstream.end(), 4, nop);
169 
170  // write the bitstream back out
171  std::fstream outputStream(generatedPath.string().c_str(), std::ios::binary | std::ios::out);
172  BOOST_REQUIRE(outputStream.good());
173  bitstream.write(outputStream);
174  outputStream.flush();
175 
176  // read the bitstream
177  //std::fstream fileStream2(generatedPath.string().c_str(), std::ios::binary | std::ios::in);
178  //BOOST_REQUIRE(fileStream2.good());
179  //Spartan3E bitstream2;
180  //bitstream2.read(fileStream2, false);
181  // write the bitstream digest to the console
182  //std::cout << bitstream2 << std::endl;
183 
184  // compare the reference and generated XDL
185  BOOST_CHECK(torc::common::fileContentsAreEqual(generatedPath, referencePath));
186 
187 }
188 
189 BOOST_AUTO_TEST_SUITE_END()
190 
191 } // namespace bitstream
192 } // namespace torc
193 
void setDesignTime(const string &inDesignTime)
Set the design time.
Header for torc::bitstream output stream helpers.
Header for the SpartanPacket class.
Header for the DirectoryTree class.
Header for the DeviceDesignator class.
static SpartanPacket makeType2Write(uint32_t inCount, uint32_t *inWords)
Construct a type 2 write packet.
Header for Boost.Test helper functions.
Header for Boost.Test helper functions.
void setDesignName(const string &inDesignName)
Set the design name.
void setDesignDate(const string &inDesignDate)
Set the design date.
static uint32_t makeHeader(EPacketType inType, EOpcode inOpcode, uint32_t inAddress, uint32_t inCount)
Construct a packet header.
boost::filesystem::path path
Header for the SpartanBitstream class.
static uint32_t makeSubfield(ERegister inRegister, const std::string &inSubfield, const std::string &inSetting)
Return the masked value for a subfield of the specified register.
Definition: Spartan3E.cpp:186
void write(std::ostream &inStream, uint8_t inVal)
Write a uint8_t to the stream.
static SpartanPacket makeNullType1Write(uint32_t inAddress)
Construct a null type 1 write packet.
static SpartanPacket makeType1Write(uint32_t inAddress, uint32_t inWord)
Construct a type 1 write packet.
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.
Bitstream packet for Spartan class architectures.
BOOST_AUTO_TEST_CASE(hexCharacterToDec)
void setDeviceName(const string &inDeviceName)
Set the device name.
static const boost::filesystem::path & getExecutablePath(void)
Returns the absolute path to the executable directory.