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"
12 int GroupBlock::counter = 1;
14 GroupBlock::GroupBlock(Graph *_graph, GroupBlock *_parent, bool createIfaces) throw(Exception) : AbstractBlock(_graph) {
17 GroupInterface* clk = NULL;
18 GroupInterface* rst = NULL;
20 // force topGroup to false if this group has a parent
23 name = QString("sub_group")+"_"+QString::number(counter++);
27 name = QString("top_group");
28 // creating external clk/rst interfaces
33 clk = new GroupInterface(this,"ext_clk_0", AbstractInterface::Input, AbstractInterface::Clock);
34 rst = new GroupInterface(this,"ext_reset_0", AbstractInterface::Input, AbstractInterface::Reset);
39 // get all clock and reset from parent
40 QList<AbstractInterface*> lstClk = parent->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
41 QList<AbstractInterface*> lstRst = parent->getInterfaces(AbstractInterface::Input, AbstractInterface::Reset);
42 foreach(AbstractInterface* iface, lstClk) {
43 clk = new GroupInterface(this,iface->getName(),AbstractInterface::Input, AbstractInterface::Clock);
46 foreach(AbstractInterface* iface, lstRst) {
47 rst = new GroupInterface(this,iface->getName(),AbstractInterface::Input, AbstractInterface::Reset);
54 GroupBlock::~GroupBlock() {
55 foreach(AbstractBlock* block, blocks) {
60 bool GroupBlock::isGroupBlock() {
64 bool GroupBlock::isTopGroupBlock() {
68 void GroupBlock::setParent(AbstractBlock *_parent) {
75 void GroupBlock::removeAllBlocks() {
76 foreach(AbstractBlock* block, blocks) {
77 if (block->isGroupBlock()) {
78 GroupBlock* group = AB_TO_GRP(block);
79 group->removeAllBlocks();
85 void GroupBlock::removeBlock(AbstractBlock* block) {
86 /* CAUTION: no check is done if the block has connected interface
87 or not. Thus, they must be deleted elsewhere.
89 blocks.removeAll(block);
93 AbstractBlock *GroupBlock::getFunctionalBlockByName(QString name) {
94 foreach(AbstractBlock* block, blocks) {
95 if (block->isFunctionalBlock()) {
96 if (block->getName() == name) return block;
102 void GroupBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
105 checkedBlocks->append(this);
107 foreach(BlockParameter* param, params){
108 if(param->isUserParameter() && !param->isValueSet()){
109 if(!blocksToConfigure->contains(param->getOwner())){
110 blocksToConfigure->append(param->getOwner());
114 foreach(AbstractInterface *inter, outputs){
115 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
116 if(!checkedBlocks->contains(connectedInter->getOwner())){
117 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
124 void GroupBlock::addGenericParameter(QString name, QString type, QString value) {
125 BlockParameter* param = new BlockParameterGeneric(this, name, type, value);
126 params.append(param);
129 void GroupBlock::removeGenericParameter(QString name) {
130 BlockParameter* p = getParameterFromName(name);
131 if (p != NULL) params.removeAll(p);
134 void GroupBlock::createInputPattern() {
135 foreach(AbstractInterface* iface, getControlInputs()) {
136 ConnectedInterface* connIface = AI_TO_CON(iface);
137 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
138 connIface->setOutputPattern(pattern);
142 void GroupBlock::computeAdmittanceDelays() throw(Exception) {
143 throw(Exception(INVALID_GROUPBLOCK_USE,this));
146 void GroupBlock::checkInputPatternCompatibility() throw(Exception){
147 throw(Exception(INVALID_GROUPBLOCK_USE,this));
151 void GroupBlock::computeOutputPattern(int nbExec) throw(Exception) {
153 static QString fctName = "GroupBlock::computeOutputPattern()";
155 cout << "call to " << qPrintable(fctName) << endl;
158 cout << "computing output pattern of group " << qPrintable(name) << endl;
160 bool canCompute = false;
161 // get the input pattern on each inputs
162 createInputPattern();
164 cout << "Input pattern OK" << endl;
165 // find blocks that are connected to that inputs and generators
166 QList<AbstractBlock*> fifo;
167 foreach(AbstractBlock* block, blocks) {
170 // if a block is a generator and has control outputs, add it
171 if (block->isSourceBlock()) {
172 if (block->getControlOutputs().size() > 0) addIt = true;
175 // if the block has all its connected control inputs that are connected to an intput of the group, add it too
176 if (block->getControlInputs().size() > 0) {
178 foreach(AbstractInterface* iface, block->getControlInputs()) {
179 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
180 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
181 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
183 if (connFrom == NULL) {
187 else if (connFrom->getOwner() != this) {
195 cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
196 block->setTraversalLevel(0); // level 0 = first blocks to be evaluated
201 while (!fifo.isEmpty()) {
202 AbstractBlock* block = fifo.takeFirst();
204 if (block->getOutputPatternComputed()) continue; // block has already been processed
207 cout << "computing compatibility of " << qPrintable(block->getName()) << endl;
208 block->checkInputPatternCompatibility();
211 cout << qPrintable(block->getName()) << " is not compatible with its input pattern" << endl;
216 cout << "computing output of " << qPrintable(block->getName()) << endl;
217 block->computeOutputPattern();
220 cout << "cannot finalize output pattern computation of " << qPrintable(block->getName()) << endl;
225 /* add other blocks connected from block to the fifo but only if
226 all their connected inputs are connected to blocks that have
229 foreach(AbstractInterface* iface, block->getControlOutputs()) {
230 ConnectedInterface* conn = (ConnectedInterface*)iface;
231 foreach(ConnectedInterface* connTo, conn->getConnectedTo()) {
233 AbstractBlock* blockTo = connTo->getOwner();
234 // do sthg only if blockTo is not this group block
235 if (blockTo != this) {
236 cout << "testing if " << qPrintable(blockTo->getName()) << " has all connected inputs connected to already processed blocks" << endl;
240 foreach(AbstractInterface* iface, blockTo->getControlInputs()) {
241 cout << qPrintable(iface->getName()) << "/" << qPrintable(iface->getOwner()->getName()) << " connected from ";
242 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
243 cout << qPrintable(connFrom->getName()) << "/" << qPrintable(connFrom->getOwner()->getName()) << endl;
245 if ((connFrom != NULL) && (connFrom->getOwner()->getOutputPatternComputed() == false)) {
250 if (connFrom->getOwner()->getTraversalLevel() > maxLevel) maxLevel = connFrom->getOwner()->getTraversalLevel();
255 cout << "adding " << qPrintable(blockTo->getName()) << " to the FIFO" << endl;
256 blockTo->setTraversalLevel(maxLevel+1); // level 0 = first blocks to be evaluated
257 fifo.append(blockTo);
265 foreach(AbstractInterface* iface, getControlOutputs()) {
266 ConnectedInterface* connIface = AI_TO_CON(iface);
267 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
268 connIface->setOutputPattern(pattern);
270 setOutputPatternComputed(true);
274 QList<QString> GroupBlock::getExternalResources() {
277 foreach(AbstractBlock* block, blocks) {
278 list.append(block->getExternalResources());
283 void GroupBlock::generateVHDL(const QString& path) throw(Exception) {
285 QString coreFile = "";
288 coreFile.append(Parameters::normalizeName(name));
289 coreFile.append(".vhd");
291 QFile vhdlCore(coreFile);
293 if (!vhdlCore.open(QIODevice::WriteOnly)) {
294 throw(Exception(VHDLFILE_NOACCESS));
297 cout << "generate VHDL of block " << qPrintable(name) << " in " << qPrintable(coreFile) << endl;
298 QTextStream outCore(&vhdlCore);
300 QDomElement dummyElt;
302 generateComments(outCore,dummyElt,"");
303 generateLibraries(outCore,dummyElt);
304 generateEntity(outCore);
305 generateArchitecture(outCore,dummyElt);
307 foreach(AbstractBlock* block, blocks) {
308 block->generateVHDL(path);
311 catch(Exception err) {
319 void GroupBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
320 out << " -- VHDL generated automatically for " << name << " --" << endl << endl;
323 void GroupBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
325 out << "library IEEE;" << endl;
326 out << "use IEEE.STD_LOGIC_1164.all;" << endl;
327 out << "use IEEE.numeric_std.all;" << endl;
331 void GroupBlock::generateEntityOrComponentBody(QTextStream& out, int indentLevel, bool hasController) throw(Exception) {
335 for(i=0;i<indentLevel;i++) {
339 QList<BlockParameter*> listGenerics = getGenericParameters();
340 QList<AbstractInterface*> listInputs = getInputs();
341 QList<AbstractInterface*> listOutputs = getOutputs();
342 QList<AbstractInterface*> listBidirs = getBidirs();
344 if (!listGenerics.isEmpty()) {
345 out << indent << " generic (" << endl;
346 for(i=0;i<listGenerics.size()-1;i++) {
347 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
349 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
350 out << indent << " );" << endl;
353 out << indent << " port (" << endl;
355 // Generation of the clk & rst signals
356 out << indent << " -- clk/rst" << endl;
357 foreach(AbstractInterface* iface, listInputs) {
358 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
359 out << indent << " " << iface->getName() << " : in std_logic;" << endl;
364 foreach(AbstractInterface* iface, getInterfaces()) {
365 if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
367 // Generation of the data/control signals
372 foreach(AbstractInterface* iface, listInputs) {
373 if(iface->getPurpose() == AbstractInterface::Data) {
375 out << indent << " -- input data ports" << endl;
379 if (count == 0) flag = AbstractInterface::NoComma;
380 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
384 foreach(AbstractInterface* iface, listInputs) {
385 if(iface->getPurpose() == AbstractInterface::Control) {
387 out << indent << " -- input control ports" << endl;
391 if (count == 0) flag = AbstractInterface::NoComma;
392 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
396 foreach(AbstractInterface* iface, listOutputs) {
397 if(iface->getPurpose() == AbstractInterface::Data) {
399 out << indent << " -- output data ports" << endl;
403 if (count == 0) flag = AbstractInterface::NoComma;
404 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
408 foreach(AbstractInterface* iface, listOutputs) {
409 if(iface->getPurpose() == AbstractInterface::Control) {
411 out << indent << " -- output control ports" << endl;
415 if (count == 0) flag = AbstractInterface::NoComma;
416 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
420 foreach(AbstractInterface* iface, listBidirs) {
421 if(iface->getPurpose() == AbstractInterface::Data) {
423 out << indent << " -- bidirs data ports" << endl;
427 if (count == 0) flag = AbstractInterface::NoComma;
428 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
431 out << indent << " );" << endl << endl;
434 void GroupBlock::generateArchitecture(QTextStream& out, QDomElement &elt) throw(Exception) {
438 out << "architecture rtl of " << name << " is " << endl << endl;
440 // generate type for delays, if needed.
442 foreach(AbstractBlock* block, blocks) {
443 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
444 foreach(AbstractInterface* iface, listCtlInputs) {
445 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
446 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
447 if (modifier != NULL) {
448 ConnectedInterface* connIface = AI_TO_CON(connCtlIface->getAssociatedIface());
449 int w = connIface->getWidth();
450 if (w == -1) throw(Exception(INVALID_VALUE));
451 if (!modWidth.contains(w)) {
457 if (modWidth.size() > 0) {
459 out << " -- types for modified inputs" << endl;
460 out << " type vector_of_std_logic is array (natural range <>) of std_logic;" << endl;
461 foreach(int w, modWidth) {
466 out << " type vector_of_std_logic_vector"<< mw << " is array (natural range <>) of std_logic_vector(" << mwm1 << " downto 0);" << endl;
472 // generate the components
473 foreach(AbstractBlock* block, blocks) {
475 block->generateComponent(out,false);
484 out << " ----------------------------" << endl;
485 out << " -- SIGNALS" << endl;
486 out << " ----------------------------" << endl << endl;
488 // if this is top group, signals to synchronize inputs
490 out << " -- signals to synchronize inputs" << endl;
491 foreach(AbstractInterface* iface, getInputs()) {
492 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
493 QString name = iface->toVHDL(AbstractInterface::Signal,0);
494 name.replace(" : ","_sync : ");
495 out << " signal " << name<< endl;
502 foreach(AbstractBlock* block, blocks) {
504 out << " -- signals from output ports of " << block->getName() << endl;
505 QList<AbstractInterface*> listOutputs = block->getOutputs();
506 foreach(AbstractInterface* iface, listOutputs) {
507 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
508 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
510 else if (block->getName().startsWith("clkrstgen")) {
511 if ((iface->getPurpose() == AbstractInterface::Clock)||(iface->getPurpose() == AbstractInterface::Reset)) {
512 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
523 // signal for modifiers
524 foreach(AbstractBlock* block, blocks) {
525 bool hasModif = false;
526 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
528 foreach(AbstractInterface* iface, listCtlInputs) {
529 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
530 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
531 if (modifier != NULL) {
538 out << " -- signals for modified input ports of " << block->getName() << endl;
539 foreach(AbstractInterface* iface, listCtlInputs) {
540 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
541 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
542 if (modifier != NULL) {
543 out << modifier->toVHDL(AbstractInputModifier::Signal,0) << endl;
554 out << "begin" << endl;
556 // generate signals that goes to the output ports
558 out << " -- connections to output ports of " << name << endl;
559 QList<AbstractInterface*> listOutputs = getOutputs();
560 foreach(AbstractInterface* iface, listOutputs) {
561 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
562 ConnectedInterface* connIface = AI_TO_CON(iface);
563 ConnectedInterface* fromIface = connIface->getConnectedFrom();
564 out << " " << connIface->getName() << " <= " << fromIface->toVHDL(AbstractInterface::Instance,0) << ";" << endl;
572 // generate instances
573 foreach(AbstractBlock* block, blocks) {
575 out << " " << block->getName() << "_1 : " << block->getName() << endl;
577 QList<BlockParameter*> listGenerics = block->getGenericParameters();
578 QList<AbstractInterface*> listInputs = block->getInputs();
579 QList<AbstractInterface*> listOutputs = block->getOutputs();
580 QList<AbstractInterface*> listBidirs = block->getBidirs();
582 if (!listGenerics.isEmpty()) {
583 out << " generic map (" << endl;
584 for(i=0;i<listGenerics.size()-1;i++) {
585 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance, BlockParameter::NoComma) << "," << endl;
587 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance,BlockParameter::NoComma) << endl;
591 out << " port map (" << endl;
592 QString portMap = "";
594 for(i=0;i<listInputs.size();i++) {
595 ConnectedInterface* connIface = AI_TO_CON(listInputs.at(i));
596 ConnectedInterface* fromIface = connIface->getConnectedFrom();
598 if (fromIface->isFunctionalInterface()) {
599 portMap += " " + connIface->getName() + " => ";
601 if (connIface->getPurpose() == AbstractInterface::Data) {
602 ConnectedInterface* connCtlIface = AI_TO_CON(connIface->getAssociatedIface());
603 if ((connCtlIface != NULL) && (connCtlIface->getInputModifier() != NULL)) {
607 else if (connIface->getPurpose() == AbstractInterface::Control) {
608 if (connIface->getInputModifier() != NULL) {
613 portMap += connIface->getOwner()->getName()+"_"+connIface->getName()+"_mod,\n";
616 portMap += fromIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
619 else if (fromIface->isGroupInterface()) {
620 if ((fromIface->getOwner()->isTopGroupBlock()) && ((fromIface->getPurpose() == AbstractInterface::Data)||(fromIface->getPurpose() == AbstractInterface::Control))) {
621 portMap += " " + connIface->getName() + " => " + fromIface->getOwner()->getName()+ "_"+ fromIface->getName() + "_sync,\n";
624 portMap += " " + connIface->getName() + " => " + fromIface->getName() + ",\n";
628 if (listOutputs.size()>0) {
629 for(i=0;i<listOutputs.size();i++) {
630 ConnectedInterface* connIface = AI_TO_CON(listOutputs.at(i));
631 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
634 if (listBidirs.size()>0) {
635 for(i=0;i<listBidirs.size();i++) {
636 ConnectedInterface* connIface = AI_TO_CON(listBidirs.at(i));
637 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
641 out << portMap << endl;
644 out << " );" << endl;
652 // generate input modifiers
653 foreach(AbstractBlock* block, blocks) {
655 foreach(AbstractInterface* iface, block->getControlInputs()) {
656 ConnectedInterface* connIface = AI_TO_CON(iface);
657 // check if it is connected
658 if (connIface->getConnectedFrom() == NULL) {
659 throw(Exception(IFACE_NOT_CONNECTED,this));
661 AbstractInputModifier* modifier = connIface->getInputModifier();
662 if (modifier != NULL) {
664 out << modifier->toVHDL(AbstractInputModifier::Architecture,0) << endl;
674 // generate input sync process for each clock domain
675 out << " -- process to synchronize inputs of top group" << endl;
676 for(int i=0;i<graph->getClocks().size();i++) {
677 // check if there are some inputs that must be sync with clock domain i
678 bool mustSync = false;
679 foreach(AbstractInterface* iface, getInputs()) {
680 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
681 if (iface->getClockDomain() == i) {
688 out << "sync_inputs_" << i << " : process(from_clkrstgen_" << i << "_clk,from_clkrstgen_" << i << "_reset)" << endl;
689 out << " begin" << endl;
690 out << " if from_clkrstgen_" << i << "_reset = '1' then" << endl;
691 foreach(AbstractInterface* iface, getInputs()) {
692 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
693 if (iface->getClockDomain() == i) {
694 if (iface->getWidth() == 0) {
695 out << " " << name << "_" << iface->getName() << "_sync <= '0';" << endl;
698 out << " " << name << "_" << iface->getName() << "_sync <= (others => '0');" << endl;
703 out << " elsif rising_edge(from_clkrstgen_" << i << "_clk) then" << endl;
704 foreach(AbstractInterface* iface, getInputs()) {
705 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
706 if (iface->getClockDomain() == i) {
707 if (iface->getWidth() == 0) {
708 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
711 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
716 out << " end if;" << endl;
717 out << " end process sync_inputs_" << i << ";" << endl;
724 out << "end architecture rtl;" << endl;
727 void GroupBlock::generateController(QTextStream &out) throw(Exception) {