1 #include "GroupBlock.h"
2 #include "BlockParameterGeneric.h"
3 #include "AbstractInterface.h"
4 #include "ConnectedInterface.h"
5 #include "GroupInterface.h"
8 #include "Parameters.h"
9 #include "DelayInputModifier.h"
11 int GroupBlock::counter = 1;
13 GroupBlock::GroupBlock(GroupBlock *_parent) throw(Exception) : AbstractBlock() {
15 GroupInterface* clk = NULL;
16 GroupInterface* rst = NULL;
18 // force topGroup to false if this group has a parent
19 if (_parent != NULL) {
21 name = QString("sub_group")+"_"+QString::number(counter++);
22 // creating clk/rst interfaces
23 clk = new GroupInterface(this,"clk", AbstractInterface::Input, AbstractInterface::Clock);
24 rst = new GroupInterface(this,"reset", AbstractInterface::Input, AbstractInterface::Reset);
32 AbstractBlock* source = (AbstractBlock *)(e.getSource());
33 cerr << qPrintable(source->getName()) << ":" << qPrintable(e.getMessage()) << endl;
39 name = QString("top_group");
40 // creating external clk/rst interfaces
41 clk = new GroupInterface(this,"ext_clk", AbstractInterface::Input, AbstractInterface::Clock);
42 rst = new GroupInterface(this,"ext_reset", AbstractInterface::Input, AbstractInterface::Reset);
45 // creating clkrstgen block and connecting it to this: done in Dispatcher since this has no access to library
49 if (_parent != NULL) {
54 AbstractBlock* source = (AbstractBlock *)(e.getSource());
55 cerr << qPrintable(source->getName()) << ":" << qPrintable(e.getMessage()) << endl;
62 GroupBlock::~GroupBlock() {
63 foreach(AbstractBlock* block, blocks) {
68 bool GroupBlock::isGroupBlock() {
72 bool GroupBlock::isTopGroupBlock() {
76 void GroupBlock::setParent(AbstractBlock *_parent) {
83 void GroupBlock::removeAllBlocks() {
84 foreach(AbstractBlock* block, blocks) {
85 if (block->isGroupBlock()) {
86 GroupBlock* group = AB_TO_GRP(block);
87 group->removeAllBlocks();
93 void GroupBlock::removeBlock(AbstractBlock* block) {
94 /* CAUTION: no check is done if the block has connected interface
95 or not. Thus, they must be deleted elsewhere.
97 blocks.removeAll(block);
101 AbstractBlock *GroupBlock::getFunctionalBlockByName(QString name) {
102 foreach(AbstractBlock* block, blocks) {
103 if (block->isFunctionalBlock()) {
104 if (block->getName() == name) return block;
110 void GroupBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
113 checkedBlocks->append(this);
115 foreach(BlockParameter* param, params){
116 if(param->isUserParameter() && !param->isValueSet()){
117 if(!blocksToConfigure->contains(param->getOwner())){
118 blocksToConfigure->append(param->getOwner());
122 foreach(AbstractInterface *inter, outputs){
123 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
124 if(!checkedBlocks->contains(connectedInter->getOwner())){
125 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
132 void GroupBlock::addGenericParameter(QString name, QString type, QString value) {
133 BlockParameter* param = new BlockParameterGeneric(this, name, type, value);
134 params.append(param);
137 void GroupBlock::removeGenericParameter(QString name) {
138 BlockParameter* p = getParameterFromName(name);
139 if (p != NULL) params.removeAll(p);
142 void GroupBlock::createInputPattern() {
143 foreach(AbstractInterface* iface, getControlInputs()) {
144 ConnectedInterface* connIface = AI_TO_CON(iface);
145 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
146 connIface->setOutputPattern(pattern);
150 void GroupBlock::computeAdmittanceDelays() throw(Exception) {
151 throw(Exception(INVALID_GROUPBLOCK_USE));
154 void GroupBlock::checkInputPatternCompatibility() throw(Exception){
155 throw(Exception(INVALID_GROUPBLOCK_USE));
159 void GroupBlock::computeOutputPattern(int nbExec) throw(Exception) {
161 static QString fctName = "GroupBlock::computeOutputPattern()";
163 cout << "call to " << qPrintable(fctName) << endl;
166 cout << "computing output pattern of group " << qPrintable(name) << endl;
168 bool canCompute = false;
169 // get the input pattern on each inputs
170 createInputPattern();
172 cout << "Input pattern OK" << endl;
173 // find blocks that are connected to that inputs and generators
174 QList<AbstractBlock*> fifo;
175 foreach(AbstractBlock* block, blocks) {
178 // if a block is a generator and has control outputs, add it
179 if (block->isGeneratorBlock()) {
180 if (block->getControlOutputs().size() > 0) addIt = true;
183 // if the block has all its connected control inputs that are connected to an intput of the group, add it too
184 if (block->getControlInputs().size() > 0) {
186 foreach(AbstractInterface* iface, block->getControlInputs()) {
187 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
188 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
189 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
191 if (connFrom == NULL) {
195 else if (connFrom->getOwner() != this) {
203 cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
204 block->setTraversalLevel(0); // level 0 = first blocks to be evaluated
209 while (!fifo.isEmpty()) {
210 AbstractBlock* block = fifo.takeFirst();
212 if (block->getPatternComputed()) continue; // block has already been processed
214 cout << "computing compat and output for " << qPrintable(block->getName()) << endl;
218 block->checkInputPatternCompatibility();
221 cout << qPrintable(block->getName()) << " is not compatible with its input pattern" << endl;
226 block->computeOutputPattern();
229 cout << "cannot finalize output pattern computation of " << qPrintable(block->getName()) << endl;
233 block->setPatternComputed(true);
234 /* add other blocks connected from block to the fifo but only if
235 all their connected inputs are connected to blocks that have
238 foreach(AbstractInterface* iface, block->getControlOutputs()) {
239 ConnectedInterface* conn = (ConnectedInterface*)iface;
240 foreach(ConnectedInterface* connTo, conn->getConnectedTo()) {
242 AbstractBlock* block1 = connTo->getOwner();
243 cout << "testing if " << qPrintable(block1->getName()) << " has all connected inputs connected to already processed blocks" << endl;
247 foreach(AbstractInterface* iface, block1->getControlInputs()) {
248 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
249 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
250 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
252 if ((connFrom != NULL) && (connFrom->getOwner()->getPatternComputed() == false)) {
257 if (connFrom->getOwner()->getTraversalLevel() > maxLevel) maxLevel = connFrom->getOwner()->getTraversalLevel();
262 cout << "adding " << qPrintable(block1->getName()) << " to the FIFO" << endl;
263 block1->setTraversalLevel(maxLevel+1); // level 0 = first blocks to be evaluated
271 foreach(AbstractInterface* iface, getControlOutputs()) {
272 ConnectedInterface* connIface = AI_TO_CON(iface);
273 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
274 connIface->setOutputPattern(pattern);
276 setPatternComputed(true);
280 QList<QString> GroupBlock::getExternalResources() {
283 foreach(AbstractBlock* block, blocks) {
284 list.append(block->getExternalResources());
289 void GroupBlock::generateVHDL(const QString& path) throw(Exception) {
291 QString coreFile = "";
294 coreFile.append(Parameters::normalizeName(name));
295 coreFile.append(".vhd");
297 QFile vhdlCore(coreFile);
299 if (!vhdlCore.open(QIODevice::WriteOnly)) {
300 throw(Exception(VHDLFILE_NOACCESS));
303 cout << "generate VHDL of block " << qPrintable(name) << " in " << qPrintable(coreFile) << endl;
304 QTextStream outCore(&vhdlCore);
306 QDomElement dummyElt;
308 generateComments(outCore,dummyElt,"");
309 generateLibraries(outCore,dummyElt);
310 generateEntity(outCore);
311 generateArchitecture(outCore,dummyElt);
313 foreach(AbstractBlock* block, blocks) {
314 block->generateVHDL(path);
317 catch(Exception err) {
325 void GroupBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
326 out << " -- VHDL generated automatically for " << name << " --" << endl << endl;
329 void GroupBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
331 out << "library IEEE;" << endl;
332 out << "use IEEE.STD_LOGIC_1164.all;" << endl;
333 out << "use IEEE.numeric_std.all;" << endl;
337 void GroupBlock::generateEntityOrComponentBody(QTextStream& out, int indentLevel, bool hasController) throw(Exception) {
341 for(i=0;i<indentLevel;i++) {
345 QList<BlockParameter*> listGenerics = getGenericParameters();
346 QList<AbstractInterface*> listInputs = getInputs();
347 QList<AbstractInterface*> listOutputs = getOutputs();
348 QList<AbstractInterface*> listBidirs = getBidirs();
350 if (!listGenerics.isEmpty()) {
351 out << indent << " generic (" << endl;
352 for(i=0;i<listGenerics.size()-1;i++) {
353 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
355 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
356 out << indent << " );" << endl;
359 out << indent << " port (" << endl;
361 // Generation of the clk & rst signals
362 out << indent << " -- clk/rst" << endl;
363 foreach(AbstractInterface* iface, listInputs) {
364 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
365 out << indent << " " << iface->getName() << " : in std_logic;" << endl;
370 foreach(AbstractInterface* iface, getInterfaces()) {
371 if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
373 // Generation of the data/control signals
378 foreach(AbstractInterface* iface, listInputs) {
379 if(iface->getPurpose() == AbstractInterface::Data) {
381 out << indent << " -- input data ports" << endl;
385 if (count == 0) flag = AbstractInterface::NoComma;
386 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
390 foreach(AbstractInterface* iface, listInputs) {
391 if(iface->getPurpose() == AbstractInterface::Control) {
393 out << indent << " -- input control ports" << endl;
397 if (count == 0) flag = AbstractInterface::NoComma;
398 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
402 foreach(AbstractInterface* iface, listOutputs) {
403 if(iface->getPurpose() == AbstractInterface::Data) {
405 out << indent << " -- output data ports" << endl;
409 if (count == 0) flag = AbstractInterface::NoComma;
410 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
414 foreach(AbstractInterface* iface, listOutputs) {
415 if(iface->getPurpose() == AbstractInterface::Control) {
417 out << indent << " -- output control ports" << endl;
421 if (count == 0) flag = AbstractInterface::NoComma;
422 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
426 foreach(AbstractInterface* iface, listBidirs) {
427 if(iface->getPurpose() == AbstractInterface::Data) {
429 out << indent << " -- bidirs data ports" << endl;
433 if (count == 0) flag = AbstractInterface::NoComma;
434 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
437 out << indent << " );" << endl << endl;
440 void GroupBlock::generateArchitecture(QTextStream& out, QDomElement &elt) throw(Exception) {
444 out << "architecture rtl of " << name << " is " << endl << endl;
446 // generate type for delays, if needed.
448 foreach(AbstractBlock* block, blocks) {
449 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
450 foreach(AbstractInterface* iface, listCtlInputs) {
451 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
452 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
453 if (modifier != NULL) {
454 ConnectedInterface* connIface = AI_TO_CON(connCtlIface->getAssociatedIface());
455 int w = connIface->getWidth();
456 if (w == -1) throw(Exception(INVALID_VALUE));
457 if (!modWidth.contains(w)) {
463 if (modWidth.size() > 0) {
465 out << " -- types for modified inputs" << endl;
466 out << " type vector_of_std_logic is array (natural range <>) of std_logic;" << endl;
467 foreach(int w, modWidth) {
472 out << " type vector_of_std_logic_vector"<< mw << " is array (natural range <>) of std_logic_vector(" << mwm1 << " downto 0);" << endl;
478 // generate the components
479 foreach(AbstractBlock* block, blocks) {
481 block->generateComponent(out,false);
490 out << " ----------------------------" << endl;
491 out << " -- SIGNALS" << endl;
492 out << " ----------------------------" << endl << endl;
495 foreach(AbstractBlock* block, blocks) {
497 out << " -- signals from output ports of " << block->getName() << endl;
498 QList<AbstractInterface*> listOutputs = block->getOutputs();
499 foreach(AbstractInterface* iface, listOutputs) {
500 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
501 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
503 else if (block->getName() == "clkrstgen") {
504 if ((iface->getPurpose() == AbstractInterface::Clock)||(iface->getPurpose() == AbstractInterface::Reset)) {
505 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
516 // signal for modifiers
517 foreach(AbstractBlock* block, blocks) {
518 bool hasModif = false;
519 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
521 foreach(AbstractInterface* iface, listCtlInputs) {
522 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
523 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
524 if (modifier != NULL) {
531 out << " -- signals for modified input ports of " << block->getName() << endl;
532 foreach(AbstractInterface* iface, listCtlInputs) {
533 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
534 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
535 if (modifier != NULL) {
536 out << modifier->toVHDL(AbstractInputModifier::Signal,0) << endl;
547 out << "begin" << endl;
549 // generate signals that goes to the output ports
551 out << " -- connections to output ports of " << name << endl;
552 QList<AbstractInterface*> listOutputs = getOutputs();
553 foreach(AbstractInterface* iface, listOutputs) {
554 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
555 ConnectedInterface* connIface = AI_TO_CON(iface);
556 ConnectedInterface* fromIface = connIface->getConnectedFrom();
557 out << " " << connIface->getName() << " <= " << fromIface->toVHDL(AbstractInterface::Instance,0) << ";" << endl;
565 // generate instances
566 foreach(AbstractBlock* block, blocks) {
568 out << " " << block->getName() << "_1 : " << block->getName() << endl;
570 QList<BlockParameter*> listGenerics = block->getGenericParameters();
571 QList<AbstractInterface*> listInputs = block->getInputs();
572 QList<AbstractInterface*> listOutputs = block->getOutputs();
573 QList<AbstractInterface*> listBidirs = block->getBidirs();
575 if (!listGenerics.isEmpty()) {
576 out << " generic map (" << endl;
577 for(i=0;i<listGenerics.size()-1;i++) {
578 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance, BlockParameter::NoComma) << "," << endl;
580 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance,BlockParameter::NoComma) << endl;
584 out << " port map (" << endl;
585 QString portMap = "";
587 for(i=0;i<listInputs.size();i++) {
588 ConnectedInterface* connIface = AI_TO_CON(listInputs.at(i));
589 ConnectedInterface* fromIface = connIface->getConnectedFrom();
591 if (fromIface->isFunctionalInterface()) {
592 portMap += " " + connIface->getName() + " => ";
594 if (connIface->getPurpose() == AbstractInterface::Data) {
595 ConnectedInterface* connCtlIface = AI_TO_CON(connIface->getAssociatedIface());
596 if ((connCtlIface != NULL) && (connCtlIface->getInputModifier() != NULL)) {
600 else if (connIface->getPurpose() == AbstractInterface::Control) {
601 if (connIface->getInputModifier() != NULL) {
606 portMap += connIface->getOwner()->getName()+"_"+connIface->getName()+"_mod,\n";
609 portMap += fromIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
612 else if (fromIface->isGroupInterface()) {
613 portMap += " " + connIface->getName() + " => " + fromIface->getName() + ",\n";
616 if (listOutputs.size()>0) {
617 for(i=0;i<listOutputs.size();i++) {
618 ConnectedInterface* connIface = AI_TO_CON(listOutputs.at(i));
619 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
622 if (listBidirs.size()>0) {
623 for(i=0;i<listBidirs.size();i++) {
624 ConnectedInterface* connIface = AI_TO_CON(listBidirs.at(i));
625 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
629 out << portMap << endl;
632 out << " );" << endl;
640 // generate input modifiers
641 foreach(AbstractBlock* block, blocks) {
643 foreach(AbstractInterface* iface, block->getControlInputs()) {
644 ConnectedInterface* connIface = AI_TO_CON(iface);
645 // check if it is connected
646 if (connIface->getConnectedFrom() == NULL) {
647 throw(Exception(IFACE_NOT_CONNECTED,this));
649 AbstractInputModifier* modifier = connIface->getInputModifier();
650 if (modifier != NULL) {
652 out << modifier->toVHDL(AbstractInputModifier::Architecture,0) << endl;
661 out << "end architecture rtl;" << endl;
664 void GroupBlock::generateController(QTextStream &out) throw(Exception) {