OpenASIP  2.0
MemoryGenerator.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 MemoryGenerator.cc
26  *
27  * Implementation of MemoryGenerator class.
28  *
29  * @author Otto Esko 2010 (otto.esko-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <iostream>
34 #include <map>
35 #include "MemoryGenerator.hh"
36 #include "PlatformIntegrator.hh"
37 #include "NetlistBlock.hh"
38 #include "VirtualNetlistBlock.hh"
39 #include "NetlistPort.hh"
40 #include "Exception.hh"
41 #include "HDLPort.hh"
42 #include "FileSystem.hh"
43 #include "Environment.hh"
45 #include "FUArchitecture.hh"
46 #include "FUImplementation.hh"
47 #include "FUExternalPort.hh"
48 #include "FunctionUnit.hh"
49 #include "InverterBlock.hh"
52 using ProGe::NetlistPort;
55 
58 
60  int mauWidth, int widthInMaus, int addrWidth, TCEString initFile,
61  const PlatformIntegrator* integrator, std::ostream& warningStream,
62  std::ostream& errorStream)
63  : mauWidth_(mauWidth),
64  widthInMaus_(widthInMaus),
65  addrWidth_(addrWidth),
66  initFile_(initFile),
67  integrator_(integrator),
68  warningStream_(warningStream),
69  errorStream_(errorStream),
70  lsuArch_(NULL),
71  lsuPorts_() {}
72 
74 
75  for (PortMap::iterator i = memPorts_.begin(); i != memPorts_.end(); i++) {
76  if (i->second != NULL)
77  delete i->second;
78  }
79 }
80 
81 bool
83  const ProGe::NetlistBlock& ttaCore, int coreId,
84  std::vector<TCEString>& reasons) const {
85  for (std::string port : lsuPorts_) {
86  if (!checkFuPort(port, reasons)) {
87  return false;
88  }
89  }
90  bool foundAll = true;
91  PortMap::const_iterator iter = memPorts_.begin();
92  while (iter != memPorts_.end()) {
93  TCEString corePort = corePortName(iter->first, coreId);
94  if (ttaCore.port(corePort) == NULL) {
95  TCEString message = "Couldn't find port " + corePort +
96  " from toplevel";
97  reasons.push_back(message);
98  foundAll = false;
99  }
100  iter++;
101  }
102  return foundAll;
103 }
104 
105 bool
107  const std::string fuPort, std::vector<TCEString>& reasons) const {
108  PortMap::const_iterator iter = memPorts_.find(fuPort);
109  if (iter == memPorts_.end()) {
110  TCEString msg;
111  msg << "MemoryGenerator does not have port " << fuPort;
112  reasons.push_back(msg);
113  return false;
114  }
115  return true;
116 }
117 
118 void
120  const ProGe::NetlistBlock& ttaCore, ProGe::NetlistBlock& integratorBlock,
121  int memIndex, int coreId) {
122  BlockPair blocks =
123  createMemoryNetlistBlock(integratorBlock, memIndex, coreId);
124  NetlistBlock* mem = blocks.first;
125  VirtualNetlistBlock* virt = blocks.second;
126  assert(mem != NULL);
127  assert(virt != NULL);
128  if (virt->portCount() > 0) {
129  integratorBlock.addSubBlock(virt);
130  }
131 
132  for (int i = 0; i < portCount(); i++) {
133  const HDLPort* hdlPort = port(i);
134  NetlistPort* memPort = mem->port(hdlPort->name());
135  if (memPort == NULL) {
136  memPort = virt->port(hdlPort->name());
137  if (memPort == NULL) {
138  TCEString msg = "Port ";
139  msg << hdlPort->name() << " not found from netlist block";
140  throw InvalidData(__FILE__, __LINE__, "MemoryGenerator", msg);
141  }
142  }
143 
144  TCEString portName = corePortName(portKeyName(hdlPort), coreId);
145  const NetlistPort* corePort = NULL;
146  // clock and reset must be connected to new toplevel ports
147  if (portName == platformIntegrator()->clockPort()->name()) {
148  corePort = platformIntegrator()->clockPort();
149  } else if (portName == platformIntegrator()->resetPort()->name()) {
150  corePort = platformIntegrator()->resetPort();
151  } else {
152  corePort = ttaCore.port(portName);
153  }
154  assert(corePort != NULL);
155 
156  connectPorts(
157  integratorBlock, *memPort, *corePort, hdlPort->needsInversion(),
158  coreId);
159  }
160 }
161 
164  ProGe::NetlistBlock& integratorBlock, int memIndex, int coreId) {
165  VirtualNetlistBlock* staticConnectionsBlock = new VirtualNetlistBlock(
166  moduleName() + "_virt", instanceName(coreId, memIndex) + "_virt");
167 
168  NetlistBlock* mem =
169  new NetlistBlock(moduleName(), instanceName(coreId, memIndex));
170  integratorBlock.addSubBlock(mem);
171 
172  for (int i = 0; i < parameterCount(); i++) {
173  mem->setParameter(parameter(i));
174  }
175  for (int i = 0; i < portCount(); i++) {
176  const HDLPort* hdlPort = port(i);
177  NetlistPort* memPort = NULL;
178  if (hdlPort->hasStaticValue()) {
179  memPort = hdlPort->convertToNetlistPort(*staticConnectionsBlock);
180  } else {
181  memPort = hdlPort->convertToNetlistPort(*mem);
182  }
183  assert(memPort != NULL);
184  }
185 
186  BlockPair blocks;
187  blocks.first = mem;
188  blocks.second = staticConnectionsBlock;
189  return blocks;
190 }
191 
192 int
194 
195  return mauWidth_*widthInMaus_;
196 }
197 
198 int
200 
201  return mauWidth_;
202 }
203 
204 int
206 
207  return widthInMaus_;
208 }
209 
210 
211 int
213 
214  return addrWidth_;
215 }
216 
217 
218 TCEString
220 
221  return initFile_;
222 }
223 
224 const PlatformIntegrator*
226 
227  return integrator_;
228 }
229 
230 std::ostream&
232 
233  return warningStream_;
234 }
235 
236 std::ostream&
238 
239  return errorStream_;
240 }
241 
242 int
244 
245  return memPorts_.size();
246 }
247 
248 const HDLPort*
249 MemoryGenerator::port(int index) const {
250 
251  if (index > static_cast<int>(memPorts_.size())) {
252  TCEString message = "Index out of range";
253  throw OutOfRange(__FILE__, __LINE__, "MemoryGenerator", message);
254  }
255  PortMap::const_iterator iter = memPorts_.begin();
256  for (int i = 0; i < index; i++) {
257  iter++;
258  }
259  return iter->second;
260 }
261 
262 
263 const HDLPort*
265 
266  if (memPorts_.find(name) == memPorts_.end()) {
267  TCEString message = "Port " + name + " not found";
268  throw KeyNotFound(__FILE__, __LINE__, "MemoryGenerator", message);
269  }
270  return memPorts_.find(name)->second;
271 }
272 
273 
274 TCEString
276 
277  TCEString name = "";
278  PortMap::const_iterator iter = memPorts_.begin();
279  while (iter != memPorts_.end()) {
280  if (iter->second == port) {
281  name = iter->first;
282  break;
283  }
284  iter++;
285  }
286  if (name.empty()) {
287  TCEString message = "Key for port " + port->name() + " not found";
288  throw KeyNotFound(__FILE__, __LINE__, "MemoryGenerator", message);
289  }
290  return name;
291 }
292 
293 void
295 
296  assert(port != NULL);
297  memPorts_.insert(std::pair<TCEString, HDLPort*>(name, port));
298 }
299 
300 
301 int
303 
304  return params_.size();
305 }
306 
307 const ProGe::Parameter&
308 MemoryGenerator::parameter(int index) const {
309  return params_.at(index);
310 }
311 
312 void
314  ProGe::Parameter toAdd(add.name(), add.type(), add.value());
315  params_.push_back(toAdd);
316 }
317 
318 TCEString
320 
322 }
323 
324 TCEString
326 
327  TCEString path = Environment::dataDirPath("ProGe");
328  path << FileSystem::DIRECTORY_SEPARATOR << "platform";
329  return path;
330 }
331 
332 void
334  const TCEString& inFile,
335  const TCEString& outFile,
336  const TCEString& entity) const {
337 
339  inst.setEntityString(entity);
340  inst.instantiateTemplateFile(inFile, outFile);
341 }
342 
343 bool
345  return lsuArch_ != NULL;
346 }
347 
350 
351  assert(lsuArch_ != NULL);
352  return *lsuArch_;
353 }
354 
355 TCEString
357  const TCEString& portBaseName, int coreId) const {
358  // clock and reset port names are global
359  if (portBaseName == integrator_->clockPort()->name() ||
360  portBaseName == integrator_->resetPort()->name()) {
361  return portBaseName;
362  }
363 
364  TCEString portName;
365  if (coreId >= 0) {
366  portName << "core" << coreId << "_";
367  }
368  if (lsuArch_ != NULL) {
369  portName << "fu_" << lsuArchitecture().name() << "_";
370  }
371  portName << portBaseName;
372  return portName;
373 }
374 
375 void
377  TTAMachine::FunctionUnit& lsuArch, std::vector<std::string> lsuPorts) {
378  lsuArch_ = &lsuArch;
379  lsuPorts_ = lsuPorts;
380 }
381 
382 TCEString
383 MemoryGenerator::memoryIndexString(int coreId, int memIndex) const {
384  TCEString index;
385  if (coreId >= 0) {
386  index << coreId << "_";
387  }
388  return index << memIndex;
389 }
390 
391 void
393  ProGe::NetlistBlock& netlistBlock, const ProGe::NetlistPort& memPort,
394  const ProGe::NetlistPort& corePort, bool inverted, int /*coreId*/) {
395  if (inverted) {
396  ProGe::InverterBlock* InvertedBlock =
397  new ProGe::InverterBlock(corePort, memPort);
398  netlistBlock.addSubBlock(InvertedBlock);
399 
400  netlistBlock.netlist().connect(
401  corePort, InvertedBlock->inputPort(), 0, 0, 1);
402  netlistBlock.netlist().connect(
403  InvertedBlock->outputPort(), memPort, 0, 0, 1);
404 
405  } else {
406  if (memPort.dataType() == corePort.dataType()) {
407  netlistBlock.netlist().connect(memPort, corePort);
408  } else {
409  // bit to bit vector connection, connect lowest bits
410  netlistBlock.netlist().connect(memPort, corePort, 0, 0, 1);
411  }
412  }
413 }
ProGe::InverterBlock
Definition: InverterBlock.hh:47
HDB::FUArchitecture
Definition: FUArchitecture.hh:55
HDB::FUExternalPort
Definition: FUExternalPort.hh:52
MemoryGenerator::platformIntegrator
const PlatformIntegrator * platformIntegrator() const
Definition: MemoryGenerator.cc:225
MemoryGenerator::mauWidth_
int mauWidth_
Definition: MemoryGenerator.hh:217
ProGe::NetlistBlock::netlist
virtual const Netlist & netlist() const
Definition: BaseNetlistBlock.cc:348
MemoryGenerator::widthInMaus_
int widthInMaus_
Definition: MemoryGenerator.hh:218
MemoryGenerator::port
const HDLPort * port(int index) const
Definition: MemoryGenerator.cc:249
PlatformIntegrator::clockPort
ProGe::NetlistPort * clockPort() const
Definition: PlatformIntegrator.cc:692
PlatformIntegrator::coreEntityName
TCEString coreEntityName() const
Definition: PlatformIntegrator.cc:126
MemoryGenerator::instanceName
virtual TCEString instanceName(int coreId, int memIndex) const =0
FileSystem.hh
MemoryGenerator::createMemoryNetlistBlock
virtual MemoryGenerator::BlockPair createMemoryNetlistBlock(ProGe::NetlistBlock &integratorBlock, int memIndex, int coreId)
Definition: MemoryGenerator.cc:163
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
ProGe::NetlistBlock
Definition: NetlistBlock.hh:61
MemoryGenerator::initializationFile
TCEString initializationFile() const
Definition: MemoryGenerator.cc:219
HDLTemplateInstantiator
Definition: HDLTemplateInstantiator.hh:45
MemoryGenerator::addPort
void addPort(const TCEString &name, HDLPort *port)
Definition: MemoryGenerator.cc:294
Exception.hh
HDLPort
Definition: PlatformIntegrator/HDLPort.hh:48
FUArchitecture.hh
ProGe::Parameter::type
const TCEString & type() const
Definition: Parameter.cc:138
OutOfRange
Definition: Exception.hh:320
MemoryGenerator.hh
MemoryGenerator::ttaCoreName
TCEString ttaCoreName() const
Definition: MemoryGenerator.cc:319
MemoryGenerator::MemoryGenerator
MemoryGenerator(int memMauWidth, int widthInMaus, int addrWidth, TCEString initFile, const PlatformIntegrator *integrator, std::ostream &warningStream, std::ostream &errorStream)
Definition: MemoryGenerator.cc:59
HDLTemplateInstantiator.hh
ProGe::NetlistBlock::setParameter
void setParameter(const std::string &name, const std::string &type, const std::string &value)
Definition: NetlistBlock.cc:89
MemoryGenerator::instantiateTemplate
void instantiateTemplate(const TCEString &inFile, const TCEString &outFile, const TCEString &entity) const
Definition: MemoryGenerator.cc:333
MemoryGenerator::lsuArchitecture
const TTAMachine::FunctionUnit & lsuArchitecture() const
Definition: MemoryGenerator.cc:349
MemoryGenerator::templatePath
TCEString templatePath() const
Definition: MemoryGenerator.cc:325
MemoryGenerator::~MemoryGenerator
virtual ~MemoryGenerator()
Definition: MemoryGenerator.cc:73
MemoryGenerator::memoryIndexString
TCEString memoryIndexString(int coreId, int memIndex) const
Definition: MemoryGenerator.cc:383
HDLPort::convertToNetlistPort
ProGe::NetlistPort * convertToNetlistPort(ProGe::NetlistBlock &block) const
Definition: HDLPort.cc:127
ProGe::NetlistBlock::portCount
virtual size_t portCount() const
Definition: BaseNetlistBlock.cc:248
MemoryGenerator::initFile_
TCEString initFile_
Definition: MemoryGenerator.hh:221
ProGe::Netlist::connect
bool connect(const NetlistPort &port1, const NetlistPort &port2, int port1FirstBit, int port2FirstBit, int width=1)
Definition: Netlist.cc:83
MemoryGenerator::addMemory
virtual void addMemory(const ProGe::NetlistBlock &ttaCore, ProGe::NetlistBlock &integratorBlock, int memIndex, int coreId)
Definition: MemoryGenerator.cc:119
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
MemoryGenerator::lsuPorts_
std::vector< std::string > lsuPorts_
Definition: MemoryGenerator.hh:232
InvalidData
Definition: Exception.hh:149
HDLPort::hasStaticValue
bool hasStaticValue() const
Definition: HDLPort.cc:153
PlatformIntegrator::resetPort
ProGe::NetlistPort * resetPort() const
Definition: PlatformIntegrator.cc:704
MemoryGenerator::CLOCK_PORT
static const TCEString CLOCK_PORT
Definition: MemoryGenerator.hh:234
MemoryGenerator::parameter
const ProGe::Parameter & parameter(int index) const
Definition: MemoryGenerator.cc:308
MemoryGenerator::isCompatible
virtual bool isCompatible(const ProGe::NetlistBlock &ttaCore, int coreId, std::vector< TCEString > &reasons) const
Definition: MemoryGenerator.cc:82
MemoryGenerator::portByKeyName
const HDLPort * portByKeyName(TCEString name) const
Definition: MemoryGenerator.cc:264
ProGe::Parameter
Definition: Parameter.hh:62
MemoryGenerator::checkFuPort
virtual bool checkFuPort(const std::string fuPort, std::vector< TCEString > &reasons) const
Definition: MemoryGenerator.cc:106
NetlistPort.hh
ProGe::Parameter::name
const TCEString & name() const
Definition: Parameter.cc:133
MemoryGenerator::RESET_PORT
static const TCEString RESET_PORT
Definition: MemoryGenerator.hh:235
NetlistBlock.hh
Environment.hh
HDLPort::name
TCEString name() const
Definition: HDLPort.cc:87
ProGe::Parameter::value
const TCEString & value() const
Definition: Parameter.cc:143
HDLPort.hh
ProGe::InverterBlock::outputPort
const NetlistPort & outputPort() const
Definition: InverterBlock.cc:61
MemoryGenerator::memPorts_
PortMap memPorts_
Definition: MemoryGenerator.hh:228
ProGe::VirtualNetlistBlock
Definition: VirtualNetlistBlock.hh:52
MemoryGenerator::memoryMauSize
int memoryMauSize() const
Definition: MemoryGenerator.cc:199
MemoryGenerator::addLsu
void addLsu(TTAMachine::FunctionUnit &lsuArch, std::vector< std::string > lsuPorts)
Definition: MemoryGenerator.cc:376
MemoryGenerator::BlockPair
std::pair< ProGe::NetlistBlock *, ProGe::VirtualNetlistBlock * > BlockPair
Definition: MemoryGenerator.hh:148
MemoryGenerator::moduleName
virtual TCEString moduleName() const =0
ProGe::NetlistPort::name
std::string name() const
Definition: NetlistPort.cc:283
InverterBlock.hh
MemoryGenerator::warningStream
std::ostream & warningStream()
Definition: MemoryGenerator.cc:231
FUImplementation.hh
MemoryGenerator::lsuArch_
TTAMachine::FunctionUnit * lsuArch_
Definition: MemoryGenerator.hh:231
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
ProGe::NetlistPort::dataType
DataType dataType() const
Definition: NetlistPort.cc:362
MemoryGenerator::corePortName
TCEString corePortName(const TCEString &portBaseName, int coreId) const
Definition: MemoryGenerator.cc:356
FUExternalPort.hh
MemoryGenerator::connectPorts
virtual void connectPorts(ProGe::NetlistBlock &netlistBlock, const ProGe::NetlistPort &memPort, const ProGe::NetlistPort &corePort, bool inverted, int coreId)
Definition: MemoryGenerator.cc:392
ProGe::NetlistBlock::addSubBlock
void addSubBlock(BaseNetlistBlock *subBlock, const std::string &instanceName="")
Definition: BaseNetlistBlock.cc:405
MemoryGenerator::memoryWidthInMaus
int memoryWidthInMaus() const
Definition: MemoryGenerator.cc:205
MemoryGenerator::portCount
int portCount() const
Definition: MemoryGenerator.cc:243
HDLTemplateInstantiator::setEntityString
void setEntityString(const TCEString &entityStr)
Definition: HDLTemplateInstantiator.hh:51
TCEString
Definition: TCEString.hh:53
PlatformIntegrator.hh
MemoryGenerator::hasLSUArchitecture
bool hasLSUArchitecture() const
Definition: MemoryGenerator.cc:344
MemoryGenerator::addParameter
void addParameter(const ProGe::Parameter &add)
Definition: MemoryGenerator.cc:313
MemoryGenerator::params_
ParameterList params_
Definition: MemoryGenerator.hh:229
VirtualNetlistBlock.hh
MemoryGenerator::warningStream_
std::ostream & warningStream_
Definition: MemoryGenerator.hh:225
ProGe::NetlistPort
Definition: NetlistPort.hh:70
PlatformIntegrator
Definition: PlatformIntegrator.hh:65
MemoryGenerator::errorStream_
std::ostream & errorStream_
Definition: MemoryGenerator.hh:226
MemoryGenerator::portKeyName
TCEString portKeyName(const HDLPort *port) const
Definition: MemoryGenerator.cc:275
ProGe::InverterBlock::inputPort
const NetlistPort & inputPort() const
Definition: InverterBlock.cc:56
KeyNotFound
Definition: Exception.hh:285
MemoryGenerator::errorStream
std::ostream & errorStream()
Definition: MemoryGenerator.cc:237
MemoryGenerator::memoryTotalWidth
int memoryTotalWidth() const
Definition: MemoryGenerator.cc:193
HDLTemplateInstantiator::instantiateTemplateFile
void instantiateTemplateFile(const std::string &templateFile, const std::string &dstFile)
Definition: HDLTemplateInstantiator.cc:113
MemoryGenerator::memoryAddrWidth
int memoryAddrWidth() const
Definition: MemoryGenerator.cc:212
MemoryGenerator::integrator_
const PlatformIntegrator * integrator_
Definition: MemoryGenerator.hh:223
MemoryGenerator::parameterCount
int parameterCount() const
Definition: MemoryGenerator.cc:302
ProGe::NetlistBlock::port
virtual NetlistPort * port(const std::string &portName, bool partialMatch=true)
Definition: NetlistBlock.cc:97
Environment::dataDirPath
static std::string dataDirPath(const std::string &prog)
Definition: Environment.cc:176
MemoryGenerator::addrWidth_
int addrWidth_
Definition: MemoryGenerator.hh:219
FunctionUnit.hh
HDLPort::needsInversion
bool needsInversion() const
Definition: HDLPort.cc:122