1 #include "FunctionalBlock.h"
\r
2 #include "ReferenceBlock.h"
\r
3 #include "GroupBlock.h"
\r
4 #include "AbstractInterface.h"
\r
5 #include "FunctionalInterface.h"
\r
6 #include "ReferenceInterface.h"
\r
7 #include "BlockParameter.h"
\r
8 #include "ArithmeticEvaluator.h"
\r
11 FunctionalBlock::FunctionalBlock(Graph *_graph, GroupBlock *_parent, ReferenceBlock *_reference, bool createIfaces) throw(Exception) : AbstractBlock(_graph) {
\r
12 //if (! _reference->isReferenceBlock()) throw(Exception(BLOCK_INVALID_TYPE));
\r
13 //if (! _group->isGroupBlock()) throw(Exception(BLOCK_INVALID_TYPE));
\r
14 reference = _reference;
\r
16 name = reference->getName();
\r
18 if (reference->getImplementations().isEmpty()) {
\r
19 implementation = NULL;
\r
20 cout << "block has no implementation" << endl;
\r
23 implementation = reference->getImplementations().at(0);
\r
34 // create parameters from reference block
\r
35 QList<BlockParameter*> lstParam = reference->getParameters();
\r
36 for(int i=0;i<lstParam.size();i++) {
\r
37 p = lstParam.at(i)->clone();
\r
46 FunctionalBlock::~FunctionalBlock() {
\r
47 if (evaluator != NULL) delete evaluator;
\r
50 void FunctionalBlock::parametersValidation(QList<AbstractBlock*>* checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
\r
52 checkedBlocks->append(this);
\r
54 foreach(BlockParameter* param, params){
\r
55 if(param->isUserParameter() && !param->isValueSet()){
\r
56 if(!blocksToConfigure->contains(param->getOwner())){
\r
57 blocksToConfigure->append(param->getOwner());
\r
61 foreach(AbstractInterface *inter, outputs){
\r
62 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
\r
63 if(!checkedBlocks->contains(connectedInter->getOwner())){
\r
64 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
\r
71 bool FunctionalBlock::isFunctionalBlock() {
\r
75 bool FunctionalBlock::isStimuliBlock() {
\r
76 if (parent == NULL) return true;
\r
80 void FunctionalBlock::populate() {
\r
83 AbstractInterface* inter;
\r
84 ConnectedInterface* toClk = NULL;
\r
85 ConnectedInterface* toRst = NULL;
\r
86 // create interfaces from reference block
\r
87 QList<AbstractInterface *> lstRef = reference->getInterfaces();
\r
88 // store relation between functional and reference
\r
89 QHash<AbstractInterface *, AbstractInterface *> hashIface;
\r
90 for(i=0;i<lstRef.size();i++) {
\r
92 inter = new FunctionalInterface(this, AI_TO_REF(lstRef.at(i)));
\r
94 catch(Exception e) {
\r
95 cerr << "Abnormal case: " << qPrintable(e.getDefaultMessage()) << endl << "Aborting execution." << endl;
\r
98 hashIface.insert(lstRef.at(i),inter);
\r
99 addInterface(inter);
\r
100 /* WARNING FOR THE FUTURE :
\r
101 in case of there are several clock interfaces ofr that block
\r
102 it would be a good idea to make the user choose which one
\r
103 must be connected to defautl clk.
\r
104 Presently, the first encountered is chosen
\r
106 if ((toClk == NULL) && (inter->getPurpose() == AbstractInterface::Clock)) {
\r
107 toClk = AI_TO_CON(inter);
\r
109 if ((toRst == NULL) && (inter->getPurpose() == AbstractInterface::Reset)) {
\r
110 toRst = AI_TO_CON(inter);
\r
114 AbstractInterface* funCtlIface = NULL;
\r
115 AbstractInterface* funDataIface = NULL;
\r
117 for(i=0;i<lstRef.size();i++) {
\r
118 AbstractInterface* refIface = lstRef.at(i);
\r
119 if (refIface->getPurpose() == AbstractInterface::Control) {
\r
120 funCtlIface = hashIface.value(refIface);
\r
121 funDataIface = hashIface.value(refIface->getAssociatedIface());
\r
122 if (! funCtlIface->setAssociatedIface(funDataIface)) {
\r
123 cerr << "Abnormal case when associating a control interface to data" << endl << "Aborting execution." << endl;
\r
130 QString FunctionalBlock::getReferenceXmlFile() {
\r
131 return ((ReferenceBlock *)reference)->getXmlFile();
\r
134 QString FunctionalBlock::getReferenceHashMd5() {
\r
135 return ((ReferenceBlock *)reference)->getHashMd5();
\r
138 void FunctionalBlock::createPatterns() throw(Exception) {
\r
139 static QString fctName = "FunctionalBlock::createPatterns()";
\r
140 #ifdef DEBUG_FCTNAME
\r
141 cout << "call to " << qPrintable(fctName) << endl;
\r
144 if (implementation->hasNoPatterns()) return;
\r
146 cout << "create patterns for block " << qPrintable(name) << endl;
\r
147 if (evaluator == NULL) evaluator = new ArithmeticEvaluator();
\r
148 if (! isSourceBlock()) {
\r
151 createConsumptionPattern();
\r
152 createProductionCounter();
\r
154 catch(Exception e) {
\r
155 throw(e); // rethrow e
\r
159 createProductionPattern();
\r
161 catch(Exception e) {
\r
164 cout << "PP of " << qPrintable(name) << endl;
\r
165 QMapIterator<AbstractInterface*,QList<char>* > it(productionPattern);
\r
166 while (it.hasNext()) {
\r
168 QList<char>* pat = it.value();
\r
169 foreach(char c, *pat) cout << (int)c;
\r
174 void FunctionalBlock::createDelta() throw(Exception) {
\r
175 static QString fctName = "FunctionalBlock::createDelta()";
\r
176 #ifdef DEBUG_FCTNAME
\r
177 cout << "call to " << qPrintable(fctName) << endl;
\r
180 QString deltaStr = implementation->getDelta();
\r
181 cout << "delta for " << qPrintable(name) << " = " << qPrintable(deltaStr) << endl;
\r
182 if (deltaStr.isEmpty()) {
\r
187 // look for parameter names
\r
190 result = evaluateExpression(deltaStr);
\r
192 catch(Exception e) {
\r
196 cout << "delta = " << delta << endl;
\r
199 void FunctionalBlock::createConsumptionPattern() throw(Exception) {
\r
200 static QString fctName = "FunctionalBlock::createConsumptionPattern()";
\r
201 #ifdef DEBUG_FCTNAME
\r
202 cout << "call to " << qPrintable(fctName) << endl;
\r
205 // first clear if already exists
\r
206 clearConsumptionPattern();
\r
209 QHash<QString,QString> consPattern = implementation->getConsumptionPattern();
\r
211 foreach(AbstractInterface* iface, getControlInputs()) {
\r
212 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
213 QString refName = connIface->getReference()->getName();
\r
214 if (! consPattern.contains(refName)) {
\r
215 throw(Exception(NO_IFACE_CP,this));
\r
216 cerr << "no consumption pattern for reference interface " << qPrintable(refName) << endl;
\r
218 QList<char>* pattern = NULL;
\r
220 pattern = expandPattern(consPattern.value(refName));
\r
222 catch(Exception e) {
\r
225 consumptionPattern.insert(connIface,pattern);
\r
226 if (lengthCP == -1) {
\r
227 lengthCP = pattern->size();
\r
230 if (pattern->size() != lengthCP) {
\r
231 throw(Exception(INVALID_IFACE_CP_LENGTH,this));
\r
237 void FunctionalBlock::createProductionPattern() throw(Exception){
\r
238 static QString fctName = "FunctionalBlock::createProductionPattern()";
\r
239 #ifdef DEBUG_FCTNAME
\r
240 cout << "call to " << qPrintable(fctName) << endl;
\r
243 // first clear if already exists
\r
244 clearProductionPattern();
\r
247 QHash<QString,QString> prodPattern = implementation->getProductionPattern();
\r
249 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
250 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
251 QString refName = connIface->getReference()->getName();
\r
252 if (! prodPattern.contains(refName)) {
\r
253 throw(Exception(NO_IFACE_PP,this));
\r
255 QList<char>* pattern = NULL;
\r
257 pattern = expandPattern(prodPattern.value(refName));
\r
259 catch(Exception e) {
\r
262 productionPattern.insert(connIface,pattern);
\r
263 if (lengthPP == -1) {
\r
264 lengthPP = pattern->size();
\r
267 if (pattern->size() != lengthPP) {
\r
268 throw(Exception(INVALID_IFACE_PP_LENGTH,this));
\r
274 void FunctionalBlock::createProductionCounter() throw(Exception) {
\r
275 static QString fctName = "FunctionalBlock::createProductionCounter()";
\r
276 #ifdef DEBUG_FCTNAME
\r
277 cout << "call to " << qPrintable(fctName) << endl;
\r
280 // first clear if already exists
\r
281 productionCounter.clear();
\r
284 QStringList counterParts = implementation->getProductionCounter().split(",");
\r
285 foreach(QString s, counterParts) {
\r
286 cout << "cont part = " << qPrintable(s) << endl;
\r
288 double val = s.toDouble(&ok);
\r
290 productionCounter.append(val);
\r
292 else if (s.at(0) == '{') {
\r
295 QStringList gen = s.split(":");
\r
296 if (gen.size() != 3) {
\r
297 throw(Exception(INVALID_IFACE_PC,this));
\r
302 for(int i=0;i<3;i++) {
\r
303 double result = 0.0;
\r
305 result = evaluateExpression(gen.at(i));
\r
307 catch(Exception e) {
\r
310 if (i==0) start = result;
\r
311 else if (i==1) nb = result;
\r
312 else if (i==2) step = result;
\r
314 for(int j=0;j<nb;j++) {
\r
315 productionCounter.append(start+j*step);
\r
319 double result = 0.0;
\r
321 result = evaluateExpression(s);
\r
323 catch(Exception e) {
\r
326 productionCounter.append(result);
\r
329 foreach(int val, productionCounter) {
\r
330 cout << val << ",";
\r
335 QList<char>* FunctionalBlock::expandPattern(const QString& patternIn) throw(Exception) {
\r
336 static QString fctName = "FunctionalBlock::expandPattern()";
\r
337 #ifdef DEBUG_FCTNAME
\r
338 cout << "call to " << qPrintable(fctName) << endl;
\r
340 /* expanding a pattern is done in two steps :
\r
341 - 1 : finding all variables that correspond to an expression
\r
342 and copy them in the pattern
\r
343 - 2 : parsing the result
\r
345 Note that the result MUST contain only variables that have a
\r
346 integer/double value. Otherwise, expanding will fail.
\r
352 QString p = replaceExpressions(patternIn);
\r
357 QList<char>* patternOut = new QList<char>();
\r
359 patternOut->append(expandPatternRecur(p,&offset));
\r
361 catch(Exception e) {
\r
368 QString FunctionalBlock::replaceExpressions(const QString& patternIn) throw(Exception) {
\r
370 QString res = patternIn;
\r
372 QRegularExpression re("[$][a-zA-Z0-9_]+");
\r
376 QRegularExpressionMatchIterator matcher = re.globalMatch(res);
\r
377 while(matcher.hasNext()) {
\r
378 QRegularExpressionMatch m = matcher.next();
\r
379 QString param = m.captured(0);
\r
380 QString paramName = param;
\r
381 paramName.remove(0,1);
\r
382 BlockParameter* p = getParameterFromName(paramName);
\r
383 if ((p != NULL) && (p->getType() == BlockParameter::Expression)) {
\r
384 res.replace(param,p->getStringValue());
\r
386 cout << "found an expr: " << qPrintable(paramName) << ", patern => " << qPrintable(res) << endl;
\r
393 QList<char> FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset) throw(Exception) {
\r
395 QList<char> patternOut;
\r
397 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != ')')) {
\r
399 QChar c = patternIn.at(*offset);
\r
403 patternOut.append(expandPatternRecur(patternIn,offset));
\r
405 catch(Exception e) {
\r
409 else if (c == '0') {
\r
410 patternOut.append(0);
\r
412 else if (c == '1') {
\r
413 patternOut.append(1);
\r
415 else if (c == 'X') {
\r
416 patternOut.append(-1);
\r
418 else if (c == '{') {
\r
420 QString expr = "";
\r
421 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
\r
422 expr += patternIn.at(*offset);
\r
425 if (*offset == patternIn.size()) {
\r
426 throw(Exception(INVALID_IFACE_PATTERN,this));
\r
430 repeat = evaluateExpression(expr);
\r
432 catch(Exception e) {
\r
437 patternOut.removeLast();
\r
440 // repeat just the last value in currentGroup
\r
441 char last = patternOut.last();
\r
442 //cout << "repeat last char " << repeat << " times : " << (int)last << endl;
\r
443 for(int i=1;i<(int)repeat;i++) {
\r
444 patternOut.append(last);
\r
451 // must check if after ), there is a {
\r
452 if ((*offset < patternIn.size()-1) && (patternIn.at(*offset+1) == '{')) {
\r
454 QString expr = "";
\r
455 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
\r
456 expr += patternIn.at(*offset);
\r
459 if (*offset == patternIn.size()) {
\r
460 throw(Exception(INVALID_IFACE_PATTERN,this));
\r
464 repeat = evaluateExpression(expr);
\r
466 catch(Exception e) {
\r
470 QList<char> voidList;
\r
475 cout << "repeat last group " << repeat << " times : ";
\r
476 foreach (char c, currentGroup) cout <<(int)c;
\r
479 QList<char> single = patternOut;
\r
480 for(int i=1;i<(int)repeat;i++) {
\r
481 patternOut.append(single);
\r
488 double FunctionalBlock::evaluateExpression(const QString& expression) throw(Exception) {
\r
489 static QString fctName = "FunctionalBlock::evaluateExpression()";
\r
490 #ifdef DEBUG_FCTNAME
\r
491 cout << "call to " << qPrintable(fctName) << endl;
\r
494 QHash<QString,double> vars;
\r
495 evaluator->setExpression(expression);
\r
496 QList<QString> varNames = evaluator->getVariableNames();
\r
497 foreach (QString name, varNames) {
\r
498 QString paramName = name;
\r
499 paramName.remove(0,1);
\r
500 BlockParameter* param = getParameterFromName(paramName);
\r
501 if (param == NULL) {
\r
502 throw(Exception(EVAL_PARAM_UNKNOWN,this));
\r
505 int val = param->getDoubleValue(&okVal);
\r
507 throw(Exception(EVAL_PARAM_NOVALUE,this));
\r
509 vars.insert(name,(double)val);
\r
512 evaluator->setVariablesValue(vars);
\r
513 double result = 0.0;
\r
515 result = evaluator->evaluate();
\r
518 cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl;
\r
519 throw(Exception(EVAL_INVALID_EXPR,this));
\r
524 void FunctionalBlock::computeAdmittanceDelays() throw(Exception) {
\r
525 static QString fctName = "FunctionalBlock::computeAdmittanceDelays()";
\r
526 #ifdef DEBUG_FCTNAME
\r
527 cout << "call to " << qPrintable(fctName) << endl;
\r
529 QList<int> inClock;
\r
532 clearAdmittanceDelays();
\r
534 // trying to synchronize the first one in AP
\r
535 QMapIterator<AbstractInterface*,QList<char>* > iterAP(admittance);
\r
536 QMapIterator<AbstractInterface*,QList<char>* > iterIP(inputPattern);
\r
538 while (iterAP.hasNext()) {
\r
541 QList<char>* ap = iterAP.value();
\r
542 QList<char>* ip = iterIP.value();
\r
544 while ((first < lengthIP) && (ip->at(first) == 0)) first++;
\r
545 while ((first < lengthAP) && (ap->at(first) == 0)) first--;
\r
546 delays.append(first);
\r
548 QList<int>* delays = new QList<int>();
\r
549 admittanceDelays.insert(iterAP.key(), delays);
\r
552 QMapIterator<AbstractInterface*,QList<int>* > iterDelays(admittanceDelays);
\r
554 // get the delay to apply
\r
556 for(int i=0;i<delays.size();i++) {
\r
557 if (delays[i] > maxDelay) maxDelay = delays[i];
\r
559 // adding the delays to IP
\r
562 while (iterIP.hasNext()) {
\r
565 QList<char>* ip = iterIP.value();
\r
566 QList<int>* d = iterDelays.value();
\r
567 d->append(maxDelay-delays[i]);
\r
568 cout << "prependind " << qPrintable(iterIP.key()->getName()) << " with " << (maxDelay-delays[i]) << " 0" << endl;
\r
569 for(int j=0;j<maxDelay-delays[i];j++) {
\r
572 for(int j=0;j<delays[i];j++) {
\r
577 lengthIP += maxDelay;
\r
579 cout << "IP length = " << lengthIP << ", AP length = " << lengthAP << endl;
\r
585 // if AP is a valid group, search for the next valid group in IP
\r
586 if (isValidDataGroup(admittance,apIndex)) {
\r
588 while ((ipIndex < lengthIP) && (! isValidDataGroup(inputPattern,ipIndex))) ipIndex++;
\r
589 if (ipIndex == lengthIP) {
\r
597 iterDelays.toFront();
\r
599 if (samePatterns(inputPattern,ipIndex,admittance,apIndex)) {
\r
600 while (iterAP.hasNext()) {
\r
603 QList<char>* ap = iterAP.value();
\r
604 if (ap->at(apIndex) == 1) {
\r
605 QList<int>* d = iterDelays.value();
\r
606 d->append(0); // the 1 is at its good place, so no delay
\r
611 cout << "diff between IP and AP at " << apIndex << endl;
\r
612 // search for the next 1 in IP for every input that has a 1 in AP
\r
614 while (iterAP.hasNext()) {
\r
618 QList<char>* ap = iterAP.value();
\r
619 QList<char>* ip = iterIP.value();
\r
620 QList<int>* d = iterDelays.value();
\r
621 // case 1: 1 in IP is too late
\r
622 if ((ap->at(apIndex) == 1) && (ip->at(ipIndex) == 0)) {
\r
624 while ( ((ipIndex+delay) < lengthIP) && (ip->at(ipIndex+delay) == 0) ) delay++;
\r
625 cout << "found a delay of " << (-delay) << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
\r
626 // moving the 1 to its normal pos.
\r
627 ip->replace(ipIndex,1);
\r
628 ip->replace(ipIndex+delay,0);
\r
631 // case 2: 1 in IP is too soon
\r
632 else if ((ap->at(apIndex) == 0) && (ip->at(ipIndex) == 1)) {
\r
634 while ( ((apIndex+delay) < lengthAP) && (ap->at(apIndex+delay) == 0) ) delay++;
\r
635 cout << "found a delay of " << delay << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
\r
636 // search for next 0 in IP to put the 1
\r
637 int k = ipIndex+delay;
\r
638 while ((k < lengthIP) && (ip->at(k) == 1)) k++;
\r
639 ip->replace(ipIndex,0);
\r
644 if (! samePatterns(inputPattern,inClock,admittance,apIndex)) {
\r
645 cout << "Abnormal case while searching for delays" << endl;
\r
651 if ((apIndex >= lengthAP) || (ipIndex >= lengthIP)) stop = true;
\r
653 iterDelays.toFront();
\r
654 while (iterDelays.hasNext()) {
\r
656 QList<int>* d = iterDelays.value();
\r
657 foreach(int v, *d) {
\r
665 void FunctionalBlock::createInputPattern() throw(Exception) {
\r
666 static QString fctName = "FunctionalBlock::createInputPattern())";
\r
667 #ifdef DEBUG_FCTNAME
\r
668 cout << "call to " << qPrintable(fctName) << endl;
\r
671 cout << "creating input pattern" << endl;
\r
673 foreach(AbstractInterface* iface, getControlInputs()) {
\r
675 ConnectedInterface* connIface = AI_TO_CON(iface);
\r
676 // check if it is connected
\r
677 if (connIface->getConnectedFrom() == NULL) {
\r
678 throw(Exception(IFACE_NOT_CONNECTED,this));
\r
680 // get the precursor output pattern
\r
681 QList<char>* out = connIface->getConnectedFrom()->getOutputPattern();
\r
682 AbstractInputModifier* modifier = connIface->getInputModifier();
\r
683 // check if the input is modified
\r
684 if (modifier != NULL) {
\r
686 out = modifier->getModifiedInput(out);
\r
689 if (out->size() == 0) {
\r
690 clearInputPattern();
\r
691 throw(Exception(NO_IFACE_IP,this));
\r
693 if (lengthIP == -1) {
\r
694 lengthIP = out->size();
\r
697 if (out->size() < lengthIP) lengthIP = out->size();
\r
700 QList<char>* in = new QList<char>(*out);
\r
701 foreach(char c, *in) {
\r
705 inputPattern.insert(connIface,in);
\r
707 // search the last valid group in IP,
\r
708 while(! isValidDataGroup(inputPattern,lengthIP-1)) {
\r
709 //removeDataGroup(inputPattern,lengthIP-1);
\r
714 void FunctionalBlock::createAdmittance(int nbExec) throw(Exception) {
\r
715 static QString fctName = "FunctionalBlock::createAdmittance()";
\r
716 #ifdef DEBUG_FCTNAME
\r
717 cout << "call to " << qPrintable(fctName) << endl;
\r
719 // firstly, copy CP in AP
\r
720 QMapIterator<AbstractInterface*,QList<char>* > iterC(consumptionPattern);
\r
721 while (iterC.hasNext()) {
\r
723 QList<char>* pattern = new QList<char>(*(iterC.value()));
\r
724 admittance.insert(iterC.key(), pattern);
\r
726 lengthAP = lengthCP;
\r
728 cout << "trigger 1 at c.c. 0" << endl;
\r
729 for(int i=1;i<nbExec;i++) {
\r
730 // searching for the clock cycle for which a new exec starts
\r
732 while ((clock < lengthAP) && (nbGroup < delta)) {
\r
733 if (isValidDataGroup(admittance,clock)) nbGroup+=1;
\r
736 while ((clock < lengthAP) && (! isValidDataGroup(admittance,clock))) clock+=1;
\r
737 cout << "trigger " << (i+1) << " at c.c. " << clock << endl;
\r
739 // combine CP with AP at sc
\r
740 for(int j=0;j<lengthCP;j++) {
\r
741 // first case : column of CP must be placed beyond AP's end.
\r
742 if (sc == lengthAP) {
\r
743 cout << i << "," << j << " append in AP at " << sc << endl;
\r
744 appendToPattern(consumptionPattern,j,admittance,1);
\r
748 // second case : CP and AP can be combined directly (i.e. no X | 1 to do)
\r
749 else if (canCombinePatterns(consumptionPattern,j,admittance,sc)) {
\r
750 cout << i << "," << j << " combine at " << sc << endl;
\r
751 combinePatterns(consumptionPattern,j,admittance,sc);
\r
754 // third case : CP has an X column
\r
755 else if (isOnlyXDataGroup(consumptionPattern,j)) {
\r
756 cout << i << "," << j << " shift rigth AP to combine at " << sc << endl;
\r
757 shiftRightPattern(admittance,sc);
\r
759 if (! canCombinePatterns(consumptionPattern,j,admittance,sc)) {
\r
760 cerr << "Abnormal case when combining AP and CP" << endl;
\r
762 combinePatterns(consumptionPattern,j,admittance,sc);
\r
765 // fourth case : AP has an X column
\r
766 else if (isOnlyXDataGroup(admittance,sc)) {
\r
767 cout << i << "," << j << " jump c.c. for CP at " << sc << endl;
\r
772 throw(INVALID_DELTA_CP);
\r
776 // turn all X into 0
\r
777 QMapIterator<AbstractInterface*,QList<char>* > iterA(admittance);
\r
778 while (iterA.hasNext()) {
\r
780 QList<char>* pattern = iterA.value();
\r
781 for(int i=0;i<pattern->size();i++) {
\r
782 if (pattern->at(i) == -1) pattern->replace(i,0);
\r
783 cout << (int)(pattern->at(i));
\r
789 void FunctionalBlock::checkInputPatternCompatibility() throw(Exception) {
\r
790 static QString fctName = "FunctionalBlock::checkInputPatternCompatibility()";
\r
791 #ifdef DEBUG_FCTNAME
\r
792 cout << "call to " << qPrintable(fctName) << endl;
\r
795 // firstly, create input pattern
\r
797 createInputPattern();
\r
799 catch(Exception e) {
\r
802 int nbExec = createTriggers();
\r
803 cout << qPrintable(name) << " will exec. " << nbExec << " times." << endl;
\r
806 createAdmittance(nbExec);
\r
808 catch(Exception e) {
\r
809 cout << "cannot create admittance" << endl;
\r
813 int clock = 0; // index in IP
\r
814 int i = 0; // index in AP
\r
815 while ((clock < lengthIP) && (i < lengthAP)) {
\r
817 // if AP is a valid group, search for the next valid group in IP
\r
818 if (isValidDataGroup(admittance,i)) {
\r
819 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
820 if (clock == lengthIP) {
\r
821 cerr << "Abnormal case: end of IP has been reached without finding a valid group" << endl;
\r
822 throw(Exception(IP_END_NULLCOL,this));
\r
825 /* at that point 2 cases of compat : IP(clock) and AP(i) are equal valid group, or
\r
826 are both null columns
\r
828 if (! samePatterns(inputPattern,clock,admittance,i)) {
\r
829 cout << "AP(" << i << ") and IP(" << clock << ") are not equal" << endl;
\r
830 throw(Exception(IP_AP_NOTCOMPAT,this)); // IP and AP not compatible
\r
835 if (clock < lengthIP) {
\r
836 throw(Exception(AP_TOO_SHORT,this));
\r
837 cerr << "Abnormal case: AP is to short" << endl;
\r
841 void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
\r
842 static QString fctName = "FunctionalBlock::computeOutputPattern()";
\r
843 #ifdef DEBUG_FCTNAME
\r
844 cout << "call to " << qPrintable(fctName) << endl;
\r
847 clearOutputPattern();
\r
849 if (specialType != NotSpecial) {
\r
850 cerr << "Abnormal case: the block is special and output pattern is computed normally" << endl;
\r
851 throw(Exception(INVALID_FUNBLOCK_USE,this));
\r
854 cout << "computing output pattern of " << qPrintable(name) << endl;
\r
856 // in case of inputPattern not created, do it
\r
857 if (lengthIP <= 0) {
\r
859 cout << "Strange case: input pattern is not created while it is time to compute output pattern !" << endl;
\r
860 // collect the input patterns for each input
\r
862 createInputPattern();
\r
864 catch(Exception e) {
\r
867 cout << "input pattern array initialized with min. len " << lengthIP << endl;
\r
870 // initialize the output pattern
\r
872 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
873 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
874 lengthOP = lengthIP+productionPattern.value(connIface)->size();
\r
875 QList<char>* pattern = new QList<char>();
\r
876 for(int i=0;i<lengthOP;i++) pattern->append(0);
\r
877 connIface->setOutputPattern(pattern);
\r
878 outputPattern.insert(connIface,pattern);
\r
880 cout << "output pattern array initialized" << endl;
\r
884 // search for the beginning of the first execution.
\r
885 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
886 cout << "found 1st exec clock: " << clock << endl;
\r
888 while (clock < lengthIP) {
\r
889 // initialize counters for current execution.
\r
890 int p = 0; // index in production pattern
\r
891 int o = 0; // clock+o will give the clock cycle of each output group
\r
892 int cip = 0; // clock+cip give the clock cycle of an input group
\r
893 int ccp = 0; // ccp give a column in the consumptio pattern
\r
894 int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP
\r
895 int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP
\r
896 bool cannotCompleteExec = false;
\r
897 for(int m=0;m<productionCounter.size();m++) {
\r
898 // search for the first production in PP
\r
899 while (!isValidDataGroup(productionPattern,p)) {
\r
903 int gap = 0; // count the number of extra null columns
\r
904 // search for PC(m) valid input group in IP
\r
905 while (nip < productionCounter.at(m)) {
\r
906 if (clock+cip < lengthIP) {
\r
907 if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
\r
912 cannotCompleteExec = true;
\r
917 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
919 // search for PC(m) valid input group in IP
\r
920 while (ncp < productionCounter.at(m)) {
\r
921 if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
\r
925 o += gap; // to take into acocunt of extra null columns
\r
926 combinePatterns(productionPattern,p,outputPattern,clock+o);
\r
931 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
933 // current exec. taken into accunt
\r
936 // search for the next exec.
\r
939 while ((clock < lengthIP) && (nip < delta)) {
\r
940 if (isValidDataGroup(inputPattern,clock)) nip += 1;
\r
941 if (nip < delta) clock += 1;
\r
943 cout << "found exec " << nbExec << " at clock: " << clock << endl;
\r
945 // find the last valid output data group
\r
946 while(! isValidDataGroup(outputPattern,lengthOP-1)) {
\r
947 removeDataGroup(outputPattern,lengthOP-1);
\r
951 // clear input pattern
\r
952 clearInputPattern();
\r
954 setOutputPatternComputed(true);
\r
960 void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
\r
961 static QString fctName = "FunctionalBlock::computeOutputPattern()";
\r
962 #ifdef DEBUG_FCTNAME
\r
963 cout << "call to " << qPrintable(fctName) << endl;
\r
966 // case 1: the block is a generator for which output pattern
\r
967 // must be computed for a nbExec following executions
\r
970 cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;
\r
971 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
972 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
973 // create output pattern
\r
974 QList<char>* pp = productionPattern.value(connIface);
\r
975 QList<char>* pattern = new QList<char>(*pp);
\r
976 for(int i=1;i<nbExec;i++) pattern->append(*pp);
\r
977 // assign pattern to interface
\r
978 connIface->setOutputPattern(pattern);
\r
979 // store it in QMap
\r
980 outputPattern.insert(connIface,pattern);
\r
984 cout << "computing output pattern of " << qPrintable(name) << endl;
\r
986 // in case of inputPattern not created, do it
\r
987 if (lengthIP <= 0) {
\r
988 // collect the input patterns for each input
\r
990 createInputPattern();
\r
992 catch(Exception e) {
\r
995 cout << "input pattern array initialized with min. len " << lengthIP << endl;
\r
998 // initialize the output pattern
\r
1000 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
1001 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
1002 lengthOP = lengthIP+productionPattern.value(connIface)->size();
\r
1003 QList<char>* pattern = new QList<char>();
\r
1004 for(int i=0;i<lengthOP;i++) pattern->append(0);
\r
1005 connIface->setOutputPattern(pattern);
\r
1006 outputPattern.insert(connIface,pattern);
\r
1008 cout << "output pattern array initialized" << endl;
\r
1012 // search for the beginning of the first execution.
\r
1013 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
1014 cout << "found 1st exec clock: " << clock << endl;
\r
1016 while (clock < lengthIP) {
\r
1017 // initialize counters for current execution.
\r
1018 int p = 0; // index in production pattern
\r
1019 int o = 0; // clock+o will give the clock cycle of each output group
\r
1020 int cip = 0; // clock+cip give the clock cycle of an input group
\r
1021 int ccp = 0; // ccp give a column in the consumptio pattern
\r
1022 int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP
\r
1023 int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP
\r
1024 bool cannotCompleteExec = false;
\r
1025 for(int m=0;m<productionCounter.size();m++) {
\r
1026 // search for the first production in PP
\r
1027 while (!isValidDataGroup(productionPattern,p)) {
\r
1031 int gap = 0; // count the number of extra null columns
\r
1032 // search for PC(m) valid input group in IP
\r
1033 while (nip < productionCounter.at(m)) {
\r
1034 if (clock+cip < lengthIP) {
\r
1035 if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
\r
1040 cannotCompleteExec = true;
\r
1045 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
1047 // search for PC(m) valid input group in IP
\r
1048 while (ncp < productionCounter.at(m)) {
\r
1049 if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
\r
1053 o += gap; // to take into acocunt of extra null columns
\r
1054 combinePatterns(productionPattern,p,outputPattern,clock+o);
\r
1059 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
1061 // current exec. taken into accunt
\r
1064 // search for the next exec.
\r
1067 while ((clock < lengthIP) && (nip < delta)) {
\r
1068 if (isValidDataGroup(inputPattern,clock)) nip += 1;
\r
1069 if (nip < delta) clock += 1;
\r
1071 cout << "found exec " << nbExec << " at clock: " << clock << endl;
\r
1073 // find the last valid output data group
\r
1074 while(! isValidDataGroup(outputPattern,lengthOP-1)) {
\r
1075 removeDataGroup(outputPattern,lengthOP-1);
\r
1079 // clear input pattern
\r
1080 clearInputPattern();
\r
1084 bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
\r
1086 if (patternSrc.size() != patternDest.size()) return false;
\r
1087 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1088 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1089 while (iterSrc.hasNext()) {
\r
1092 QList<char>* srcPat = iterSrc.value();
\r
1093 QList<char>* destPat = iterDest.value();
\r
1094 if (srcCol >= srcPat->size()) return false;
\r
1095 if (destCol >= destPat->size()) return false;
\r
1096 if (srcPat->at(srcCol) != destPat->at(destCol)) return false;
\r
1101 bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, const QList<int> &srcCols, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
\r
1102 if (patternSrc.size() != srcCols.size()) return false;
\r
1103 if (patternSrc.size() != patternDest.size()) return false;
\r
1105 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1106 QListIterator<int> iterSrcCol(srcCols);
\r
1107 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1108 while (iterSrc.hasNext()) {
\r
1110 int srcCol = iterSrcCol.next();
\r
1112 QList<char>* srcPat = iterSrc.value();
\r
1113 QList<char>* destPat = iterDest.value();
\r
1114 if (srcCol >= srcPat->size()) return false;
\r
1115 if (destCol >= destPat->size()) return false;
\r
1116 if (srcPat->at(srcCol) != destPat->at(destCol)) return false;
\r
1121 bool FunctionalBlock::canCombinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
\r
1122 if (patternSrc.size() != patternDest.size()) return false;
\r
1123 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1124 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1125 while (iterSrc.hasNext()) {
\r
1128 QList<char>* srcPat = iterSrc.value();
\r
1129 QList<char>* destPat = iterDest.value();
\r
1130 if (srcCol >= srcPat->size()) return false;
\r
1131 if (destCol >= destPat->size()) return false;
\r
1132 if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return false;
\r
1133 if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return false;
\r
1138 void FunctionalBlock::combinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
\r
1139 if (patternSrc.size() != patternDest.size()) return;
\r
1140 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1141 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1142 while (iterSrc.hasNext()) {
\r
1145 QList<char>* srcPat = iterSrc.value();
\r
1146 QList<char>* destPat = iterDest.value();
\r
1147 if (srcCol >= srcPat->size()) return;
\r
1148 if (destCol >= destPat->size()) return;
\r
1149 if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return;
\r
1150 if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return;
\r
1151 destPat->replace(destCol,destPat->at(destCol) | srcPat->at(srcCol));
\r
1155 void FunctionalBlock::appendToPattern(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int nbCols) {
\r
1156 if (patternSrc.size() != patternDest.size()) return;
\r
1157 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1158 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1159 while (iterSrc.hasNext()) {
\r
1162 QList<char>* srcPat = iterSrc.value();
\r
1163 QList<char>* destPat = iterDest.value();
\r
1165 while ((srcCol+i < srcPat->size()) && (i<nbCols)) {
\r
1166 destPat->append(srcPat->at(srcCol+i));
\r
1172 void FunctionalBlock::removeDataGroup(QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1173 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1174 while (iterSrc.hasNext()) {
\r
1176 QList<char>* srcPat = iterSrc.value();
\r
1177 if (offset < srcPat->size()) {
\r
1178 srcPat->removeAt(offset);
\r
1183 void FunctionalBlock::shiftRightPattern(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1184 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1185 while (iterSrc.hasNext()) {
\r
1187 QList<char>* srcPat = iterSrc.value();
\r
1188 if (offset < srcPat->size()) {
\r
1189 srcPat->insert(offset,0);
\r
1194 bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1195 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1196 while (iterSrc.hasNext()) {
\r
1198 QList<char>* srcPat = iterSrc.value();
\r
1199 if (offset >= srcPat->size()) return false;
\r
1200 if (srcPat->at(offset) == 1) return true;
\r
1205 bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface*, QList<char>* >& pattern, const QList<int> offsets) {
\r
1206 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1207 QListIterator<int> iterOffsets(offsets);
\r
1208 while (iterSrc.hasNext()) {
\r
1210 int offset = iterOffsets.next();
\r
1211 QList<char>* srcPat = iterSrc.value();
\r
1212 if (offset >= srcPat->size()) return false;
\r
1213 if (srcPat->at(offset) == 1) return true;
\r
1218 bool FunctionalBlock::isOnlyXDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1219 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1220 while (iterSrc.hasNext()) {
\r
1222 QList<char>* srcPat = iterSrc.value();
\r
1223 if (offset >= srcPat->size()) return false;
\r
1224 if (srcPat->at(offset) != -1) return false;
\r
1229 void FunctionalBlock::clearConsumptionPattern() {
\r
1230 QMapIterator<AbstractInterface*, QList<char>* > iterP(consumptionPattern);
\r
1231 while (iterP.hasNext()) {
\r
1233 QList<char>* pattern = iterP.value();
\r
1234 if (pattern != NULL) delete pattern;
\r
1236 consumptionPattern.clear();
\r
1240 void FunctionalBlock::clearProductionPattern() {
\r
1241 QMapIterator<AbstractInterface*, QList<char>* > iterP(productionPattern);
\r
1242 while (iterP.hasNext()) {
\r
1244 QList<char>* pattern = iterP.value();
\r
1245 if (pattern != NULL) delete pattern;
\r
1247 productionPattern.clear();
\r
1251 void FunctionalBlock::clearInputPattern() {
\r
1253 QMapIterator<AbstractInterface*,QList<char>* > iterI(inputPattern);
\r
1254 while (iterI.hasNext()) {
\r
1256 QList<char>* pattern = iterI.value();
\r
1257 if (pattern != NULL) delete pattern;
\r
1259 inputPattern.clear();
\r
1263 void FunctionalBlock::clearOutputPattern() {
\r
1265 QMapIterator<AbstractInterface*,QList<char>* > iterO(outputPattern);
\r
1266 while (iterO.hasNext()) {
\r
1268 ConnectedInterface* connIface = AI_TO_CON(iterO.key());
\r
1269 connIface->resetOutputPattern();
\r
1270 QList<char>* pattern = iterO.value();
\r
1271 if (pattern != NULL) delete pattern;
\r
1273 outputPattern.clear();
\r
1277 void FunctionalBlock::clearAdmittanceDelays() {
\r
1278 QMapIterator<AbstractInterface*, QList<int>* > iterA(admittanceDelays);
\r
1279 while (iterA.hasNext()) {
\r
1281 QList<int>* d = iterA.value();
\r
1282 if (d != NULL) delete d;
\r
1284 admittanceDelays.clear();
\r
1287 int FunctionalBlock::createTriggers() {
\r
1289 /* NB: this method returns the number of executions that have been started
\r
1290 but not necessary completed.
\r
1292 if (delta <= 0) return 0;
\r
1294 // search for the first exec.
\r
1295 while ((offset < lengthIP) && (! isValidDataGroup(inputPattern,offset))) offset++;
\r
1296 if (offset == lengthIP) return 0;
\r
1297 triggers.append(offset);
\r
1299 for(int i = offset;i<lengthIP;i++) {
\r
1300 if (isValidDataGroup(inputPattern,i)) nbGroup++;
\r
1301 if (nbGroup == delta+1) {
\r
1302 triggers.append(i);
\r
1306 return triggers.size();
\r
1309 QList<QString> FunctionalBlock::getExternalResources() {
\r
1311 BlockImplementation* impl = reference->getImplementations().at(0); // for now only take first impl available
\r
1312 QList<QString> list = impl->getResources();
\r
1313 foreach(QString s, list) {
\r
1314 cout << qPrintable(s) << " ";
\r
1322 void FunctionalBlock::generateVHDL(const QString& path) throw(Exception){
\r
1324 BlockImplementation* impl = reference->getImplementations().at(0); // for now only take first impl available
\r
1326 QFile implFile(impl->getXmlFile());
\r
1328 // reading in into QDomDocument
\r
1329 QDomDocument document("implFile");
\r
1331 if (!implFile.open(QIODevice::ReadOnly)) {
\r
1332 throw(Exception(IMPLFILE_NOACCESS));
\r
1334 if (!document.setContent(&implFile)) {
\r
1336 throw(Exception(IMPLFILE_NOACCESS));
\r
1340 bool genController = false;
\r
1341 QString coreFile = "";
\r
1342 QString controllerFile = "";
\r
1344 if (reference->isWBConfigurable()) {
\r
1345 genController = true;
\r
1346 controllerFile = path;
\r
1347 controllerFile += "/";
\r
1348 controllerFile.append(name);
\r
1349 controllerFile.append("_ctrl.vhd");
\r
1352 controllerFile = "nofile.vhd";
\r
1356 coreFile.append(name);
\r
1357 coreFile.append(".vhd");
\r
1359 QFile vhdlCore(coreFile);
\r
1360 QFile vhdlController(controllerFile);
\r
1362 if (!vhdlCore.open(QIODevice::WriteOnly)) {
\r
1363 throw(Exception(VHDLFILE_NOACCESS));
\r
1366 if (genController) {
\r
1367 if (!vhdlController.open(QIODevice::WriteOnly)) {
\r
1368 throw(Exception(VHDLFILE_NOACCESS));
\r
1371 QTextStream outCore(&vhdlCore);
\r
1372 QTextStream outController;
\r
1373 if (genController) {
\r
1374 outController.setDevice(&vhdlController);
\r
1378 //Get the root element
\r
1379 QDomElement impl = document.documentElement();
\r
1380 QDomElement eltComments = impl.firstChildElement("comments");
\r
1381 generateComments(outCore,eltComments, coreFile);
\r
1382 QDomElement eltLibs = eltComments.nextSiblingElement("libraries");
\r
1383 generateLibraries(outCore, eltLibs);
\r
1384 generateEntity(outCore, genController);
\r
1385 QDomElement eltArch = eltLibs.nextSiblingElement("architecture");
\r
1386 generateArchitecture(outCore, eltArch );
\r
1387 if (genController) {
\r
1388 generateController(outController);
\r
1391 catch(Exception err) {
\r
1396 vhdlController.close();
\r
1400 void FunctionalBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
\r
1402 for(int i = 0; i < 50; i++) {
\r
1405 out << "\n--" << endl;
\r
1406 QString fileName = coreFile;
\r
1407 out << "-- File : " << fileName << endl;
\r
1408 out << "--" << endl;
\r
1409 QDomElement eltAuthor = elt.firstChildElement("author");
\r
1410 QString firstName = eltAuthor.attribute("firstname","");
\r
1411 QString lastName = eltAuthor.attribute("lastname","");
\r
1412 QString mail = eltAuthor.attribute("mail","");
\r
1413 out << "-- Author(s) : "<<firstName+" "<<lastName<<" ("<<mail<<")" << endl;
\r
1414 out << "--" << endl;
\r
1415 QDomElement eltLog = eltAuthor.nextSiblingElement("log");
\r
1416 QString crea = eltLog.attribute("creation","");
\r
1417 out << "-- Creation Date : "<<crea<< endl;
\r
1418 out << "--" << endl;
\r
1419 QDomNodeList listModifs = eltLog.elementsByTagName("modification");
\r
1420 for(int j=0;j<listModifs.length();j++) {
\r
1421 QDomNode nodeModif = listModifs.at(j);
\r
1422 QDomElement eltModif = nodeModif.toElement();
\r
1424 out << "-- Description : " << endl;
\r
1425 QStringList lines = reference->getDescription().split("\n");
\r
1426 foreach(QString line, lines) {
\r
1427 out << "-- " << line << endl;
\r
1429 out << "--" << endl;
\r
1430 QDomElement eltNote = eltLog.nextSiblingElement("notes");
\r
1431 QDomElement note = eltNote.firstChildElement();
\r
1432 QString noteTxt = note.text();
\r
1433 out << "-- Notes :" << endl;
\r
1434 lines = noteTxt.split("\n");
\r
1435 foreach(QString line, lines) {
\r
1436 out << "-- " << line << endl;
\r
1438 out << "--" << endl;
\r
1439 for(int i = 0; i < 50; i++) {
\r
1442 out << endl << endl;
\r
1445 void FunctionalBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
\r
1447 QDomNodeList listLib = elt.elementsByTagName("library");
\r
1448 for(int i = 0; i < listLib.length(); i++) {
\r
1449 QDomNode nodeLib = listLib.item(i);
\r
1450 QDomElement eltLib = nodeLib.toElement();
\r
1451 QString nameLib = eltLib.attribute("name","none");
\r
1452 out << "library " << nameLib << ";" << endl;
\r
1453 QDomNodeList listPack = eltLib.elementsByTagName("package");
\r
1454 for(int j = 0; j < listPack.length(); j++) {
\r
1455 QDomNode nodePack = listPack.item(j);
\r
1456 QDomElement eltPack = nodePack.toElement();
\r
1457 QString namePack = eltPack.attribute("name","none");
\r
1458 QString usePack = eltPack.attribute("use","none");
\r
1459 out << "use " << nameLib << "." << namePack << "." << usePack << ";" << endl;
\r
1466 void FunctionalBlock::generateEntityOrComponentBody(QTextStream& out, int indentLevel, bool hasController) throw(Exception) {
\r
1469 QString indent = "";
\r
1470 for(i=0;i<indentLevel;i++) {
\r
1474 //QList<BlockParameter*> listParams = reference->getParameters();
\r
1475 QList<AbstractInterface*> listInputs = getInputs();
\r
1476 QList<AbstractInterface*> listOutputs = getOutputs();
\r
1477 QList<AbstractInterface*> listBidirs = getBidirs();
\r
1479 // Generation of the generics
\r
1480 QList<BlockParameter*> listGenerics = getGenericParameters();
\r
1481 if ((!listGenerics.isEmpty()) || (hasController)) {
\r
1482 out << indent << " generic (" << endl;
\r
1483 if (hasController) {
\r
1484 out << indent << " wb_data_width : integer = 16;" << endl;
\r
1485 out << indent << " wb_addr_width : integer = 12";
\r
1486 if (!listGenerics.isEmpty()) out << indent << ";";
\r
1489 for(i=0;i<listGenerics.size()-1;i++) {
\r
1490 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
\r
1492 out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
\r
1494 out << indent << " );" << endl;
\r
1497 out << indent << " port (" << endl;
\r
1499 QString ports = "";
\r
1500 QTextStream outPorts(&ports);
\r
1502 // Generation of the clk & rst signals
\r
1503 outPorts << indent << " -- clk/rst" << endl;
\r
1504 foreach(AbstractInterface* iface, listInputs) {
\r
1505 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
\r
1506 outPorts << indent << " " << iface->getName() << " : in std_logic;" << endl;
\r
1509 foreach(AbstractInterface* iface, listOutputs) {
\r
1510 if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
\r
1511 outPorts << indent << " " << iface->getName() << " : out std_logic;" << endl;
\r
1515 if (hasController) {
\r
1516 // Generation of the wishbone signals
\r
1517 outPorts << indent << " -- registers r/w via wishbone" << endl;
\r
1518 QList<BlockParameter*> listWB = reference->getWishboneParameters();
\r
1519 for(i=0;i<listWB.size()-1;i++) {
\r
1520 outPorts << indent << " " << listWB.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
\r
1522 outPorts << indent << " " << listWB.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
\r
1525 // Generation of the data/control signals
\r
1527 QList<AbstractInterface*> listIface = getInterfaces(AbstractInterface::Input, AbstractInterface::Data);
\r
1528 if (listIface.size()>0) {
\r
1529 outPorts << indent << " -- input data ports" << endl;
\r
1530 foreach(AbstractInterface* iface, listIface) {
\r
1531 outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;
\r
1534 listIface = getInterfaces(AbstractInterface::Input, AbstractInterface::Control);
\r
1535 if (listIface.size()>0) {
\r
1536 outPorts << indent << " -- input control ports" << endl;
\r
1537 foreach(AbstractInterface* iface, listIface) {
\r
1538 outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;
\r
1541 listIface = getInterfaces(AbstractInterface::Output, AbstractInterface::Data);
\r
1542 if (listIface.size()>0) {
\r
1543 outPorts << indent << " -- output data ports" << endl;
\r
1544 foreach(AbstractInterface* iface, listIface) {
\r
1545 outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;
\r
1548 listIface = getInterfaces(AbstractInterface::Output, AbstractInterface::Control);
\r
1549 if (listIface.size()>0) {
\r
1550 outPorts << indent << " -- output control ports" << endl;
\r
1551 foreach(AbstractInterface* iface, listIface) {
\r
1552 outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;
\r
1555 listIface = getInterfaces(AbstractInterface::InOut, AbstractInterface::Data);
\r
1556 if (listIface.size()>0) {
\r
1557 outPorts << indent << " -- bidirs data ports" << endl;
\r
1558 foreach(AbstractInterface* iface, listIface) {
\r
1559 outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;
\r
1566 out << indent << " );" << endl << endl;
\r
1570 void FunctionalBlock::generateArchitecture(QTextStream& out, QDomElement &elt ) throw(Exception) {
\r
1571 QRegularExpression rxPort("@\\{([a-zA-Z0-9_]+)\\}");
\r
1573 QString code = elt.text();
\r
1574 //cout << qPrintable(code) << endl;
\r
1575 out << "architecture rtl of " << name << " is" << endl;
\r
1577 QStringList listLine = code.split("\n");
\r
1578 for(int i =0; i < listLine.size(); i++) {
\r
1579 QString line = listLine.at(i).simplified();
\r
1582 if(listLine.at(i).contains(QRegularExpression("@foreach{"))) {
\r
1583 while(listLine.at(i).compare("@endforeach") != -1) {
\r
1584 expr = expr + listLine.at(i) + '\n';
\r
1587 expr = expr + listLine.at(i);
\r
1588 out << evalComplex(expr, 1) << '\n';
\r
1590 if(listLine.at(i).contains(QRegularExpression("@caseeach{"))) {
\r
1591 while(listLine.at(i).compare("@endcaseeach") != -1) {
\r
1592 expr = expr + listLine.at(i) + '\n';
\r
1595 expr = expr + listLine.at(i);
\r
1596 out << evalComplex(expr, 2) << '\n';
\r
1599 if(line.contains("@{")) {
\r
1600 QMap<QString,QString> modifs;
\r
1601 //cout << qPrintable(line) << endl;
\r
1602 QRegularExpressionMatchIterator matchPort = rxPort.globalMatch(line);
\r
1603 while(matchPort.hasNext()) {
\r
1604 QRegularExpressionMatch m = matchPort.next();
\r
1605 QString refName = m.captured(1);
\r
1606 AbstractInterface* refIface = reference->getIfaceFromName(refName);
\r
1607 QString funName = getIfaceUserName(refIface);
\r
1608 if (!funName.isEmpty()) {
\r
1609 modifs.insert(m.captured(0),funName);
\r
1610 //cout << "replace " << qPrintable(refIface->getName()) << " by " << qPrintable(funIface->getName()) << endl;
\r
1613 QMapIterator<QString,QString> iterM(modifs);
\r
1614 while(iterM.hasNext()) {
\r
1616 QString oldName = iterM.key();
\r
1617 QString newName = iterM.value();
\r
1618 line.replace(oldName,newName);
\r
1621 out << line << endl;
\r
1624 out << "end rtl;" << endl;
\r
1627 void FunctionalBlock::generateController(QTextStream &out) throw(Exception) {
\r
1631 QString FunctionalBlock::getIfaceUserName(AbstractInterface* refIface) {
\r
1633 if (! refIface->isReferenceInterface()) return "";
\r
1635 AbstractInterface* funcIface = NULL;
\r
1637 if (refIface->getDirection() == AbstractInterface::Input) {
\r
1638 foreach(AbstractInterface* iface, getInputs()) {
\r
1639 FunctionalInterface* fi = AI_TO_FUN(iface);
\r
1640 if (fi->getReference() == refIface) {
\r
1641 funcIface = iface;
\r
1646 else if (refIface->getDirection() == AbstractInterface::Output) {
\r
1647 foreach(AbstractInterface* iface, getOutputs()) {
\r
1648 FunctionalInterface* fi = AI_TO_FUN(iface);
\r
1649 if (fi->getReference() == refIface) {
\r
1650 funcIface = iface;
\r
1655 else if (refIface->getDirection() == AbstractInterface::InOut) {
\r
1656 foreach(AbstractInterface* iface, getBidirs()) {
\r
1657 FunctionalInterface* fi = AI_TO_FUN(iface);
\r
1658 if (fi->getReference() == refIface) {
\r
1659 funcIface = iface;
\r
1664 if (funcIface == NULL) return "";
\r
1666 return funcIface->getName();
\r