torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Om1UnitTest.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 generic object model class.
18 
19 #include <boost/test/unit_test.hpp>
20 #include <iostream>
21 #include <sstream>
29 #include <boost/regex.hpp>
30 
31 namespace torc {
32 namespace generic {
33 
34 BOOST_AUTO_TEST_SUITE(generic)
35 
36  class VisitNet {
37  public:
38  typedef boost::shared_ptr<Net> NetSharedPtr;
39  void operator ()(NetSharedPtr& inNetPtr) const {
40  int sources = 0;
41  int sinks = 0;
42  // look for a single source and a single sink on the net
43  // get the ports
44  typedef boost::shared_ptr<Port> PortSharedPtr;
45  std::vector<PortSharedPtr> ports;
46  inNetPtr->getConnectedPorts(ports);
47  std::vector<PortSharedPtr>::const_iterator pp = ports.begin();
48  std::vector<PortSharedPtr>::const_iterator pe = ports.end();
49  while(pp < pe) {
50  const PortSharedPtr& portPtr = *pp++;
51  EPortDirection direction = portPtr->getDirection();
52  switch(direction) {
53  case ePortDirectionIn: sources++; break;
54  case ePortDirectionOut: sinks++; break;
55  case ePortDirectionInOut: sources++; sinks++; break;
56  default: break;
57  }
58  }
59  // get the port refs
60  typedef boost::shared_ptr<PortReference> PortReferenceSharedPtr;
61  std::vector<PortReferenceSharedPtr> portReferences;
62  inNetPtr->getConnectedPortRefs(portReferences);
63  // convert the port refs to ports
64  std::vector<PortReferenceSharedPtr>::const_iterator rp = portReferences.begin();
65  std::vector<PortReferenceSharedPtr>::const_iterator re = portReferences.end();
66  while(rp < re) {
67  const PortReferenceSharedPtr& portRefPtr = *rp++;
68  const PortSharedPtr& portPtr = portRefPtr->getMaster();
69  EPortDirection direction = portPtr->getDirection();
70  switch(direction) {
71  case ePortDirectionIn: sinks++; break;
72  case ePortDirectionOut: sources++; break;
73  case ePortDirectionInOut: sources++; sinks++; break;
74  default: break;
75  }
76  }
77  BOOST_REQUIRE_EQUAL(sources, 1);
78  std::cerr << "Net " << inNetPtr->getName() << ": "
79  << ports.size() << " ports, "
80  << portReferences.size() << " portRefs, "
81  << sources << " sources, "
82  << sinks << " sinks"
83  << std::endl;
84  }
85  };
86 
87 /// \brief Unit test for the generic object model.
88 BOOST_AUTO_TEST_CASE(Om1UnitTest) {
89 
90  // create string streams for both the generated and parsed EDIF
91  std::stringstream generated;
92  std::stringstream parsed;
93 
94  std::cout << "Generating " << "and.edf" << " ... ";
95  try {
96  // create the object factory
97  boost::shared_ptr<ObjectFactory> objectFactoryPtr(new ObjectFactory());
98 
99  // create the design root
100  RootSharedPtr rootPtr = objectFactoryPtr->newRootPtr("and");
101  rootPtr->setOriginalName("AND");
102 
103  // create the VIRTEX and work libraries
104  LibrarySharedPtr virtexLibraryPtr = objectFactoryPtr->newLibraryPtr("VIRTEX", rootPtr);
105  LibrarySharedPtr workLibraryPtr = objectFactoryPtr->newLibraryPtr("work", rootPtr);
106 
107  // create the LUT2 cell and its PRIM view
108  CellSharedPtr lut2CellPtr = objectFactoryPtr->newCellPtr("LUT2", virtexLibraryPtr);
109  ViewSharedPtr lut2ViewPtr = objectFactoryPtr->newViewPtr("PRIM", lut2CellPtr);
110  ScalarPortSharedPtr lut2I0PortPtr = objectFactoryPtr->newScalarPortPtr("I0",
111  ePortDirectionIn, lut2ViewPtr);
112  ScalarPortSharedPtr lut2I1PortPtr = objectFactoryPtr->newScalarPortPtr("I1",
113  ePortDirectionIn, lut2ViewPtr);
114  ScalarPortSharedPtr lut2OPortPtr = objectFactoryPtr->newScalarPortPtr("O",
115  ePortDirectionOut, lut2ViewPtr);
116 
117  // create the and cell and its PRIM view
118  CellSharedPtr andCellPtr = objectFactoryPtr->newCellPtr("and", workLibraryPtr,
119  Cell::eTypeGeneric, "AND");
120  ViewSharedPtr andViewPtr = objectFactoryPtr->newViewPtr("verilog", andCellPtr);
121  ScalarPortSharedPtr andI0PortPtr = objectFactoryPtr->newScalarPortPtr("I0",
122  ePortDirectionIn, andViewPtr);
123  ScalarPortSharedPtr andI1PortPtr = objectFactoryPtr->newScalarPortPtr("I1",
124  ePortDirectionIn, andViewPtr);
125  ScalarPortSharedPtr andOPortPtr = objectFactoryPtr->newScalarPortPtr("O",
126  ePortDirectionOut, andViewPtr);
127 
128  // create the oZ0 instance of type VIRTEX:LUT2:PRIM
129  SingleInstanceSharedPtr oZ0InstancePtr = objectFactoryPtr->newSingleInstancePtr("oZ0",
130  andViewPtr, lut2ViewPtr, "o");
131  ScalarPortReferenceSharedPtr oZ0I0PortReferencePtr
132  = objectFactoryPtr->newScalarPortReferencePtr(oZ0InstancePtr, lut2I0PortPtr);
133  ScalarPortReferenceSharedPtr oZ0I1PortReferencePtr
134  = objectFactoryPtr->newScalarPortReferencePtr(oZ0InstancePtr, lut2I1PortPtr);
135  ScalarPortReferenceSharedPtr oZ0OPortReferencePtr
136  = objectFactoryPtr->newScalarPortReferencePtr(oZ0InstancePtr, lut2OPortPtr);
137 
138  // create the i0 net
139  ScalarNetSharedPtr i0NetPtr = objectFactoryPtr->newScalarNetPtr("i0", andViewPtr);
140  andI0PortPtr->connect(i0NetPtr);
141  oZ0I0PortReferencePtr->connect(i0NetPtr);
142  ScalarNetSharedPtr i1NetPtr = objectFactoryPtr->newScalarNetPtr("i1", andViewPtr);
143  andI1PortPtr->connect(i1NetPtr);
144  oZ0I1PortReferencePtr->connect(i1NetPtr);
145  ScalarNetSharedPtr oNetPtr = objectFactoryPtr->newScalarNetPtr("o", andViewPtr);
146  andOPortPtr->connect(oNetPtr);
147  oZ0OPortReferencePtr->connect(oNetPtr);
148 
149  // attach the INIT 8 property to the oZ0 instance
150  PropertySharedPtr initPropertyPtr = objectFactoryPtr->newPropertyPtr("INIT", oZ0InstancePtr,
152 (void) initPropertyPtr;
153 
154  // instantiate the design
155  DesignSharedPtr designPtr = objectFactoryPtr->newDesignPtr("and", rootPtr, "and", "work",
156  "AND");
157 
158  // attach the PART xc5vlx30ff324-1 property to the and design instance
159  PropertySharedPtr partPropertyPtr = objectFactoryPtr->newPropertyPtr("PART", designPtr,
160  Value(Value::eValueTypeString, std::string("xc5vlx30ff324-1")));
161  partPropertyPtr->setOwner("Xilinx");
162 
163  std::cout << "done." << std::endl;
164 // PrintInfo info(generated);
165 // rootPtr->accept(info);
166 
167  Decompiler decompiler(rootPtr);
168  decompiler();
169 
170  // now insert inverter pairs on each net
171 // VisitNet visitNet;
172 // andViewPtr->applyOnAllNets(visitNet);
173 
174  } catch(Error& e) {
175  std::cerr << MessageTable::instance()->getMessage(e.getErrorMessageId()) << std::endl;
176  const std::vector<Error::StackFrameInfo>& stack = e.getStackTrace();
177  for(std::vector<Error::StackFrameInfo>::const_iterator it = stack.begin();
178  it != stack.end(); it++) {
179  std::cerr << " " << (*it).getFunction() << "() [" << (*it).getFile() << ":"
180  << (*it).getLine() << "]" << std::endl;
181  }
182  }
183 /*
184  try {
185  std::cout << "Parsing " << "and.edf" << " ... ";
186  boost::shared_ptr<ObjectFactory> factoryPtr(new ObjectFactory());
187  boost::shared_ptr<Root> rootPtr;
188  factoryPtr->create(rootPtr);
189  boost::shared_ptr<Linker> linkerPtr(new Linker(rootPtr));
190  ParserOptions options;
191  EdifParser parser;
192  parser.parse("and.edf", rootPtr, linkerPtr, factoryPtr, options);
193  std::cout << "done." << std::endl;
194  //PrintInfo info(parsed);
195  //rootPtr->accept(info);
196 // Decompiler decompiler(rootPtr);
197 // decompiler();
198  } catch(Error& e) {
199  std::cerr << MessageTable::instance()->getMessage(e.getErrorMessageId()) << std::endl;
200  const std::vector<Error::StackFrameInfo>& stack = e.getStackTrace();
201  for(std::vector<Error::StackFrameInfo>::const_iterator it = stack.begin();
202  it != stack.end(); it++) {
203  std::cerr << " " << (*it).getFunction() << "() [" << (*it).getFile() << ":"
204  << (*it).getLine() << "]" << std::endl;
205  }
206  }
207 */
208  // verify that the generated and parsed outputs are identical
209  BOOST_CHECK_EQUAL(generated.str(), parsed.str());
210 
211 }
212 
213 BOOST_AUTO_TEST_SUITE_END()
214 
215 } // namespace generic
216 } // namespace torc
217 
218 
219 #if 0
220 (edif (rename and "AND")
221  (edifVersion 2 0 0)
222  (edifLevel 0)
223  (keywordMap (keywordLevel 0))
224  (status
225  (written
226  (timeStamp 2010 8 30 21 51 32)
227  (author "Synplicity, Inc.")
228  (program "Synplify Pro" (version "C-2009.06-SP1, mapper map450rcp1sp1, Build 037R"))
229  )
230  )
231  (library VIRTEX
232  (edifLevel 0)
233  (technology (numberDefinition ))
234  (cell LUT2 (cellType GENERIC)
235  (view PRIM (viewType NETLIST)
236  (interface
237  (port I0 (direction INPUT))
238  (port I1 (direction INPUT))
239  (port O (direction OUTPUT))
240  )
241  )
242  )
243  )
244  (library work
245  (edifLevel 0)
246  (technology (numberDefinition ))
247  (cell (rename and "AND") (cellType GENERIC)
248  (view verilog (viewType NETLIST)
249  (interface
250  (port i0 (direction INPUT))
251  (port i1 (direction INPUT))
252  (port o (direction OUTPUT))
253  )
254  (contents
255  (instance (rename oZ0 "o") (viewRef PRIM (cellRef LUT2 (libraryRef VIRTEX)))
256  (property INIT (string "8"))
257  )
258  (net i0 (joined
259  (portRef i0)
260  (portRef I0 (instanceRef oZ0))
261  ))
262  (net i1 (joined
263  (portRef i1)
264  (portRef I1 (instanceRef oZ0))
265  ))
266  (net o (joined
267  (portRef O (instanceRef oZ0))
268  (portRef o)
269  ))
270  )
271  )
272  )
273  )
274  (design (rename and "AND") (cellRef and (libraryRef work))
275  (property PART (string "xc5vlx30ff324-1") (owner "Xilinx")))
276 )
277 #endif
Bison stack class.
static MessageTable * instance()
boost::shared_ptr< ScalarPort > ScalarPortSharedPtr
boost::shared_ptr< ScalarNet > ScalarNetSharedPtr
boost::shared_ptr< Net > NetSharedPtr
Definition: Om1UnitTest.cpp:38
BOOST_AUTO_TEST_CASE(Om1UnitTest)
Unit test for the generic object model.
Definition: Om1UnitTest.cpp:88
boost::shared_ptr< ScalarPortReference > ScalarPortReferenceSharedPtr
std::string string
boost::shared_ptr< Design > DesignSharedPtr
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
const std::vector< StackFrameInfo > & getStackTrace() const
Definition: Error.hpp:175
boost::shared_ptr< Library > LibrarySharedPtr
boost::shared_ptr< PortReference > PortReferenceSharedPtr
const MessageId getErrorMessageId() const
Definition: Error.hpp:195
boost::shared_ptr< View > ViewSharedPtr
boost::shared_ptr< Cell > CellSharedPtr
boost::shared_ptr< Port > PortSharedPtr
boost::shared_ptr< Property > PropertySharedPtr
boost::shared_ptr< SingleInstance > SingleInstanceSharedPtr
std::string getMessage(MessageId inId) const
boost::shared_ptr< Root > RootSharedPtr