OpenASIP  2.0
SOPCBuilderFileGenerator.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2010 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file SOPCBuilderFileGenerator.cc
26  *
27  * Implementation of SOPCBuilderFileGenerator class.
28  *
29  * @author Otto Esko 2010 (otto.esko-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <fstream>
34 #include <cassert>
36 #include "PlatformIntegrator.hh"
37 #include "NetlistBlock.hh"
38 #include "NetlistPort.hh"
39 #include "Exception.hh"
40 #include "FileSystem.hh"
42 #include "MapTools.hh"
43 using std::ofstream;
44 using std::vector;
45 using std::map;
46 using std::endl;
47 
48 
51  "_hw.tcl";
53  "set_module_property";
55  "add_file";
62  "std.standard.all ieee.std_logic_1164.all ieee.std_logic_arith.all "
63  "work.globals.all work.util.all work.imem_mau.all";
65  "dev_family_g";
66 
67 
68 
69 
71  TCEString toplevelEntity,
72  const PlatformIntegrator* integrator):
73  ProjectFileGenerator(toplevelEntity, integrator),
74  clock_(new SOPCInterface(
75  SOPCInterface::SOPC_CLOCK_INT_NAME,
76  SOPCInterface::SOPC_CLOCK_INT_DECLR)),
77  export_(new SOPCInterface(
78  SOPCInterface::SOPC_EXPORT_INT_NAME,
79  SOPCInterface::SOPC_EXPORT_INT_DECLR)) {
80 
81  clock_->setProperty("ptfSchematicName", "\"\"");
84 
87 }
88 
89 
91 
92  if (clock_ != NULL) {
93  delete clock_;
94  }
95  if (export_ != NULL) {
96  delete export_;
97  }
99 }
100 
101 
102 void
104 
105  int masterInterfaces = countAvalonMMMasters();
106  if (masterInterfaces == 0) {
107  TCEString msg = "Couldn't find any Avalon MM Master interfaces.";
108  InvalidData exc(__FILE__, __LINE__, "SOPCBuilderFileGenerator", msg);
109  throw exc;
110  }
111 
113 
115  ofstream output;
116  output.open(outputFileName.c_str());
117  if (!output) {
118  TCEString msg =
119  "Couldn't open file " + outputFileName + " for writing";
120  IOException exc(__FILE__, __LINE__, "SOPCBuilderFileGenerator", msg);
121  throw exc;
122  }
123 
124  output << "# Generated by SOPCBuilderGenerator" << endl << endl;
125  writeModuleProperties(output);
126 
127  writeGenerics(output);
128 
129  writeFileList(output);
130 
131  writeInterfaces(output);
132 
133  output.close();
134 }
135 
136 
137 /**
138  * Counts how many Avalon MM Master interfaces toplevel netlis block has.
139  *
140  * Avalon MM interface is recognised by the address port. Address signal in
141  * first of the 2 mandatory signals in Avalon MM interface. Direction of the
142  * address port determines whether it belongs to a master or slave interface.
143  *
144  * @return Number of found Avalon MM Master interfaces
145  */
146 int
148 
149  const ProGe::NetlistBlock& top = integrator()->toplevelBlock();
150  TCEString addressPortName =
152  int found = 0;
153  for (size_t i = 0; i < top.portCount(); i++) {
154  TCEString portName = top.port(i).name();
155  if (portName.find(addressPortName) != TCEString::npos) {
156  if (top.port(i).direction() == ProGe::OUT) {
157  found++;
158  }
159  }
160  }
161  return found;
162 }
163 
164 
165 void
167 
169  stream
170  << "# Module properties" << endl
171  << property << " NAME " << toplevelEntity() << endl
172  << property << " VERSION 1.0" << endl
173  << property << " GROUP \"" << SOPC_DEFAULT_GROUP << "\"" << endl
174  << property << " DISPLAY_NAME " << toplevelEntity() << endl
175  << property << " LIBRARIES {" << SOPC_DEFAULT_VHDL_LIBS;
176 
177  if (integrator()->toplevelBlock().netlist().parameterCount() > 0) {
178  stream << " work." << toplevelEntity() << "_params.all";
179  }
180  stream << "}" << endl;
181 
182  TCEString toplevelFile =
183  integrator()->outputFilePath(toplevelEntity() + ".vhdl");
184  stream
185  << property << " TOP_LEVEL_HDL_FILE " << toplevelFile << endl
186  << property << " TOP_LEVEL_HDL_MODULE " << toplevelEntity() << endl
187  << property << " INSTANTIATE_IN_SYSTEM_MODULE true" << endl
188  << property << " EDITABLE false" << endl
189  << property << " SIMULATION_MODEL_IN_VERILOG false" << endl
190  << property << " SIMULATION_MODEL_IN_VHDL false" << endl
191  << property << " SIMULATION_MODEL_HAS_TULIPS false" << endl
192  << property << " SIMULATION_MODEL_IS_OBFUSCATED false" << endl
193  << endl;
194 }
195 
196 
197 void
199 
200  const ProGe::NetlistBlock& top = integrator()->toplevelBlock();
201  if (top.parameterCount() == 0) {
202  return;
203  }
204  stream << "# toplevel parameters" << endl;
205  for (size_t i = 0; i < top.parameterCount(); i++) {
206  TCEString line;
207  line << "add_parameter " << top.parameter(i).name() << " ";
208  if (top.parameter(i).type().lower() == "string") {
209  line << "STRING ";
210  if (!top.parameter(i).value().startsWith("\"")) line << "\"";
211  line << top.parameter(i).value();
212  if (!top.parameter(i).value().endsWith("\"")) line << "\"";
213  } else if (top.parameter(i).type().lower() == "integer") {
214  line << "INTEGER " << top.parameter(i).value();
215  } else {
216  // unknown type, ignore
217  continue;
218  }
219  stream << line << endl;
220  if (top.parameter(i).name().lower() == PI_DEVICE_FAMILY_GENERIC) {
221  stream << "set_parameter_property " << PI_DEVICE_FAMILY_GENERIC
222  << " DISPLAY_NAME \"Device family (change if necessary)\""
223  << endl;
224  }
225  }
226  stream << endl;
227 }
228 
229 void
231 
232  stream << "# module hdl files" << endl;
233  for (unsigned int i = 0; i < hdlFileList().size(); i++) {
234  stream << SOPC_ADD_FILE << " " << hdlFileList().at(i)
235  << " {SYNTHESIS SIMULATION}" << endl;
236  }
237  for (unsigned int i = 0; i < memInitFileList().size(); i++) {
238  stream << SOPC_ADD_FILE << " " << memInitFileList().at(i)
239  << " {SYNTHESIS SIMULATION}" << endl;
240  }
241  stream << endl;
242 }
243 
244 
245 void
247 
248  const ProGe::NetlistBlock& top = integrator()->toplevelBlock();
249  for (size_t i = 0; i < top.portCount(); i++) {
250  TCEString portName = top.port(i).name();
251 
252  bool needToExport = true;
253  if (portName == TTA_CLOCK_NAME || portName == TTA_RESET_NAME) {
254  continue;
255  } else if (portName.find(HDB_AVALON_PREFIX) != TCEString::npos) {
256  needToExport = !handleAvalonSignal(top.port(i));
257  }
258 
259  if (needToExport) {
260  exportSignal(top.port(i));
261  }
262  }
263 }
264 
265 
266 bool
268  const ProGe::NetlistPort& port) {
269 
270  TCEString fuName = extractFUName(port.name(), HDB_AVALON_PREFIX);
271 
272  if (fuName.empty()) {
273  TCEString msg = "Failed to extract FU name from: " + port.name();
274  InvalidData exc(__FILE__, __LINE__, "SOPCBuilderFileGenerator", msg);
275  throw exc;
276  }
277 
278  if (!MapTools::containsKey(masters_ ,fuName)) {
279  TCEString interfaceName =
282  masters_[fuName] = new AvalonMMMasterInterface(
283  interfaceName, declr, HDB_AVALON_PREFIX, *clock_);
284  }
285 
286  AvalonMMMasterInterface* master = getMaster(fuName);
287  bool isAvalonPort = false;
288  if (!master->isValidPort(port)) {
289  isAvalonPort = false;
290  } else {
291  master->addPort(port);
292  isAvalonPort = true;
293  }
294  return isAvalonPort;
295 }
296 
297 
298 void
300 
301  int width = 32;
302  if (port.realWidthAvailable()) {
303  width = port.realWidth();
304  }
306  export_->setPort(port.name(), intPortName, port.direction(), width);
307 }
308 
309 
310 void
311 SOPCBuilderFileGenerator::writeInterfaces(std::ostream& stream) const {
312 
313  clock_->writeInterface(stream);
314 
315  std::map<TCEString, AvalonMMMasterInterface*>::const_iterator iter =
316  masters_.begin();
317  while (iter != masters_.end()) {
318  AvalonMMMasterInterface* master = iter->second;
319  if (master->hasPorts()) {
320  master->writeInterface(stream);
321  }
322  iter++;
323  }
324  if (export_->hasPorts()) {
325  export_->writeInterface(stream);
326  }
327 }
328 
329 
330 
331 
332 
335 
336  std::map<TCEString, AvalonMMMasterInterface*>::iterator iter =
337  masters_.find(fuName);
338 
339  assert(iter != masters_.end());
340  return iter->second;
341 }
ProjectFileGenerator::integrator
const PlatformIntegrator * integrator() const
Definition: ProjectFileGenerator.cc:101
SOPCInterface::hasPorts
bool hasPorts() const
Definition: SOPCInterface.cc:104
SOPCInterface::setProperty
void setProperty(const TCEString &propertyName, const TCEString &propertyValue)
Definition: SOPCInterface.cc:73
SOPCBuilderFileGenerator::writeGenerics
void writeGenerics(std::ostream &stream)
Definition: SOPCBuilderFileGenerator.cc:198
FileSystem.hh
AvalonMMMasterInterface::addPort
void addPort(const ProGe::NetlistPort &port)
Definition: AvalonMMMasterInterface.cc:106
SOPCBuilderFileGenerator::~SOPCBuilderFileGenerator
virtual ~SOPCBuilderFileGenerator()
Definition: SOPCBuilderFileGenerator.cc:90
TCEString::lower
TCEString lower() const
Definition: TCEString.cc:78
ProGe::NetlistBlock
Definition: NetlistBlock.hh:61
SOPCInterface::SOPC_ASSOCIATED_CLOCK
static const TCEString SOPC_ASSOCIATED_CLOCK
Definition: SOPCInterface.hh:70
SOPCBuilderFileGenerator::writeModuleProperties
void writeModuleProperties(std::ostream &stream)
Definition: SOPCBuilderFileGenerator.cc:166
outputFileName
static std::string outputFileName(const std::string &adfFile)
Definition: CreateBEM.cc:77
TCEString::startsWith
bool startsWith(const std::string &str) const
ProjectFileGenerator::extractFUName
TCEString extractFUName(const TCEString &port, const TCEString &delimiter) const
Definition: ProjectFileGenerator.cc:120
ProjectFileGenerator::toplevelEntity
TCEString toplevelEntity() const
Definition: ProjectFileGenerator.hh:66
Exception.hh
SOPCBuilderFileGenerator::SOPC_SET_MODULE_PROPERTY
static const TCEString SOPC_SET_MODULE_PROPERTY
Definition: SOPCBuilderFileGenerator.hh:90
SOPCBuilderFileGenerator::SOPC_ADD_FILE
static const TCEString SOPC_ADD_FILE
Definition: SOPCBuilderFileGenerator.hh:91
ProGe::BaseNetlistBlock::parameterCount
virtual size_t parameterCount() const
Definition: BaseNetlistBlock.cc:231
ProGe::Parameter::type
const TCEString & type() const
Definition: Parameter.cc:138
SOPCBuilderFileGenerator::exportSignal
void exportSignal(const ProGe::NetlistPort &port)
Definition: SOPCBuilderFileGenerator.cc:299
MapTools.hh
SOPCInterface
Definition: SOPCInterface.hh:41
ProGe::NetlistPort::direction
Direction direction() const
Definition: NetlistPort.cc:373
SOPCBuilderFileGenerator::writeProjectFiles
virtual void writeProjectFiles()
Definition: SOPCBuilderFileGenerator.cc:103
AvalonMMMasterInterface::AVALON_MM_ADDRESS
static const TCEString AVALON_MM_ADDRESS
Definition: AvalonMMMasterInterface.hh:56
MapTools::deleteAllValues
static void deleteAllValues(MapType &aMap)
SOPCBuilderFileGenerator::HDB_AVALON_PREFIX
static const TCEString HDB_AVALON_PREFIX
Definition: SOPCBuilderFileGenerator.hh:80
SOPCInterface::writeInterface
virtual void writeInterface(std::ostream &stream) const
Definition: SOPCInterface.cc:110
ProGe::NetlistBlock::portCount
virtual size_t portCount() const
Definition: BaseNetlistBlock.cc:248
AvalonMMMasterInterface.hh
SOPCBuilderFileGenerator::handleAvalonSignal
bool handleAvalonSignal(const ProGe::NetlistPort &port)
Definition: SOPCBuilderFileGenerator.cc:267
assert
#define assert(condition)
Definition: Application.hh:86
ProjectFileGenerator::memInitFileList
const std::vector< TCEString > & memInitFileList() const
Definition: ProjectFileGenerator.cc:94
AvalonMMMasterInterface::isValidPort
bool isValidPort(const ProGe::NetlistPort &port) const
Definition: AvalonMMMasterInterface.cc:100
SOPCInterface::SOPC_EXPORT_NAME
static const TCEString SOPC_EXPORT_NAME
Definition: SOPCInterface.hh:80
ProGe::NetlistPort::realWidthAvailable
bool realWidthAvailable() const
Definition: NetlistPort.cc:334
SOPCBuilderFileGenerator::createInterfaces
void createInterfaces()
Definition: SOPCBuilderFileGenerator.cc:246
InvalidData
Definition: Exception.hh:149
TCEString::endsWith
bool endsWith(const std::string &str) const
ProjectFileGenerator
Definition: ProjectFileGenerator.hh:41
SOPCInterface::setPort
void setPort(const TCEString &hdlName, const TCEString &interfaceName, ProGe::Direction direction, int width)
Definition: SOPCInterface.cc:90
NetlistPort.hh
SOPCInterface::SOPC_MASTER_INT_DECLR
static const TCEString SOPC_MASTER_INT_DECLR
Definition: SOPCInterface.hh:73
ProGe::Parameter::name
const TCEString & name() const
Definition: Parameter.cc:133
SOPCBuilderFileGenerator::masters_
std::map< TCEString, AvalonMMMasterInterface * > masters_
Definition: SOPCBuilderFileGenerator.hh:77
AvalonMMMasterInterface::writeInterface
virtual void writeInterface(std::ostream &stream) const
Definition: AvalonMMMasterInterface.cc:124
NetlistBlock.hh
SOPCBuilderFileGenerator::getMaster
AvalonMMMasterInterface * getMaster(const TCEString &fuName)
Definition: SOPCBuilderFileGenerator.cc:334
SOPCInterface::name
TCEString name() const
Definition: SOPCInterface.cc:98
ProGe::Parameter::value
const TCEString & value() const
Definition: Parameter.cc:143
SOPCBuilderFileGenerator::export_
SOPCInterface * export_
Definition: SOPCBuilderFileGenerator.hh:76
SOPCBuilderFileGenerator.hh
PlatformIntegrator::toplevelBlock
const ProGe::NetlistBlock & toplevelBlock() const
Definition: PlatformIntegrator.cc:537
SOPCBuilderFileGenerator::SOPCBuilderFileGenerator
SOPCBuilderFileGenerator(TCEString toplevelEntity, const PlatformIntegrator *integrator)
Definition: SOPCBuilderFileGenerator.cc:70
ProGe::NetlistPort::name
std::string name() const
Definition: NetlistPort.cc:283
SOPCBuilderFileGenerator::SOPC_DEFAULT_GROUP
static const TCEString SOPC_DEFAULT_GROUP
Definition: SOPCBuilderFileGenerator.hh:88
SOPCInterface::SOPC_MASTER_INT_NAME
static const TCEString SOPC_MASTER_INT_NAME
Definition: SOPCInterface.hh:72
ProGe::OUT
@ OUT
Output port.
Definition: ProGeTypes.hh:54
SOPCBuilderFileGenerator::writeFileList
void writeFileList(std::ostream &stream)
Definition: SOPCBuilderFileGenerator.cc:230
ProGe::NetlistPort::realWidth
int realWidth() const
Definition: NetlistPort.cc:348
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
AvalonMMMasterInterface
Definition: AvalonMMMasterInterface.hh:39
TCEString
Definition: TCEString.hh:53
PlatformIntegrator.hh
SOPCBuilderFileGenerator::TTA_RESET_NAME
static const TCEString TTA_RESET_NAME
Definition: SOPCBuilderFileGenerator.hh:84
ProGe::NetlistPort
Definition: NetlistPort.hh:70
SOPCBuilderFileGenerator::SOPC_CLOCK_NAME
static const TCEString SOPC_CLOCK_NAME
Definition: SOPCBuilderFileGenerator.hh:85
ProjectFileGenerator::hdlFileList
const std::vector< TCEString > & hdlFileList() const
Definition: ProjectFileGenerator.cc:87
PlatformIntegrator
Definition: PlatformIntegrator.hh:65
SOPCBuilderFileGenerator::SOPC_DEFAULT_VHDL_LIBS
static const TCEString SOPC_DEFAULT_VHDL_LIBS
Definition: SOPCBuilderFileGenerator.hh:89
IOException
Definition: Exception.hh:130
PlatformIntegrator::outputFilePath
TCEString outputFilePath(TCEString fileName, bool absolute=false) const
Definition: PlatformIntegrator.cc:154
SOPCBuilderFileGenerator::countAvalonMMMasters
int countAvalonMMMasters() const
Definition: SOPCBuilderFileGenerator.cc:147
SOPCBuilderFileGenerator::clock_
SOPCInterface * clock_
Definition: SOPCBuilderFileGenerator.hh:75
SOPCBuilderFileGenerator::PI_DEVICE_FAMILY_GENERIC
static const TCEString PI_DEVICE_FAMILY_GENERIC
Definition: SOPCBuilderFileGenerator.hh:93
ProGe::NetlistBlock::parameter
virtual const Parameter & parameter(const std::string &name) const
Definition: BaseNetlistBlock.cc:198
ProGe::NetlistBlock::port
virtual NetlistPort * port(const std::string &portName, bool partialMatch=true)
Definition: NetlistBlock.cc:97
SOPCBuilderFileGenerator::SOPC_RESET_NAME
static const TCEString SOPC_RESET_NAME
Definition: SOPCBuilderFileGenerator.hh:86
ProGe::IN
@ IN
Input port.
Definition: ProGeTypes.hh:53
SOPCBuilderFileGenerator::SOPC_COMPONENT_FILE_TYPE
static const TCEString SOPC_COMPONENT_FILE_TYPE
Definition: SOPCBuilderFileGenerator.hh:81
SOPCBuilderFileGenerator::writeInterfaces
void writeInterfaces(std::ostream &stream) const
Definition: SOPCBuilderFileGenerator.cc:311
SOPCBuilderFileGenerator::TTA_CLOCK_NAME
static const TCEString TTA_CLOCK_NAME
Definition: SOPCBuilderFileGenerator.hh:83