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() {
16 GroupInterface* clk = NULL;
17 GroupInterface* rst = NULL;
19 // force topGroup to false if this group has a parent
22 name = QString("sub_group")+"_"+QString::number(counter++);
26 name = QString("top_group");
27 // creating external clk/rst interfaces
32 clk = new GroupInterface(this,"ext_clk_0", AbstractInterface::Input, AbstractInterface::Clock);
33 rst = new GroupInterface(this,"ext_reset_0", AbstractInterface::Input, AbstractInterface::Reset);
38 // get all clock and reset from parent
39 QList<AbstractInterface*> lstClk = parent->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
40 QList<AbstractInterface*> lstRst = parent->getInterfaces(AbstractInterface::Input, AbstractInterface::Reset);
41 foreach(AbstractInterface* iface, lstClk) {
42 clk = new GroupInterface(this,iface->getName(),AbstractInterface::Input, AbstractInterface::Clock);
45 foreach(AbstractInterface* iface, lstRst) {
46 rst = new GroupInterface(this,iface->getName(),AbstractInterface::Input, AbstractInterface::Reset);
53 GroupBlock::~GroupBlock() {
54 foreach(AbstractBlock* block, blocks) {
59 bool GroupBlock::isGroupBlock() {
63 bool GroupBlock::isTopGroupBlock() {
67 void GroupBlock::setParent(AbstractBlock *_parent) {
74 void GroupBlock::removeAllBlocks() {
75 foreach(AbstractBlock* block, blocks) {
76 if (block->isGroupBlock()) {
77 GroupBlock* group = AB_TO_GRP(block);
78 group->removeAllBlocks();
84 void GroupBlock::removeBlock(AbstractBlock* block) {
85 /* CAUTION: no check is done if the block has connected interface
86 or not. Thus, they must be deleted elsewhere.
88 blocks.removeAll(block);
92 AbstractBlock *GroupBlock::getFunctionalBlockByName(QString name) {
93 foreach(AbstractBlock* block, blocks) {
94 if (block->isFunctionalBlock()) {
95 if (block->getName() == name) return block;
101 void GroupBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
104 checkedBlocks->append(this);
106 foreach(BlockParameter* param, params){
107 if(param->isUserParameter() && !param->isValueSet()){
108 if(!blocksToConfigure->contains(param->getOwner())){
109 blocksToConfigure->append(param->getOwner());
113 foreach(AbstractInterface *inter, outputs){
114 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
115 if(!checkedBlocks->contains(connectedInter->getOwner())){
116 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
123 void GroupBlock::addGenericParameter(QString name, QString type, QString value) {
124 BlockParameter* param = new BlockParameterGeneric(this, name, type, value);
125 params.append(param);
128 void GroupBlock::removeGenericParameter(QString name) {
129 BlockParameter* p = getParameterFromName(name);
130 if (p != NULL) params.removeAll(p);
133 void GroupBlock::createInputPattern() {
134 foreach(AbstractInterface* iface, getControlInputs()) {
135 ConnectedInterface* connIface = AI_TO_CON(iface);
136 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
137 connIface->setOutputPattern(pattern);
141 void GroupBlock::computeAdmittanceDelays() throw(Exception) {
142 throw(Exception(INVALID_GROUPBLOCK_USE,this));
145 void GroupBlock::checkInputPatternCompatibility() throw(Exception){
146 throw(Exception(INVALID_GROUPBLOCK_USE,this));
150 void GroupBlock::computeOutputPattern(int nbExec) throw(Exception) {
152 static QString fctName = "GroupBlock::computeOutputPattern()";
154 cout << "call to " << qPrintable(fctName) << endl;
157 cout << "computing output pattern of group " << qPrintable(name) << endl;
159 bool canCompute = false;
160 // get the input pattern on each inputs
161 createInputPattern();
163 cout << "Input pattern OK" << endl;
164 // find blocks that are connected to that inputs and generators
165 QList<AbstractBlock*> fifo;
166 foreach(AbstractBlock* block, blocks) {
169 // if a block is a generator and has control outputs, add it
170 if (block->isSourceBlock()) {
171 if (block->getControlOutputs().size() > 0) addIt = true;
174 // if the block has all its connected control inputs that are connected to an intput of the group, add it too
175 if (block->getControlInputs().size() > 0) {
177 foreach(AbstractInterface* iface, block->getControlInputs()) {
178 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
179 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
180 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
182 if (connFrom == NULL) {
186 else if (connFrom->getOwner() != this) {
194 cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
195 block->setTraversalLevel(0); // level 0 = first blocks to be evaluated
200 while (!fifo.isEmpty()) {
201 AbstractBlock* block = fifo.takeFirst();
203 if (block->getOutputPatternComputed()) continue; // block has already been processed
205 cout << "computing compat and output for " << qPrintable(block->getName()) << endl;
209 block->checkInputPatternCompatibility();
212 cout << qPrintable(block->getName()) << " is not compatible with its input pattern" << 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* block1 = connTo->getOwner();
234 cout << "testing if " << qPrintable(block1->getName()) << " has all connected inputs connected to already processed blocks" << endl;
238 foreach(AbstractInterface* iface, block1->getControlInputs()) {
239 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
240 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
241 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
243 if ((connFrom != NULL) && (connFrom->getOwner()->getOutputPatternComputed() == false)) {
248 if (connFrom->getOwner()->getTraversalLevel() > maxLevel) maxLevel = connFrom->getOwner()->getTraversalLevel();
253 cout << "adding " << qPrintable(block1->getName()) << " to the FIFO" << endl;
254 block1->setTraversalLevel(maxLevel+1); // level 0 = first blocks to be evaluated
262 foreach(AbstractInterface* iface, getControlOutputs()) {
263 ConnectedInterface* connIface = AI_TO_CON(iface);
264 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
265 connIface->setOutputPattern(pattern);
267 setOutputPatternComputed(true);
271 QList<QString> GroupBlock::getExternalResources() {
274 foreach(AbstractBlock* block, blocks) {
275 list.append(block->getExternalResources());
280 void GroupBlock::generateVHDL(const QString& path) throw(Exception) {
282 QString coreFile = "";
285 coreFile.append(Parameters::normalizeName(name));
286 coreFile.append(".vhd");
288 QFile vhdlCore(coreFile);
290 if (!vhdlCore.open(QIODevice::WriteOnly)) {
291 throw(Exception(VHDLFILE_NOACCESS));
294 cout << "generate VHDL of block " << qPrintable(name) << " in " << qPrintable(coreFile) << endl;
295 QTextStream outCore(&vhdlCore);
297 QDomElement dummyElt;
299 generateComments(outCore,dummyElt,"");
300 generateLibraries(outCore,dummyElt);
301 generateEntity(outCore);
302 generateArchitecture(outCore,dummyElt);
304 foreach(AbstractBlock* block, blocks) {
305 block->generateVHDL(path);
308 catch(Exception err) {
316 void GroupBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
317 out << " -- VHDL generated automatically for " << name << " --" << endl << endl;
320 void GroupBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
322 out << "library IEEE;" << endl;
323 out << "use IEEE.STD_LOGIC_1164.all;" << endl;
324 out << "use IEEE.numeric_std.all;" << endl;
328 void GroupBlock::generateEntityOrComponentBody(QTextStream& out, int indentLevel, bool hasController) throw(Exception) {
332 for(i=0;i<indentLevel;i++) {
336 QList<BlockParameter*> listGenerics = getGenericParameters();
337 QList<AbstractInterface*> listInputs = getInputs();
338 QList<AbstractInterface*> listOutputs = getOutputs();
339 QList<AbstractInterface*> listBidirs = getBidirs();
341 if (!listGenerics.isEmpty()) {
342 out << indent << " generic (" << endl;
343 for(i=0;i<listGenerics.size()-1;i++) {
344 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
346 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
347 out << indent << " );" << endl;
350 out << indent << " port (" << endl;
352 // Generation of the clk & rst signals
353 out << indent << " -- clk/rst" << endl;
354 foreach(AbstractInterface* iface, listInputs) {
355 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
356 out << indent << " " << iface->getName() << " : in std_logic;" << endl;
361 foreach(AbstractInterface* iface, getInterfaces()) {
362 if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
364 // Generation of the data/control signals
369 foreach(AbstractInterface* iface, listInputs) {
370 if(iface->getPurpose() == AbstractInterface::Data) {
372 out << indent << " -- input data ports" << endl;
376 if (count == 0) flag = AbstractInterface::NoComma;
377 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
381 foreach(AbstractInterface* iface, listInputs) {
382 if(iface->getPurpose() == AbstractInterface::Control) {
384 out << indent << " -- input control ports" << endl;
388 if (count == 0) flag = AbstractInterface::NoComma;
389 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
393 foreach(AbstractInterface* iface, listOutputs) {
394 if(iface->getPurpose() == AbstractInterface::Data) {
396 out << indent << " -- output data ports" << endl;
400 if (count == 0) flag = AbstractInterface::NoComma;
401 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
405 foreach(AbstractInterface* iface, listOutputs) {
406 if(iface->getPurpose() == AbstractInterface::Control) {
408 out << indent << " -- output control ports" << endl;
412 if (count == 0) flag = AbstractInterface::NoComma;
413 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
417 foreach(AbstractInterface* iface, listBidirs) {
418 if(iface->getPurpose() == AbstractInterface::Data) {
420 out << indent << " -- bidirs data ports" << endl;
424 if (count == 0) flag = AbstractInterface::NoComma;
425 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
428 out << indent << " );" << endl << endl;
431 void GroupBlock::generateArchitecture(QTextStream& out, QDomElement &elt) throw(Exception) {
435 out << "architecture rtl of " << name << " is " << endl << endl;
437 // generate type for delays, if needed.
439 foreach(AbstractBlock* block, blocks) {
440 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
441 foreach(AbstractInterface* iface, listCtlInputs) {
442 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
443 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
444 if (modifier != NULL) {
445 ConnectedInterface* connIface = AI_TO_CON(connCtlIface->getAssociatedIface());
446 int w = connIface->getWidth();
447 if (w == -1) throw(Exception(INVALID_VALUE));
448 if (!modWidth.contains(w)) {
454 if (modWidth.size() > 0) {
456 out << " -- types for modified inputs" << endl;
457 out << " type vector_of_std_logic is array (natural range <>) of std_logic;" << endl;
458 foreach(int w, modWidth) {
463 out << " type vector_of_std_logic_vector"<< mw << " is array (natural range <>) of std_logic_vector(" << mwm1 << " downto 0);" << endl;
469 // generate the components
470 foreach(AbstractBlock* block, blocks) {
472 block->generateComponent(out,false);
481 out << " ----------------------------" << endl;
482 out << " -- SIGNALS" << endl;
483 out << " ----------------------------" << endl << endl;
485 // signals to synchronize inputs
486 out << " -- signals to synchronize inputs" << endl;
487 foreach(AbstractInterface* iface, getInputs()) {
488 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
489 QString name = iface->toVHDL(AbstractInterface::Signal,0);
490 name.replace(" : ","_sync : ");
491 out << " signal " << name<< endl;
497 foreach(AbstractBlock* block, blocks) {
499 out << " -- signals from output ports of " << block->getName() << endl;
500 QList<AbstractInterface*> listOutputs = block->getOutputs();
501 foreach(AbstractInterface* iface, listOutputs) {
502 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
503 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
505 else if (block->getName() == "clkrstgen") {
506 if ((iface->getPurpose() == AbstractInterface::Clock)||(iface->getPurpose() == AbstractInterface::Reset)) {
507 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
518 // signal for modifiers
519 foreach(AbstractBlock* block, blocks) {
520 bool hasModif = false;
521 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
523 foreach(AbstractInterface* iface, listCtlInputs) {
524 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
525 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
526 if (modifier != NULL) {
533 out << " -- signals for modified input ports of " << block->getName() << endl;
534 foreach(AbstractInterface* iface, listCtlInputs) {
535 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
536 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
537 if (modifier != NULL) {
538 out << modifier->toVHDL(AbstractInputModifier::Signal,0) << endl;
549 out << "begin" << endl;
551 // generate signals that goes to the output ports
553 out << " -- connections to output ports of " << name << endl;
554 QList<AbstractInterface*> listOutputs = getOutputs();
555 foreach(AbstractInterface* iface, listOutputs) {
556 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
557 ConnectedInterface* connIface = AI_TO_CON(iface);
558 ConnectedInterface* fromIface = connIface->getConnectedFrom();
559 out << " " << connIface->getName() << " <= " << fromIface->toVHDL(AbstractInterface::Instance,0) << ";" << endl;
567 // generate instances
568 foreach(AbstractBlock* block, blocks) {
570 out << " " << block->getName() << "_1 : " << block->getName() << endl;
572 QList<BlockParameter*> listGenerics = block->getGenericParameters();
573 QList<AbstractInterface*> listInputs = block->getInputs();
574 QList<AbstractInterface*> listOutputs = block->getOutputs();
575 QList<AbstractInterface*> listBidirs = block->getBidirs();
577 if (!listGenerics.isEmpty()) {
578 out << " generic map (" << endl;
579 for(i=0;i<listGenerics.size()-1;i++) {
580 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance, BlockParameter::NoComma) << "," << endl;
582 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance,BlockParameter::NoComma) << endl;
586 out << " port map (" << endl;
587 QString portMap = "";
589 for(i=0;i<listInputs.size();i++) {
590 ConnectedInterface* connIface = AI_TO_CON(listInputs.at(i));
591 ConnectedInterface* fromIface = connIface->getConnectedFrom();
593 if (fromIface->isFunctionalInterface()) {
594 portMap += " " + connIface->getName() + " => ";
596 if (connIface->getPurpose() == AbstractInterface::Data) {
597 ConnectedInterface* connCtlIface = AI_TO_CON(connIface->getAssociatedIface());
598 if ((connCtlIface != NULL) && (connCtlIface->getInputModifier() != NULL)) {
602 else if (connIface->getPurpose() == AbstractInterface::Control) {
603 if (connIface->getInputModifier() != NULL) {
608 portMap += connIface->getOwner()->getName()+"_"+connIface->getName()+"_mod,\n";
611 portMap += fromIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
614 else if (fromIface->isGroupInterface()) {
615 if ((fromIface->getOwner()->isTopGroupBlock()) && ((fromIface->getPurpose() == AbstractInterface::Data)||(fromIface->getPurpose() == AbstractInterface::Control))) {
616 portMap += " " + connIface->getName() + " => " + fromIface->getOwner()->getName()+ "_"+ fromIface->getName() + "_sync,\n";
619 portMap += " " + connIface->getName() + " => " + fromIface->getName() + ",\n";
623 if (listOutputs.size()>0) {
624 for(i=0;i<listOutputs.size();i++) {
625 ConnectedInterface* connIface = AI_TO_CON(listOutputs.at(i));
626 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
629 if (listBidirs.size()>0) {
630 for(i=0;i<listBidirs.size();i++) {
631 ConnectedInterface* connIface = AI_TO_CON(listBidirs.at(i));
632 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
636 out << portMap << endl;
639 out << " );" << endl;
647 // generate input modifiers
648 foreach(AbstractBlock* block, blocks) {
650 foreach(AbstractInterface* iface, block->getControlInputs()) {
651 ConnectedInterface* connIface = AI_TO_CON(iface);
652 // check if it is connected
653 if (connIface->getConnectedFrom() == NULL) {
654 throw(Exception(IFACE_NOT_CONNECTED,this));
656 AbstractInputModifier* modifier = connIface->getInputModifier();
657 if (modifier != NULL) {
659 out << modifier->toVHDL(AbstractInputModifier::Architecture,0) << endl;
669 // generate input sync process
670 out << " -- process to synchronize inputs of top group" << endl;
671 out << "sync_inputs : process(from_clkrstgen_clk,from_clkrstgen_reset)" << endl;
672 out << " begin" << endl;
673 out << " if from_clkrstgen_reset = '1' then" << endl;
674 foreach(AbstractInterface* iface, getInputs()) {
675 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
676 if (iface->getWidth() == 0) {
677 out << " " << name << "_" << iface->getName() << "_sync <= '0';" << endl;
680 out << " " << name << "_" << iface->getName() << "_sync <= (others => '0');" << endl;
684 out << " elsif rising_edge(from_clkrstgen_clk) then" << endl;
685 foreach(AbstractInterface* iface, getInputs()) {
686 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
687 if (iface->getWidth() == 0) {
688 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
691 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
695 out << " end if;" << endl;
696 out << " end process sync_inputs;" << endl;
701 out << "end architecture rtl;" << endl;
704 void GroupBlock::generateController(QTextStream &out) throw(Exception) {