1 #include "GroupBlock.h"
2 #include "BlockParameterGeneric.h"
3 #include "AbstractInterface.h"
4 #include "ConnectedInterface.h"
5 #include "GroupInterface.h"
8 #include "Parameters.h"
10 int GroupBlock::counter = 1;
12 GroupBlock::GroupBlock(GroupBlock *_parent) throw(Exception) : AbstractBlock() {
14 GroupInterface* clk = NULL;
15 GroupInterface* rst = NULL;
17 // force topGroup to false if this group has a parent
18 if (_parent != NULL) {
20 name = QString("sub_group")+"_"+QString::number(counter++);
21 // creating clk/rst interfaces
22 clk = new GroupInterface(this,"clk", AbstractInterface::Input, AbstractInterface::Clock);
23 rst = new GroupInterface(this,"reset", AbstractInterface::Input, AbstractInterface::Reset);
29 name = QString("top_group");
30 // creating external clk/rst interfaces
31 clk = new GroupInterface(this,"ext_clk", AbstractInterface::Input, AbstractInterface::Clock);
32 rst = new GroupInterface(this,"ext_reset", AbstractInterface::Input, AbstractInterface::Reset);
35 // creating clkrstgen block and connecting it to this: done in Dispatcher since this has no access to library
39 if (_parent != NULL) {
44 AbstractBlock* source = (AbstractBlock *)(e.getSource());
45 cerr << qPrintable(source->getName()) << ":" << qPrintable(e.getMessage()) << 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 inputs that are connected to an intput of the group, add it too
175 foreach(AbstractInterface* iface, block->getControlInputs()) {
176 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
177 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
178 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
180 if (connFrom == NULL) {
184 else if (connFrom->getOwner() != this) {
191 cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
192 block->setTraversalLevel(0); // level 0 = first blocks to be evaluated
197 while (!fifo.isEmpty()) {
198 AbstractBlock* block = fifo.takeFirst();
200 if (block->getPatternComputed()) continue; // block has already been processed
202 cout << "computing compat and output for " << qPrintable(block->getName()) << endl;
206 block->checkInputPatternCompatibility();
209 cout << qPrintable(block->getName()) << " is not compatible with its input pattern" << endl;
214 block->computeOutputPattern();
217 cout << "cannot finalize output pattern computation of " << qPrintable(block->getName()) << endl;
221 block->setPatternComputed(true);
222 /* add other blocks connected from block to the fifo but only if
223 all their connected inputs are connected to blocks that have
226 foreach(AbstractInterface* iface, block->getControlOutputs()) {
227 ConnectedInterface* conn = (ConnectedInterface*)iface;
228 foreach(ConnectedInterface* connTo, conn->getConnectedTo()) {
230 AbstractBlock* block1 = connTo->getOwner();
231 cout << "testing if " << qPrintable(block1->getName()) << " has all connected inputs connected to already processed blocks" << endl;
235 foreach(AbstractInterface* iface, block1->getControlInputs()) {
236 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
237 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
238 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
240 if ((connFrom != NULL) && (connFrom->getOwner()->getPatternComputed() == false)) {
245 if (connFrom->getOwner()->getTraversalLevel() > maxLevel) maxLevel = connFrom->getOwner()->getTraversalLevel();
250 cout << "adding " << qPrintable(block1->getName()) << " to the FIFO" << endl;
251 block1->setTraversalLevel(maxLevel+1); // level 0 = first blocks to be evaluated
259 foreach(AbstractInterface* iface, getControlOutputs()) {
260 ConnectedInterface* connIface = AI_TO_CON(iface);
261 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
262 connIface->setOutputPattern(pattern);
264 setPatternComputed(true);
268 void GroupBlock::generateVHDL(const QString& path) throw(Exception) {
270 QString coreFile = "";
273 coreFile.append(Parameters::normalizeName(name));
274 coreFile.append(".vhd");
276 QFile vhdlCore(coreFile);
278 if (!vhdlCore.open(QIODevice::WriteOnly)) {
279 throw(Exception(VHDLFILE_NOACCESS));
282 QTextStream outCore(&vhdlCore);
285 generateComments(outCore);
286 generateLibraries(outCore);
287 generateEntity(outCore);
288 generateArchitecture(outCore);
290 catch(Exception err) {
298 void GroupBlock::generateComments(QTextStream& out) throw(Exception) {
299 out << " -- VHDL generated automatically for " << name << " --" << endl << endl;
302 void GroupBlock::generateLibraries(QTextStream& out) throw(Exception) {
304 out << "library IEEE;" << endl;
305 out << "use IEEE.STD_LOGIC_1164.all;" << endl;
306 out << "use IEEE.numeric_std.all;" << endl;
310 void GroupBlock::generateEntity(QTextStream& out) throw(Exception) {
314 out << "entity " << name << " is " << endl;
316 QList<BlockParameter*> listGenerics = getGenericParameters();
317 QList<AbstractInterface*> listInputs = getInputs();
318 QList<AbstractInterface*> listOutputs = getOutputs();
319 QList<AbstractInterface*> listBidirs = getBidirs();
321 if (!listGenerics.isEmpty()) {
322 out << " generic (" << endl;
323 for(i=0;i<listGenerics.size()-1;i++) {
324 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
326 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
327 out << " );" << endl;
330 out << " port (" << endl;
332 // Generation of the clk & rst signals
333 out << " -- clk/rst" << endl;
334 foreach(AbstractInterface* iface, listInputs) {
335 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
336 out << " " << iface->getName() << " : in std_logic;" << endl;
341 foreach(AbstractInterface* iface, getInterfaces()) {
342 if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
344 // Generation of the data/control signals
349 foreach(AbstractInterface* iface, listInputs) {
350 if(iface->getPurpose() == AbstractInterface::Data) {
352 out << " -- input data ports" << endl;
356 if (count == 0) flag = AbstractInterface::NoComma;
357 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
361 foreach(AbstractInterface* iface, listInputs) {
362 if(iface->getPurpose() == AbstractInterface::Control) {
364 out << " -- input control ports" << endl;
368 if (count == 0) flag = AbstractInterface::NoComma;
369 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
373 foreach(AbstractInterface* iface, listOutputs) {
374 if(iface->getPurpose() == AbstractInterface::Data) {
376 out << " -- output data ports" << endl;
380 if (count == 0) flag = AbstractInterface::NoComma;
381 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
385 foreach(AbstractInterface* iface, listOutputs) {
386 if(iface->getPurpose() == AbstractInterface::Control) {
388 out << " -- output control ports" << endl;
392 if (count == 0) flag = AbstractInterface::NoComma;
393 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
397 foreach(AbstractInterface* iface, listBidirs) {
398 if(iface->getPurpose() == AbstractInterface::Data) {
400 out << " -- bidirs data ports" << endl;
404 if (count == 0) flag = AbstractInterface::NoComma;
405 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
408 out << " );" << endl << endl;
409 out << "end " << name << ";" << endl << endl;
413 void GroupBlock::generateArchitecture(QTextStream& out) throw(Exception) {