1 #include "GroupBlock.h"
2 #include "BlockParameterGeneric.h"
3 #include "AbstractInterface.h"
4 #include "ConnectedInterface.h"
5 #include "GroupInterface.h"
8 #include "Parameters.h"
10 int GroupBlock::counter = 1;
12 GroupBlock::GroupBlock(GroupBlock *_parent) throw(Exception) : AbstractBlock() {
14 GroupInterface* clk = NULL;
15 GroupInterface* rst = NULL;
17 // force topGroup to false if this group has a parent
18 if (_parent != NULL) {
20 name = QString("sub_group")+"_"+QString::number(counter++);
21 // creating clk/rst interfaces
22 clk = new GroupInterface(this,"clk", AbstractInterface::Input, AbstractInterface::Clock);
23 rst = new GroupInterface(this,"reset", AbstractInterface::Input, AbstractInterface::Reset);
31 AbstractBlock* source = (AbstractBlock *)(e.getSource());
32 cerr << qPrintable(source->getName()) << ":" << qPrintable(e.getMessage()) << endl;
38 name = QString("top_group");
39 // creating external clk/rst interfaces
40 clk = new GroupInterface(this,"ext_clk", AbstractInterface::Input, AbstractInterface::Clock);
41 rst = new GroupInterface(this,"ext_reset", AbstractInterface::Input, AbstractInterface::Reset);
44 // creating clkrstgen block and connecting it to this: done in Dispatcher since this has no access to library
48 if (_parent != NULL) {
53 AbstractBlock* source = (AbstractBlock *)(e.getSource());
54 cerr << qPrintable(source->getName()) << ":" << qPrintable(e.getMessage()) << endl;
61 GroupBlock::~GroupBlock() {
62 foreach(AbstractBlock* block, blocks) {
67 bool GroupBlock::isGroupBlock() {
71 bool GroupBlock::isTopGroupBlock() {
75 void GroupBlock::setParent(AbstractBlock *_parent) {
82 void GroupBlock::removeAllBlocks() {
83 foreach(AbstractBlock* block, blocks) {
84 if (block->isGroupBlock()) {
85 GroupBlock* group = AB_TO_GRP(block);
86 group->removeAllBlocks();
92 void GroupBlock::removeBlock(AbstractBlock* block) {
93 /* CAUTION: no check is done if the block has connected interface
94 or not. Thus, they must be deleted elsewhere.
96 blocks.removeAll(block);
100 AbstractBlock *GroupBlock::getFunctionalBlockByName(QString name) {
101 foreach(AbstractBlock* block, blocks) {
102 if (block->isFunctionalBlock()) {
103 if (block->getName() == name) return block;
109 void GroupBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
112 checkedBlocks->append(this);
114 foreach(BlockParameter* param, params){
115 if(param->isUserParameter() && !param->isValueSet()){
116 if(!blocksToConfigure->contains(param->getOwner())){
117 blocksToConfigure->append(param->getOwner());
121 foreach(AbstractInterface *inter, outputs){
122 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
123 if(!checkedBlocks->contains(connectedInter->getOwner())){
124 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
131 void GroupBlock::addGenericParameter(QString name, QString type, QString value) {
132 BlockParameter* param = new BlockParameterGeneric(this, name, type, value);
133 params.append(param);
136 void GroupBlock::removeGenericParameter(QString name) {
137 BlockParameter* p = getParameterFromName(name);
138 if (p != NULL) params.removeAll(p);
141 void GroupBlock::createInputPattern() {
142 foreach(AbstractInterface* iface, getControlInputs()) {
143 ConnectedInterface* connIface = AI_TO_CON(iface);
144 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
145 connIface->setOutputPattern(pattern);
149 void GroupBlock::computeAdmittanceDelays() throw(Exception) {
150 throw(Exception(INVALID_GROUPBLOCK_USE));
153 void GroupBlock::checkInputPatternCompatibility() throw(Exception){
154 throw(Exception(INVALID_GROUPBLOCK_USE));
158 void GroupBlock::computeOutputPattern(int nbExec) throw(Exception) {
160 static QString fctName = "GroupBlock::computeOutputPattern()";
162 cout << "call to " << qPrintable(fctName) << endl;
165 cout << "computing output pattern of group " << qPrintable(name) << endl;
167 bool canCompute = false;
168 // get the input pattern on each inputs
169 createInputPattern();
171 cout << "Input pattern OK" << endl;
172 // find blocks that are connected to that inputs and generators
173 QList<AbstractBlock*> fifo;
174 foreach(AbstractBlock* block, blocks) {
177 // if a block is a generator and has control outputs, add it
178 if (block->isGeneratorBlock()) {
179 if (block->getControlOutputs().size() > 0) addIt = true;
182 // if the block has all its connected control inputs that are connected to an intput of the group, add it too
183 if (block->getControlInputs().size() > 0) {
185 foreach(AbstractInterface* iface, block->getControlInputs()) {
186 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
187 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
188 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
190 if (connFrom == NULL) {
194 else if (connFrom->getOwner() != this) {
202 cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
203 block->setTraversalLevel(0); // level 0 = first blocks to be evaluated
208 while (!fifo.isEmpty()) {
209 AbstractBlock* block = fifo.takeFirst();
211 if (block->getPatternComputed()) continue; // block has already been processed
213 cout << "computing compat and output for " << qPrintable(block->getName()) << endl;
217 block->checkInputPatternCompatibility();
220 cout << qPrintable(block->getName()) << " is not compatible with its input pattern" << endl;
225 block->computeOutputPattern();
228 cout << "cannot finalize output pattern computation of " << qPrintable(block->getName()) << endl;
232 block->setPatternComputed(true);
233 /* add other blocks connected from block to the fifo but only if
234 all their connected inputs are connected to blocks that have
237 foreach(AbstractInterface* iface, block->getControlOutputs()) {
238 ConnectedInterface* conn = (ConnectedInterface*)iface;
239 foreach(ConnectedInterface* connTo, conn->getConnectedTo()) {
241 AbstractBlock* block1 = connTo->getOwner();
242 cout << "testing if " << qPrintable(block1->getName()) << " has all connected inputs connected to already processed blocks" << endl;
246 foreach(AbstractInterface* iface, block1->getControlInputs()) {
247 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
248 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
249 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
251 if ((connFrom != NULL) && (connFrom->getOwner()->getPatternComputed() == false)) {
256 if (connFrom->getOwner()->getTraversalLevel() > maxLevel) maxLevel = connFrom->getOwner()->getTraversalLevel();
261 cout << "adding " << qPrintable(block1->getName()) << " to the FIFO" << endl;
262 block1->setTraversalLevel(maxLevel+1); // level 0 = first blocks to be evaluated
270 foreach(AbstractInterface* iface, getControlOutputs()) {
271 ConnectedInterface* connIface = AI_TO_CON(iface);
272 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
273 connIface->setOutputPattern(pattern);
275 setPatternComputed(true);
279 void GroupBlock::generateVHDL(const QString& path) throw(Exception) {
281 QString coreFile = "";
284 coreFile.append(Parameters::normalizeName(name));
285 coreFile.append(".vhd");
287 QFile vhdlCore(coreFile);
289 if (!vhdlCore.open(QIODevice::WriteOnly)) {
290 throw(Exception(VHDLFILE_NOACCESS));
293 cout << "generate VHDL of block " << qPrintable(name) << " in " << qPrintable(coreFile) << endl;
294 QTextStream outCore(&vhdlCore);
296 QDomElement dummyElt;
298 generateComments(outCore,dummyElt,"");
299 generateLibraries(outCore,dummyElt);
300 generateEntity(outCore);
301 generateArchitecture(outCore,dummyElt);
303 catch(Exception err) {
311 void GroupBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
312 out << " -- VHDL generated automatically for " << name << " --" << endl << endl;
315 void GroupBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
317 out << "library IEEE;" << endl;
318 out << "use IEEE.STD_LOGIC_1164.all;" << endl;
319 out << "use IEEE.numeric_std.all;" << endl;
323 void GroupBlock::generateEntityOrComponentBody(QTextStream& out, int indentLevel, bool hasController) throw(Exception) {
327 for(i=0;i<indentLevel;i++) {
331 QList<BlockParameter*> listGenerics = getGenericParameters();
332 QList<AbstractInterface*> listInputs = getInputs();
333 QList<AbstractInterface*> listOutputs = getOutputs();
334 QList<AbstractInterface*> listBidirs = getBidirs();
336 if (!listGenerics.isEmpty()) {
337 out << indent << " generic (" << endl;
338 for(i=0;i<listGenerics.size()-1;i++) {
339 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
341 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
342 out << indent << " );" << endl;
345 out << indent << " port (" << endl;
347 // Generation of the clk & rst signals
348 out << indent << " -- clk/rst" << endl;
349 foreach(AbstractInterface* iface, listInputs) {
350 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
351 out << indent << " " << iface->getName() << " : in std_logic;" << endl;
356 foreach(AbstractInterface* iface, getInterfaces()) {
357 if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
359 // Generation of the data/control signals
364 foreach(AbstractInterface* iface, listInputs) {
365 if(iface->getPurpose() == AbstractInterface::Data) {
367 out << indent << " -- input data ports" << endl;
371 if (count == 0) flag = AbstractInterface::NoComma;
372 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
376 foreach(AbstractInterface* iface, listInputs) {
377 if(iface->getPurpose() == AbstractInterface::Control) {
379 out << indent << " -- input control ports" << endl;
383 if (count == 0) flag = AbstractInterface::NoComma;
384 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
388 foreach(AbstractInterface* iface, listOutputs) {
389 if(iface->getPurpose() == AbstractInterface::Data) {
391 out << indent << " -- output data ports" << endl;
395 if (count == 0) flag = AbstractInterface::NoComma;
396 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
400 foreach(AbstractInterface* iface, listOutputs) {
401 if(iface->getPurpose() == AbstractInterface::Control) {
403 out << indent << " -- output control ports" << endl;
407 if (count == 0) flag = AbstractInterface::NoComma;
408 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
412 foreach(AbstractInterface* iface, listBidirs) {
413 if(iface->getPurpose() == AbstractInterface::Data) {
415 out << indent << " -- bidirs data ports" << endl;
419 if (count == 0) flag = AbstractInterface::NoComma;
420 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
423 out << indent << " );" << endl << endl;
426 void GroupBlock::generateArchitecture(QTextStream& out, QDomElement &elt) throw(Exception) {
430 out << "architecture rtl of " << name << " is " << endl << endl;
432 // generate the components
433 foreach(AbstractBlock* block, blocks) {
435 block->generateComponent(out,false);
444 out << " ----------------------------" << endl;
445 out << " SIGNALS" << endl;
446 out << " ----------------------------" << endl << endl;
449 foreach(AbstractBlock* block, blocks) {
451 out << " -- signals from output ports of " << block->getName() << endl;
452 QList<AbstractInterface*> listOutputs = block->getOutputs();
453 foreach(AbstractInterface* iface, listOutputs) {
454 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
455 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
457 else if (block->getName() == "clkrstgen") {
458 if ((iface->getPurpose() == AbstractInterface::Clock)||(iface->getPurpose() == AbstractInterface::Reset)) {
459 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
469 foreach(AbstractBlock* block, blocks) {
471 out << " -- signals for modified input ports of " << block->getName() << endl;
472 QList<AbstractInterface*> listInputs = block->getInputs();
473 foreach(AbstractInterface* iface, listInputs) {
474 if (iface->getPurpose() == AbstractInterface::Control) {
475 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
476 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
477 if (modifier != NULL) {
478 out << modifier->toVHDL(AbstractInputModifier::Signal,0) << endl;
489 out << "begin" << endl;
491 // generate signals that goes to the output ports
493 out << " -- connections to output ports of " << name << endl;
494 QList<AbstractInterface*> listOutputs = getOutputs();
495 foreach(AbstractInterface* iface, listOutputs) {
496 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
497 ConnectedInterface* connIface = AI_TO_CON(iface);
498 ConnectedInterface* fromIface = connIface->getConnectedFrom();
499 out << " " << connIface->getName() << " <= " << fromIface->toVHDL(AbstractInterface::Instance,0) << ";" << endl;
507 // generate instances
508 foreach(AbstractBlock* block, blocks) {
510 out << " " << block->getName() << "_1 : " << block->getName() << endl;
512 QList<BlockParameter*> listGenerics = block->getGenericParameters();
513 QList<AbstractInterface*> listInputs = block->getInputs();
514 QList<AbstractInterface*> listOutputs = block->getOutputs();
515 QList<AbstractInterface*> listBidirs = block->getBidirs();
517 if (!listGenerics.isEmpty()) {
518 out << " generic map (" << endl;
519 for(i=0;i<listGenerics.size()-1;i++) {
520 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance, BlockParameter::NoComma) << "," << endl;
522 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance,BlockParameter::NoComma) << endl;
526 out << " port map (" << endl;
527 QString portMap = "";
529 for(i=0;i<listInputs.size();i++) {
530 ConnectedInterface* connIface = AI_TO_CON(listInputs.at(i));
531 ConnectedInterface* fromIface = connIface->getConnectedFrom();
533 if (fromIface->isFunctionalInterface()) {
534 portMap += " " + connIface->getName() + " => ";
536 if (connIface->getPurpose() == AbstractInterface::Data) {
537 ConnectedInterface* connCtlIface = AI_TO_CON(connIface->getAssociatedIface());
538 if ((connCtlIface != NULL) && (connCtlIface->getInputModifier() != NULL)) {
542 else if (connIface->getPurpose() == AbstractInterface::Control) {
543 if (connIface->getInputModifier() != NULL) {
548 portMap += connIface->getOwner()->getName()+"_"+connIface->getName()+"_mod,\n";
551 portMap += fromIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
554 else if (fromIface->isGroupInterface()) {
555 portMap += " " + connIface->getName() + " => " + fromIface->getName() + ",\n";
558 if (listOutputs.size()>0) {
559 for(i=0;i<listOutputs.size();i++) {
560 ConnectedInterface* connIface = AI_TO_CON(listOutputs.at(i));
561 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
564 if (listBidirs.size()>0) {
565 for(i=0;i<listBidirs.size();i++) {
566 ConnectedInterface* connIface = AI_TO_CON(listBidirs.at(i));
567 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
571 out << portMap << endl;
574 out << " );" << endl;
582 // generate input modifiers
583 foreach(AbstractBlock* block, blocks) {
585 foreach(AbstractInterface* iface, block->getControlInputs()) {
586 ConnectedInterface* connIface = AI_TO_CON(iface);
587 // check if it is connected
588 if (connIface->getConnectedFrom() == NULL) {
589 throw(Exception(IFACE_NOT_CONNECTED,this));
591 AbstractInputModifier* modifier = connIface->getInputModifier();
592 if (modifier != NULL) {
594 out << modifier->toVHDL(AbstractInputModifier::Architecture,0) << endl;
603 out << "end architecture rtl;" << endl;
606 void GroupBlock::generateController(QTextStream &out) throw(Exception) {