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(GroupBlock *_parent, ReferenceBlock *_reference) throw(Exception) : AbstractBlock() {
\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 FunctionalBlock::~FunctionalBlock() {
\r
35 if (evaluator != NULL) delete evaluator;
\r
38 void FunctionalBlock::parametersValidation(QList<AbstractBlock*>* checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
\r
40 checkedBlocks->append(this);
\r
42 foreach(BlockParameter* param, params){
\r
43 if(param->isUserParameter() && !param->isValueSet()){
\r
44 if(!blocksToConfigure->contains(param->getOwner())){
\r
45 blocksToConfigure->append(param->getOwner());
\r
49 foreach(AbstractInterface *inter, outputs){
\r
50 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
\r
51 if(!checkedBlocks->contains(connectedInter->getOwner())){
\r
52 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
\r
59 bool FunctionalBlock::isFunctionalBlock() {
\r
63 bool FunctionalBlock::isSourceBlock() {
\r
64 if (parent == NULL) return true;
\r
68 void FunctionalBlock::populate() {
\r
71 AbstractInterface* inter;
\r
73 // create parameters from reference block
\r
74 QList<BlockParameter*> lstParam = reference->getParameters();
\r
75 for(i=0;i<lstParam.size();i++) {
\r
76 p = lstParam.at(i)->clone();
\r
80 // create interfaces from reference block
\r
81 QList<AbstractInterface *> lstRef = reference->getInterfaces();
\r
82 // store relation between functional and reference
\r
83 QHash<AbstractInterface *, AbstractInterface *> hashIface;
\r
84 for(i=0;i<lstRef.size();i++) {
\r
86 inter = new FunctionalInterface(this, AI_TO_REF(lstRef.at(i)));
\r
88 catch(Exception e) {
\r
89 cerr << "Abnormal case: " << qPrintable(e.getDefaultMessage()) << endl << "Aborting execution." << endl;
\r
92 hashIface.insert(lstRef.at(i),inter);
\r
94 addInterface(inter);
\r
97 AbstractInterface* funCtlIface = NULL;
\r
98 AbstractInterface* funDataIface = NULL;
\r
100 for(i=0;i<lstRef.size();i++) {
\r
101 AbstractInterface* refIface = lstRef.at(i);
\r
102 if (refIface->getPurpose() == AbstractInterface::Control) {
\r
103 funCtlIface = hashIface.value(refIface);
\r
104 funDataIface = hashIface.value(refIface->getAssociatedIface());
\r
105 if (! funCtlIface->setAssociatedIface(funDataIface)) {
\r
106 cerr << "Abnormal case when associating a control interface to data" << endl << "Aborting execution." << endl;
\r
114 QString FunctionalBlock::getReferenceXmlFile() {
\r
115 return ((ReferenceBlock *)reference)->getXmlFile();
\r
118 QString FunctionalBlock::getReferenceHashMd5() {
\r
119 return ((ReferenceBlock *)reference)->getHashMd5();
\r
122 void FunctionalBlock::createPatterns() throw(Exception) {
\r
123 static QString fctName = "FunctionalBlock::createPatterns()";
\r
124 #ifdef DEBUG_FCTNAME
\r
125 cout << "call to " << qPrintable(fctName) << endl;
\r
128 cout << "create patterns for block " << qPrintable(name) << endl;
\r
129 if (evaluator == NULL) evaluator = new ArithmeticEvaluator();
\r
130 if (! isGeneratorBlock()) {
\r
133 createConsumptionPattern();
\r
134 createProductionCounter();
\r
136 catch(Exception e) {
\r
137 throw(e); // rethrow e
\r
141 createProductionPattern();
\r
143 catch(Exception e) {
\r
146 cout << "PP of " << qPrintable(name) << endl;
\r
147 QMapIterator<AbstractInterface*,QList<char>* > it(productionPattern);
\r
148 while (it.hasNext()) {
\r
150 QList<char>* pat = it.value();
\r
151 foreach(char c, *pat) cout << (int)c;
\r
156 void FunctionalBlock::createDelta() throw(Exception) {
\r
157 static QString fctName = "FunctionalBlock::createDelta()";
\r
158 #ifdef DEBUG_FCTNAME
\r
159 cout << "call to " << qPrintable(fctName) << endl;
\r
162 QString deltaStr = implementation->getDelta();
\r
163 cout << "delta for " << qPrintable(name) << " = " << qPrintable(deltaStr) << endl;
\r
164 if (deltaStr.isEmpty()) {
\r
169 // look for parameter names
\r
172 result = evaluateExpression(deltaStr);
\r
174 catch(Exception e) {
\r
178 cout << "delta = " << delta << endl;
\r
181 void FunctionalBlock::createConsumptionPattern() throw(Exception) {
\r
182 static QString fctName = "FunctionalBlock::createConsumptionPattern()";
\r
183 #ifdef DEBUG_FCTNAME
\r
184 cout << "call to " << qPrintable(fctName) << endl;
\r
187 // first clear if already exists
\r
188 clearConsumptionPattern();
\r
191 QHash<QString,QString> consPattern = implementation->getConsumptionPattern();
\r
193 foreach(AbstractInterface* iface, getControlInputs()) {
\r
194 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
195 QString refName = connIface->getReference()->getName();
\r
196 if (! consPattern.contains(refName)) {
\r
197 throw(Exception(NO_IFACE_CP,this));
\r
198 cerr << "no consumption pattern for reference interface " << qPrintable(refName) << endl;
\r
200 QList<char>* pattern = NULL;
\r
202 pattern = expandPattern(consPattern.value(refName));
\r
204 catch(Exception e) {
\r
207 consumptionPattern.insert(connIface,pattern);
\r
208 if (lengthCP == -1) {
\r
209 lengthCP = pattern->size();
\r
212 if (pattern->size() != lengthCP) {
\r
213 throw(Exception(INVALID_IFACE_CP_LENGTH,this));
\r
219 void FunctionalBlock::createProductionPattern() throw(Exception){
\r
220 static QString fctName = "FunctionalBlock::createProductionPattern()";
\r
221 #ifdef DEBUG_FCTNAME
\r
222 cout << "call to " << qPrintable(fctName) << endl;
\r
225 // first clear if already exists
\r
226 clearProductionPattern();
\r
229 QHash<QString,QString> prodPattern = implementation->getProductionPattern();
\r
231 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
232 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
233 QString refName = connIface->getReference()->getName();
\r
234 if (! prodPattern.contains(refName)) {
\r
235 throw(Exception(NO_IFACE_PP,this));
\r
237 QList<char>* pattern = NULL;
\r
239 pattern = expandPattern(prodPattern.value(refName));
\r
241 catch(Exception e) {
\r
244 productionPattern.insert(connIface,pattern);
\r
245 if (lengthPP == -1) {
\r
246 lengthPP = pattern->size();
\r
249 if (pattern->size() != lengthPP) {
\r
250 throw(Exception(INVALID_IFACE_PP_LENGTH,this));
\r
256 void FunctionalBlock::createProductionCounter() throw(Exception) {
\r
257 static QString fctName = "FunctionalBlock::createProductionCounter()";
\r
258 #ifdef DEBUG_FCTNAME
\r
259 cout << "call to " << qPrintable(fctName) << endl;
\r
262 // first clear if already exists
\r
263 productionCounter.clear();
\r
266 QStringList counterParts = implementation->getProductionCounter().split(",");
\r
267 foreach(QString s, counterParts) {
\r
268 cout << "cont part = " << qPrintable(s) << endl;
\r
270 double val = s.toDouble(&ok);
\r
272 productionCounter.append(val);
\r
274 else if (s.at(0) == '{') {
\r
277 QStringList gen = s.split(":");
\r
278 if (gen.size() != 3) {
\r
279 throw(Exception(INVALID_IFACE_PC,this));
\r
284 for(int i=0;i<3;i++) {
\r
285 double result = 0.0;
\r
287 result = evaluateExpression(gen.at(i));
\r
289 catch(Exception e) {
\r
292 if (i==0) start = result;
\r
293 else if (i==1) nb = result;
\r
294 else if (i==2) step = result;
\r
296 for(int j=0;j<nb;j++) {
\r
297 productionCounter.append(start+j*step);
\r
301 double result = 0.0;
\r
303 result = evaluateExpression(s);
\r
305 catch(Exception e) {
\r
308 productionCounter.append(result);
\r
311 foreach(int val, productionCounter) {
\r
312 cout << val << ",";
\r
317 QList<char>* FunctionalBlock::expandPattern(const QString& patternIn) throw(Exception) {
\r
318 static QString fctName = "FunctionalBlock::expandPattern()";
\r
319 #ifdef DEBUG_FCTNAME
\r
320 cout << "call to " << qPrintable(fctName) << endl;
\r
322 /* expanding a pattern is done in two steps :
\r
323 - 1 : finding all variables that correspond to an expression
\r
324 and copy them in the pattern
\r
325 - 2 : parsing the result
\r
327 Note that the result MUST contain only variables that have a
\r
328 integer/double value. Otherwise, expanding will fail.
\r
334 QString p = replaceExpressions(patternIn);
\r
339 QList<char>* patternOut = new QList<char>();
\r
341 patternOut->append(expandPatternRecur(p,&offset));
\r
343 catch(Exception e) {
\r
350 QString FunctionalBlock::replaceExpressions(const QString& patternIn) throw(Exception) {
\r
352 QString res = patternIn;
\r
354 QRegularExpression re("[$][a-zA-Z0-9_]+");
\r
358 QRegularExpressionMatchIterator matcher = re.globalMatch(res);
\r
359 while(matcher.hasNext()) {
\r
360 QRegularExpressionMatch m = matcher.next();
\r
361 QString param = m.captured(0);
\r
362 QString paramName = param;
\r
363 paramName.remove(0,1);
\r
364 BlockParameter* p = getParameterFromName(paramName);
\r
365 if ((p != NULL) && (p->getType() == BlockParameter::Expression)) {
\r
366 res.replace(param,p->getStringValue());
\r
368 cout << "found an expr: " << qPrintable(paramName) << ", patern => " << qPrintable(res) << endl;
\r
375 QList<char> FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset) throw(Exception) {
\r
377 QList<char> patternOut;
\r
379 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != ')')) {
\r
381 QChar c = patternIn.at(*offset);
\r
385 patternOut.append(expandPatternRecur(patternIn,offset));
\r
387 catch(Exception e) {
\r
391 else if (c == '0') {
\r
392 patternOut.append(0);
\r
394 else if (c == '1') {
\r
395 patternOut.append(1);
\r
397 else if (c == 'X') {
\r
398 patternOut.append(-1);
\r
400 else if (c == '{') {
\r
402 QString expr = "";
\r
403 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
\r
404 expr += patternIn.at(*offset);
\r
407 if (*offset == patternIn.size()) {
\r
408 throw(Exception(INVALID_IFACE_PATTERN,this));
\r
412 repeat = evaluateExpression(expr);
\r
414 catch(Exception e) {
\r
417 // repeat just the last value in currentGroup
\r
418 char last = patternOut.last();
\r
419 //cout << "repeat last char " << repeat << " times : " << (int)last << endl;
\r
421 for(int i=1;i<(int)repeat;i++) {
\r
422 patternOut.append(last);
\r
428 // must check if after ), there is a {
\r
429 if ((*offset < patternIn.size()-1) && (patternIn.at(*offset+1) == '{')) {
\r
431 QString expr = "";
\r
432 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
\r
433 expr += patternIn.at(*offset);
\r
436 if (*offset == patternIn.size()) {
\r
437 throw(Exception(INVALID_IFACE_PATTERN,this));
\r
441 repeat = evaluateExpression(expr);
\r
443 catch(Exception e) {
\r
447 cout << "repeat last group " << repeat << " times : ";
\r
448 foreach (char c, currentGroup) cout <<(int)c;
\r
451 QList<char> single = patternOut;
\r
452 for(int i=1;i<(int)repeat;i++) {
\r
453 patternOut.append(single);
\r
459 double FunctionalBlock::evaluateExpression(const QString& expression) throw(Exception) {
\r
460 static QString fctName = "FunctionalBlock::evaluateExpression()";
\r
461 #ifdef DEBUG_FCTNAME
\r
462 cout << "call to " << qPrintable(fctName) << endl;
\r
465 QHash<QString,double> vars;
\r
466 evaluator->setExpression(expression);
\r
467 QList<QString> varNames = evaluator->getVariableNames();
\r
468 foreach (QString name, varNames) {
\r
469 QString paramName = name;
\r
470 paramName.remove(0,1);
\r
471 BlockParameter* param = getParameterFromName(paramName);
\r
472 if (param == NULL) {
\r
473 throw(Exception(EVAL_PARAM_UNKNOWN,this));
\r
476 int val = param->getDoubleValue(&okVal);
\r
478 throw(Exception(EVAL_PARAM_NOVALUE,this));
\r
480 vars.insert(name,(double)val);
\r
483 evaluator->setVariablesValue(vars);
\r
484 double result = 0.0;
\r
486 result = evaluator->evaluate();
\r
489 cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl;
\r
490 throw(Exception(EVAL_INVALID_EXPR,this));
\r
495 void FunctionalBlock::computeAdmittanceDelays() throw(Exception) {
\r
496 static QString fctName = "FunctionalBlock::computeAdmittanceDelays()";
\r
497 #ifdef DEBUG_FCTNAME
\r
498 cout << "call to " << qPrintable(fctName) << endl;
\r
500 QList<int> inClock;
\r
503 clearAdmittanceDelays();
\r
505 // trying to synchronize the first one in AP
\r
506 QMapIterator<AbstractInterface*,QList<char>* > iterAP(admittance);
\r
507 QMapIterator<AbstractInterface*,QList<char>* > iterIP(inputPattern);
\r
509 while (iterAP.hasNext()) {
\r
512 QList<char>* ap = iterAP.value();
\r
513 QList<char>* ip = iterIP.value();
\r
515 while ((first < lengthIP) && (ip->at(first) == 0)) first++;
\r
516 while ((first < lengthAP) && (ap->at(first) == 0)) first--;
\r
517 delays.append(first);
\r
519 QList<int>* delays = new QList<int>();
\r
520 admittanceDelays.insert(iterAP.key(), delays);
\r
523 QMapIterator<AbstractInterface*,QList<int>* > iterDelays(admittanceDelays);
\r
525 // get the delay to apply
\r
527 for(int i=0;i<delays.size();i++) {
\r
528 if (delays[i] > maxDelay) maxDelay = delays[i];
\r
530 // adding the delays to IP
\r
533 while (iterIP.hasNext()) {
\r
536 QList<char>* ip = iterIP.value();
\r
537 QList<int>* d = iterDelays.value();
\r
538 d->append(maxDelay-delays[i]);
\r
539 cout << "prependind " << qPrintable(iterIP.key()->getName()) << " with " << (maxDelay-delays[i]) << " 0" << endl;
\r
540 for(int j=0;j<maxDelay-delays[i];j++) {
\r
543 for(int j=0;j<delays[i];j++) {
\r
548 lengthIP += maxDelay;
\r
550 cout << "IP length = " << lengthIP << ", AP length = " << lengthAP << endl;
\r
556 // if AP is a valid group, search for the next valid group in IP
\r
557 if (isValidDataGroup(admittance,apIndex)) {
\r
559 while ((ipIndex < lengthIP) && (! isValidDataGroup(inputPattern,ipIndex))) ipIndex++;
\r
560 if (ipIndex == lengthIP) {
\r
568 iterDelays.toFront();
\r
570 if (samePatterns(inputPattern,ipIndex,admittance,apIndex)) {
\r
571 while (iterAP.hasNext()) {
\r
574 QList<char>* ap = iterAP.value();
\r
575 if (ap->at(apIndex) == 1) {
\r
576 QList<int>* d = iterDelays.value();
\r
577 d->append(0); // the 1 is at its good place, so no delay
\r
582 cout << "diff between IP and AP at " << apIndex << endl;
\r
583 // search for the next 1 in IP for every input that has a 1 in AP
\r
585 while (iterAP.hasNext()) {
\r
589 QList<char>* ap = iterAP.value();
\r
590 QList<char>* ip = iterIP.value();
\r
591 QList<int>* d = iterDelays.value();
\r
592 // case 1: 1 in IP is too late
\r
593 if ((ap->at(apIndex) == 1) && (ip->at(ipIndex) == 0)) {
\r
595 while ( ((ipIndex+delay) < lengthIP) && (ip->at(ipIndex+delay) == 0) ) delay++;
\r
596 cout << "found a delay of " << (-delay) << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
\r
597 // moving the 1 to its normal pos.
\r
598 ip->replace(ipIndex,1);
\r
599 ip->replace(ipIndex+delay,0);
\r
602 // case 2: 1 in IP is too soon
\r
603 else if ((ap->at(apIndex) == 0) && (ip->at(ipIndex) == 1)) {
\r
605 while ( ((apIndex+delay) < lengthAP) && (ap->at(apIndex+delay) == 0) ) delay++;
\r
606 cout << "found a delay of " << delay << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
\r
607 // search for next 0 in IP to put the 1
\r
608 int k = ipIndex+delay;
\r
609 while ((k < lengthIP) && (ip->at(k) == 1)) k++;
\r
610 ip->replace(ipIndex,0);
\r
615 if (! samePatterns(inputPattern,inClock,admittance,apIndex)) {
\r
616 cout << "Abnormal case while searching for delays" << endl;
\r
622 if ((apIndex >= lengthAP) || (ipIndex >= lengthIP)) stop = true;
\r
624 iterDelays.toFront();
\r
625 while (iterDelays.hasNext()) {
\r
627 QList<int>* d = iterDelays.value();
\r
628 foreach(int v, *d) {
\r
636 void FunctionalBlock::createInputPattern() throw(Exception) {
\r
637 static QString fctName = "FunctionalBlock::createInputPattern())";
\r
638 #ifdef DEBUG_FCTNAME
\r
639 cout << "call to " << qPrintable(fctName) << endl;
\r
643 foreach(AbstractInterface* iface, getControlInputs()) {
\r
645 ConnectedInterface* connIface = AI_TO_CON(iface);
\r
646 // check if it is connected
\r
647 if (connIface->getConnectedFrom() == NULL) {
\r
648 throw(Exception(IFACE_NOT_CONNECTED,this));
\r
650 // get the precursor output pattern
\r
651 QList<char>* out = connIface->getConnectedFrom()->getOutputPattern();
\r
653 ConnectedInterface* assoIface = AI_TO_CON(connIface->getAssociatedIface());
\r
654 AbstractInputModifier* modifier = assoIface->getInputModifier();
\r
655 // check if the input is modified
\r
656 if (modifier != NULL) {
\r
658 out = modifier->getModifiedInput(out);
\r
661 if (out->size() == 0) {
\r
662 clearInputPattern();
\r
663 throw(Exception(NO_IFACE_IP,this));
\r
665 if (lengthIP == -1) {
\r
666 lengthIP = out->size();
\r
669 if (out->size() < lengthIP) lengthIP = out->size();
\r
672 QList<char>* in = new QList<char>(*out);
\r
673 foreach(char c, *in) {
\r
677 inputPattern.insert(connIface,in);
\r
679 // search the last valid group in IP,
\r
680 while(! isValidDataGroup(inputPattern,lengthIP-1)) {
\r
681 //removeDataGroup(inputPattern,lengthIP-1);
\r
686 void FunctionalBlock::createAdmittance(int nbExec) throw(Exception) {
\r
687 static QString fctName = "FunctionalBlock::createAdmittance()";
\r
688 #ifdef DEBUG_FCTNAME
\r
689 cout << "call to " << qPrintable(fctName) << endl;
\r
691 // firstly, copy CP in AP
\r
692 QMapIterator<AbstractInterface*,QList<char>* > iterC(consumptionPattern);
\r
693 while (iterC.hasNext()) {
\r
695 QList<char>* pattern = new QList<char>(*(iterC.value()));
\r
696 admittance.insert(iterC.key(), pattern);
\r
698 lengthAP = lengthCP;
\r
700 cout << "trigger 1 at c.c. 0" << endl;
\r
701 for(int i=1;i<nbExec;i++) {
\r
702 // searching for the clock cycle for which a new exec starts
\r
704 while ((clock < lengthAP) && (nbGroup < delta)) {
\r
705 if (isValidDataGroup(admittance,clock)) nbGroup+=1;
\r
708 while ((clock < lengthAP) && (! isValidDataGroup(admittance,clock))) clock+=1;
\r
709 cout << "trigger " << (i+1) << " at c.c. " << clock << endl;
\r
711 // combine CP with AP at sc
\r
712 for(int j=0;j<lengthCP;j++) {
\r
713 // first case : column of CP must be placed beyond AP's end.
\r
714 if (sc == lengthAP) {
\r
715 cout << i << "," << j << " append in AP at " << sc << endl;
\r
716 appendToPattern(consumptionPattern,j,admittance,1);
\r
720 // second case : CP and AP can be combined directly (i.e. no X | 1 to do)
\r
721 else if (canCombinePatterns(consumptionPattern,j,admittance,sc)) {
\r
722 cout << i << "," << j << " combine at " << sc << endl;
\r
723 combinePatterns(consumptionPattern,j,admittance,sc);
\r
726 // third case : CP has an X column
\r
727 else if (isOnlyXDataGroup(consumptionPattern,j)) {
\r
728 cout << i << "," << j << " shift rigth AP to combine at " << sc << endl;
\r
729 shiftRightPattern(admittance,sc);
\r
731 if (! canCombinePatterns(consumptionPattern,j,admittance,sc)) {
\r
732 cerr << "Abnormal case when combining AP and CP" << endl;
\r
734 combinePatterns(consumptionPattern,j,admittance,sc);
\r
737 // fourth case : AP has an X column
\r
738 else if (isOnlyXDataGroup(admittance,sc)) {
\r
739 cout << i << "," << j << " jump c.c. for CP at " << sc << endl;
\r
744 throw(INVALID_DELTA_CP);
\r
748 // turn all X into 0
\r
749 QMapIterator<AbstractInterface*,QList<char>* > iterA(admittance);
\r
750 while (iterA.hasNext()) {
\r
752 QList<char>* pattern = iterA.value();
\r
753 for(int i=0;i<pattern->size();i++) {
\r
754 if (pattern->at(i) == -1) pattern->replace(i,0);
\r
755 cout << (int)(pattern->at(i));
\r
761 void FunctionalBlock::checkInputPatternCompatibility() throw(Exception) {
\r
762 static QString fctName = "FunctionalBlock::checkInputPatternCompatibility()";
\r
763 #ifdef DEBUG_FCTNAME
\r
764 cout << "call to " << qPrintable(fctName) << endl;
\r
767 // firstly, create input pattern
\r
769 createInputPattern();
\r
771 catch(Exception e) {
\r
774 int nbExec = createTriggers();
\r
775 cout << qPrintable(name) << " will exec. " << nbExec << " times." << endl;
\r
778 createAdmittance(nbExec);
\r
780 catch(Exception e) {
\r
781 cout << "cannot create admittance" << endl;
\r
785 int clock = 0; // index in IP
\r
786 int i = 0; // index in AP
\r
787 while ((clock < lengthIP) && (i < lengthAP)) {
\r
789 // if AP is a valid group, search for the next valid group in IP
\r
790 if (isValidDataGroup(admittance,i)) {
\r
791 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
792 if (clock == lengthIP) {
\r
793 cerr << "Abnormal case: end of IP has been reached without finding a valid group" << endl;
\r
794 throw(Exception(IP_END_NULLCOL,this));
\r
797 /* at that point 2 cases of compat : IP(clock) and AP(i) are equal valid group, or
\r
798 are both null columns
\r
800 if (! samePatterns(inputPattern,clock,admittance,i)) {
\r
801 cout << "AP(" << i << ") and IP(" << clock << ") are not equal" << endl;
\r
802 throw(Exception(IP_AP_NOTCOMPAT,this)); // IP and AP not compatible
\r
807 if (clock < lengthIP) {
\r
808 throw(Exception(AP_TOO_SHORT,this));
\r
809 cerr << "Abnormal case: AP is to short" << endl;
\r
813 void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
\r
814 static QString fctName = "FunctionalBlock::computeOutputPattern()";
\r
815 #ifdef DEBUG_FCTNAME
\r
816 cout << "call to " << qPrintable(fctName) << endl;
\r
819 /* case 1: the block is a generator for which output pattern
\r
820 must be computed for a nbExec following executions
\r
825 cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;
\r
826 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
827 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
828 // create output pattern
\r
829 QList<char>* pp = productionPattern.value(connIface);
\r
830 QList<char>* pattern = new QList<char>(*pp);
\r
831 for(int i=1;i<nbExec;i++) pattern->append(*pp);
\r
832 // assign pattern to interface
\r
833 connIface->setOutputPattern(pattern);
\r
834 // store it in QMap
\r
835 outputPattern.insert(connIface,pattern);
\r
839 cout << "computing output pattern of " << qPrintable(name) << endl;
\r
841 // in case of inputPattern not created, do it
\r
842 if (lengthIP <= 0) {
\r
844 cout << "Strange case: input pattern is not created while it is time to compute output pattern !" << endl;
\r
845 // collect the input patterns for each input
\r
847 createInputPattern();
\r
849 catch(Exception e) {
\r
852 cout << "input pattern array initialized with min. len " << lengthIP << endl;
\r
855 // initialize the output pattern
\r
857 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
858 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
859 lengthOP = lengthIP+productionPattern.value(connIface)->size();
\r
860 QList<char>* pattern = new QList<char>();
\r
861 for(int i=0;i<lengthOP;i++) pattern->append(0);
\r
862 connIface->setOutputPattern(pattern);
\r
863 outputPattern.insert(connIface,pattern);
\r
865 cout << "output pattern array initialized" << endl;
\r
869 // search for the beginning of the first execution.
\r
870 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
871 cout << "found 1st exec clock: " << clock << endl;
\r
873 while (clock < lengthIP) {
\r
874 // initialize counters for current execution.
\r
875 int p = 0; // index in production pattern
\r
876 int o = 0; // clock+o will give the clock cycle of each output group
\r
877 int cip = 0; // clock+cip give the clock cycle of an input group
\r
878 int ccp = 0; // ccp give a column in the consumptio pattern
\r
879 int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP
\r
880 int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP
\r
881 bool cannotCompleteExec = false;
\r
882 for(int m=0;m<productionCounter.size();m++) {
\r
883 // search for the first production in PP
\r
884 while (!isValidDataGroup(productionPattern,p)) {
\r
888 int gap = 0; // count the number of extra null columns
\r
889 // search for PC(m) valid input group in IP
\r
890 while (nip < productionCounter.at(m)) {
\r
891 if (clock+cip < lengthIP) {
\r
892 if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
\r
897 cannotCompleteExec = true;
\r
902 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
904 // search for PC(m) valid input group in IP
\r
905 while (ncp < productionCounter.at(m)) {
\r
906 if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
\r
910 o += gap; // to take into acocunt of extra null columns
\r
911 combinePatterns(productionPattern,p,outputPattern,clock+o);
\r
916 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
918 // current exec. taken into accunt
\r
921 // search for the next exec.
\r
924 while ((clock < lengthIP) && (nip < delta)) {
\r
925 if (isValidDataGroup(inputPattern,clock)) nip += 1;
\r
926 if (nip < delta) clock += 1;
\r
928 cout << "found exec " << nbExec << " at clock: " << clock << endl;
\r
930 // find the last valid output data group
\r
931 while(! isValidDataGroup(outputPattern,lengthOP-1)) {
\r
932 removeDataGroup(outputPattern,lengthOP-1);
\r
936 // clear input pattern
\r
937 clearInputPattern();
\r
943 void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
\r
944 static QString fctName = "FunctionalBlock::computeOutputPattern()";
\r
945 #ifdef DEBUG_FCTNAME
\r
946 cout << "call to " << qPrintable(fctName) << endl;
\r
949 // case 1: the block is a generator for which output pattern
\r
950 // must be computed for a nbExec following executions
\r
953 cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;
\r
954 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
955 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
956 // create output pattern
\r
957 QList<char>* pp = productionPattern.value(connIface);
\r
958 QList<char>* pattern = new QList<char>(*pp);
\r
959 for(int i=1;i<nbExec;i++) pattern->append(*pp);
\r
960 // assign pattern to interface
\r
961 connIface->setOutputPattern(pattern);
\r
962 // store it in QMap
\r
963 outputPattern.insert(connIface,pattern);
\r
967 cout << "computing output pattern of " << qPrintable(name) << endl;
\r
969 // in case of inputPattern not created, do it
\r
970 if (lengthIP <= 0) {
\r
971 // collect the input patterns for each input
\r
973 createInputPattern();
\r
975 catch(Exception e) {
\r
978 cout << "input pattern array initialized with min. len " << lengthIP << endl;
\r
981 // initialize the output pattern
\r
983 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
984 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
985 lengthOP = lengthIP+productionPattern.value(connIface)->size();
\r
986 QList<char>* pattern = new QList<char>();
\r
987 for(int i=0;i<lengthOP;i++) pattern->append(0);
\r
988 connIface->setOutputPattern(pattern);
\r
989 outputPattern.insert(connIface,pattern);
\r
991 cout << "output pattern array initialized" << endl;
\r
995 // search for the beginning of the first execution.
\r
996 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
997 cout << "found 1st exec clock: " << clock << endl;
\r
999 while (clock < lengthIP) {
\r
1000 // initialize counters for current execution.
\r
1001 int p = 0; // index in production pattern
\r
1002 int o = 0; // clock+o will give the clock cycle of each output group
\r
1003 int cip = 0; // clock+cip give the clock cycle of an input group
\r
1004 int ccp = 0; // ccp give a column in the consumptio pattern
\r
1005 int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP
\r
1006 int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP
\r
1007 bool cannotCompleteExec = false;
\r
1008 for(int m=0;m<productionCounter.size();m++) {
\r
1009 // search for the first production in PP
\r
1010 while (!isValidDataGroup(productionPattern,p)) {
\r
1014 int gap = 0; // count the number of extra null columns
\r
1015 // search for PC(m) valid input group in IP
\r
1016 while (nip < productionCounter.at(m)) {
\r
1017 if (clock+cip < lengthIP) {
\r
1018 if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
\r
1023 cannotCompleteExec = true;
\r
1028 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
1030 // search for PC(m) valid input group in IP
\r
1031 while (ncp < productionCounter.at(m)) {
\r
1032 if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
\r
1036 o += gap; // to take into acocunt of extra null columns
\r
1037 combinePatterns(productionPattern,p,outputPattern,clock+o);
\r
1042 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
1044 // current exec. taken into accunt
\r
1047 // search for the next exec.
\r
1050 while ((clock < lengthIP) && (nip < delta)) {
\r
1051 if (isValidDataGroup(inputPattern,clock)) nip += 1;
\r
1052 if (nip < delta) clock += 1;
\r
1054 cout << "found exec " << nbExec << " at clock: " << clock << endl;
\r
1056 // find the last valid output data group
\r
1057 while(! isValidDataGroup(outputPattern,lengthOP-1)) {
\r
1058 removeDataGroup(outputPattern,lengthOP-1);
\r
1062 // clear input pattern
\r
1063 clearInputPattern();
\r
1067 bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
\r
1069 if (patternSrc.size() != patternDest.size()) return false;
\r
1070 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1071 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1072 while (iterSrc.hasNext()) {
\r
1075 QList<char>* srcPat = iterSrc.value();
\r
1076 QList<char>* destPat = iterDest.value();
\r
1077 if (srcCol >= srcPat->size()) return false;
\r
1078 if (destCol >= destPat->size()) return false;
\r
1079 if (srcPat->at(srcCol) != destPat->at(destCol)) return false;
\r
1084 bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, const QList<int> &srcCols, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
\r
1085 if (patternSrc.size() != srcCols.size()) return false;
\r
1086 if (patternSrc.size() != patternDest.size()) return false;
\r
1088 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1089 QListIterator<int> iterSrcCol(srcCols);
\r
1090 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1091 while (iterSrc.hasNext()) {
\r
1093 int srcCol = iterSrcCol.next();
\r
1095 QList<char>* srcPat = iterSrc.value();
\r
1096 QList<char>* destPat = iterDest.value();
\r
1097 if (srcCol >= srcPat->size()) return false;
\r
1098 if (destCol >= destPat->size()) return false;
\r
1099 if (srcPat->at(srcCol) != destPat->at(destCol)) return false;
\r
1104 bool FunctionalBlock::canCombinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
\r
1105 if (patternSrc.size() != patternDest.size()) return false;
\r
1106 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1107 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1108 while (iterSrc.hasNext()) {
\r
1111 QList<char>* srcPat = iterSrc.value();
\r
1112 QList<char>* destPat = iterDest.value();
\r
1113 if (srcCol >= srcPat->size()) return false;
\r
1114 if (destCol >= destPat->size()) return false;
\r
1115 if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return false;
\r
1116 if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return false;
\r
1121 void FunctionalBlock::combinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
\r
1122 if (patternSrc.size() != patternDest.size()) return;
\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;
\r
1131 if (destCol >= destPat->size()) return;
\r
1132 if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return;
\r
1133 if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return;
\r
1134 destPat->replace(destCol,destPat->at(destCol) | srcPat->at(srcCol));
\r
1138 void FunctionalBlock::appendToPattern(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int nbCols) {
\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
1148 while ((srcCol+i < srcPat->size()) && (i<nbCols)) {
\r
1149 destPat->append(srcPat->at(srcCol+i));
\r
1155 void FunctionalBlock::removeDataGroup(QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1156 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1157 while (iterSrc.hasNext()) {
\r
1159 QList<char>* srcPat = iterSrc.value();
\r
1160 if (offset < srcPat->size()) {
\r
1161 srcPat->removeAt(offset);
\r
1166 void FunctionalBlock::shiftRightPattern(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1167 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1168 while (iterSrc.hasNext()) {
\r
1170 QList<char>* srcPat = iterSrc.value();
\r
1171 if (offset < srcPat->size()) {
\r
1172 srcPat->insert(offset,0);
\r
1177 bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1178 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1179 while (iterSrc.hasNext()) {
\r
1181 QList<char>* srcPat = iterSrc.value();
\r
1182 if (offset >= srcPat->size()) return false;
\r
1183 if (srcPat->at(offset) == 1) return true;
\r
1188 bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface*, QList<char>* >& pattern, const QList<int> offsets) {
\r
1189 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1190 QListIterator<int> iterOffsets(offsets);
\r
1191 while (iterSrc.hasNext()) {
\r
1193 int offset = iterOffsets.next();
\r
1194 QList<char>* srcPat = iterSrc.value();
\r
1195 if (offset >= srcPat->size()) return false;
\r
1196 if (srcPat->at(offset) == 1) return true;
\r
1201 bool FunctionalBlock::isOnlyXDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1202 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1203 while (iterSrc.hasNext()) {
\r
1205 QList<char>* srcPat = iterSrc.value();
\r
1206 if (offset >= srcPat->size()) return false;
\r
1207 if (srcPat->at(offset) != -1) return false;
\r
1212 void FunctionalBlock::clearConsumptionPattern() {
\r
1213 QMapIterator<AbstractInterface*, QList<char>* > iterP(consumptionPattern);
\r
1214 while (iterP.hasNext()) {
\r
1216 QList<char>* pattern = iterP.value();
\r
1217 if (pattern != NULL) delete pattern;
\r
1219 consumptionPattern.clear();
\r
1223 void FunctionalBlock::clearProductionPattern() {
\r
1224 QMapIterator<AbstractInterface*, QList<char>* > iterP(productionPattern);
\r
1225 while (iterP.hasNext()) {
\r
1227 QList<char>* pattern = iterP.value();
\r
1228 if (pattern != NULL) delete pattern;
\r
1230 productionPattern.clear();
\r
1234 void FunctionalBlock::clearInputPattern() {
\r
1236 QMapIterator<AbstractInterface*,QList<char>* > iterI(inputPattern);
\r
1237 while (iterI.hasNext()) {
\r
1239 QList<char>* pattern = iterI.value();
\r
1240 if (pattern != NULL) delete pattern;
\r
1242 inputPattern.clear();
\r
1246 void FunctionalBlock::clearAdmittanceDelays() {
\r
1247 QMapIterator<AbstractInterface*, QList<int>* > iterA(admittanceDelays);
\r
1248 while (iterA.hasNext()) {
\r
1250 QList<int>* d = iterA.value();
\r
1251 if (d != NULL) delete d;
\r
1253 admittanceDelays.clear();
\r
1256 int FunctionalBlock::createTriggers() {
\r
1258 /* NB: this method returns the number of executions that have been started
\r
1259 but not necessary completed.
\r
1261 if (delta <= 0) return 0;
\r
1263 // search for the first exec.
\r
1264 while ((offset < lengthIP) && (! isValidDataGroup(inputPattern,offset))) offset++;
\r
1265 if (offset == lengthIP) return 0;
\r
1266 triggers.append(offset);
\r
1268 for(int i = offset;i<lengthIP;i++) {
\r
1269 if (isValidDataGroup(inputPattern,i)) nbGroup++;
\r
1270 if (nbGroup == delta+1) {
\r
1271 triggers.append(i);
\r
1275 return triggers.size();
\r