#include "ArithmeticEvaluator.h"\r
\r
\r
-FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference) throw(Exception) : AbstractBlock() {\r
+FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference, bool createIfaces) throw(Exception) : AbstractBlock() {\r
//if (! _reference->isReferenceBlock()) throw(Exception(BLOCK_INVALID_TYPE));\r
//if (! _group->isGroupBlock()) throw(Exception(BLOCK_INVALID_TYPE));\r
reference = _reference;\r
lengthPP = -1;\r
delta = -1;\r
evaluator = NULL;\r
+\r
+ if (createIfaces) {\r
+ populate();\r
+ }\r
+\r
}\r
\r
FunctionalBlock::~FunctionalBlock() {\r
addParameter(p);\r
}\r
\r
+ ConnectedInterface* toClk = NULL;\r
+ ConnectedInterface* toRst = NULL;\r
// create interfaces from reference block\r
QList<AbstractInterface *> lstRef = reference->getInterfaces();\r
// store relation between functional and reference\r
exit(1);\r
}\r
hashIface.insert(lstRef.at(i),inter);\r
-\r
addInterface(inter);\r
+ /* WARNING FOR THE FUTURE :\r
+ in case of there are several clock interfaces ofr that block\r
+ it would be a good idea to make the user choose which one\r
+ must be connected to defautl clk.\r
+ Presently, the first encountered is chosen\r
+ */\r
+ if ((toClk == NULL) && (inter->getPurpose() == AbstractInterface::Clock)) {\r
+ toClk = AI_TO_CON(inter);\r
+ }\r
+ if ((toRst == NULL) && (inter->getPurpose() == AbstractInterface::Reset)) {\r
+ toRst = AI_TO_CON(inter);\r
+ }\r
}\r
\r
AbstractInterface* funCtlIface = NULL;\r
}\r
}\r
\r
-\r
QString FunctionalBlock::getReferenceXmlFile() {\r
return ((ReferenceBlock *)reference)->getXmlFile();\r
}\r
#ifdef DEBUG_FCTNAME\r
cout << "call to " << qPrintable(fctName) << endl;\r
#endif\r
+\r
+ if (implementation->hasNoPatterns()) return;\r
\r
cout << "create patterns for block " << qPrintable(name) << endl;\r
if (evaluator == NULL) evaluator = new ArithmeticEvaluator();\r
}\r
catch(Exception e) {\r
throw(e);\r
- } \r
+ }\r
+ cout << "PP of " << qPrintable(name) << endl;\r
+ QMapIterator<AbstractInterface*,QList<char>* > it(productionPattern);\r
+ while (it.hasNext()) {\r
+ it.next();\r
+ QList<char>* pat = it.value();\r
+ foreach(char c, *pat) cout << (int)c;\r
+ cout << endl;\r
+ }\r
}\r
\r
void FunctionalBlock::createDelta() throw(Exception) {\r
cout << "call to " << qPrintable(fctName) << endl;\r
#endif\r
\r
+ // first clear if already exists\r
+ clearConsumptionPattern();\r
+\r
lengthCP = -1; \r
QHash<QString,QString> consPattern = implementation->getConsumptionPattern(); \r
\r
FunctionalInterface* connIface = AI_TO_FUN(iface);\r
QString refName = connIface->getReference()->getName(); \r
if (! consPattern.contains(refName)) {\r
- throw(Exception(NO_IFACE_CP));\r
+ throw(Exception(NO_IFACE_CP,this));\r
cerr << "no consumption pattern for reference interface " << qPrintable(refName) << endl;\r
}\r
QList<char>* pattern = NULL;\r
}\r
else {\r
if (pattern->size() != lengthCP) {\r
- throw(Exception(INVALID_IFACE_CP_LENGTH));\r
+ throw(Exception(INVALID_IFACE_CP_LENGTH,this));\r
}\r
}\r
} \r
#ifdef DEBUG_FCTNAME\r
cout << "call to " << qPrintable(fctName) << endl;\r
#endif\r
+\r
+ // first clear if already exists\r
+ clearProductionPattern();\r
\r
lengthPP = -1; \r
QHash<QString,QString> prodPattern = implementation->getProductionPattern(); \r
FunctionalInterface* connIface = AI_TO_FUN(iface);\r
QString refName = connIface->getReference()->getName(); \r
if (! prodPattern.contains(refName)) {\r
- throw(Exception(NO_IFACE_PP)); \r
+ throw(Exception(NO_IFACE_PP,this));\r
}\r
QList<char>* pattern = NULL;\r
try {\r
}\r
else {\r
if (pattern->size() != lengthPP) {\r
- throw(Exception(INVALID_IFACE_PP_LENGTH));\r
+ throw(Exception(INVALID_IFACE_PP_LENGTH,this));\r
}\r
}\r
} \r
#ifdef DEBUG_FCTNAME\r
cout << "call to " << qPrintable(fctName) << endl;\r
#endif\r
+\r
+ // first clear if already exists\r
+ productionCounter.clear();\r
+\r
\r
QStringList counterParts = implementation->getProductionCounter().split(",");\r
foreach(QString s, counterParts) {\r
s.chop(1);\r
QStringList gen = s.split(":");\r
if (gen.size() != 3) {\r
- throw(Exception(INVALID_IFACE_PC));\r
+ throw(Exception(INVALID_IFACE_PC,this));\r
}\r
int start = 0;\r
int nb = 0;\r
#ifdef DEBUG_FCTNAME\r
cout << "call to " << qPrintable(fctName) << endl;\r
#endif\r
- \r
- QList<char> lst;\r
- QString p = patternIn;\r
+ /* expanding a pattern is done in two steps :\r
+ - 1 : finding all variables that correspond to an expression\r
+ and copy them in the pattern\r
+ - 2 : parsing the result\r
+\r
+ Note that the result MUST contain only variables that have a\r
+ integer/double value. Otherwise, expanding will fail.\r
+\r
+ */\r
+\r
+ // first step.\r
+\r
+ QString p = replaceExpressions(patternIn);\r
+\r
+ QList<char> lst; \r
p.append(')');\r
int offset = 0; \r
QList<char>* patternOut = new QList<char>();\r
try {\r
- expandPatternRecur(p,&offset, patternOut); \r
+ patternOut->append(expandPatternRecur(p,&offset));\r
}\r
catch(Exception e) {\r
throw(e);\r
return patternOut;\r
}\r
\r
-void FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset, QList<char>* patternOut) throw(Exception) { \r
+QString FunctionalBlock::replaceExpressions(const QString& patternIn) throw(Exception) {\r
+\r
+ QString res = patternIn;\r
+ bool stop = false;\r
+ QRegularExpression re("[$][a-zA-Z0-9_]+");\r
+\r
+ while (!stop) {\r
+ stop = true;\r
+ QRegularExpressionMatchIterator matcher = re.globalMatch(res);\r
+ while(matcher.hasNext()) {\r
+ QRegularExpressionMatch m = matcher.next();\r
+ QString param = m.captured(0);\r
+ QString paramName = param;\r
+ paramName.remove(0,1);\r
+ BlockParameter* p = getParameterFromName(paramName);\r
+ if ((p != NULL) && (p->getType() == BlockParameter::Expression)) {\r
+ res.replace(param,p->getStringValue());\r
+ stop = false;\r
+ cout << "found an expr: " << qPrintable(paramName) << ", patern => " << qPrintable(res) << endl;\r
+ }\r
+ }\r
+ }\r
+ return res;\r
+}\r
+\r
+QList<char> FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset) throw(Exception) {\r
\r
+ QList<char> patternOut;\r
+\r
while ((*offset < patternIn.size()) && (patternIn.at(*offset) != ')')) {\r
\r
QChar c = patternIn.at(*offset);\r
if (c == '(') {\r
*offset += 1;\r
try {\r
- expandPatternRecur(patternIn,offset, patternOut);\r
+ patternOut.append(expandPatternRecur(patternIn,offset));\r
}\r
catch(Exception e) {\r
throw(e);\r
}\r
}\r
else if (c == '0') {\r
- patternOut->append(0);\r
+ patternOut.append(0);\r
}\r
else if (c == '1') {\r
- patternOut->append(1);\r
+ patternOut.append(1);\r
}\r
else if (c == 'X') {\r
- patternOut->append(-1);\r
+ patternOut.append(-1);\r
}\r
else if (c == '{') {\r
*offset += 1;\r
*offset += 1;\r
}\r
if (*offset == patternIn.size()) {\r
- throw(Exception(INVALID_IFACE_PATTERN));\r
+ throw(Exception(INVALID_IFACE_PATTERN,this));\r
}\r
double repeat = 0;\r
try {\r
catch(Exception e) {\r
throw(e);\r
}\r
+ if (repeat == 0) {\r
+ // remove the last\r
+ patternOut.removeLast();\r
+ }\r
+ else {\r
// repeat just the last value in currentGroup\r
- char last = patternOut->last(); \r
- //cout << "repeat last char " << repeat << " times : " << (int)last << endl;\r
- \r
- for(int i=1;i<(int)repeat;i++) {\r
- patternOut->append(last);\r
+ char last = patternOut.last();\r
+ //cout << "repeat last char " << repeat << " times : " << (int)last << endl;\r
+ for(int i=1;i<(int)repeat;i++) {\r
+ patternOut.append(last);\r
+ }\r
}\r
} \r
*offset += 1;\r
*offset += 1;\r
}\r
if (*offset == patternIn.size()) {\r
- throw(Exception(INVALID_IFACE_PATTERN));\r
+ throw(Exception(INVALID_IFACE_PATTERN,this));\r
}\r
double repeat = 0;\r
try {\r
catch(Exception e) {\r
throw(e);\r
}\r
- /*\r
+ if (repeat == 0) {\r
+ QList<char> voidList;\r
+ return voidList;\r
+ }\r
+ else {\r
+ /*\r
cout << "repeat last group " << repeat << " times : ";\r
foreach (char c, currentGroup) cout <<(int)c;\r
- cout << endl; \r
+ cout << endl;\r
*/\r
- QList<char> single = *patternOut;\r
- for(int i=1;i<(int)repeat;i++) {\r
- patternOut->append(single);\r
- } \r
+ QList<char> single = patternOut;\r
+ for(int i=1;i<(int)repeat;i++) {\r
+ patternOut.append(single);\r
+ }\r
+ }\r
} \r
+ return patternOut;\r
}\r
\r
double FunctionalBlock::evaluateExpression(const QString& expression) throw(Exception) {\r
foreach (QString name, varNames) {\r
QString paramName = name;\r
paramName.remove(0,1);\r
- BlockParameter* param = reference->getParameterFromName(paramName); \r
+ BlockParameter* param = getParameterFromName(paramName);\r
if (param == NULL) {\r
- throw(Exception(EVAL_PARAM_UNKNOWN));\r
+ throw(Exception(EVAL_PARAM_UNKNOWN,this));\r
}\r
bool okVal;\r
- int val = param->getDoubleValue(&okVal);\r
+ int val = param->getDoubleValue(&okVal); \r
if (!okVal) {\r
- throw(Exception(EVAL_PARAM_NOVALUE)); \r
+ throw(Exception(EVAL_PARAM_NOVALUE,this));\r
}\r
vars.insert(name,(double)val); \r
}\r
}\r
catch(int index) {\r
cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl;\r
- throw(Exception(EVAL_INVALID_EXPR));\r
+ throw(Exception(EVAL_INVALID_EXPR,this));\r
}\r
return result;\r
}\r
\r
+void FunctionalBlock::computeAdmittanceDelays() throw(Exception) {\r
+ static QString fctName = "FunctionalBlock::computeAdmittanceDelays()";\r
+#ifdef DEBUG_FCTNAME\r
+ cout << "call to " << qPrintable(fctName) << endl;\r
+#endif\r
+ QList<int> inClock;\r
+ QList<int> delays;\r
+\r
+ clearAdmittanceDelays();\r
+\r
+ // trying to synchronize the first one in AP\r
+ QMapIterator<AbstractInterface*,QList<char>* > iterAP(admittance);\r
+ QMapIterator<AbstractInterface*,QList<char>* > iterIP(inputPattern);\r
+\r
+ while (iterAP.hasNext()) {\r
+ iterAP.next();\r
+ iterIP.next();\r
+ QList<char>* ap = iterAP.value();\r
+ QList<char>* ip = iterIP.value();\r
+ int first = 0;\r
+ while ((first < lengthIP) && (ip->at(first) == 0)) first++;\r
+ while ((first < lengthAP) && (ap->at(first) == 0)) first--;\r
+ delays.append(first);\r
+ inClock.append(0);\r
+ QList<int>* delays = new QList<int>();\r
+ admittanceDelays.insert(iterAP.key(), delays);\r
+ }\r
+\r
+ QMapIterator<AbstractInterface*,QList<int>* > iterDelays(admittanceDelays);\r
+\r
+ // get the delay to apply\r
+ int maxDelay = 0;\r
+ for(int i=0;i<delays.size();i++) {\r
+ if (delays[i] > maxDelay) maxDelay = delays[i];\r
+ }\r
+ // adding the delays to IP\r
+ iterIP.toFront();\r
+ int i = 0;\r
+ while (iterIP.hasNext()) {\r
+ iterIP.next();\r
+ iterDelays.next();\r
+ QList<char>* ip = iterIP.value();\r
+ QList<int>* d = iterDelays.value();\r
+ d->append(maxDelay-delays[i]);\r
+ cout << "prependind " << qPrintable(iterIP.key()->getName()) << " with " << (maxDelay-delays[i]) << " 0" << endl;\r
+ for(int j=0;j<maxDelay-delays[i];j++) {\r
+ ip->prepend(0);\r
+ }\r
+ for(int j=0;j<delays[i];j++) {\r
+ ip->append(0);\r
+ }\r
+ i++;\r
+ }\r
+ lengthIP += maxDelay;\r
+\r
+ cout << "IP length = " << lengthIP << ", AP length = " << lengthAP << endl;\r
+ bool stop = false;\r
+ int apIndex = 0;\r
+ int ipIndex = 0;\r
+ while (!stop) {\r
+\r
+ // if AP is a valid group, search for the next valid group in IP\r
+ if (isValidDataGroup(admittance,apIndex)) {\r
+\r
+ while ((ipIndex < lengthIP) && (! isValidDataGroup(inputPattern,ipIndex))) ipIndex++;\r
+ if (ipIndex == lengthIP) {\r
+ stop = true;\r
+ continue;\r
+ }\r
+ }\r
+\r
+ iterAP.toFront();\r
+ iterIP.toFront();\r
+ iterDelays.toFront();\r
+\r
+ if (samePatterns(inputPattern,ipIndex,admittance,apIndex)) {\r
+ while (iterAP.hasNext()) {\r
+ iterAP.next();\r
+ iterDelays.next();\r
+ QList<char>* ap = iterAP.value();\r
+ if (ap->at(apIndex) == 1) {\r
+ QList<int>* d = iterDelays.value();\r
+ d->append(0); // the 1 is at its good place, so no delay\r
+ }\r
+ }\r
+ }\r
+ else {\r
+ cout << "diff between IP and AP at " << apIndex << endl;\r
+ // search for the next 1 in IP for every input that has a 1 in AP\r
+\r
+ while (iterAP.hasNext()) {\r
+ iterAP.next();\r
+ iterIP.next();\r
+ iterDelays.next();\r
+ QList<char>* ap = iterAP.value();\r
+ QList<char>* ip = iterIP.value();\r
+ QList<int>* d = iterDelays.value();\r
+ // case 1: 1 in IP is too late\r
+ if ((ap->at(apIndex) == 1) && (ip->at(ipIndex) == 0)) {\r
+ int delay = 1;\r
+ while ( ((ipIndex+delay) < lengthIP) && (ip->at(ipIndex+delay) == 0) ) delay++;\r
+ cout << "found a delay of " << (-delay) << " for iface " << qPrintable(iterAP.key()->getName()) << endl;\r
+ // moving the 1 to its normal pos.\r
+ ip->replace(ipIndex,1);\r
+ ip->replace(ipIndex+delay,0);\r
+ d->append(-delay);\r
+ }\r
+ // case 2: 1 in IP is too soon\r
+ else if ((ap->at(apIndex) == 0) && (ip->at(ipIndex) == 1)) {\r
+ int delay = 1;\r
+ while ( ((apIndex+delay) < lengthAP) && (ap->at(apIndex+delay) == 0) ) delay++;\r
+ cout << "found a delay of " << delay << " for iface " << qPrintable(iterAP.key()->getName()) << endl;\r
+ // search for next 0 in IP to put the 1\r
+ int k = ipIndex+delay;\r
+ while ((k < lengthIP) && (ip->at(k) == 1)) k++;\r
+ ip->replace(ipIndex,0);\r
+ ip->replace(k,1);\r
+ d->append(delay);\r
+ }\r
+ }\r
+ if (! samePatterns(inputPattern,inClock,admittance,apIndex)) {\r
+ cout << "Abnormal case while searching for delays" << endl;\r
+ }\r
+ }\r
+\r
+ apIndex++;\r
+ ipIndex++;\r
+ if ((apIndex >= lengthAP) || (ipIndex >= lengthIP)) stop = true;\r
+ }\r
+ iterDelays.toFront();\r
+ while (iterDelays.hasNext()) {\r
+ iterDelays.next();\r
+ QList<int>* d = iterDelays.value();\r
+ foreach(int v, *d) {\r
+ cout << v << " ";\r
+ }\r
+ cout << endl;\r
+ }\r
+\r
+}\r
+\r
void FunctionalBlock::createInputPattern() throw(Exception) {\r
static QString fctName = "FunctionalBlock::createInputPattern())";\r
#ifdef DEBUG_FCTNAME\r
#endif\r
\r
lengthIP = -1;\r
- foreach(AbstractInterface* iface, getControlInputs()) { \r
+ foreach(AbstractInterface* iface, getControlInputs()) {\r
+\r
ConnectedInterface* connIface = AI_TO_CON(iface);\r
+ // check if it is connected\r
+ if (connIface->getConnectedFrom() == NULL) {\r
+ throw(Exception(IFACE_NOT_CONNECTED,this));\r
+ }\r
+ // get the precursor output pattern\r
QList<char>* out = connIface->getConnectedFrom()->getOutputPattern();\r
+ AbstractInputModifier* modifier = connIface->getInputModifier();\r
+ // check if the input is modified\r
+ if (modifier != NULL) {\r
+\r
+ out = modifier->getModifiedInput(out);\r
+ }\r
+\r
if (out->size() == 0) {\r
clearInputPattern();\r
- throw(Exception(NO_IFACE_IP));\r
+ throw(Exception(NO_IFACE_IP,this));\r
}\r
if (lengthIP == -1) {\r
lengthIP = out->size();\r
while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;\r
if (clock == lengthIP) {\r
cerr << "Abnormal case: end of IP has been reached without finding a valid group" << endl;\r
- throw(Exception(IP_END_NULLCOL)); \r
+ throw(Exception(IP_END_NULLCOL,this));\r
}\r
} \r
/* at that point 2 cases of compat : IP(clock) and AP(i) are equal valid group, or\r
*/\r
if (! samePatterns(inputPattern,clock,admittance,i)) {\r
cout << "AP(" << i << ") and IP(" << clock << ") are not equal" << endl;\r
- throw(Exception(IP_AP_NOTCOMPAT)); // IP and AP not compatible\r
+ throw(Exception(IP_AP_NOTCOMPAT,this)); // IP and AP not compatible\r
}\r
clock++;\r
i++;\r
}\r
if (clock < lengthIP) {\r
- throw(Exception(AP_TOO_SHORT));\r
+ throw(Exception(AP_TOO_SHORT,this));\r
cerr << "Abnormal case: AP is to short" << endl; \r
} \r
}\r
#ifdef DEBUG_FCTNAME\r
cout << "call to " << qPrintable(fctName) << endl;\r
#endif\r
- \r
+\r
+ clearOutputPattern();\r
+\r
/* case 1: the block is a generator for which output pattern\r
must be computed for a nbExec following executions\r
*/\r
- \r
- \r
+\r
if (nbExec > 0) {\r
cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;\r
foreach(AbstractInterface* iface, getControlOutputs()) {\r
\r
// in case of inputPattern not created, do it\r
if (lengthIP <= 0) {\r
+\r
+ cout << "Strange case: input pattern is not created while it is time to compute output pattern !" << endl;\r
// collect the input patterns for each input \r
try {\r
createInputPattern();\r
} \r
}\r
\r
+/*\r
+\r
+void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {\r
+ static QString fctName = "FunctionalBlock::computeOutputPattern()";\r
+#ifdef DEBUG_FCTNAME\r
+ cout << "call to " << qPrintable(fctName) << endl;\r
+#endif\r
+\r
+ // case 1: the block is a generator for which output pattern\r
+ // must be computed for a nbExec following executions\r
+\r
+ if (nbExec > 0) {\r
+ cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;\r
+ foreach(AbstractInterface* iface, getControlOutputs()) {\r
+ FunctionalInterface* connIface = AI_TO_FUN(iface);\r
+ // create output pattern\r
+ QList<char>* pp = productionPattern.value(connIface);\r
+ QList<char>* pattern = new QList<char>(*pp);\r
+ for(int i=1;i<nbExec;i++) pattern->append(*pp);\r
+ // assign pattern to interface\r
+ connIface->setOutputPattern(pattern);\r
+ // store it in QMap\r
+ outputPattern.insert(connIface,pattern);\r
+ }\r
+ }\r
+ else {\r
+ cout << "computing output pattern of " << qPrintable(name) << endl;\r
+\r
+ // in case of inputPattern not created, do it\r
+ if (lengthIP <= 0) {\r
+ // collect the input patterns for each input\r
+ try {\r
+ createInputPattern();\r
+ }\r
+ catch(Exception e) {\r
+ throw(e);\r
+ }\r
+ cout << "input pattern array initialized with min. len " << lengthIP << endl;\r
+ }\r
+\r
+ // initialize the output pattern\r
+ lengthOP = 0;\r
+ foreach(AbstractInterface* iface, getControlOutputs()) {\r
+ FunctionalInterface* connIface = AI_TO_FUN(iface);\r
+ lengthOP = lengthIP+productionPattern.value(connIface)->size();\r
+ QList<char>* pattern = new QList<char>();\r
+ for(int i=0;i<lengthOP;i++) pattern->append(0);\r
+ connIface->setOutputPattern(pattern);\r
+ outputPattern.insert(connIface,pattern);\r
+ }\r
+ cout << "output pattern array initialized" << endl;\r
+\r
+ int clock = 0;\r
+ nbExec = 0;\r
+ // search for the beginning of the first execution.\r
+ while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;\r
+ cout << "found 1st exec clock: " << clock << endl;\r
+\r
+ while (clock < lengthIP) {\r
+ // initialize counters for current execution.\r
+ int p = 0; // index in production pattern\r
+ int o = 0; // clock+o will give the clock cycle of each output group\r
+ int cip = 0; // clock+cip give the clock cycle of an input group\r
+ int ccp = 0; // ccp give a column in the consumptio pattern\r
+ int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP\r
+ int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP\r
+ bool cannotCompleteExec = false;\r
+ for(int m=0;m<productionCounter.size();m++) {\r
+ // search for the first production in PP\r
+ while (!isValidDataGroup(productionPattern,p)) {\r
+ p += 1;\r
+ o += 1;\r
+ }\r
+ int gap = 0; // count the number of extra null columns\r
+ // search for PC(m) valid input group in IP\r
+ while (nip < productionCounter.at(m)) {\r
+ if (clock+cip < lengthIP) {\r
+ if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;\r
+ cip += 1;\r
+ gap += 1;\r
+ }\r
+ else {\r
+ cannotCompleteExec = true;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern\r
+\r
+ // search for PC(m) valid input group in IP\r
+ while (ncp < productionCounter.at(m)) {\r
+ if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;\r
+ ccp += 1;\r
+ gap -= 1;\r
+ }\r
+ o += gap; // to take into acocunt of extra null columns\r
+ combinePatterns(productionPattern,p,outputPattern,clock+o);\r
+ p += 1;\r
+ o += 1;\r
+ }\r
+\r
+ if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern\r
+\r
+ // current exec. taken into accunt\r
+ nbExec += 1;\r
+\r
+ // search for the next exec.\r
+ clock += 1;\r
+ nip = 0;\r
+ while ((clock < lengthIP) && (nip < delta)) {\r
+ if (isValidDataGroup(inputPattern,clock)) nip += 1;\r
+ if (nip < delta) clock += 1;\r
+ }\r
+ cout << "found exec " << nbExec << " at clock: " << clock << endl;\r
+ }\r
+ // find the last valid output data group\r
+ while(! isValidDataGroup(outputPattern,lengthOP-1)) {\r
+ removeDataGroup(outputPattern,lengthOP-1);\r
+ lengthOP -= 1;\r
+ }\r
+\r
+ // clear input pattern\r
+ clearInputPattern();\r
+ }\r
+}\r
+*/\r
bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {\r
\r
if (patternSrc.size() != patternDest.size()) return false;\r
return true;\r
}\r
\r
+bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, const QList<int> &srcCols, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {\r
+ if (patternSrc.size() != srcCols.size()) return false;\r
+ if (patternSrc.size() != patternDest.size()) return false;\r
+\r
+ QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);\r
+ QListIterator<int> iterSrcCol(srcCols);\r
+ QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);\r
+ while (iterSrc.hasNext()) {\r
+ iterSrc.next();\r
+ int srcCol = iterSrcCol.next();\r
+ iterDest.next();\r
+ QList<char>* srcPat = iterSrc.value();\r
+ QList<char>* destPat = iterDest.value();\r
+ if (srcCol >= srcPat->size()) return false;\r
+ if (destCol >= destPat->size()) return false;\r
+ if (srcPat->at(srcCol) != destPat->at(destCol)) return false;\r
+ }\r
+ return true;\r
+}\r
+\r
bool FunctionalBlock::canCombinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {\r
if (patternSrc.size() != patternDest.size()) return false;\r
QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);\r
return false;\r
}\r
\r
+bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface*, QList<char>* >& pattern, const QList<int> offsets) {\r
+ QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);\r
+ QListIterator<int> iterOffsets(offsets);\r
+ while (iterSrc.hasNext()) {\r
+ iterSrc.next();\r
+ int offset = iterOffsets.next();\r
+ QList<char>* srcPat = iterSrc.value();\r
+ if (offset >= srcPat->size()) return false;\r
+ if (srcPat->at(offset) == 1) return true;\r
+ }\r
+ return false;\r
+}\r
+\r
bool FunctionalBlock::isOnlyXDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {\r
QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern); \r
while (iterSrc.hasNext()) {\r
lengthIP = -1;\r
}\r
\r
+void FunctionalBlock::clearOutputPattern() {\r
+\r
+ QMapIterator<AbstractInterface*,QList<char>* > iterO(outputPattern);\r
+ while (iterO.hasNext()) {\r
+ iterO.next();\r
+ ConnectedInterface* connIface = AI_TO_CON(iterO.key());\r
+ connIface->resetOutputPattern();\r
+ QList<char>* pattern = iterO.value();\r
+ if (pattern != NULL) delete pattern;\r
+ }\r
+ outputPattern.clear();\r
+ lengthOP = -1;\r
+}\r
+\r
+void FunctionalBlock::clearAdmittanceDelays() {\r
+ QMapIterator<AbstractInterface*, QList<int>* > iterA(admittanceDelays);\r
+ while (iterA.hasNext()) {\r
+ iterA.next();\r
+ QList<int>* d = iterA.value();\r
+ if (d != NULL) delete d;\r
+ }\r
+ admittanceDelays.clear();\r
+}\r
+\r
int FunctionalBlock::createTriggers() {\r
triggers.clear();\r
/* NB: this method returns the number of executions that have been started\r
} \r
return triggers.size();\r
}\r
+\r
+QList<QString> FunctionalBlock::getExternalResources() {\r
+\r
+ BlockImplementation* impl = reference->getImplementations().at(0); // for now only take first impl available\r
+ QList<QString> list = impl->getResources();\r
+ foreach(QString s, list) {\r
+ cout << qPrintable(s) << " ";\r
+ }\r
+ cout << endl;\r
+\r
+ return list;\r
+}\r
+\r
+\r
+void FunctionalBlock::generateVHDL(const QString& path) throw(Exception){\r
+ \r
+ BlockImplementation* impl = reference->getImplementations().at(0); // for now only take first impl available \r
+\r
+ QFile implFile(impl->getXmlFile());\r
+\r
+ // reading in into QDomDocument\r
+ QDomDocument document("implFile");\r
+\r
+ if (!implFile.open(QIODevice::ReadOnly)) {\r
+ throw(Exception(IMPLFILE_NOACCESS));\r
+ }\r
+ if (!document.setContent(&implFile)) {\r
+ implFile.close();\r
+ throw(Exception(IMPLFILE_NOACCESS));\r
+ }\r
+ implFile.close();\r
+\r
+ bool genController = false;\r
+ QString coreFile = "";\r
+ QString controllerFile = "";\r
+\r
+ if (reference->isWBConfigurable()) {\r
+ genController = true;\r
+ controllerFile = path;\r
+ controllerFile += "/";\r
+ controllerFile.append(name);\r
+ controllerFile.append("_ctrl.vhd"); \r
+ }\r
+ else {\r
+ controllerFile = "nofile.vhd"; \r
+ }\r
+ coreFile = path;\r
+ coreFile += "/";\r
+ coreFile.append(name);\r
+ coreFile.append(".vhd");\r
+\r
+ QFile vhdlCore(coreFile);\r
+ QFile vhdlController(controllerFile);\r
+\r
+ if (!vhdlCore.open(QIODevice::WriteOnly)) {\r
+ throw(Exception(VHDLFILE_NOACCESS));\r
+ }\r
+\r
+ if (genController) {\r
+ if (!vhdlController.open(QIODevice::WriteOnly)) {\r
+ throw(Exception(VHDLFILE_NOACCESS));\r
+ }\r
+ }\r
+ QTextStream outCore(&vhdlCore);\r
+ QTextStream outController;\r
+ if (genController) {\r
+ outController.setDevice(&vhdlController);\r
+ }\r
+\r
+ try {\r
+ //Get the root element\r
+ QDomElement impl = document.documentElement();\r
+ QDomElement eltComments = impl.firstChildElement("comments");\r
+ generateComments(outCore,eltComments, coreFile);\r
+ QDomElement eltLibs = eltComments.nextSiblingElement("libraries");\r
+ generateLibraries(outCore, eltLibs);\r
+ generateEntity(outCore, genController);\r
+ QDomElement eltArch = eltLibs.nextSiblingElement("architecture");\r
+ generateArchitecture(outCore, eltArch );\r
+ if (genController) {\r
+ generateController(outController);\r
+ }\r
+ }\r
+ catch(Exception err) {\r
+ throw(err);\r
+ }\r
+\r
+ vhdlCore.close();\r
+ vhdlController.close();\r
+ \r
+ }\r
+\r
+void FunctionalBlock::generateComments(QTextStream& out, QDomElement &elt, QString coreFile) throw(Exception) {\r
+\r
+ for(int i = 0; i < 50; i++) {\r
+ out << "--";\r
+ }\r
+ out << "\n--" << endl;\r
+ QString fileName = coreFile;\r
+ out << "-- File : " << fileName << endl;\r
+ out << "--" << endl;\r
+ QDomElement eltAuthor = elt.firstChildElement("author");\r
+ QString firstName = eltAuthor.attribute("firstname","");\r
+ QString lastName = eltAuthor.attribute("lastname","");\r
+ QString mail = eltAuthor.attribute("mail","");\r
+ out << "-- Author(s) : "<<firstName+" "<<lastName<<" ("<<mail<<")" << endl;\r
+ out << "--" << endl;\r
+ QDomElement eltLog = eltAuthor.nextSiblingElement("log");\r
+ QString crea = eltLog.attribute("creation","");\r
+ out << "-- Creation Date : "<<crea<< endl;\r
+ out << "--" << endl;\r
+ QDomNodeList listModifs = eltLog.elementsByTagName("modification");\r
+ for(int j=0;j<listModifs.length();j++) {\r
+ QDomNode nodeModif = listModifs.at(j);\r
+ QDomElement eltModif = nodeModif.toElement();\r
+ }\r
+ out << "-- Description : " << endl;\r
+ out << reference->getDescription() << endl;\r
+ out << "--" << endl;\r
+ QDomElement eltNote = eltLog.nextSiblingElement("notes");\r
+ QDomElement note = eltNote.firstChildElement();\r
+ QString noteTxt = note.text();\r
+ out << "-- Notes :\n"<<noteTxt<<endl;\r
+ out << "--" << endl;\r
+ for(int i = 0; i < 50; i++) {\r
+ out << "--";\r
+ }\r
+ out << endl << endl;\r
+}\r
+\r
+void FunctionalBlock::generateLibraries(QTextStream& out, QDomElement &elt) throw(Exception) {\r
+ \r
+ QDomNodeList listLib = elt.elementsByTagName("library");\r
+ for(int i = 0; i < listLib.length(); i++) {\r
+ QDomNode nodeLib = listLib.item(i);\r
+ QDomElement eltLib = nodeLib.toElement();\r
+ QString nameLib = eltLib.attribute("name","none");\r
+ out << "library " << nameLib << ";" << endl;\r
+ QDomNodeList listPack = eltLib.elementsByTagName("package");\r
+ for(int j = 0; j < listPack.length(); j++) {\r
+ QDomNode nodePack = listPack.item(j);\r
+ QDomElement eltPack = nodePack.toElement();\r
+ QString namePack = eltPack.attribute("name","none");\r
+ QString usePack = eltPack.attribute("use","none");\r
+ out << "use " << nameLib << "." << namePack << "." << usePack << ";" << endl;\r
+ }\r
+ out << endl;\r
+ }\r
+}\r
+\r
+\r
+void FunctionalBlock::generateEntityOrComponentBody(QTextStream& out, int indentLevel, bool hasController) throw(Exception) {\r
+\r
+ int i=0;\r
+ QString indent = "";\r
+ for(i=0;i<indentLevel;i++) {\r
+ indent += " ";\r
+ }\r
+ \r
+ //QList<BlockParameter*> listParams = reference->getParameters();\r
+ QList<AbstractInterface*> listInputs = getInputs();\r
+ QList<AbstractInterface*> listOutputs = getOutputs();\r
+ QList<AbstractInterface*> listBidirs = getBidirs(); \r
+\r
+ // Generation of the generics\r
+ QList<BlockParameter*> listGenerics = getGenericParameters();\r
+ if ((!listGenerics.isEmpty()) || (hasController)) {\r
+ out << indent << " generic (" << endl;\r
+ if (hasController) {\r
+ out << indent << " wb_data_width : integer = 16;" << endl;\r
+ out << indent << " wb_addr_width : integer = 12";\r
+ if (!listGenerics.isEmpty()) out << indent << ";";\r
+ out << endl;\r
+ }\r
+ for(i=0;i<listGenerics.size()-1;i++) {\r
+ out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;\r
+ }\r
+ out << indent << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;\r
+\r
+ out << indent << " );" << endl;\r
+ }\r
+\r
+ out << indent << " port (" << endl;\r
+\r
+ QString ports = "";\r
+ QTextStream outPorts(&ports);\r
+\r
+ // Generation of the clk & rst signals\r
+ outPorts << indent << " -- clk/rst" << endl;\r
+ foreach(AbstractInterface* iface, listInputs) {\r
+ if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {\r
+ outPorts << indent << " " << iface->getName() << " : in std_logic;" << endl;\r
+ }\r
+ }\r
+ foreach(AbstractInterface* iface, listOutputs) {\r
+ if(iface->getPurpose() == AbstractInterface::Clock || iface->getPurpose() == AbstractInterface::Reset) {\r
+ outPorts << indent << " " << iface->getName() << " : out std_logic;" << endl;\r
+ }\r
+ }\r
+\r
+ if (hasController) {\r
+ // Generation of the wishbone signals\r
+ outPorts << indent << " -- registers r/w via wishbone" << endl;\r
+ QList<BlockParameter*> listWB = reference->getWishboneParameters();\r
+ for(i=0;i<listWB.size()-1;i++) {\r
+ outPorts << indent << " " << listWB.at(i)->toVHDL(BlockParameter::Entity, 0) << endl;\r
+ }\r
+ outPorts << indent << " " << listWB.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma) << endl;\r
+ }\r
+\r
+ // Generation of the data/control signals\r
+\r
+ QList<AbstractInterface*> listIface = getInterfaces(AbstractInterface::Input, AbstractInterface::Data);\r
+ if (listIface.size()>0) {\r
+ outPorts << indent << " -- input data ports" << endl;\r
+ foreach(AbstractInterface* iface, listIface) {\r
+ outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;\r
+ }\r
+ }\r
+ listIface = getInterfaces(AbstractInterface::Input, AbstractInterface::Control);\r
+ if (listIface.size()>0) {\r
+ outPorts << indent << " -- input control ports" << endl;\r
+ foreach(AbstractInterface* iface, listIface) {\r
+ outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;\r
+ }\r
+ }\r
+ listIface = getInterfaces(AbstractInterface::Output, AbstractInterface::Data);\r
+ if (listIface.size()>0) {\r
+ outPorts << indent << " -- output data ports" << endl;\r
+ foreach(AbstractInterface* iface, listIface) {\r
+ outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;\r
+ }\r
+ }\r
+ listIface = getInterfaces(AbstractInterface::Output, AbstractInterface::Control);\r
+ if (listIface.size()>0) {\r
+ outPorts << indent << " -- output control ports" << endl;\r
+ foreach(AbstractInterface* iface, listIface) {\r
+ outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;\r
+ }\r
+ }\r
+ listIface = getInterfaces(AbstractInterface::InOut, AbstractInterface::Data);\r
+ if (listIface.size()>0) {\r
+ outPorts << indent << " -- bidirs data ports" << endl;\r
+ foreach(AbstractInterface* iface, listIface) {\r
+ outPorts << indent << " " << iface->toVHDL(AbstractInterface::Entity, 0) << endl;\r
+ }\r
+ }\r
+\r
+ ports.chop(2);\r
+ ports += "\n";\r
+ out << ports;\r
+ out << indent << " );" << endl << endl;\r
+\r
+}\r
+\r
+void FunctionalBlock::generateArchitecture(QTextStream& out, QDomElement &elt ) throw(Exception) {\r
+ QRegularExpression rxPort("@\\{([a-zA-Z0-9_]+)\\}");\r
+ QString expr;\r
+ QString code = elt.text();\r
+ //cout << qPrintable(code) << endl;\r
+ out << "architecture rtl of " << name << " is" << endl;\r
+\r
+ QStringList listLine = code.split("\n");\r
+ for(int i =0; i < listLine.size(); i++) {\r
+ QString line = listLine.at(i).simplified();\r
+\r
+ /*\r
+ if(listLine.at(i).contains(QRegularExpression("@foreach{"))) {\r
+ while(listLine.at(i).compare("@endforeach") != -1) {\r
+ expr = expr + listLine.at(i) + '\n';\r
+ i++;\r
+ }\r
+ expr = expr + listLine.at(i);\r
+ out << evalComplex(expr, 1) << '\n';\r
+ }\r
+ if(listLine.at(i).contains(QRegularExpression("@caseeach{"))) {\r
+ while(listLine.at(i).compare("@endcaseeach") != -1) {\r
+ expr = expr + listLine.at(i) + '\n';\r
+ i++;\r
+ }\r
+ expr = expr + listLine.at(i);\r
+ out << evalComplex(expr, 2) << '\n';\r
+ }\r
+*/\r
+ if(line.contains("@{")) {\r
+ QMap<QString,QString> modifs;\r
+ //cout << qPrintable(line) << endl;\r
+ QRegularExpressionMatchIterator matchPort = rxPort.globalMatch(line);\r
+ while(matchPort.hasNext()) {\r
+ QRegularExpressionMatch m = matchPort.next();\r
+ QString refName = m.captured(1);\r
+ AbstractInterface* refIface = reference->getIfaceFromName(refName);\r
+ QString funName = getIfaceUserName(refIface);\r
+ if (!funName.isEmpty()) {\r
+ modifs.insert(m.captured(0),funName);\r
+ //cout << "replace " << qPrintable(refIface->getName()) << " by " << qPrintable(funIface->getName()) << endl;\r
+ }\r
+ }\r
+ QMapIterator<QString,QString> iterM(modifs);\r
+ while(iterM.hasNext()) {\r
+ iterM.next();\r
+ QString oldName = iterM.key();\r
+ QString newName = iterM.value();\r
+ line.replace(oldName,newName);\r
+ }\r
+ }\r
+ out << line << endl;\r
+ }\r
+\r
+ out << "end rtl;" << endl;\r
+}\r
+\r
+void FunctionalBlock::generateController(QTextStream &out) throw(Exception) {\r
+ \r
+}\r
+\r
+QString FunctionalBlock::getIfaceUserName(AbstractInterface* refIface) {\r
+\r
+ if (! refIface->isReferenceInterface()) return "";\r
+\r
+ AbstractInterface* funcIface = NULL;\r
+\r
+ if (refIface->getDirection() == AbstractInterface::Input) {\r
+ foreach(AbstractInterface* iface, getInputs()) {\r
+ FunctionalInterface* fi = AI_TO_FUN(iface);\r
+ if (fi->getReference() == refIface) {\r
+ funcIface = iface;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ else if (refIface->getDirection() == AbstractInterface::Output) {\r
+ foreach(AbstractInterface* iface, getOutputs()) {\r
+ FunctionalInterface* fi = AI_TO_FUN(iface);\r
+ if (fi->getReference() == refIface) {\r
+ funcIface = iface;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ else if (refIface->getDirection() == AbstractInterface::InOut) {\r
+ foreach(AbstractInterface* iface, getBidirs()) {\r
+ FunctionalInterface* fi = AI_TO_FUN(iface);\r
+ if (fi->getReference() == refIface) {\r
+ funcIface = iface;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ if (funcIface == NULL) return "";\r
+\r
+ return funcIface->getName();\r
+}\r
+\r