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, bool createIfaces) 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;
494 // signals to synchronize inputs
495 out << " -- signals to synchronize inputs" << endl;
496 foreach(AbstractInterface* iface, getInputs()) {
497 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
498 QString name = iface->toVHDL(AbstractInterface::Signal,0);
499 name.replace(" : ","_sync : ");
500 out << " signal " << name<< endl;
506 foreach(AbstractBlock* block, blocks) {
508 out << " -- signals from output ports of " << block->getName() << endl;
509 QList<AbstractInterface*> listOutputs = block->getOutputs();
510 foreach(AbstractInterface* iface, listOutputs) {
511 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
512 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
514 else if (block->getName() == "clkrstgen") {
515 if ((iface->getPurpose() == AbstractInterface::Clock)||(iface->getPurpose() == AbstractInterface::Reset)) {
516 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
527 // signal for modifiers
528 foreach(AbstractBlock* block, blocks) {
529 bool hasModif = false;
530 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
532 foreach(AbstractInterface* iface, listCtlInputs) {
533 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
534 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
535 if (modifier != NULL) {
542 out << " -- signals for modified input ports of " << block->getName() << endl;
543 foreach(AbstractInterface* iface, listCtlInputs) {
544 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
545 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
546 if (modifier != NULL) {
547 out << modifier->toVHDL(AbstractInputModifier::Signal,0) << endl;
558 out << "begin" << endl;
560 // generate signals that goes to the output ports
562 out << " -- connections to output ports of " << name << endl;
563 QList<AbstractInterface*> listOutputs = getOutputs();
564 foreach(AbstractInterface* iface, listOutputs) {
565 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
566 ConnectedInterface* connIface = AI_TO_CON(iface);
567 ConnectedInterface* fromIface = connIface->getConnectedFrom();
568 out << " " << connIface->getName() << " <= " << fromIface->toVHDL(AbstractInterface::Instance,0) << ";" << endl;
576 // generate instances
577 foreach(AbstractBlock* block, blocks) {
579 out << " " << block->getName() << "_1 : " << block->getName() << endl;
581 QList<BlockParameter*> listGenerics = block->getGenericParameters();
582 QList<AbstractInterface*> listInputs = block->getInputs();
583 QList<AbstractInterface*> listOutputs = block->getOutputs();
584 QList<AbstractInterface*> listBidirs = block->getBidirs();
586 if (!listGenerics.isEmpty()) {
587 out << " generic map (" << endl;
588 for(i=0;i<listGenerics.size()-1;i++) {
589 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance, BlockParameter::NoComma) << "," << endl;
591 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance,BlockParameter::NoComma) << endl;
595 out << " port map (" << endl;
596 QString portMap = "";
598 for(i=0;i<listInputs.size();i++) {
599 ConnectedInterface* connIface = AI_TO_CON(listInputs.at(i));
600 ConnectedInterface* fromIface = connIface->getConnectedFrom();
602 if (fromIface->isFunctionalInterface()) {
603 portMap += " " + connIface->getName() + " => ";
605 if (connIface->getPurpose() == AbstractInterface::Data) {
606 ConnectedInterface* connCtlIface = AI_TO_CON(connIface->getAssociatedIface());
607 if ((connCtlIface != NULL) && (connCtlIface->getInputModifier() != NULL)) {
611 else if (connIface->getPurpose() == AbstractInterface::Control) {
612 if (connIface->getInputModifier() != NULL) {
617 portMap += connIface->getOwner()->getName()+"_"+connIface->getName()+"_mod,\n";
620 portMap += fromIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
623 else if (fromIface->isGroupInterface()) {
624 if ((fromIface->getOwner()->isTopGroupBlock()) && ((fromIface->getPurpose() == AbstractInterface::Data)||(fromIface->getPurpose() == AbstractInterface::Control))) {
625 portMap += " " + connIface->getName() + " => " + fromIface->getOwner()->getName()+ "_"+ fromIface->getName() + "_sync,\n";
628 portMap += " " + connIface->getName() + " => " + fromIface->getName() + ",\n";
632 if (listOutputs.size()>0) {
633 for(i=0;i<listOutputs.size();i++) {
634 ConnectedInterface* connIface = AI_TO_CON(listOutputs.at(i));
635 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
638 if (listBidirs.size()>0) {
639 for(i=0;i<listBidirs.size();i++) {
640 ConnectedInterface* connIface = AI_TO_CON(listBidirs.at(i));
641 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
645 out << portMap << endl;
648 out << " );" << endl;
656 // generate input modifiers
657 foreach(AbstractBlock* block, blocks) {
659 foreach(AbstractInterface* iface, block->getControlInputs()) {
660 ConnectedInterface* connIface = AI_TO_CON(iface);
661 // check if it is connected
662 if (connIface->getConnectedFrom() == NULL) {
663 throw(Exception(IFACE_NOT_CONNECTED,this));
665 AbstractInputModifier* modifier = connIface->getInputModifier();
666 if (modifier != NULL) {
668 out << modifier->toVHDL(AbstractInputModifier::Architecture,0) << endl;
678 // generate input sync process
679 out << " -- process to synchronize inputs of top group" << endl;
680 out << "sync_inputs : process(from_clkrstgen_clk,from_clkrstgen_reset)" << endl;
681 out << " begin" << endl;
682 out << " if from_clkrstgen_reset = '1' then" << endl;
683 foreach(AbstractInterface* iface, getInputs()) {
684 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
685 if (iface->getWidth() == 0) {
686 out << " " << name << "_" << iface->getName() << "_sync <= '0';" << endl;
689 out << " " << name << "_" << iface->getName() << "_sync <= (others => '0');" << endl;
693 out << " elsif rising_edge(from_clkrstgen_clk) then" << endl;
694 foreach(AbstractInterface* iface, getInputs()) {
695 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
696 if (iface->getWidth() == 0) {
697 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
700 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
704 out << " end if;" << endl;
705 out << " end process sync_inputs;" << endl;
710 out << "end architecture rtl;" << endl;
713 void GroupBlock::generateController(QTextStream &out) throw(Exception) {