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);
284 QDomElement dummyElt;
286 generateComments(outCore,dummyElt,"");
287 generateLibraries(outCore,dummyElt);
288 generateEntity(outCore);
289 generateArchitecture(outCore,dummyElt);
291 catch(Exception err) {
299 void GroupBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
300 out << " -- VHDL generated automatically for " << name << " --" << endl << endl;
303 void GroupBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
305 out << "library IEEE;" << endl;
306 out << "use IEEE.STD_LOGIC_1164.all;" << endl;
307 out << "use IEEE.numeric_std.all;" << endl;
311 void GroupBlock::generateEntity(QTextStream& out, bool hasController) throw(Exception) {
315 out << "entity " << name << " is " << endl;
317 QList<BlockParameter*> listGenerics = getGenericParameters();
318 QList<AbstractInterface*> listInputs = getInputs();
319 QList<AbstractInterface*> listOutputs = getOutputs();
320 QList<AbstractInterface*> listBidirs = getBidirs();
322 if (!listGenerics.isEmpty()) {
323 out << " generic (" << endl;
324 for(i=0;i<listGenerics.size()-1;i++) {
325 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
327 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
328 out << " );" << endl;
331 out << " port (" << endl;
333 // Generation of the clk & rst signals
334 out << " -- clk/rst" << endl;
335 foreach(AbstractInterface* iface, listInputs) {
336 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
337 out << " " << iface->getName() << " : in std_logic;" << endl;
342 foreach(AbstractInterface* iface, getInterfaces()) {
343 if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
345 // Generation of the data/control signals
350 foreach(AbstractInterface* iface, listInputs) {
351 if(iface->getPurpose() == AbstractInterface::Data) {
353 out << " -- input data ports" << endl;
357 if (count == 0) flag = AbstractInterface::NoComma;
358 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
362 foreach(AbstractInterface* iface, listInputs) {
363 if(iface->getPurpose() == AbstractInterface::Control) {
365 out << " -- input control ports" << endl;
369 if (count == 0) flag = AbstractInterface::NoComma;
370 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
374 foreach(AbstractInterface* iface, listOutputs) {
375 if(iface->getPurpose() == AbstractInterface::Data) {
377 out << " -- output data ports" << endl;
381 if (count == 0) flag = AbstractInterface::NoComma;
382 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
386 foreach(AbstractInterface* iface, listOutputs) {
387 if(iface->getPurpose() == AbstractInterface::Control) {
389 out << " -- output control ports" << endl;
393 if (count == 0) flag = AbstractInterface::NoComma;
394 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
398 foreach(AbstractInterface* iface, listBidirs) {
399 if(iface->getPurpose() == AbstractInterface::Data) {
401 out << " -- bidirs data ports" << endl;
405 if (count == 0) flag = AbstractInterface::NoComma;
406 out << " " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
409 out << " );" << endl << endl;
410 out << "end " << name << ";" << endl << endl;
414 void GroupBlock::generateArchitecture(QTextStream& out, QDomElement &elt) throw(Exception) {
418 void GroupBlock::generateController(QTextStream &out) throw(Exception) {