1 #include "BlockImplementation.h"
\r
3 #include "FunctionalBlock.h"
\r
4 #include "ReferenceBlock.h"
\r
5 #include "ReferenceInterface.h"
\r
6 #include "FunctionalInterface.h"
\r
7 #include "BlockParameter.h"
\r
10 BlockImplementation::BlockImplementation(const QString& _xmlFile) {
\r
11 xmlFile = _xmlFile;
\r
13 evaluator = new ArithmeticEvaluator;
\r
14 evaluator->setVariableMarkers("@$");
\r
17 BlockImplementation::BlockImplementation(const QString& _xmlFile, const QString &_referenceXml, const QString &_referenceMd5) {
\r
18 xmlFile = _xmlFile;
\r
19 referenceXml = _referenceXml;
\r
20 referenceMd5 = _referenceMd5;
\r
23 void BlockImplementation::generateVHDL(FunctionalBlock* _block, const QString &path) throw(Exception) {
\r
27 QFile implFile(xmlFile);
\r
29 // reading in into QDomDocument
\r
30 QDomDocument document("implFile");
\r
32 if (!implFile.open(QIODevice::ReadOnly)) {
\r
33 throw(Exception(IMPLFILE_NOACCESS));
\r
35 if (!document.setContent(&implFile)) {
\r
37 throw(Exception(IMPLFILE_NOACCESS));
\r
41 bool genController = false;
\r
42 QString coreFile = "";
\r
43 QString controllerFile = "";
\r
45 if (reference->isWBConfigurable()) {
\r
46 genController = true;
\r
47 controllerFile = path;
\r
48 controllerFile.append(block->getName());
\r
49 controllerFile.append("_ctrl.vhd");
\r
52 controllerFile = "nofile.vhd";
\r
55 coreFile.append(block->getName());
\r
56 coreFile.append(".vhd");
\r
58 QFile vhdlCore(coreFile);
\r
59 QFile vhdlController(controllerFile);
\r
61 if (!vhdlCore.open(QIODevice::WriteOnly)) {
\r
62 throw(Exception(VHDLFILE_NOACCESS));
\r
65 if (genController) {
\r
66 if (!vhdlController.open(QIODevice::WriteOnly)) {
\r
67 throw(Exception(VHDLFILE_NOACCESS));
\r
70 QTextStream outCore(&vhdlCore);
\r
71 QTextStream outController;
\r
72 if (genController) {
\r
73 outController.setDevice(&vhdlController);
\r
79 //Get the root element
\r
80 QDomElement impl = document.documentElement();
\r
81 QDomElement eltComments = impl.firstChildElement("comments");
\r
82 generateComments(eltComments, coreFile, outCore);
\r
83 QDomElement eltLibs = eltComments.nextSiblingElement("libraries");
\r
84 generateLibraries(eltLibs, outCore);
\r
85 generateEntity(outCore, genController);
\r
86 QDomElement eltArch = eltLibs.nextSiblingElement("architecture");
\r
87 generateArchitecture(eltArch, outCore);
\r
88 if (genController) {
\r
89 generateController(outController);
\r
92 catch(Exception err) {
\r
97 vhdlController.close();
\r
100 // This function generates the comments part of the VHDL document
\r
101 void BlockImplementation::generateComments(QDomElement &elt, QString coreFile, QTextStream& out) throw(Exception) {
\r
103 for(int i = 0; i < 50; i++) {
\r
107 QString fileName = coreFile;
\r
108 out << "-- File : " << fileName << "\n";
\r
110 QDomElement eltAuthor = elt.firstChildElement("author");
\r
111 QString firstName = eltAuthor.attribute("firstname","");
\r
112 QString lastName = eltAuthor.attribute("lastname","");
\r
113 QString mail = eltAuthor.attribute("mail","");
\r
114 out << "-- Author(s) : "<<firstName+" "<<lastName<<" ("<<mail<<")\n";
\r
116 QDomElement eltDate = eltAuthor.nextSiblingElement("date");
\r
117 QString crea = eltDate.attribute("creation","");
\r
118 out << "-- Creation Date : "<<crea<<"\n";
\r
120 QDomElement eltRelated = eltDate.nextSiblingElement("related_files");
\r
121 QString relateds = eltRelated.attribute("list","");
\r
122 out << "-- Related files :\n"<<relateds<<"\n";
\r
124 QDomElement eltDesc = eltRelated.nextSiblingElement("description");
\r
125 QDomElement desc = eltDesc.firstChildElement();
\r
126 QString descTxt = desc.text();
\r
127 out << "-- Decription :\n"<<descTxt<<"\n";
\r
129 QDomElement eltNote = eltDesc.nextSiblingElement("description");
\r
130 QDomElement note = eltNote.firstChildElement();
\r
131 QString noteTxt = note.text();
\r
132 out << "-- Note :\n"<<noteTxt<<"\n";
\r
134 for(int i = 0; i < 50; i++) {
\r
140 // This function generates the library part of the VHDL document
\r
141 void BlockImplementation::generateLibraries(QDomElement &elt, QTextStream& out) throw(Exception) {
\r
143 QDomNodeList listLib = elt.elementsByTagName("library");
\r
144 for(int i = 0; i < listLib.length(); i++) {
\r
145 QDomNode nodeLib = listLib.item(i);
\r
146 QDomElement eltLib = nodeLib.toElement();
\r
147 QString nameLib = eltLib.attribute("name", "");
\r
148 out << "library " << nameLib << ";\n";
\r
149 QDomNodeList listPack = eltLib.elementsByTagName("package");
\r
150 for(int j = 0; j < listPack.length(); j++) {
\r
151 QDomNode nodePack = listPack.item(j);
\r
152 QDomElement eltPack = nodePack.toElement();
\r
153 QString namePack = eltPack.attribute("name", "");
\r
154 QString usePack = elt.attribute("use","");
\r
155 out << "use " << nameLib << "." << namePack << "." << usePack << ";\n";
\r
161 // This function generates the entity part of the VHDL document
\r
162 void BlockImplementation::generateEntity(QTextStream& out, bool hasController) throw(Exception) {
\r
165 nameEnt = reference->getName();
\r
166 //QList<BlockParameter*> listParams = reference->getParameters();
\r
167 QList<AbstractInterface*> listInputs = reference->getInputs();
\r
168 QList<AbstractInterface*> listOutputs = reference->getOutputs();
\r
169 QList<AbstractInterface*> listBidirs = reference->getBidirs();
\r
170 QString typePort, namePort;
\r
172 out << "entity " << nameEnt << " is\n";
\r
175 /* TODO : rewrite the generation to take into acocunt the new object hierarchy */
\r
177 // Generation of the generics
\r
178 QList<BlockParameter*> listGenerics = reference->getGenericParameters();
\r
179 if ((!listGenerics.isEmpty()) || (hasController)) {
\r
180 out << " generic (" << endl;
\r
181 if (hasController) {
\r
182 out << " wb_data_width : integer = 16;" << endl;
\r
183 out << " wb_addr_width : integer = 12";
\r
184 if (!listGenerics.isEmpty()) out << ";";
\r
187 for(i=0;i<listGenerics.size()-1;i++) {
\r
188 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity, 0);
\r
190 out << " " << listGenerics.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma);
\r
192 out << " );" << endl;
\r
195 out << " port (" << endl;
\r
197 // Generation of the clk & rst signals
\r
198 out << " -- clk/rst" << endl;
\r
199 for(int i = 0; i < listInputs.size(); i++) {
\r
200 if(listInputs.at(i)->getPurpose() == AbstractInterface::Clock || listInputs.at(i)->getPurpose() == AbstractInterface::Reset) {
\r
201 out << " " << listInputs.at(i)->getName() << " : in std_logic;" << endl;
\r
205 if (hasController) {
\r
206 // Generation of the wishbone signals
\r
207 out << " -- registers r/w via wishbone" << endl;
\r
208 QList<BlockParameter*> listWB = reference->getWishboneParameters();
\r
209 for(i=0;i<listWB.size()-1;i++) {
\r
210 out << " " << listWB.at(i)->toVHDL(BlockParameter::Entity, 0);
\r
212 out << " " << listWB.at(i)->toVHDL(BlockParameter::Entity,BlockParameter::NoComma);
\r
216 // Generation of the data signals
\r
217 out << "-- data ports\n";
\r
218 for(int i = 0; i < listInputs.size(); i++) {
\r
219 namePort = getIfaceUserName(reference->AbstractBlock::getIfaceFromName(listInputs.at(i)->getName()));
\r
220 if(listInputs.at(i)->getWidth().compare("1"))
\r
221 typePort = "std_logic";
\r
223 typePort = calculateWidth(listInputs.at(i)->getWidth());
\r
224 if(listInputs.at(i)->getPurpose() == 1)
\r
225 out << namePort << " : in std_logic_vector(" << typePort << " -1 downto 0) ;\n";
\r
228 for(int i = 0; i < listOutputs.size(); i++) {
\r
229 namePort = getIfaceUserName(reference->AbstractBlock::getIfaceFromName(listOutputs.at(i)->getName()));
\r
230 if(listOutputs.at(i)->getWidth().compare("1"))
\r
231 typePort = "std_logic";
\r
233 typePort = calculateWidth(listOutputs.at(i)->getWidth());
\r
234 if(listOutputs.at(i)->getPurpose() == 1)
\r
235 out << namePort << " : out std_logic_vector(" << typePort << " -1 downto 0) ;\n";
\r
238 for(int i = 0; i < listBidirs.size(); i++) {
\r
239 namePort = getIfaceUserName(reference->AbstractBlock::getIfaceFromName(listBidirs.at(i)->getName()));
\r
240 if(listBidirs.at(i)->getWidth().compare(("1")))
\r
241 typePort = "std_logic";
\r
243 typePort = calculateWidth((listBidirs.at(i)->getWidth()));
\r
244 if(listBidirs.at(i)->getPurpose() == 1)
\r
245 out << namePort << " : inout std_logic_vector(" << typePort << " -1 downto 0) ;\n";
\r
249 // This function generates the architecture part of the VHDL document
\r
250 void BlockImplementation::generateArchitecture(QDomElement &elt, QTextStream& out) throw(Exception) {
\r
253 QDomElement eltArch = elt.nextSiblingElement("architecture");
\r
254 out << "architecture " << nameEnt <<"_1 of " << nameEnt << "is\n";
\r
255 QString implText = eltArch.text();
\r
256 QStringList listLine = implText.split("\n");
\r
257 for(int i =0; i < listLine.size(); i++) {
\r
258 if(listLine.at(i).contains(QRegularExpression("@foreach{")) != -1) {
\r
259 while(listLine.at(i).compare("@endforeach") != -1) {
\r
260 expr = expr + listLine.at(i) + '\n';
\r
263 expr = expr + listLine.at(i);
\r
264 out << evalComplex(expr, 1) << '\n';
\r
266 if(listLine.at(i).contains(QRegularExpression("@caseeach{")) != -1) {
\r
267 while(listLine.at(i).compare("@endcaseeach") != -1) {
\r
268 expr = expr + listLine.at(i) + '\n';
\r
271 expr = expr + listLine.at(i);
\r
272 out << evalComplex(expr, 2) << '\n';
\r
275 if(listLine.at(i).contains('@') == -1)
\r
276 out << listLine.at(i) << "\n";
\r
278 out << eval(listLine.at(i), out) << "\n";
\r
282 void BlockImplementation::generateController(QTextStream &out) throw(Exception) {
\r
285 QString BlockImplementation::eval(QString line, QTextStream& out) {
\r
286 QString res, s, begLine, endLine, expr;
\r
287 evaluator->setExpression(line);
\r
288 QRegExp *rxString = new QRegExp("(.*)@{(.*)}(.*)");
\r
289 QRegExp *rxValue = new QRegExp("(.*)@val{(.*)}(.*)");
\r
290 QRegExp *rxExpr = new QRegExp(".*@eval{(.*)}.*");
\r
292 int nbAt = line.count('@');
\r
294 for(int i = 0; i < line.size(); i++) {
\r
295 if(rxString->indexIn(line)) {
\r
296 begLine = rxString->cap(1);
\r
297 s = rxString->cap(2);
\r
298 endLine = rxString->cap(3);
\r
299 res = begLine + evalString(s) + endLine + '\n';
\r
303 for(int i = 0; i < line.size(); i++) {
\r
304 if(rxValue->indexIn(line)) {
\r
305 begLine = rxValue->cap(1);
\r
306 s = rxValue->cap(2);
\r
307 endLine = rxValue->cap(3);
\r
308 res = begLine + evalValue(s) + endLine + '\n';
\r
312 for(int i = 0; i < line.size(); i++) {
\r
313 if(rxExpr->indexIn(line)) {
\r
314 expr = rxExpr->cap(1);
\r
315 if(expr.count('@') == 0) {
\r
316 evaluator->setExpression(expr);
\r
317 s = QString::number(evaluator->evaluate());
\r
319 res = begLine + s + endLine + '\n';
\r
327 QString BlockImplementation::evalComplex(QString line, int id) {
\r
328 QString res, s, begLine, endLine, expr;
\r
329 QRegExp *rxString = new QRegExp("(.*)@{(.*)}(.*)");
\r
330 QRegExp *rxValue = new QRegExp("(.*)@val{(.*)}(.*)");
\r
331 QRegExp *rxExpr = new QRegExp(".*@eval{(.*)}.*");
\r
332 QRegExp *rxFor = new QRegExp("@foreach{.*}(@{.*})(.*)@endforeach");
\r
333 QRegExp *rxCase = new QRegExp("@caseeach{.*,(.*),(.*)}(@{.*})(.*)@endcaseeach");
\r
334 QRegExp *rxCaseDown = new QRegExp("@#-:(.*)");
\r
335 QRegExp *rxCaseUp = new QRegExp("@#:(.*)");
\r
336 evaluator->setExpression(line);
\r
338 int nbAt = line.count('@') - 2;
\r
340 for(int i = 0; i < line.size(); i++) {
\r
341 if(rxString->indexIn(line)) {
\r
342 begLine = rxString->cap(1);
\r
343 s = rxString->cap(2);
\r
344 endLine = rxString->cap(3);
\r
345 if(evalStringComplex(s)->size() == 0)
\r
346 line = begLine + evalString(s) + endLine;
\r
350 for(int i = 0; i < line.size(); i++) {
\r
351 if(rxValue->indexIn(line)) {
\r
352 begLine = rxValue->cap(1);
\r
353 s = rxValue->cap(2);
\r
354 endLine = rxValue->cap(3);
\r
355 line = begLine + evalValue(s) + endLine;
\r
359 for(int i = 0; i < line.size(); i++) {
\r
360 if(rxExpr->indexIn(line)) {
\r
361 expr = rxExpr->cap(1);
\r
362 if(expr.count('@') == 0) {
\r
363 evaluator->setExpression(expr);
\r
364 s = QString::number(evaluator->evaluate());
\r
366 res = begLine + s + endLine + '\n';
\r
373 if(rxFor->indexIn(line)) {
\r
374 QString intName, instruc;
\r
375 intName = rxFor->cap(1);
\r
376 instruc = rxFor->cap(2);
\r
377 QList<AbstractInterface*> *intList = evalStringComplex(intName);
\r
378 if(intList->size() != 0) {
\r
379 for(int i = 0; i < intList->size(); i++) {
\r
380 res = intList->at(i)->getName() + instruc + '\n';
\r
387 if(rxCase->indexIn(line)) {
\r
388 QString intName, sigName, cases, instruc;
\r
390 sigName = rxCase->cap(1);
\r
391 cases = rxCase->cap(2);
\r
392 intName = rxCase->cap(3);
\r
393 instruc = rxCase->cap(4);
\r
394 QList<AbstractInterface*> *intList = evalStringComplex(intName);
\r
395 int listSize = intList->count();
\r
396 res = "case " + sigName + " is\n";
\r
397 if(rxCaseUp->indexIn(cases)) {
\r
398 number = rxCaseUp->cap(1).toInt();
\r
399 for(int j = number; j < listSize; j++) {
\r
401 for(int i = 0; i < listSize; i++) {
\r
402 res += "\twhen " + QString::number(number) + " " + intList->at(i)->getName() + instruc + "\n";
\r
406 res += "\twhen " + number + ' ' + intName + instruc + "\n";
\r
410 if(rxCaseDown->indexIn(cases)) {
\r
411 number = rxCaseDown->cap(1).toInt();
\r
412 for(int j = number; j < listSize; j++) {
\r
414 for(int i = 0; i < listSize; i++) {
\r
415 res += "\twhen " + QString::number(number) + " " + intList->at(i)->getName() + instruc + "\n";
\r
419 res += "\twhen " + number + ' ' + intName + instruc + "\n";
\r
422 res += "end case ;\n";
\r
429 QString BlockImplementation::evalString(QString s) {
\r
431 QString name = getIfaceUserName(block->AbstractBlock::getIfaceFromName(s));
\r
435 QList<AbstractInterface*>* BlockImplementation::evalStringComplex(QString s) {
\r
438 QList<AbstractInterface*> *listInterfaces = new QList<AbstractInterface*>();
\r
439 AbstractInterface *inter = block->AbstractBlock::getIfaceFromName(s);
\r
440 QList<AbstractInterface*> listIntBlock = block->getInterfaces();
\r
441 for(int i = 0; i < listIntBlock.size(); i++) {
\r
442 if(inter->getName().compare(listIntBlock.at(i)->getName()) < -1) {
\r
443 listInterfaces->insert(j, inter);
\r
447 return listInterfaces;
\r
450 QString BlockImplementation::evalValue(QString s) {
\r
453 if(paramMap.contains(s))
\r
454 val = paramMap.value(s);
\r
458 QString BlockImplementation::getIfaceUserName(AbstractInterface* refIface) {
\r
460 if (! refIface->isReferenceInterface()) return "";
\r
462 AbstractInterface* funcIface = NULL;
\r
464 if (refIface->getDirection() == AbstractInterface::Input) {
\r
465 foreach(AbstractInterface* iface, block->getInputs()) {
\r
466 FunctionalInterface* fi = (FunctionalInterface*)iface;
\r
467 if (fi->getReference() == refIface) {
\r
473 else if (refIface->getDirection() == AbstractInterface::Output) {
\r
474 foreach(AbstractInterface* iface, block->getOutputs()) {
\r
475 FunctionalInterface* fi = (FunctionalInterface*)iface;
\r
476 if (fi->getReference() == refIface) {
\r
482 else if (refIface->getDirection() == AbstractInterface::InOut) {
\r
483 foreach(AbstractInterface* iface, block->getBidirs()) {
\r
484 FunctionalInterface* fi = (FunctionalInterface*)iface;
\r
485 if (fi->getReference() == refIface) {
\r
491 if (funcIface == NULL) return "";
\r
493 return funcIface->getName();
\r
496 QDataStream& operator<<(QDataStream &out, const BlockImplementation &impl) {
\r
498 out.setVersion(QDataStream::Qt_5_0);
\r
500 QByteArray blockData;
\r
501 QDataStream toWrite(&blockData, QIODevice::WriteOnly);
\r
503 toWrite << impl.xmlFile;
\r
504 toWrite << impl.referenceXml;
\r
505 toWrite << impl.referenceMd5;
\r
512 QDataStream& operator>>(QDataStream &in, BlockImplementation &impl) {
\r
516 in.setVersion(QDataStream::Qt_5_0);
\r
520 in >> impl.xmlFile;
\r
521 in >> impl.referenceXml;
\r
522 in >> impl.referenceMd5;
\r
527 QString BlockImplementation::calculateWidth(QString s){
\r
528 QRegExp *rxWidth = new QRegExp("$*([a-zA-Z0-9_-]*)");
\r
529 QStringList matchList = s.split(" ");
\r
532 QList<BlockParameter*> listParams = reference->getParameters();
\r
534 while ((pos = rxWidth->indexIn(s, pos)) != -1) {
\r
535 matchList << rxWidth->cap(1);
\r
536 pos += rxWidth->matchedLength();
\r
539 for (int i = 0; i < matchList.size(); i++) {
\r
540 QString match = matchList.at(i);
\r
541 if(rxWidth->indexIn(match)) {
\r
542 for(int j = 0; j < listParams.size(); j++) {
\r
543 if(match.compare(listParams.at(j)->getName())) {
\r
544 BlockParameter *param = listParams.at(i);
\r
545 if(param->getContext() == "generic") {
\r
546 match = match.remove('$');
\r
549 match = param->getValue().toString();
\r
555 line = matchList.join(' ');
\r
556 evaluator->setExpression(line);
\r
557 res = evaluator->evaluate();
\r