X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/blast.git/blobdiff_plain/cfe8322b50c9ef08ffbc3e52b0849bca4cd1d0bf..18fecf25efe710631fabecbb9f689c2997fdfe18:/FunctionalBlock.cpp?ds=sidebyside

diff --git a/FunctionalBlock.cpp b/FunctionalBlock.cpp
index 93e8377..2d455db 100644
--- a/FunctionalBlock.cpp
+++ b/FunctionalBlock.cpp
@@ -5,6 +5,7 @@
 #include "FunctionalInterface.h"
 #include "ReferenceInterface.h"
 #include "BlockParameter.h"
+#include "ArithmeticEvaluator.h"
 
 
 FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference) throw(Exception) :  AbstractBlock() {
@@ -13,14 +14,27 @@ FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference
   reference = _reference;
   parent = _parent;
   name = reference->getName();
-  consumptionPattern = NULL;
-  lengthCP = 0;
-  nbConsumingPorts = 0;
-  productionPattern = NULL;
-  lengthPP = 0;
-  nbProducingPorts = 0;  
+
+  if (reference->getImplementations().isEmpty()) {
+    implementation = NULL;
+    cout << "block has no implementation" << endl;
+  }
+  else {
+    implementation = reference->getImplementations().at(0);
+  }
+  lengthAP = -1;
+  lengthCP = -1;
+  lengthIP = -1;
+  lengthOP = -1;
+  lengthPP = -1;
+  delta = -1;
+  evaluator = NULL;
+
 }
 
+FunctionalBlock::~FunctionalBlock() {
+  if (evaluator != NULL) delete evaluator;
+}
 
 void FunctionalBlock::parametersValidation(QList<AbstractBlock*>* checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
   /*
@@ -64,6 +78,8 @@ void FunctionalBlock::populate() {
     addParameter(p);
   }
 
+  ConnectedInterface* toClk = NULL;
+  ConnectedInterface* toRst = NULL;
   // create interfaces from reference block
   QList<AbstractInterface *> lstRef = reference->getInterfaces();
   // store relation between functional and reference
@@ -77,8 +93,19 @@ void FunctionalBlock::populate() {
       exit(1);
     }
     hashIface.insert(lstRef.at(i),inter);
-
     addInterface(inter);
+    /* WARNING FOR THE FUTURE :
+       in case of there are several clock interfaces ofr that block
+       it would be a godd idea to make the user choose which one
+       must be connected to defautl clk.
+       Presently, the first encountered is chosen
+     */
+    if ((toClk == NULL) && (inter->getPurpose() == AbstractInterface::Clock)) {
+      toClk = AI_TO_CON(inter);
+    }
+    if ((toRst == NULL) && (inter->getPurpose() == AbstractInterface::Reset)) {
+      toRst = AI_TO_CON(inter);
+    }
   }
     
   AbstractInterface* funCtlIface = NULL;
@@ -95,8 +122,19 @@ void FunctionalBlock::populate() {
       }       
     }
   }
-}
 
+  // connect clk and rst to group clk/rst or to clkrstgen
+  if ((name != "clkrstgen") && (parent != NULL)) {
+    try {
+      connectClkReset();
+    }
+    catch(Exception e) {
+      AbstractBlock* source = (AbstractBlock *)(e.getSource());
+      cerr << qPrintable(source->getName()) << ":" << qPrintable(e.getMessage()) << endl;
+      throw(e);
+    }
+  }
+}
 
 QString FunctionalBlock::getReferenceXmlFile() {
     return ((ReferenceBlock *)reference)->getXmlFile();
@@ -106,60 +144,757 @@ QString FunctionalBlock::getReferenceHashMd5() {
     return ((ReferenceBlock *)reference)->getHashMd5();
 }
 
-void FunctionalBlock::computeOutputPattern(int nbExec) {
+void FunctionalBlock::createPatterns() throw(Exception) {
+  static QString fctName = "FunctionalBlock::createPatterns()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+  
+  cout << "create patterns for block " << qPrintable(name) << endl;
+  if (evaluator == NULL) evaluator = new ArithmeticEvaluator();
+  if (! isGeneratorBlock()) {
+    try {
+      createDelta();
+      createConsumptionPattern();    
+      createProductionCounter();
+    }
+    catch(Exception e) {
+      throw(e); // rethrow e
+    }
+  }
+  try {
+    createProductionPattern();
+  }
+  catch(Exception e) {
+    throw(e);
+  }
+  cout << "PP of " << qPrintable(name) << endl;
+  QMapIterator<AbstractInterface*,QList<char>* > it(productionPattern);
+  while (it.hasNext()) {
+    it.next();
+    QList<char>* pat = it.value();
+    foreach(char c, *pat) cout << (int)c;
+    cout << endl;
+  }
+}
+
+void FunctionalBlock::createDelta() throw(Exception) {
+  static QString fctName = "FunctionalBlock::createDelta()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif 
+  
+  QString deltaStr = implementation->getDelta();  
+  cout << "delta for " << qPrintable(name) << " = " << qPrintable(deltaStr) << endl;
+  if (deltaStr.isEmpty()) {
+    delta = -1;
+    return;
+  }
+  
+  // look for parameter names  
+  double result = 0;
+  try {
+    result = evaluateExpression(deltaStr);
+  }
+  catch(Exception e) {
+    throw(e);
+  }
+  delta = result;
+  cout << "delta = " << delta << endl;
+}
+
+void FunctionalBlock::createConsumptionPattern()  throw(Exception) {
+  static QString fctName = "FunctionalBlock::createConsumptionPattern()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+  
+  // first clear if already exists
+  clearConsumptionPattern();
+
+  lengthCP = -1;  
+  QHash<QString,QString> consPattern = implementation->getConsumptionPattern();  
+  
+  foreach(AbstractInterface* iface, getControlInputs()) {       
+    FunctionalInterface* connIface = AI_TO_FUN(iface);
+    QString refName = connIface->getReference()->getName();    
+    if (! consPattern.contains(refName)) {
+      throw(Exception(NO_IFACE_CP,this));
+      cerr << "no consumption pattern for reference interface " << qPrintable(refName) << endl;
+    }
+    QList<char>* pattern = NULL;
+    try {
+      pattern = expandPattern(consPattern.value(refName));
+    }
+    catch(Exception e) {
+      throw(e);
+    }
+    consumptionPattern.insert(connIface,pattern);
+    if (lengthCP == -1) {
+      lengthCP = pattern->size();
+    }
+    else {
+      if (pattern->size() != lengthCP) {
+        throw(Exception(INVALID_IFACE_CP_LENGTH,this));
+      }
+    }
+  }          
+}
+
+void FunctionalBlock::createProductionPattern() throw(Exception){  
+  static QString fctName = "FunctionalBlock::createProductionPattern()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+
+  // first clear if already exists
+  clearProductionPattern();
+  
+  lengthPP = -1;  
+  QHash<QString,QString> prodPattern = implementation->getProductionPattern();  
+  
+  foreach(AbstractInterface* iface, getControlOutputs()) {    
+    FunctionalInterface* connIface = AI_TO_FUN(iface);
+    QString refName = connIface->getReference()->getName();    
+    if (! prodPattern.contains(refName)) {
+      throw(Exception(NO_IFACE_PP,this));
+    }
+    QList<char>* pattern = NULL;
+    try {
+      pattern = expandPattern(prodPattern.value(refName));
+    }
+    catch(Exception e) {
+      throw(e);
+    }
+    productionPattern.insert(connIface,pattern);
+    if (lengthPP == -1) {
+      lengthPP = pattern->size();
+    }
+    else {
+      if (pattern->size() != lengthPP) {
+        throw(Exception(INVALID_IFACE_PP_LENGTH,this));
+      }
+    }
+  }      
+}
+
+void FunctionalBlock::createProductionCounter() throw(Exception) {
+  static QString fctName = "FunctionalBlock::createProductionCounter()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+
+  // first clear if already exists
+  productionCounter.clear();
+
+  
+  QStringList counterParts = implementation->getProductionCounter().split(",");
+  foreach(QString s, counterParts) {
+    cout << "cont part = " << qPrintable(s) << endl;
+    bool ok;
+    double val = s.toDouble(&ok);
+    if (ok) {
+      productionCounter.append(val);
+    }
+    else if (s.at(0) == '{') {
+      s.remove(0,1);
+      s.chop(1);
+      QStringList gen = s.split(":");
+      if (gen.size() != 3) {
+        throw(Exception(INVALID_IFACE_PC,this));
+      }
+      int start = 0;
+      int nb = 0;
+      int step = 0;
+      for(int i=0;i<3;i++) {        
+        double result = 0.0;
+        try {
+          result = evaluateExpression(gen.at(i));
+        }
+        catch(Exception e) {
+          throw(e);
+        }
+        if (i==0) start = result;
+        else if (i==1) nb = result;
+        else if (i==2) step = result;
+      }
+      for(int j=0;j<nb;j++) {
+        productionCounter.append(start+j*step);
+      }
+    }
+    else {      
+      double result = 0.0;
+      try {
+        result = evaluateExpression(s);
+      }
+      catch(Exception e) {
+        throw(e);
+      }
+      productionCounter.append(result);      
+    }
+  }
+  foreach(int val, productionCounter) {
+    cout << val << ",";
+  }
+  cout << endl;
+}
+
+QList<char>* FunctionalBlock::expandPattern(const QString& patternIn) throw(Exception) {
+  static QString fctName = "FunctionalBlock::expandPattern()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+  /* expanding a pattern is done in two steps :
+      - 1 : finding all variables that correspond to an expression
+            and copy them in the pattern
+      - 2 : parsing the result
+
+      Note that the result MUST contain only variables that have a
+      integer/double value. Otherwise, expanding will fail.
+
+   */
+
+  // first step.
+
+  QString p = replaceExpressions(patternIn);
+
+  QList<char> lst;  
+  p.append(')');
+  int offset = 0;  
+  QList<char>* patternOut = new QList<char>();
+  try {
+    patternOut->append(expandPatternRecur(p,&offset));
+  }
+  catch(Exception e) {
+    throw(e);
+  }
+
+  return patternOut;
+}
+
+QString FunctionalBlock::replaceExpressions(const QString& patternIn) throw(Exception) {
+
+  QString res = patternIn;
+  bool stop = false;
+  QRegularExpression re("[$][a-zA-Z0-9_]+");
+
+  while (!stop) {
+    stop = true;
+    QRegularExpressionMatchIterator matcher = re.globalMatch(res);
+    while(matcher.hasNext()) {
+      QRegularExpressionMatch m = matcher.next();
+      QString param = m.captured(0);
+      QString paramName = param;
+      paramName.remove(0,1);
+      BlockParameter* p = getParameterFromName(paramName);
+      if ((p != NULL) && (p->getType() == BlockParameter::Expression)) {
+        res.replace(param,p->getStringValue());
+        stop = false;
+        cout << "found an expr: " << qPrintable(paramName) << ", patern => " << qPrintable(res) << endl;
+      }
+    }
+  }
+  return res;
+}
+
+QList<char> FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset) throw(Exception) {
+  
+  QList<char> patternOut;
+
+  while ((*offset < patternIn.size()) && (patternIn.at(*offset) != ')')) {
+    
+    QChar c = patternIn.at(*offset);
+    if (c == '(') {
+      *offset += 1;
+      try {
+        patternOut.append(expandPatternRecur(patternIn,offset));
+      }
+      catch(Exception e) {
+        throw(e);
+      }
+    }
+    else if (c == '0') {
+      patternOut.append(0);
+    }
+    else if (c == '1') {
+      patternOut.append(1);
+    }
+    else if (c == 'X') {
+      patternOut.append(-1);
+    }
+    else if (c == '{') {
+      *offset += 1;
+      QString expr = "";      
+      while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
+        expr += patternIn.at(*offset);        
+        *offset += 1;
+      }
+      if (*offset == patternIn.size()) {
+        throw(Exception(INVALID_IFACE_PATTERN,this));
+      }
+      double repeat = 0;
+      try {
+        repeat = evaluateExpression(expr);
+      }
+      catch(Exception e) {
+        throw(e);
+      }
+      // repeat just the last value in currentGroup
+      char last = patternOut.last();
+      //cout << "repeat last char " << repeat << " times : " << (int)last << endl;
+      
+      for(int i=1;i<(int)repeat;i++) {
+        patternOut.append(last);
+      }
+    }    
+    *offset += 1;
+  }
+  
+  // must check if after ), there is a {
+  if ((*offset < patternIn.size()-1) && (patternIn.at(*offset+1) == '{')) {
+    *offset += 2;
+    QString expr = "";      
+    while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
+      expr += patternIn.at(*offset);        
+      *offset += 1;
+    }
+    if (*offset == patternIn.size()) {
+      throw(Exception(INVALID_IFACE_PATTERN,this));
+    }
+    double repeat = 0;
+    try {
+      repeat = evaluateExpression(expr);
+    }
+    catch(Exception e) {
+      throw(e);
+    }
+    /*
+    cout << "repeat last group " << repeat << " times : ";
+    foreach (char c, currentGroup) cout <<(int)c;
+    cout << endl;  
+    */
+    QList<char> single = patternOut;
+    for(int i=1;i<(int)repeat;i++) {
+      patternOut.append(single);
+    }    
+  }  
+  return patternOut;
+}
+
+double FunctionalBlock::evaluateExpression(const QString& expression) throw(Exception) {
+  static QString fctName = "FunctionalBlock::evaluateExpression()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+    
+  QHash<QString,double> vars;
+  evaluator->setExpression(expression);
+  QList<QString> varNames = evaluator->getVariableNames();
+  foreach (QString name, varNames) {
+    QString paramName = name;
+    paramName.remove(0,1);
+    BlockParameter* param = getParameterFromName(paramName);
+    if (param == NULL) {
+      throw(Exception(EVAL_PARAM_UNKNOWN,this));
+    }
+    bool okVal;
+    int val = param->getDoubleValue(&okVal);    
+    if (!okVal) {
+      throw(Exception(EVAL_PARAM_NOVALUE,this));
+    }
+    vars.insert(name,(double)val);    
+  }
+  
+  evaluator->setVariablesValue(vars);
+  double result = 0.0;
+  try {
+    result = evaluator->evaluate();
+  }
+  catch(int index) {
+    cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl;
+    throw(Exception(EVAL_INVALID_EXPR,this));
+  }
+  return result;
+}
+
+void FunctionalBlock::computeAdmittanceDelays() throw(Exception) {
+  static QString fctName = "FunctionalBlock::computeAdmittanceDelays()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+  QList<int> inClock;
+  QList<int> delays;
+
+  clearAdmittanceDelays();
+
+  // trying to synchronize the first one in AP
+  QMapIterator<AbstractInterface*,QList<char>* > iterAP(admittance);
+  QMapIterator<AbstractInterface*,QList<char>* > iterIP(inputPattern);
+
+  while (iterAP.hasNext()) {
+    iterAP.next();
+    iterIP.next();
+    QList<char>* ap = iterAP.value();
+    QList<char>* ip = iterIP.value();
+    int first = 0;
+    while ((first < lengthIP) && (ip->at(first) == 0)) first++;
+    while ((first < lengthAP) && (ap->at(first) == 0)) first--;
+    delays.append(first);
+    inClock.append(0);
+    QList<int>* delays = new QList<int>();
+    admittanceDelays.insert(iterAP.key(), delays);
+  }
+
+  QMapIterator<AbstractInterface*,QList<int>* > iterDelays(admittanceDelays);
+
+  // get the delay to apply
+  int maxDelay = 0;
+  for(int i=0;i<delays.size();i++) {
+    if (delays[i] > maxDelay) maxDelay = delays[i];
+  }
+  // adding the delays to IP
+  iterIP.toFront();
+  int i = 0;
+  while (iterIP.hasNext()) {
+    iterIP.next();
+    iterDelays.next();
+    QList<char>* ip = iterIP.value();
+    QList<int>* d = iterDelays.value();
+    d->append(maxDelay-delays[i]);
+    cout << "prependind " << qPrintable(iterIP.key()->getName()) << " with " << (maxDelay-delays[i]) << " 0" << endl;
+    for(int j=0;j<maxDelay-delays[i];j++) {
+      ip->prepend(0);
+    }
+    for(int j=0;j<delays[i];j++) {
+      ip->append(0);
+    }
+    i++;
+  }
+  lengthIP += maxDelay;
+
+  cout << "IP length = " << lengthIP << ", AP length = " << lengthAP << endl;
+  bool stop = false;
+  int apIndex = 0;
+  int ipIndex = 0;
+  while (!stop) {
+
+    // if AP is a valid group, search for the next valid group in IP
+    if (isValidDataGroup(admittance,apIndex)) {
+
+      while ((ipIndex < lengthIP) && (! isValidDataGroup(inputPattern,ipIndex))) ipIndex++;
+      if (ipIndex == lengthIP) {
+        stop = true;
+        continue;
+      }
+    }
+
+    iterAP.toFront();
+    iterIP.toFront();
+    iterDelays.toFront();
+
+    if (samePatterns(inputPattern,ipIndex,admittance,apIndex)) {
+      while (iterAP.hasNext()) {
+        iterAP.next();
+        iterDelays.next();
+        QList<char>* ap = iterAP.value();
+        if (ap->at(apIndex) == 1) {
+          QList<int>* d = iterDelays.value();
+          d->append(0); // the 1 is at its good place, so no delay
+        }
+      }
+    }
+    else {
+      cout << "diff between IP and AP at " << apIndex << endl;
+      // search for the next 1 in IP for every input that has a 1 in AP
+
+      while (iterAP.hasNext()) {
+        iterAP.next();
+        iterIP.next();
+        iterDelays.next();
+        QList<char>* ap = iterAP.value();
+        QList<char>* ip = iterIP.value();
+        QList<int>* d = iterDelays.value();
+        // case 1: 1 in IP is too late
+        if ((ap->at(apIndex) == 1) && (ip->at(ipIndex) == 0)) {
+          int delay = 1;
+          while ( ((ipIndex+delay) < lengthIP) && (ip->at(ipIndex+delay) == 0) ) delay++;
+          cout << "found a delay of " << (-delay) << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
+          // moving the 1 to its normal pos.
+          ip->replace(ipIndex,1);
+          ip->replace(ipIndex+delay,0);
+          d->append(-delay);
+        }
+        // case 2: 1 in IP is too soon
+        else if ((ap->at(apIndex) == 0) && (ip->at(ipIndex) == 1)) {
+          int delay = 1;
+          while ( ((apIndex+delay) < lengthAP) && (ap->at(apIndex+delay) == 0) ) delay++;
+          cout << "found a delay of " << delay << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
+          // search for next 0 in IP to put the 1
+          int k = ipIndex+delay;
+          while ((k < lengthIP) && (ip->at(k) == 1)) k++;
+          ip->replace(ipIndex,0);
+          ip->replace(k,1);
+          d->append(delay);
+        }
+      }
+      if (! samePatterns(inputPattern,inClock,admittance,apIndex)) {
+         cout << "Abnormal case while searching for delays" << endl;
+      }
+    }
+
+    apIndex++;
+    ipIndex++;
+    if ((apIndex >= lengthAP) || (ipIndex >= lengthIP)) stop = true;
+  }
+  iterDelays.toFront();
+  while (iterDelays.hasNext()) {
+    iterDelays.next();
+    QList<int>* d = iterDelays.value();
+    foreach(int v, *d) {
+      cout << v << " ";
+    }
+    cout << endl;
+  }
+
+}
+
+void FunctionalBlock::createInputPattern()  throw(Exception) {
+  static QString fctName = "FunctionalBlock::createInputPattern())";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+  
+  lengthIP = -1;
+  foreach(AbstractInterface* iface, getControlInputs()) {
+
+    ConnectedInterface* connIface = AI_TO_CON(iface);
+    // check if it is connected
+    if (connIface->getConnectedFrom() == NULL) {
+      throw(Exception(IFACE_NOT_CONNECTED,this));
+    }
+    // get the precursor output pattern
+    QList<char>* out = connIface->getConnectedFrom()->getOutputPattern();
+    AbstractInputModifier* modifier = connIface->getInputModifier();
+    // check if the input is modified
+    if (modifier != NULL) {
+
+      out = modifier->getModifiedInput(out);
+    }
+
+    if (out->size() == 0) {
+      clearInputPattern();
+      throw(Exception(NO_IFACE_IP,this));
+    }
+    if (lengthIP == -1) {
+      lengthIP = out->size();
+    }
+    else {
+      if (out->size() < lengthIP) lengthIP = out->size();
+    }
+    
+    QList<char>* in = new QList<char>(*out);
+    foreach(char c, *in) {
+      cout << (int)c;
+    }
+    cout << endl;    
+    inputPattern.insert(connIface,in);    
+  }
+  // search the last valid group in IP,
+  while(! isValidDataGroup(inputPattern,lengthIP-1)) {
+    //removeDataGroup(inputPattern,lengthIP-1);
+    lengthIP -= 1;
+  }
+}
+
+void FunctionalBlock::createAdmittance(int nbExec) throw(Exception) {
+  static QString fctName = "FunctionalBlock::createAdmittance()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif  
+  // firstly, copy CP in AP
+  QMapIterator<AbstractInterface*,QList<char>* > iterC(consumptionPattern);
+  while (iterC.hasNext()) {
+    iterC.next();
+    QList<char>* pattern = new QList<char>(*(iterC.value()));
+    admittance.insert(iterC.key(), pattern);    
+  }
+  lengthAP = lengthCP;
+  int clock = 0;  
+  cout << "trigger 1 at c.c. 0" << endl;
+  for(int i=1;i<nbExec;i++) {
+    // searching for the clock cycle for which a new exec starts
+    int nbGroup = 0;
+    while ((clock < lengthAP) && (nbGroup < delta)) {
+      if (isValidDataGroup(admittance,clock)) nbGroup+=1;
+      clock += 1;
+    }
+    while ((clock < lengthAP) && (! isValidDataGroup(admittance,clock))) clock+=1;
+    cout << "trigger " << (i+1) << " at c.c. " << clock << endl;
+    int sc = clock;
+    // combine CP with AP at sc
+    for(int j=0;j<lengthCP;j++) {
+      // first case : column of CP must be placed beyond AP's end.
+      if (sc == lengthAP) {
+        cout << i << "," << j << " append in AP at " << sc << endl;
+        appendToPattern(consumptionPattern,j,admittance,1);
+        lengthAP += 1;
+        sc += 1;	       
+      }
+      // second case : CP and AP can be combined directly (i.e. no X | 1 to do)
+      else if (canCombinePatterns(consumptionPattern,j,admittance,sc)) {
+        cout << i << "," << j << " combine at " << sc << endl;
+        combinePatterns(consumptionPattern,j,admittance,sc);
+        sc += 1;
+      }
+      // third case : CP has an X column
+      else if (isOnlyXDataGroup(consumptionPattern,j)) {
+        cout << i << "," << j << " shift rigth AP to combine at " << sc << endl;
+        shiftRightPattern(admittance,sc);
+        lengthAP += 1;
+        if (! canCombinePatterns(consumptionPattern,j,admittance,sc)) {
+          cerr << "Abnormal case when combining AP and CP" << endl;
+        }
+        combinePatterns(consumptionPattern,j,admittance,sc);        
+        sc += 1;
+      }
+      // fourth case : AP has an X column
+      else if (isOnlyXDataGroup(admittance,sc)) {
+        cout << i << "," << j << " jump c.c. for CP at " << sc << endl;        
+        sc += 1;
+        j -= 1;
+      }
+      else {
+        throw(INVALID_DELTA_CP);        
+      }
+    }
+  }
+  // turn all X into 0
+  QMapIterator<AbstractInterface*,QList<char>* > iterA(admittance);
+  while (iterA.hasNext()) {
+    iterA.next();
+    QList<char>* pattern = iterA.value();
+    for(int i=0;i<pattern->size();i++) {
+      if (pattern->at(i) == -1) pattern->replace(i,0);
+      cout << (int)(pattern->at(i));
+    }
+    cout << endl;
+  }  
+}
+
+void FunctionalBlock::checkInputPatternCompatibility() throw(Exception) {
+  static QString fctName = "FunctionalBlock::checkInputPatternCompatibility()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+    
+  // firstly, create input pattern
+  try {
+    createInputPattern();
+  }
+  catch(Exception e) {
+    throw(e);
+  }
+  int nbExec = createTriggers();
+  cout << qPrintable(name) << " will exec. " << nbExec << " times." << endl;
+  
+  try {
+    createAdmittance(nbExec);
+  }
+  catch(Exception e) {
+    cout << "cannot create admittance" << endl;
+    throw(e);
+  }
   
+  int clock = 0; // index in IP  
+  int i = 0; // index in AP  
+  while ((clock < lengthIP) && (i < lengthAP)) {
+     
+    // if AP is a valid group, search for the next valid group in IP
+    if (isValidDataGroup(admittance,i)) {
+      while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
+      if (clock == lengthIP) {
+        cerr << "Abnormal case: end of IP has been reached without finding a valid group" << endl;
+        throw(Exception(IP_END_NULLCOL,this));
+      }
+    }    
+    /* at that point 2 cases of compat : IP(clock) and AP(i) are equal valid group, or
+       are both null columns
+    */
+    if (! samePatterns(inputPattern,clock,admittance,i)) {
+      cout << "AP(" << i << ") and IP(" << clock << ") are not equal" << endl;
+      throw(Exception(IP_AP_NOTCOMPAT,this)); // IP and AP not compatible
+    }
+    clock++;
+    i++;
+  }
+  if (clock < lengthIP) {
+    throw(Exception(AP_TOO_SHORT,this));
+    cerr << "Abnormal case: AP is to short" << endl;   
+  }  
+}
+
+void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
+  static QString fctName = "FunctionalBlock::computeOutputPattern()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+
+  clearOutputPattern();
+
   /* case 1: the block is a generator for which output pattern
      must be computed for a nbExec following executions
   */
-  
+
   if (nbExec > 0) {
+    cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;
     foreach(AbstractInterface* iface, getControlOutputs()) {
-      QList<char> pattern;
-      for(int i=0;i<nbExec;i++) pattern += iface->getProductionPattern();
-      iface->setOutputPattern(pattern);
-    }    
+      FunctionalInterface* connIface = AI_TO_FUN(iface);
+      // create output pattern
+      QList<char>* pp = productionPattern.value(connIface);
+      QList<char>* pattern = new QList<char>(*pp);
+      for(int i=1;i<nbExec;i++) pattern->append(*pp);
+      // assign pattern to interface
+      connIface->setOutputPattern(pattern);
+      // store it in QMap
+      outputPattern.insert(connIface,pattern);      
+    }
   }
   else {
-    // initialize consumption and production patterns
-    initConsumptionPattern();
-    initProductionPattern();
+    cout << "computing output pattern of " << qPrintable(name) << endl;
     
-    // collect the input patterns for each input    
-    char** inputPattern = NULL;
-    int idIface = 0;
-    inputPattern = new char*[nbConsumingPorts];
-    int minLen = -1;
-    foreach(AbstractInterface* iface, getControlInputs()) {      
-      QList<char> in = iface->getConnectedFrom()->getOutputPattern();
-      if (minLen == -1) {
-        minLen = in.size();
+    // in case of inputPattern not created, do it
+    if (lengthIP <= 0) {
+
+      cout << "Strange case: input pattern is not created while it is time to compute output pattern !" << endl;
+      // collect the input patterns for each input    
+      try {
+        createInputPattern();
       }
-      else {
-        if (in.size() < minLen) minLen = in.size();
+      catch(Exception e) {
+        throw(e);
       }
-      inputPattern[idIface] = new char[in.size()];
-      int i = 0;
-      foreach(char c, in) inputPattern[idIface][i++] = c;
-      idIface += 1;      
+      cout << "input pattern array initialized with min. len " << lengthIP << endl;
     }
+    
     // initialize the output pattern    
-    char** outputPattern = NULL;
-    outputPattern = new char*[nbProducingPorts];
-    int lengthOP = 0;
-    idIface = 0;
+    lengthOP = 0;
     foreach(AbstractInterface* iface, getControlOutputs()) {
-      lengthOP = minLen+iface->getProductionPattern().size();
-      outputPattern[idIface] = new char[lengthOP];
-      memset(outputPattern[idIface],0,lengthOP);
-      idIface += 1;
+      FunctionalInterface* connIface = AI_TO_FUN(iface);      
+      lengthOP = lengthIP+productionPattern.value(connIface)->size();
+      QList<char>* pattern = new QList<char>();
+      for(int i=0;i<lengthOP;i++) pattern->append(0);
+      connIface->setOutputPattern(pattern);
+      outputPattern.insert(connIface,pattern);
     }
+    cout << "output pattern array initialized" << endl;
     
     int clock = 0;
     nbExec = 0;
     // search for the beginning of the first execution.
-    while (! isValidDataGroup(inputPattern,nbConsumingPorts,clock)) clock++;
+    while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
+    cout << "found 1st exec clock: " << clock << endl;
     
-    while (clock < minLen) {
+    while (clock < lengthIP) {
       // initialize counters for current execution.
       int p = 0; // index in production pattern
       int o = 0; // clock+o will give the clock cycle of each output group
@@ -170,15 +905,15 @@ void FunctionalBlock::computeOutputPattern(int nbExec) {
       bool cannotCompleteExec = false;
       for(int m=0;m<productionCounter.size();m++) {
         // search for the first production in PP
-        while (!isValidDataGroup(productionPattern,nbProducingPorts,p)) {
+        while (!isValidDataGroup(productionPattern,p)) {
           p += 1;
           o += 1;
         }
         int gap = 0; // count the number of extra null columns
         // search for PC(m) valid input group in IP
         while (nip < productionCounter.at(m)) {
-          if (clock+cip < minLen) {
-            if (isValidDataGroup(inputPattern,nbConsumingPorts,clock+cip)) nip += 1;
+          if (clock+cip < lengthIP) {
+            if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
             cip += 1;
             gap += 1;
           }
@@ -186,18 +921,18 @@ void FunctionalBlock::computeOutputPattern(int nbExec) {
             cannotCompleteExec = true;
             break;
           }        
-        }
+        }        
         
         if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
         
         // search for PC(m) valid input group in IP
         while (ncp < productionCounter.at(m)) {
-          if (isValidDataGroup(consumptionPattern,nbConsumingPorts,ccp)) cip += 1;
+          if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
           ccp += 1;
           gap -= 1;
         }
         o += gap; // to take into acocunt of extra null columns
-        combinePatterns(productionPattern,p,outputPattern,clock+o,1,nbProducingPorts);
+        combinePatterns(productionPattern,p,outputPattern,clock+o);
         p += 1;
         o += 1;
       }
@@ -210,100 +945,671 @@ void FunctionalBlock::computeOutputPattern(int nbExec) {
       // search for the next exec.
       clock += 1;      
       nip = 0;
-      while ((clock < minLen) && (nip < delta)) {
-        if (isValidDataGroup(inputPattern,nbConsumingPorts,clock)) nip += 1;
+      while ((clock < lengthIP) && (nip < delta)) {
+        if (isValidDataGroup(inputPattern,clock)) nip += 1;
         if (nip < delta) clock += 1;
       }
+      cout << "found exec " << nbExec << " at clock: " << clock << endl;
     }
     // find the last valid output data group
-    while(! isValidDataGroup(outputPattern,nbProducingPorts,lengthOP-1)) lengthOP -= 1;
-    
-    // copy back outputPattern info each interface
-    idIface = 0;
+    while(! isValidDataGroup(outputPattern,lengthOP-1)) {
+      removeDataGroup(outputPattern,lengthOP-1);
+      lengthOP -= 1;
+    }
+
+    // clear input pattern
+    clearInputPattern();
+  }  
+}
+
+/*
+
+void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
+  static QString fctName = "FunctionalBlock::computeOutputPattern()";
+#ifdef DEBUG_FCTNAME
+  cout << "call to " << qPrintable(fctName) << endl;
+#endif
+
+  // case 1: the block is a generator for which output pattern
+  //   must be computed for a nbExec following executions
+
+  if (nbExec > 0) {
+    cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;
     foreach(AbstractInterface* iface, getControlOutputs()) {
-      QList<char> pattern;
-      for(int i=0;i<lengthOP;i++) pattern.append(outputPattern[idIface][i]);
-      iface->setOutputPattern(pattern);      
-      idIface += 1;
+      FunctionalInterface* connIface = AI_TO_FUN(iface);
+      // create output pattern
+      QList<char>* pp = productionPattern.value(connIface);
+      QList<char>* pattern = new QList<char>(*pp);
+      for(int i=1;i<nbExec;i++) pattern->append(*pp);
+      // assign pattern to interface
+      connIface->setOutputPattern(pattern);
+      // store it in QMap
+      outputPattern.insert(connIface,pattern);
     }
-    
-    // clear inputPattern and outputPattern
-    for(int i=0;i<nbConsumingPorts; i++) {
-      delete [] inputPattern[i];
+  }
+  else {
+    cout << "computing output pattern of " << qPrintable(name) << endl;
+
+    // in case of inputPattern not created, do it
+    if (lengthIP <= 0) {
+      // collect the input patterns for each input
+      try {
+        createInputPattern();
+      }
+      catch(Exception e) {
+        throw(e);
+      }
+      cout << "input pattern array initialized with min. len " << lengthIP << endl;
+    }
+
+    // initialize the output pattern
+    lengthOP = 0;
+    foreach(AbstractInterface* iface, getControlOutputs()) {
+      FunctionalInterface* connIface = AI_TO_FUN(iface);
+      lengthOP = lengthIP+productionPattern.value(connIface)->size();
+      QList<char>* pattern = new QList<char>();
+      for(int i=0;i<lengthOP;i++) pattern->append(0);
+      connIface->setOutputPattern(pattern);
+      outputPattern.insert(connIface,pattern);
+    }
+    cout << "output pattern array initialized" << endl;
+
+    int clock = 0;
+    nbExec = 0;
+    // search for the beginning of the first execution.
+    while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
+    cout << "found 1st exec clock: " << clock << endl;
+
+    while (clock < lengthIP) {
+      // initialize counters for current execution.
+      int p = 0; // index in production pattern
+      int o = 0; // clock+o will give the clock cycle of each output group
+      int cip = 0; // clock+cip give the clock cycle of an input group
+      int ccp = 0; // ccp give a column in the consumptio pattern
+      int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP
+      int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP
+      bool cannotCompleteExec = false;
+      for(int m=0;m<productionCounter.size();m++) {
+        // search for the first production in PP
+        while (!isValidDataGroup(productionPattern,p)) {
+          p += 1;
+          o += 1;
+        }
+        int gap = 0; // count the number of extra null columns
+        // search for PC(m) valid input group in IP
+        while (nip < productionCounter.at(m)) {
+          if (clock+cip < lengthIP) {
+            if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
+            cip += 1;
+            gap += 1;
+          }
+          else {
+            cannotCompleteExec = true;
+            break;
+          }
+        }
+
+        if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
+
+        // search for PC(m) valid input group in IP
+        while (ncp < productionCounter.at(m)) {
+          if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
+          ccp += 1;
+          gap -= 1;
+        }
+        o += gap; // to take into acocunt of extra null columns
+        combinePatterns(productionPattern,p,outputPattern,clock+o);
+        p += 1;
+        o += 1;
+      }
+
+      if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
+
+      // current exec. taken into accunt
+      nbExec += 1;
+
+      // search for the next exec.
+      clock += 1;
+      nip = 0;
+      while ((clock < lengthIP) && (nip < delta)) {
+        if (isValidDataGroup(inputPattern,clock)) nip += 1;
+        if (nip < delta) clock += 1;
+      }
+      cout << "found exec " << nbExec << " at clock: " << clock << endl;
     }
-    delete [] inputPattern;
-    for(int i=0;i<nbProducingPorts; i++) {
-      delete [] outputPattern[i];
+    // find the last valid output data group
+    while(! isValidDataGroup(outputPattern,lengthOP-1)) {
+      removeDataGroup(outputPattern,lengthOP-1);
+      lengthOP -= 1;
     }
-    delete [] outputPattern;
+
+    // clear input pattern
+    clearInputPattern();
   }
 }
-
-bool FunctionalBlock::isValidDataGroup(char** pattern, int nbPorts, int clock) {
+*/
+bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
   
-  for(int i=0;i<nbPorts;i++) {
-    if (pattern[i][clock] == 1) return true;
+  if (patternSrc.size() != patternDest.size()) return false;
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
+  QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
+  while (iterSrc.hasNext()) {
+    iterSrc.next();
+    iterDest.next();    
+    QList<char>* srcPat = iterSrc.value();
+    QList<char>* destPat = iterDest.value();
+    if (srcCol >= srcPat->size()) return false;
+    if (destCol >= destPat->size()) return false;
+    if (srcPat->at(srcCol) != destPat->at(destCol)) return false;    
   }
-  return false;  
+  return true;
 }
 
-void FunctionalBlock::combinePatterns(char** patternSrc, int srcCol, char** patternDest, int destCol, int nbCols, int nbPorts ) {
-  
-  for (int i=0;i<nbCols;i++) {
-    for(int j=0;j<nbPorts;j++) {
-      patternDest[j][destCol+i] = patternDest[j][destCol+i] | patternSrc[j][srcCol+i];
+bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, const QList<int> &srcCols, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
+  if (patternSrc.size() != srcCols.size()) return false;
+  if (patternSrc.size() != patternDest.size()) return false;
+
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
+  QListIterator<int> iterSrcCol(srcCols);
+  QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
+  while (iterSrc.hasNext()) {
+    iterSrc.next();
+    int srcCol = iterSrcCol.next();
+    iterDest.next();
+    QList<char>* srcPat = iterSrc.value();
+    QList<char>* destPat = iterDest.value();
+    if (srcCol >= srcPat->size()) return false;
+    if (destCol >= destPat->size()) return false;
+    if (srcPat->at(srcCol) != destPat->at(destCol)) return false;
+  }
+  return true;
+}
+
+bool FunctionalBlock::canCombinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
+  if (patternSrc.size() != patternDest.size()) return false;
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
+  QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
+  while (iterSrc.hasNext()) {
+    iterSrc.next();
+    iterDest.next();    
+    QList<char>* srcPat = iterSrc.value();
+    QList<char>* destPat = iterDest.value();
+    if (srcCol >= srcPat->size()) return false;
+    if (destCol >= destPat->size()) return false;
+    if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return false;
+    if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return false;
+  }
+  return true;
+}
+
+void FunctionalBlock::combinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
+  if (patternSrc.size() != patternDest.size()) return;
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
+  QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
+  while (iterSrc.hasNext()) {
+    iterSrc.next();
+    iterDest.next();    
+    QList<char>* srcPat = iterSrc.value();
+    QList<char>* destPat = iterDest.value();
+    if (srcCol >= srcPat->size()) return;
+    if (destCol >= destPat->size()) return;
+    if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return;
+    if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return;    
+    destPat->replace(destCol,destPat->at(destCol) | srcPat->at(srcCol));
+  }  
+}
+
+void FunctionalBlock::appendToPattern(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int nbCols) {
+  if (patternSrc.size() != patternDest.size()) return;
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
+  QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
+  while (iterSrc.hasNext()) {
+    iterSrc.next();
+    iterDest.next();    
+    QList<char>* srcPat = iterSrc.value();
+    QList<char>* destPat = iterDest.value();    
+    int i=0;
+    while ((srcCol+i < srcPat->size()) && (i<nbCols)) {
+      destPat->append(srcPat->at(srcCol+i));
+      i++;
+    }
+  }  
+}
+
+void FunctionalBlock::removeDataGroup(QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);  
+  while (iterSrc.hasNext()) {
+    iterSrc.next();    
+    QList<char>* srcPat = iterSrc.value();
+    if (offset < srcPat->size()) {
+      srcPat->removeAt(offset);
     }
   }
-  
+}
+
+void FunctionalBlock::shiftRightPattern(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);  
+  while (iterSrc.hasNext()) {
+    iterSrc.next();    
+    QList<char>* srcPat = iterSrc.value();
+    if (offset < srcPat->size()) {
+      srcPat->insert(offset,0);
+    }
+  }
+}
+
+bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);  
+  while (iterSrc.hasNext()) {
+    iterSrc.next();    
+    QList<char>* srcPat = iterSrc.value();
+    if (offset >= srcPat->size()) return false;
+    if (srcPat->at(offset) == 1) return true;
+  }
+  return false;
+}
+
+bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface*, QList<char>* >& pattern, const QList<int> offsets) {
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
+  QListIterator<int> iterOffsets(offsets);
+  while (iterSrc.hasNext()) {
+    iterSrc.next();
+    int offset = iterOffsets.next();
+    QList<char>* srcPat = iterSrc.value();
+    if (offset >= srcPat->size()) return false;
+    if (srcPat->at(offset) == 1) return true;
+  }
+  return false;
+}
+
+bool FunctionalBlock::isOnlyXDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
+  QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);  
+  while (iterSrc.hasNext()) {
+    iterSrc.next();    
+    QList<char>* srcPat = iterSrc.value();
+    if (offset >= srcPat->size()) return false;
+    if (srcPat->at(offset) != -1) return false;
+  }
+  return true;  
 }
 
 void FunctionalBlock::clearConsumptionPattern() {
-  if (consumptionPattern == NULL) return;
+  QMapIterator<AbstractInterface*, QList<char>* > iterP(consumptionPattern);  
+  while (iterP.hasNext()) {
+    iterP.next();
+    QList<char>* pattern = iterP.value();
+    if (pattern != NULL) delete pattern;
+  }
+  consumptionPattern.clear();
+  lengthCP = -1;      
+}  
+
+void FunctionalBlock::clearProductionPattern() {
+  QMapIterator<AbstractInterface*, QList<char>* > iterP(productionPattern);  
+  while (iterP.hasNext()) {
+    iterP.next();
+    QList<char>* pattern = iterP.value();
+    if (pattern != NULL) delete pattern;
+  }
+  productionPattern.clear();
+  lengthPP = -1;
+}  
+
+void FunctionalBlock::clearInputPattern() {
   
-  for(int i=0;i<nbConsumingPorts; i++) {
-    delete [] consumptionPattern[i];
+  QMapIterator<AbstractInterface*,QList<char>* > iterI(inputPattern);
+  while (iterI.hasNext()) {
+    iterI.next();
+    QList<char>* pattern = iterI.value();
+    if (pattern != NULL) delete pattern;
   }
-  delete [] consumptionPattern;      
+  inputPattern.clear();
+  lengthIP = -1;
 }
 
-void FunctionalBlock::clearProductionPattern() {
-  if (productionPattern == NULL) return;
-  for(int i=0;i<nbProducingPorts;i++) {
-    delete [] productionPattern[i];
+void FunctionalBlock::clearOutputPattern() {
+
+  QMapIterator<AbstractInterface*,QList<char>* > iterO(outputPattern);
+  while (iterO.hasNext()) {
+    iterO.next();
+    ConnectedInterface* connIface = AI_TO_CON(iterO.key());
+    connIface->resetOutputPattern();
+    QList<char>* pattern = iterO.value();
+    if (pattern != NULL) delete pattern;
   }
-  delete [] productionPattern;
+  outputPattern.clear();
+  lengthOP = -1;
 }
 
-void FunctionalBlock::initConsumptionPattern() {
-  if (consumptionPattern != NULL) clearConsumptionPattern();
-  
-  nbConsumingPorts = getControlInputs().size();  
-  int idIface = 0;
-  consumptionPattern = new char*[nbConsumingPorts];  
-  foreach(AbstractInterface* iface, getControlInputs()) {
+void FunctionalBlock::clearAdmittanceDelays() {
+  QMapIterator<AbstractInterface*, QList<int>* > iterA(admittanceDelays);
+  while (iterA.hasNext()) {
+    iterA.next();
+    QList<int>* d = iterA.value();
+    if (d != NULL) delete d;
+  }
+  admittanceDelays.clear();
+}
+
+int FunctionalBlock::createTriggers() {
+  triggers.clear();
+  /* NB: this method returns the number of executions that have been started
+     but not necessary completed.
+  */
+  if (delta <= 0) return 0;  
+  int offset = 0;
+  // search for the first exec.
+  while ((offset < lengthIP) && (! isValidDataGroup(inputPattern,offset))) offset++;
+  if (offset == lengthIP) return 0;
+  triggers.append(offset);  
+  int nbGroup = 0;
+  for(int i = offset;i<lengthIP;i++) {
+    if (isValidDataGroup(inputPattern,i)) nbGroup++;
+    if (nbGroup == delta+1) {
+      triggers.append(i);
+      nbGroup = 1;
+    }
+  }      
+  return triggers.size();
+}
+
+void FunctionalBlock::generateVHDL(const QString& path) throw(Exception){
     
-    QList<char> in = iface->getConsumptionPattern();
-    lengthCP = in.size(); // normally, all inputs have the same lenght for CP
-    consumptionPattern[idIface] = new char[lengthCP];
-    int i = 0;
-    foreach(char c, in) consumptionPattern[idIface][i++] = c;
-    idIface += 1;    
+  BlockImplementation* impl = reference->getImplementations().at(0); // for now only take first impl available
+  QFile implFile(impl->getXmlFile());
+
+  // reading in into QDomDocument
+  QDomDocument document("implFile");
+
+  if (!implFile.open(QIODevice::ReadOnly)) {
+    throw(Exception(IMPLFILE_NOACCESS));
   }
+  if (!document.setContent(&implFile)) {
+    implFile.close();
+    throw(Exception(IMPLFILE_NOACCESS));
+  }
+  implFile.close();
+
+  bool genController = false;
+  QString coreFile = "";
+  QString controllerFile = "";
+
+  if (reference->isWBConfigurable()) {
+    genController = true;
+    controllerFile = path;
+    controllerFile += "/";
+    controllerFile.append(name);
+    controllerFile.append("_ctrl.vhd");    
+  }
+  else {
+    controllerFile = "nofile.vhd";    
+  }
+  coreFile = path;
+  coreFile += "/";
+  coreFile.append(name);
+  coreFile.append(".vhd");
+
+  QFile vhdlCore(coreFile);
+  QFile vhdlController(controllerFile);
+
+  if (!vhdlCore.open(QIODevice::WriteOnly)) {
+    throw(Exception(VHDLFILE_NOACCESS));
+  }
+
+  if (genController) {
+    if (!vhdlController.open(QIODevice::WriteOnly)) {
+      throw(Exception(VHDLFILE_NOACCESS));
+    }
+  }
+  QTextStream outCore(&vhdlCore);
+  QTextStream outController;
+  if (genController) {
+    outController.setDevice(&vhdlController);
+  }
+
+  try {
+    //Get the root element
+    QDomElement impl = document.documentElement();
+    QDomElement eltComments = impl.firstChildElement("comments");
+    generateComments(outCore,eltComments, coreFile);
+    QDomElement eltLibs = eltComments.nextSiblingElement("libraries");
+    generateLibraries(outCore, eltLibs);
+    generateEntity(outCore, genController);
+    QDomElement eltArch = eltLibs.nextSiblingElement("architecture");
+    generateArchitecture(outCore, eltArch );
+    if (genController) {
+      generateController(outController);
+    }
+  }
+  catch(Exception err) {
+    throw(err);
+  }
+
+  vhdlCore.close();
+  vhdlController.close();
+  
+ }
+
+void FunctionalBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {
+
+  for(int i = 0; i < 50; i++) {
+    out << "--";
+  }
+  out << "\n--" << endl;
+  QString fileName = coreFile;
+  out << "--  File        : " << fileName << endl;
+  out << "--" << endl;
+  QDomElement eltAuthor = elt.firstChildElement("author");
+  QString firstName = eltAuthor.attribute("firstname","");
+  QString lastName = eltAuthor.attribute("lastname","");
+  QString mail = eltAuthor.attribute("mail","");
+  out << "--  Author(s)   : "<<firstName+" "<<lastName<<" ("<<mail<<")" << endl;
+  out << "--" << endl;
+  QDomElement eltDate = eltAuthor.nextSiblingElement("date");
+  QString crea = eltDate.attribute("creation","");
+  out << "--  Creation Date   : "<<crea<< endl;
+  out << "--" << endl;
+  QDomElement eltRelated = eltDate.nextSiblingElement("related_files");
+  QString relateds = eltRelated.attribute("list","");
+  out << "--  Related files   :\n"<<relateds<<endl;
+  out << "--" << endl;
+  QDomElement eltDesc = eltRelated.nextSiblingElement("description");
+  QDomElement desc = eltDesc.firstChildElement();
+  QString descTxt = desc.text();
+  out << "--  Decription      :\n"<<descTxt<<endl;
+  out << "--" << endl;
+  QDomElement eltNote = eltDesc.nextSiblingElement("description");
+  QDomElement note = eltNote.firstChildElement();
+  QString noteTxt = note.text();
+  out << "--  Note          :\n"<<noteTxt<<endl;
+  out << "--" << endl;
+  for(int i = 0; i < 50; i++) {
+    out << "--";
+  }
+  out << endl << endl;
 }
 
-void FunctionalBlock::initProductionPattern() {
-  if (productionPattern != NULL) clearProductionPattern();
+void FunctionalBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {
   
-  nbProducingPorts = getControlOutputs().size();  
-  int idIface = 0;
-  productionPattern = new char*[nbProducingPorts];  
-  foreach(AbstractInterface* iface, getControlOutputs()) {
-    
-    QList<char> in = iface->getProductionPattern();
-    lengthPP = in.size(); // normally, all inputs have the same lenght for PP
-    productionPattern[idIface] = new char[lengthPP];
-    int i = 0;
-    foreach(char c, in) productionPattern[idIface][i++] = c;
-    idIface += 1;    
+  QDomNodeList listLib = elt.elementsByTagName("library");
+  for(int i = 0; i < listLib.length(); i++) {
+    QDomNode nodeLib = listLib.item(i);
+    QDomElement eltLib = nodeLib.toElement();
+    QString nameLib = eltLib.attribute("name","none");
+    out << "library " << nameLib << ";" << endl;
+    QDomNodeList listPack = eltLib.elementsByTagName("package");
+    for(int j = 0; j < listPack.length(); j++) {
+      QDomNode nodePack = listPack.item(j);
+      QDomElement eltPack = nodePack.toElement();
+      QString namePack = eltPack.attribute("name","none");
+      QString usePack = eltPack.attribute("use","none");
+      out << "use " << nameLib << "." << namePack << "." << usePack << endl;
+    }
+    out << endl;
+  }
+}
+
+void FunctionalBlock::generateEntity(QTextStream& out, bool hasController) throw(Exception) {
+
+  int i=0;
+  
+  //QList<BlockParameter*> listParams = reference->getParameters();
+  QList<AbstractInterface*> listInputs = getInputs();
+  QList<AbstractInterface*> listOutputs = getOutputs();
+  QList<AbstractInterface*> listBidirs = getBidirs();
+  QString typePort, namePort;
+
+  out << "entity " << name << " is" << endl;
+
+
+  /* TODO : rewrite the generation to take into acocunt the new object hierarchy */
+
+  // Generation of the generics
+  QList<BlockParameter*> listGenerics = getGenericParameters();
+  if ((!listGenerics.isEmpty()) || (hasController)) {
+    out << "  generic (" << endl;
+    if (hasController) {
+      out << "    wb_data_width : integer = 16;" << endl;
+      out << "    wb_addr_width : integer = 12";
+      if (!listGenerics.isEmpty()) out << ";";
+      out << endl;
+    }
+    for(i=0;i<listGenerics.size()-1;i++) {
+      out << "    " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
+    }
+    out << "    " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
+
+    out << "    );" << endl;
+  }
+
+  out << "  port (" << endl;
+
+  // Generation of the clk & rst signals
+  out << "    -- clk/rst" << endl;
+  foreach(AbstractInterface* iface, listInputs) {
+    if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {
+      out << "    " << iface->getName() << " : in std_logic;" << endl;
+    }
+  }
+
+  if (hasController) {
+    // Generation of the wishbone signals
+    out << "    -- registers r/w via wishbone" << endl;
+    QList<BlockParameter*> listWB = reference->getWishboneParameters();
+    for(i=0;i<listWB.size()-1;i++) {
+      out << "    " << listWB.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;
+    }
+    out << "    " << listWB.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;
+  }
+
+
+  int count = 0;
+  foreach(AbstractInterface* iface, getInterfaces()) {
+    if((iface->getPurpose() == AbstractInterface::Data)||(iface->getPurpose() == AbstractInterface::Control)) count++;
+  }
+  // Generation of the data/control signals
+
+  int flag = 0;
+  bool first = true;
+
+  foreach(AbstractInterface* iface, listInputs) {
+    if(iface->getPurpose() == AbstractInterface::Data) {
+      if (first) {
+        out << "    -- input data ports" << endl;
+        first = false;
+      }
+      count--;
+      if (count == 0) flag = AbstractInterface::NoComma;
+      out << "    " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
+    }
   }
+  first = true;
+  foreach(AbstractInterface* iface, listInputs) {
+    if(iface->getPurpose() == AbstractInterface::Control) {
+      if (first) {
+        out << "    -- input control ports" << endl;
+        first = false;
+      }
+      count--;
+      if (count == 0) flag = AbstractInterface::NoComma;
+      out << "    " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
+    }
+  }
+  first = true;
+  foreach(AbstractInterface* iface, listOutputs) {
+    if(iface->getPurpose() == AbstractInterface::Data) {
+      if (first) {
+        out << "    -- output data ports" << endl;
+        first = false;
+      }
+      count--;
+      if (count == 0) flag = AbstractInterface::NoComma;
+      out << "    " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
+    }
+  }
+  first = true;
+  foreach(AbstractInterface* iface, listOutputs) {
+    if(iface->getPurpose() == AbstractInterface::Control) {
+      if (first) {
+        out << "    -- output control ports" << endl;
+        first = false;
+      }
+      count--;
+      if (count == 0) flag = AbstractInterface::NoComma;
+      out << "    " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
+    }
+  }
+  first = true;
+  foreach(AbstractInterface* iface, listBidirs) {
+    if(iface->getPurpose() == AbstractInterface::Data) {
+      if (first) {
+        out << "    -- bidirs data ports" << endl;
+        first = false;
+      }
+      count--;
+      if (count == 0) flag = AbstractInterface::NoComma;
+      out << "    " << iface->toVHDL(AbstractInterface::Entity, flag) << endl;
+    }
+  }
+  out << "    );" << endl << endl;
+  out << "end " << name << ";" << endl << endl;
+}
+
+void FunctionalBlock::generateArchitecture(QTextStream& out, QDomElement &elt ) throw(Exception) {
+  QString expr;
+  QString code = elt.text();
+  cout << qPrintable(code) << endl;
+  out << "architecture rtl of " << name << " is" << endl;
+
+  QStringList listLine = code.split("\n");
+  for(int i =0; i < listLine.size(); i++) {
+    QString line = listLine.at(i).simplified();
+
+    /*
+    if(listLine.at(i).contains(QRegularExpression("@foreach{"))) {
+      while(listLine.at(i).compare("@endforeach") != -1) {
+        expr = expr + listLine.at(i) + '\n';
+        i++;
+      }
+      expr = expr + listLine.at(i);
+      out << evalComplex(expr, 1) << '\n';
+    }
+    if(listLine.at(i).contains(QRegularExpression("@caseeach{"))) {
+      while(listLine.at(i).compare("@endcaseeach") != -1) {
+        expr = expr + listLine.at(i) + '\n';
+        i++;
+      }
+      expr = expr + listLine.at(i);
+      out << evalComplex(expr, 2) << '\n';
+    }
+*/
+    if(line.contains("@{")) {
+      out << line << endl;
+    }
+  }
+}
+
+void FunctionalBlock::generateController(QTextStream &out) throw(Exception) {
+  
 }
+