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
46 cout << "created ext_clk and reset ifaces for top group" << endl;
52 GroupBlock::~GroupBlock() {
53 foreach(AbstractBlock* block, blocks) {
58 bool GroupBlock::isGroupBlock() {
62 bool GroupBlock::isTopGroupBlock() {
66 void GroupBlock::setParent(AbstractBlock *_parent) {
73 void GroupBlock::removeAllBlocks() {
74 foreach(AbstractBlock* block, blocks) {
75 if (block->isGroupBlock()) {
76 GroupBlock* group = AB_TO_GRP(block);
77 group->removeAllBlocks();
83 void GroupBlock::removeBlock(AbstractBlock* block) {
84 /* CAUTION: no check is done if the block has connected interface
85 or not. Thus, they must be deleted elsewhere.
87 blocks.removeAll(block);
91 AbstractBlock *GroupBlock::getFunctionalBlockByName(QString name) {
92 foreach(AbstractBlock* block, blocks) {
93 if (block->isFunctionalBlock()) {
94 if (block->getName() == name) return block;
100 void GroupBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
103 checkedBlocks->append(this);
105 foreach(BlockParameter* param, params){
106 if(param->isUserParameter() && !param->isValueSet()){
107 if(!blocksToConfigure->contains(param->getOwner())){
108 blocksToConfigure->append(param->getOwner());
112 foreach(AbstractInterface *inter, outputs){
113 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
114 if(!checkedBlocks->contains(connectedInter->getOwner())){
115 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
122 void GroupBlock::addGenericParameter(QString name, QString type, QString value) {
123 BlockParameter* param = new BlockParameterGeneric(this, name, type, value);
124 params.append(param);
127 void GroupBlock::removeGenericParameter(QString name) {
128 BlockParameter* p = getParameterFromName(name);
129 if (p != NULL) params.removeAll(p);
132 void GroupBlock::createInputPattern() {
133 foreach(AbstractInterface* iface, getControlInputs()) {
134 ConnectedInterface* connIface = AI_TO_CON(iface);
135 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
136 connIface->setOutputPattern(pattern);
140 void GroupBlock::computeAdmittanceDelays() throw(Exception) {
141 throw(Exception(INVALID_GROUPBLOCK_USE));
144 void GroupBlock::checkInputPatternCompatibility() throw(Exception){
145 throw(Exception(INVALID_GROUPBLOCK_USE));
149 void GroupBlock::computeOutputPattern(int nbExec) throw(Exception) {
151 static QString fctName = "GroupBlock::computeOutputPattern()";
153 cout << "call to " << qPrintable(fctName) << endl;
156 cout << "computing output pattern of group " << qPrintable(name) << endl;
158 bool canCompute = false;
159 // get the input pattern on each inputs
160 createInputPattern();
162 cout << "Input pattern OK" << endl;
163 // find blocks that are connected to that inputs and generators
164 QList<AbstractBlock*> fifo;
165 foreach(AbstractBlock* block, blocks) {
168 // if a block is a generator and has control outputs, add it
169 if (block->isGeneratorBlock()) {
170 if (block->getControlOutputs().size() > 0) addIt = true;
173 // if the block has all its connected control inputs that are connected to an intput of the group, add it too
174 if (block->getControlInputs().size() > 0) {
176 foreach(AbstractInterface* iface, block->getControlInputs()) {
177 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
178 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
179 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
181 if (connFrom == NULL) {
185 else if (connFrom->getOwner() != this) {
193 cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
194 block->setTraversalLevel(0); // level 0 = first blocks to be evaluated
199 while (!fifo.isEmpty()) {
200 AbstractBlock* block = fifo.takeFirst();
202 if (block->getPatternComputed()) continue; // block has already been processed
204 cout << "computing compat and output for " << qPrintable(block->getName()) << endl;
208 block->checkInputPatternCompatibility();
211 cout << qPrintable(block->getName()) << " is not compatible with its input pattern" << endl;
216 block->computeOutputPattern();
219 cout << "cannot finalize output pattern computation of " << qPrintable(block->getName()) << endl;
223 block->setPatternComputed(true);
224 /* add other blocks connected from block to the fifo but only if
225 all their connected inputs are connected to blocks that have
228 foreach(AbstractInterface* iface, block->getControlOutputs()) {
229 ConnectedInterface* conn = (ConnectedInterface*)iface;
230 foreach(ConnectedInterface* connTo, conn->getConnectedTo()) {
232 AbstractBlock* block1 = connTo->getOwner();
233 cout << "testing if " << qPrintable(block1->getName()) << " has all connected inputs connected to already processed blocks" << endl;
237 foreach(AbstractInterface* iface, block1->getControlInputs()) {
238 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
239 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
240 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
242 if ((connFrom != NULL) && (connFrom->getOwner()->getPatternComputed() == false)) {
247 if (connFrom->getOwner()->getTraversalLevel() > maxLevel) maxLevel = connFrom->getOwner()->getTraversalLevel();
252 cout << "adding " << qPrintable(block1->getName()) << " to the FIFO" << endl;
253 block1->setTraversalLevel(maxLevel+1); // level 0 = first blocks to be evaluated
261 foreach(AbstractInterface* iface, getControlOutputs()) {
262 ConnectedInterface* connIface = AI_TO_CON(iface);
263 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
264 connIface->setOutputPattern(pattern);
266 setPatternComputed(true);
270 QList<QString> GroupBlock::getExternalResources() {
273 foreach(AbstractBlock* block, blocks) {
274 list.append(block->getExternalResources());
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 foreach(AbstractBlock* block, blocks) {
304 block->generateVHDL(path);
307 catch(Exception err) {
315 void GroupBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
316 out << " -- VHDL generated automatically for " << name << " --" << endl << endl;
319 void GroupBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
321 out << "library IEEE;" << endl;
322 out << "use IEEE.STD_LOGIC_1164.all;" << endl;
323 out << "use IEEE.numeric_std.all;" << endl;
327 void GroupBlock::generateEntityOrComponentBody(QTextStream& out, int indentLevel, bool hasController) throw(Exception) {
331 for(i=0;i<indentLevel;i++) {
335 QList<BlockParameter*> listGenerics = getGenericParameters();
336 QList<AbstractInterface*> listInputs = getInputs();
337 QList<AbstractInterface*> listOutputs = getOutputs();
338 QList<AbstractInterface*> listBidirs = getBidirs();
340 if (!listGenerics.isEmpty()) {
341 out << indent << " generic (" << endl;
342 for(i=0;i<listGenerics.size()-1;i++) {
343 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
345 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
346 out << indent << " );" << endl;
349 out << indent << " port (" << endl;
351 // Generation of the clk & rst signals
352 out << indent << " -- clk/rst" << endl;
353 foreach(AbstractInterface* iface, listInputs) {
354 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
355 out << indent << " " << iface->getName() << " : in std_logic;" << endl;
360 foreach(AbstractInterface* iface, getInterfaces()) {
361 if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
363 // Generation of the data/control signals
368 foreach(AbstractInterface* iface, listInputs) {
369 if(iface->getPurpose() == AbstractInterface::Data) {
371 out << indent << " -- input data ports" << endl;
375 if (count == 0) flag = AbstractInterface::NoComma;
376 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
380 foreach(AbstractInterface* iface, listInputs) {
381 if(iface->getPurpose() == AbstractInterface::Control) {
383 out << indent << " -- input control ports" << endl;
387 if (count == 0) flag = AbstractInterface::NoComma;
388 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
392 foreach(AbstractInterface* iface, listOutputs) {
393 if(iface->getPurpose() == AbstractInterface::Data) {
395 out << indent << " -- output data ports" << endl;
399 if (count == 0) flag = AbstractInterface::NoComma;
400 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
404 foreach(AbstractInterface* iface, listOutputs) {
405 if(iface->getPurpose() == AbstractInterface::Control) {
407 out << indent << " -- output control ports" << endl;
411 if (count == 0) flag = AbstractInterface::NoComma;
412 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
416 foreach(AbstractInterface* iface, listBidirs) {
417 if(iface->getPurpose() == AbstractInterface::Data) {
419 out << indent << " -- bidirs data ports" << endl;
423 if (count == 0) flag = AbstractInterface::NoComma;
424 out << indent << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
427 out << indent << " );" << endl << endl;
430 void GroupBlock::generateArchitecture(QTextStream& out, QDomElement &elt) throw(Exception) {
434 out << "architecture rtl of " << name << " is " << endl << endl;
436 // generate type for delays, if needed.
438 foreach(AbstractBlock* block, blocks) {
439 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
440 foreach(AbstractInterface* iface, listCtlInputs) {
441 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
442 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
443 if (modifier != NULL) {
444 ConnectedInterface* connIface = AI_TO_CON(connCtlIface->getAssociatedIface());
445 int w = connIface->getWidth();
446 if (w == -1) throw(Exception(INVALID_VALUE));
447 if (!modWidth.contains(w)) {
453 if (modWidth.size() > 0) {
455 out << " -- types for modified inputs" << endl;
456 out << " type vector_of_std_logic is array (natural range <>) of std_logic;" << endl;
457 foreach(int w, modWidth) {
462 out << " type vector_of_std_logic_vector"<< mw << " is array (natural range <>) of std_logic_vector(" << mwm1 << " downto 0);" << endl;
468 // generate the components
469 foreach(AbstractBlock* block, blocks) {
471 block->generateComponent(out,false);
480 out << " ----------------------------" << endl;
481 out << " -- SIGNALS" << endl;
482 out << " ----------------------------" << endl << endl;
484 // signals to synchronize inputs
485 out << " -- signals to synchronize inputs" << endl;
486 foreach(AbstractInterface* iface, getInputs()) {
487 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
488 QString name = iface->toVHDL(AbstractInterface::Signal,0);
489 name.replace(" : ","_sync : ");
490 out << " signal " << name<< endl;
496 foreach(AbstractBlock* block, blocks) {
498 out << " -- signals from output ports of " << block->getName() << endl;
499 QList<AbstractInterface*> listOutputs = block->getOutputs();
500 foreach(AbstractInterface* iface, listOutputs) {
501 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
502 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
504 else if (block->getName() == "clkrstgen") {
505 if ((iface->getPurpose() == AbstractInterface::Clock)||(iface->getPurpose() == AbstractInterface::Reset)) {
506 out << " signal " << iface->toVHDL(AbstractInterface::Signal,0) << endl;
517 // signal for modifiers
518 foreach(AbstractBlock* block, blocks) {
519 bool hasModif = false;
520 QList<AbstractInterface*> listCtlInputs = block->getControlInputs();
522 foreach(AbstractInterface* iface, listCtlInputs) {
523 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
524 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
525 if (modifier != NULL) {
532 out << " -- signals for modified input ports of " << block->getName() << endl;
533 foreach(AbstractInterface* iface, listCtlInputs) {
534 ConnectedInterface* connCtlIface = AI_TO_CON(iface);
535 AbstractInputModifier* modifier = connCtlIface->getInputModifier();
536 if (modifier != NULL) {
537 out << modifier->toVHDL(AbstractInputModifier::Signal,0) << endl;
548 out << "begin" << endl;
550 // generate signals that goes to the output ports
552 out << " -- connections to output ports of " << name << endl;
553 QList<AbstractInterface*> listOutputs = getOutputs();
554 foreach(AbstractInterface* iface, listOutputs) {
555 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
556 ConnectedInterface* connIface = AI_TO_CON(iface);
557 ConnectedInterface* fromIface = connIface->getConnectedFrom();
558 out << " " << connIface->getName() << " <= " << fromIface->toVHDL(AbstractInterface::Instance,0) << ";" << endl;
566 // generate instances
567 foreach(AbstractBlock* block, blocks) {
569 out << " " << block->getName() << "_1 : " << block->getName() << endl;
571 QList<BlockParameter*> listGenerics = block->getGenericParameters();
572 QList<AbstractInterface*> listInputs = block->getInputs();
573 QList<AbstractInterface*> listOutputs = block->getOutputs();
574 QList<AbstractInterface*> listBidirs = block->getBidirs();
576 if (!listGenerics.isEmpty()) {
577 out << " generic map (" << endl;
578 for(i=0;i<listGenerics.size()-1;i++) {
579 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance, BlockParameter::NoComma) << "," << endl;
581 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Instance,BlockParameter::NoComma) << endl;
585 out << " port map (" << endl;
586 QString portMap = "";
588 for(i=0;i<listInputs.size();i++) {
589 ConnectedInterface* connIface = AI_TO_CON(listInputs.at(i));
590 ConnectedInterface* fromIface = connIface->getConnectedFrom();
592 if (fromIface->isFunctionalInterface()) {
593 portMap += " " + connIface->getName() + " => ";
595 if (connIface->getPurpose() == AbstractInterface::Data) {
596 ConnectedInterface* connCtlIface = AI_TO_CON(connIface->getAssociatedIface());
597 if ((connCtlIface != NULL) && (connCtlIface->getInputModifier() != NULL)) {
601 else if (connIface->getPurpose() == AbstractInterface::Control) {
602 if (connIface->getInputModifier() != NULL) {
607 portMap += connIface->getOwner()->getName()+"_"+connIface->getName()+"_mod,\n";
610 portMap += fromIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
613 else if (fromIface->isGroupInterface()) {
614 if ((fromIface->getOwner()->isTopGroupBlock()) && ((fromIface->getPurpose() == AbstractInterface::Data)||(fromIface->getPurpose() == AbstractInterface::Control))) {
615 portMap += " " + connIface->getName() + " => " + fromIface->getOwner()->getName()+ "_"+ fromIface->getName() + "_sync,\n";
618 portMap += " " + connIface->getName() + " => " + fromIface->getName() + ",\n";
622 if (listOutputs.size()>0) {
623 for(i=0;i<listOutputs.size();i++) {
624 ConnectedInterface* connIface = AI_TO_CON(listOutputs.at(i));
625 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
628 if (listBidirs.size()>0) {
629 for(i=0;i<listBidirs.size();i++) {
630 ConnectedInterface* connIface = AI_TO_CON(listBidirs.at(i));
631 portMap += " " + connIface->getName() + " => " + connIface->toVHDL(AbstractInterface::Instance, AbstractInterface::NoComma) + ",\n";
635 out << portMap << endl;
638 out << " );" << endl;
646 // generate input modifiers
647 foreach(AbstractBlock* block, blocks) {
649 foreach(AbstractInterface* iface, block->getControlInputs()) {
650 ConnectedInterface* connIface = AI_TO_CON(iface);
651 // check if it is connected
652 if (connIface->getConnectedFrom() == NULL) {
653 throw(Exception(IFACE_NOT_CONNECTED,this));
655 AbstractInputModifier* modifier = connIface->getInputModifier();
656 if (modifier != NULL) {
658 out << modifier->toVHDL(AbstractInputModifier::Architecture,0) << endl;
668 // generate input sync process
669 out << " -- process to synchronize inputs of top group" << endl;
670 out << "sync_inputs : process(from_clkrstgen_clk,from_clkrstgen_reset)" << endl;
671 out << " begin" << endl;
672 out << " if from_clkrstgen_reset = '1' then" << endl;
673 foreach(AbstractInterface* iface, getInputs()) {
674 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
675 if (iface->getWidth() == 0) {
676 out << " " << name << "_" << iface->getName() << "_sync <= '0';" << endl;
679 out << " " << name << "_" << iface->getName() << "_sync <= (others => '0');" << endl;
683 out << " elsif rising_edge(from_clkrstgen_clk) then" << endl;
684 foreach(AbstractInterface* iface, getInputs()) {
685 if ((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) {
686 if (iface->getWidth() == 0) {
687 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
690 out << " " << name << "_" << iface->getName() << "_sync <= " << iface->getName() << ";" << endl;
694 out << " end if;" << endl;
695 out << " end process sync_inputs;" << endl;
700 out << "end architecture rtl;" << endl;
703 void GroupBlock::generateController(QTextStream &out) throw(Exception) {