torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VerilogExporterVisitor.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 Source for the VerilogExporterVisitor class.
18 
22 #include <sstream>
23 
24 namespace torc {
25 namespace generic {
26 
27  using namespace std;
28 
30  // first try to look for a range directly in the name
31  string name = inVectorPort.getOriginalName();
32  size_t pos = name.find("[");
33  if(pos != string::npos) return name.substr(pos);
34 
35  // otherwise look up the vector range through its properties
36  PropertySharedPtr msbPtr
38  PropertySharedPtr lsbPtr
40  // hopefully we found the properties
41  if(msbPtr && lsbPtr) {
42  stringstream ss;
43  ss << "[" << msbPtr->getValue().get<Value::Integer>() << ":"
44  << lsbPtr->getValue().get<Value::Integer>() << "]";
45  return ss.str();
46  }
47 
48  // apparently neither the name nor the properties were set
49  return "[]";
50  }
51 
52  /// \brief Visit the top-level netlist.
53  void VerilogExporterVisitor::visit(Root& inRoot) throw (Error) {
54 // cerr << "Generic root: " << inRoot.getName() << endl;
55  inRoot.applyOnAllDesigns(mVisitor);
56  inRoot.applyOnAllLibraries(mVisitor);
57  }
58 
59  void VerilogExporterVisitor::visit(Design& inDesign) throw (Error) {
60 // cerr << "Generic design: " << inDesign.getName() << endl;
61  }
62 
63  void VerilogExporterVisitor::visit(Library& inLibrary) throw (Error) {
64  // we output nothing for inferred black boxes or cell libraries
65  if(inLibrary.getName() == VerilogNames::getInferredBlackBoxesLibraryName()) return;
66  if(inLibrary.getName() == VerilogNames::getImportedCellLibraryName()) return;
67  // process all other libraries
68 // cerr << "Generic library: " << inLibrary.getName() << endl;
69  inLibrary.applyOnAllCells(mVisitor);
70  }
71 
72  void VerilogExporterVisitor::visit(Cell& inCell) throw (Error) {
73  // declare the module
74  string name = getName(inCell);
75  mOut << "module " << name << " ";
76  inCell.applyOnAllViews(mVisitor);
77  mOut << "endmodule" << endl << endl;
78  }
79 
80  void VerilogExporterVisitor::visit(View& inView) throw (Error) {
81  // begin the port declaration
82  mOut << "(" << endl;
83  // look up the ports
84  PortSharedPtrVector ports;
85  inView.getPorts(ports);
86  // iterate through the ports
87  PortSharedPtrVector::const_iterator pp = ports.begin();
88  PortSharedPtrVector::const_iterator pe = ports.end();
89  while(pp < pe) {
90  // process each port
91  PortSharedPtr portPtr = *pp++;
92  mOut << mTab << portPtr->getName();
93  if(pp < pe) mOut << ",";
94  mOut << endl;
95  }
96  // end the port declaration
97  mOut << ");" << endl;
98 
99  inView.applyOnAllPorts(mVisitor);
100  inView.applyOnAllNets(mVisitor);
101  inView.applyOnAllInstances(mVisitor);
102  TemporaryAssignment<string> t(mPropertyContainerName, inView.getName());
103  inView.applyOnAllProperties(mVisitor);
104  }
105 
106  void VerilogExporterVisitor::visit(ScalarPort& inScalarPort) throw (Error) {
107  inScalarPort.applyOnAllProperties(mVisitor);
108  mOut << getDirection(inScalarPort) << " " << inScalarPort.getName() << ";" << endl;
109  }
110 
111  void VerilogExporterVisitor::visit(VectorPort& inVectorPort) throw (Error) {
112  inVectorPort.applyOnAllProperties(mVisitor);
113  mOut << getDirection(inVectorPort) << " " << getRange(inVectorPort) << " "
114  << inVectorPort.getName() << ";" << endl;
115  }
116 
117  void VerilogExporterVisitor::visit(VectorPortBit& inVectorPortBit) throw (Error) {
118  /// \todo Throw an exception.
119  }
120 
121  void VerilogExporterVisitor::visit(PortBundle& inPortBundle) throw (Error) {
122  /// \todo Throw an exception.
123  }
124 
125  void VerilogExporterVisitor::visit(ScalarNet& inScalarNet) throw (Error) {
126  // declare the net as a wire, unless it connects to a port
127  /// \todo Shouldn't we declare a wire unless the net contains an *input* port?
128  vector<PortSharedPtr> ports;
129  inScalarNet.getConnectedPorts(ports);
130  if(ports.size() == 0) {
131  PropertySharedPtr assignPtr
132  = inScalarNet.getProperty(VerilogNames::getTorcAssignRHSPropertyName());
133  mOut << "wire " << inScalarNet.getName();
134  if(assignPtr) mOut << " = " << assignPtr->getValue().get<Value::String>();
135  mOut << ";" << endl;
136  }
137  inScalarNet.applyOnAllProperties(mVisitor);
138  }
139 
140  void VerilogExporterVisitor::visit(VectorNet& inVectorNet) throw (Error) {}
141  void VerilogExporterVisitor::visit(VectorNetBit& inVectorNetBit) throw (Error) {}
142  void VerilogExporterVisitor::visit(NetBundle& inNetBundle) throw (Error) {}
143 
144  void VerilogExporterVisitor::visit(SingleInstance& inSingleInstance) throw (Error) {
145  // look up and export the instance information
146  CellSharedPtr cellPtr = inSingleInstance.getMaster()->getParent();
147  mOut << getName(*cellPtr) << " " << getName(inSingleInstance);
148 
149  // look up the port references
150  PortReferenceSharedPtrVector portReferences;
151  inSingleInstance.getPortReferences(portReferences);
152  bool hasPortReferences = portReferences.size() != 0;
153  PortReferenceSharedPtrVector::const_iterator prp = portReferences.begin();
154  PortReferenceSharedPtrVector::const_iterator pre = portReferences.end();
155  if(hasPortReferences) mOut << "(";
156  while(prp < pre) {
157  // process each port reference
158  PortReferenceSharedPtr portReferencePtr = *prp++;
159  //PortAttributesSharedPtr portAttributesPtr = portReferencePtr->getAttributes();
160  (portReferencePtr)->accept(*this);
161  if(prp < pre) mOut << ", ";
162  }
163  // end the port declaration
164  if(hasPortReferences) mOut << ")";
165  mOut << ";" << endl;
166  TemporaryAssignment<string> t(mPropertyContainerName, inSingleInstance.getName());
167  inSingleInstance.applyOnAllProperties(mVisitor);
168  }
169 
170  void VerilogExporterVisitor::visit(InstanceArray& inInstanceArray) throw (Error) {
171  /// \todo Throw an exception.
172  }
173 
174  void VerilogExporterVisitor::visit(InstanceArrayMember& inInstanceArrayMember) throw (Error) {
175  /// \todo Throw an exception.
176  }
177 
178  void VerilogExporterVisitor::visit(ScalarPortReference& inScalarPortRef) throw (Error) {
179 //mOut << "@";
180  // look up the master port name
181  string name = getName(*(inScalarPortRef.getMaster()));
182  mOut << "." << name << "(";
183  NetSharedPtrVector nets;
184  inScalarPortRef.getConnectedNets(nets);
185  NetSharedPtrVector::const_iterator np = nets.begin();
186  NetSharedPtrVector::const_iterator ne = nets.end();
187  if(nets.size() > 1) {} ///< \todo Throw an exception.
188  while(np < ne) {
189  NetSharedPtr netPtr = *np++;
190  mOut << getName(*netPtr);
191  break;
192  }
193  mOut << ")";
194  inScalarPortRef.applyOnAllProperties(mVisitor);
195  }
196 
197  void VerilogExporterVisitor::visit(VectorPortReference& inVectorPortRef) throw (Error) {
198 mOut << "[VECTORPORTREFERENCE]";
199 /*
200  NetSharedPtrVector nets;
201  inVectorPortRef.getConnectedNets(nets);
202  NetSharedPtrVector::const_iterator np = nets.begin();
203  NetSharedPtrVector::const_iterator ne = nets.end();
204  if(nets.size() > 1) {} ///< \todo Throw an exception.
205  while(np < ne) {
206  NetSharedPtr netPtr = *np++;
207  mOut << getName(*netPtr);
208  break;
209  }
210 */
211  }
212 
213  void VerilogExporterVisitor::visit(VectorPortBitReference& inVectorPortBitRef) throw (Error) {
214 mOut << "[VECTORPORTBITREFERENCE]";
215 /*
216  NetSharedPtrVector nets;
217  inVectorPortBitRef.getConnectedNets(nets);
218  NetSharedPtrVector::const_iterator np = nets.begin();
219  NetSharedPtrVector::const_iterator ne = nets.end();
220  if(nets.size() > 1) {} ///< \todo Throw an exception.
221  while(np < ne) {
222  NetSharedPtr netPtr = *np++;
223  mOut << getName(*netPtr) << "[";
224  copy(inVectorPortBitRef.getIndices().begin(), inVectorPortBitRef.getIndices().end(),
225  ostream_iterator<size_t>(mOut, ","));
226  mOut << "]";
227  break;
228  }
229 */
230  }
231 
232  void VerilogExporterVisitor::visit(PortBundleReference& inPortBundleRef) throw (Error) {
233  /// \todo Throw an exception.
234 mOut << "[PORTBUNDLEREFERENCE]";
235  }
236 
237  void VerilogExporterVisitor::visit(SingleParameter& inSingleParameter) throw (Error) {}
238  void VerilogExporterVisitor::visit(ParameterArray& inParamArray) throw (Error) {}
239  void VerilogExporterVisitor::visit(ParameterArrayElement& inParamArrayElement) throw (Error) {}
240 
241  void VerilogExporterVisitor::visit(Property& inProperty) throw (Error) {
242  /// \todo Handle properties.
243  const string& name = inProperty.getName();
244  const string torcPrefix = "torc_";
245  stringstream value;
246  Value propertyValue = inProperty.getValue();
247  switch(propertyValue.getType()) {
249  value << (propertyValue.get<Value::Boolean>() ? "true" : "false");
250  break;
252  value << propertyValue.get<Value::Integer>();
253  break;
255  value << "[MiNoMax]";
256  break;
258  value << propertyValue.get<Value::Number>().eval();
259  break;
261  value << "[point]";
262  break;
264  value << '"' << propertyValue.get<Value::String>() << '"';
265  break;
266  default:
267  value << "[unknown]";
268  break;
269  }
270  if(name.compare(0, torcPrefix.length(), torcPrefix) == 0) {
274  mOut << "// property " << name << endl;
275  } else {
276  mOut << "defparam " << mPropertyContainerName << "." << name << " = "
277  << value.str() << ";" << endl;
278  }
279  }
280 
281  void VerilogExporterVisitor::visit(PortList& inPortList) throw (Error) {}
282  void VerilogExporterVisitor::visit(PortListAlias& inPortListAlias) throw (Error) {}
283  void VerilogExporterVisitor::visit(Status& inStatus) throw (Error) {}
284  void VerilogExporterVisitor::visit(Permutable& inPermutable) throw (Error) {}
285  void VerilogExporterVisitor::visit(InterfaceJoinedInfo& inInterfaceJoinedInfo) throw (Error) {}
286  void VerilogExporterVisitor::visit(SimulationInfo& inSimulationInfo) throw (Error) {}
287  void VerilogExporterVisitor::visit(Simulate& inSimulate) throw (Error) {}
288  void VerilogExporterVisitor::visit(Apply& inApply) throw (Error) {}
289  void VerilogExporterVisitor::visit(LogicalResponse& inLogicalResponse) throw (Error) {}
290  void VerilogExporterVisitor::visit(LogicValue& inLogicValue) throw (Error) {}
291  void VerilogExporterVisitor::visit(LogicElement& inLogicElement) throw (Error) {}
292  void VerilogExporterVisitor::visit(WaveValue& inWaveValue) throw (Error) {}
293  void VerilogExporterVisitor::visit(Timing& inTiming) throw (Error) {}
294  void VerilogExporterVisitor::visit(Event& inEvent) throw (Error) {}
295  void VerilogExporterVisitor::visit(ForbiddenEvent& inForbiddenEvent) throw (Error) {}
296 
297 
298 } // namespace generic
299 } // namespace torc
Represents an EDIF cell.
Definition: Cell.hpp:55
Represents a bit of a net array.
static string getImportedCellLibraryName(void)
Returns the imported cell library name.
Header for the TemporaryAssignment class.
Represents areference to a standalone port.
Represents a reference to a port array.
static string getTorcAssignRHSPropertyName(void)
Property name for the wire assignment right-hand-side.
static string getTorcRangeMSBPropertyName(void)
Property name for the vector range MSB.
This class is used within simulate to describe input stimuli and expected responces over a certain ti...
Definition: Apply.hpp:37
Represents a bundle of ports.
Definition: PortBundle.hpp:44
Represents and EDIF View.
Definition: View.hpp:61
Represents a bit of a port.
Represents a member of an instance array.
An EDIF cell library.
Definition: Library.hpp:60
Permutable is used to describe a relationship in which ports are interchangeable. ...
Definition: Permutable.hpp:40
void visit(Root &inroot)
Visit the top-level netlist.
Represents a single instance of the view of a cell.
This class is used to hold all information about the logic values used within a library.
This class is used within simulationInfo construct to define a logic value to use for modeling in the...
Definition: LogicValue.hpp:42
std::string String
Definition: Value.hpp:61
static string getTorcRangeLSBPropertyName(void)
Property name for the vector range LSB.
Template class that stores the current value of a variable and restores that value when this object g...
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
Represents a port array.
Definition: VectorPort.hpp:45
boost::shared_ptr< Net > NetSharedPtr
Represents a parameter array.
boost::shared_ptr< PortReference > PortReferenceSharedPtr
Represents a net array.
Definition: VectorNet.hpp:42
Represents a standalone port.
Definition: ScalarPort.hpp:42
Represents the Interface joining information.
This class is used to provide a set of path delays or timing constrains (forbidden events) ...
Definition: Timing.hpp:40
const Type getType() const
Definition: Value.hpp:348
Represents an array of instances.
Represents a standalone net.
Definition: ScalarNet.hpp:42
std::vector< PortSharedPtr > PortSharedPtrVector
Vector of port shared pointers.
ForbiddenEvent class lists events which are forbidden during a period of times which is specified by ...
Represents different logic elements which holds array of logic values.
Represents an ordered list of port references with a name aliased.
Represents a reference to a bit of a port.
Represents an ordered list of port references.
Definition: PortList.hpp:43
Header for the VerilogExporterVisitor class.
Root of the EDIF Object Model.
Definition: Root.hpp:66
This class is used to model logicInput/logicOutput construct. This class holds information of logical...
This class is used within simulate to describe input stimuli and expected responces over a certain ti...
Definition: WaveValue.hpp:35
Represents a bundle of nets.
Definition: NetBundle.hpp:43
boost::shared_ptr< Cell > CellSharedPtr
Event is used to describe an event on a port or a net using logic state transitions. Events can also be described for unordered groups of ports or nets using portGroup or netGroup. An ordered list of ports may also be used using a portList.
Definition: Event.hpp:45
Header for the VerilogNames class.
std::vector< NetSharedPtr > NetSharedPtrVector
Vector of net shared pointers.
boost::shared_ptr< Port > PortSharedPtr
boost::shared_ptr< Property > PropertySharedPtr
PropertySharedPtr getProperty(const std::string &inName)
This class is to model simulate construct which is a named collection of simulation stimulus and resp...
Definition: Simulate.hpp:46
Represents EDIF status construct.
Definition: Status.hpp:42
Represents a reference to a bundle of ports.
_ValueType get() const
Definition: Value.hpp:386
static string getInferredBlackBoxesLibraryName(void)
Returns the inferred black box library name.
string getRange(VectorPort &inVectorPort)
Return a string describing the range of a vector port. The range is expressed in the form "[start:end...
virtual Name getOriginalName() const
std::vector< PortReferenceSharedPtr > PortReferenceSharedPtrVector
Vector of port reference shared pointers.