1 #include "ReferenceBlock.h"
3 #include "ReferenceInterface.h"
4 #include "BlockParameter.h"
5 #include "BlockParameterUser.h"
6 #include "BlockParameterGeneric.h"
7 #include "BlockParameterPort.h"
8 #include "BlockParameterWishbone.h"
10 ReferenceBlock::ReferenceBlock(const QString _xmlFile) : AbstractBlock() {
14 void ReferenceBlock::addCategory(int id) {
15 categories.append(id);
18 void ReferenceBlock::setBriefDescription(const QString& str) {
20 descriptionBrief = str;
23 void ReferenceBlock::setDetailedDescription(const QString& str) {
25 descriptionDetail = str;
28 void ReferenceBlock::addImplementation(BlockImplementation *impl) {
29 implementations.append(impl);
32 void ReferenceBlock::setHashMd5() {
34 if (file.open(QIODevice::ReadOnly)) {
35 QByteArray fileData = file.readAll();
36 QByteArray hashData = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
37 hashMd5 = QString(hashData.toHex());
38 cout << qPrintable(xmlFile) << " has md5 hash : " << qPrintable(hashMd5) << endl;
45 void ReferenceBlock::load(QDomElement &elt) throw(Exception) {
48 cout << "Block : get informations" << endl;
49 QDomElement eltInfo = elt.firstChildElement("informations");
51 loadInformations(eltInfo);
57 cout << "Block : get params" << endl;
58 QDomElement eltParams = eltInfo.nextSiblingElement("parameters");
60 loadParameters(eltParams);
66 cout << "Block : get interfaces" << endl;
67 QDomElement eltInter = eltParams.nextSiblingElement("interfaces");
69 loadInterfaces(eltInter);
75 // create interfaces that correspond to a wishbone parameter, if any.
77 createInterfaceForParameters();
84 void ReferenceBlock::loadInformations(QDomElement &elt) throw(Exception) {
87 if ((elt.isNull()) || (elt.tagName() != "informations")) throw (Exception(BLOCKFILE_CORRUPTED));
89 cout << "Block info : get name" << endl;
90 QDomNode nodeName = elt.firstChild();
91 QDomNode nodeNameTxt = nodeName.firstChild();
92 if (nodeNameTxt.isNull()) {
96 QDomText txtName = nodeNameTxt.toText();
97 name = txtName.data().trimmed();
98 cout<< "block name : " << qPrintable(name) << endl;
101 // getting categories
102 cout << "Block info : get categories" << endl;
103 QDomElement eltCat = nodeName.nextSiblingElement("category");
105 QString idsStr = eltCat.attribute("ids","none");
106 if (idsStr == "none") throw (Exception(BLOCKFILE_CORRUPTED));
107 QStringList listCat = idsStr.split(",");
108 foreach(QString str, listCat)
110 int idCat = str.toInt(&ok);
111 categories.append(idCat);
114 // getting description
115 cout << "Block info : get description" << endl;
116 QDomElement eltDesc = eltCat.nextSiblingElement("description");
118 QDomElement eltBrief = eltDesc.firstChildElement("brief");
119 QDomNode nodeBriefTxt = eltBrief.firstChild();
120 if (nodeBriefTxt.isNull()) {
121 descriptionBrief = "no brief description";
124 QDomText txtBrief = nodeBriefTxt.toText();
125 descriptionBrief = txtBrief.data().trimmed();
126 cout << "block brief desc : " << qPrintable(descriptionBrief) << endl;
129 QDomElement eltDetail = eltBrief.nextSiblingElement("detailed");
130 QDomNode nodeDetailTxt = eltDetail.firstChild();
131 if (nodeDetailTxt.isNull()) {
132 descriptionDetail = "no detailed description";
135 QDomText txtDetail = nodeDetailTxt.toText();
136 descriptionDetail = txtDetail.data().trimmed();
137 cout << "block detail desc : " << qPrintable(descriptionDetail) << endl;
141 void ReferenceBlock::loadParameters(QDomElement &elt) throw(Exception) {
143 if ((elt.isNull()) || (elt.tagName() != "parameters")) throw (Exception(BLOCKFILE_CORRUPTED));
145 QDomNodeList listNodeParam = elt.elementsByTagName("parameter");
146 for(int i=0; i<listNodeParam.size(); i++) {
147 QDomNode node = listNodeParam.at(i);
148 QDomElement elt = node.toElement();
149 QString nameStr = elt.attribute("name","none");
150 QString contextStr = elt.attribute("context","none");
151 QString typeStr = elt.attribute("type","none");
152 QString valueStr = elt.attribute("value","none");
153 BlockParameter *param = NULL;
155 if(valueStr == "none"){
156 if (contextStr == "generic") throw (Exception(BLOCKFILE_CORRUPTED)); // set is required for generics
159 if (contextStr == "user") {
160 param = new BlockParameterUser(this,nameStr,typeStr,valueStr);
162 else if (contextStr == "generic") {
163 param = new BlockParameterGeneric(this,nameStr,typeStr,valueStr);
165 else if (contextStr == "wb") {
166 QString widthStr = elt.attribute("width","none");
167 QString wbStr = elt.attribute("wishbone","none");
170 QString wbValue = "";
171 QStringList listWb = wbStr.split(",");
172 cout << "wb param has:";
173 foreach(QString s, listWb) {
174 cout << qPrintable(s) << " | ";
178 if (listWb.at(0) == "r") {
179 access = BlockParameter::Read;
181 else if (listWb.at(0) == "w") {
182 access = BlockParameter::Write;
184 wbValue = listWb.at(1).toInt(&ok);
186 if(listWb.at(1) == "true" || listWb.at(1) == "false"){
187 wbValue = listWb.at(1);
192 if(listWb.at(2) == "trigger") {
193 duration = BlockParameter::Trigger;
195 else if(listWb.at(2) == "perm") {
196 duration = BlockParameter::Permanent;
199 param = new BlockParameterWishbone(this,nameStr,typeStr,widthStr,valueStr,access,wbValue,duration);
201 else if (contextStr == "port") {
202 QString ifaceStr = elt.attribute("iface","none");
203 param = new BlockParameterPort(this,nameStr,valueStr,ifaceStr);
207 throw (Exception(BLOCKFILE_CORRUPTED));
209 params.append(param);
213 void ReferenceBlock::loadInterfaces(QDomElement &elt) throw(Exception) {
222 AbstractInterface* inter;
224 if ((elt.isNull()) || (elt.tagName() != "interfaces")) throw (Exception(BLOCKFILE_CORRUPTED));
226 QDomElement eltInputs = elt.firstChildElement("inputs");
227 // getting each input
228 QDomNodeList listNodeInputs = eltInputs.elementsByTagName("input");
229 for(int i=0;i<listNodeInputs.size();i++) {
230 QDomNode node = listNodeInputs.at(i);
231 QDomElement eltInput = node.toElement();
232 nameStr = eltInput.attribute("name","none");
233 typeStr = eltInput.attribute("type","none");
234 widthStr = eltInput.attribute("width","none");
235 purposeStr = eltInput.attribute("purpose","none");
236 cout << "block : " << this->getName().toStdString() << endl;
237 cout << "purpose for " << nameStr.toStdString() << " : " << purposeStr.toStdString() << endl;
238 purpose = ReferenceInterface::translatePurpose(purposeStr);
239 cout << "translated purpose : " << purpose << endl;
240 multStr = eltInput.attribute("multiplicity","none");
241 mult = ReferenceInterface::translateMultiplicity(multStr);
243 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Input, purpose, mult);
244 inputs.append(inter);
246 // getting each control
247 QDomNodeList listNodeInCtl = eltInputs.elementsByTagName("control");
248 for(int i=0;i<listNodeInCtl.size();i++) {
249 QDomNode node = listNodeInCtl.at(i);
250 QDomElement eltInput = node.toElement();
251 nameStr = eltInput.attribute("iface","none");
252 AbstractInterface* dataIface = getIfaceFromName(nameStr);
253 if (dataIface == NULL) throw (Exception(BLOCKFILE_CORRUPTED));
254 nameStr = dataIface->getName()+"_enb";
255 inter = new ReferenceInterface(this,nameStr,"boolean","1",AbstractInterface::Input, AbstractInterface::Control, 1);
256 if (!inter->setAssociatedIface(dataIface)) {
257 throw (Exception(BLOCKFILE_CORRUPTED));
259 cout << "created a control input named " << qPrintable(inter->getName()) << endl;
260 inputs.append(inter);
262 QDomElement eltOutputs = eltInputs.nextSiblingElement("outputs");
263 QDomNodeList listNodeOutputs = eltOutputs.elementsByTagName("output");
264 for(int i=0;i<listNodeOutputs.size();i++) {
265 QDomNode node = listNodeOutputs.at(i);
266 QDomElement eltOutput = node.toElement();
267 nameStr = eltOutput.attribute("name","none");
268 typeStr = eltOutput.attribute("type","none");
269 widthStr = eltOutput.attribute("width","none");
270 purposeStr = eltOutput.attribute("purpose","none");
271 purpose = ReferenceInterface::translatePurpose(purposeStr);
272 multStr = eltOutput.attribute("multiplicity","none");
273 mult = ReferenceInterface::translateMultiplicity(multStr);
275 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Output, purpose, mult);
276 outputs.append(inter);
278 // getting each control
279 QDomNodeList listNodeOutCtl = eltOutputs.elementsByTagName("control");
280 for(int i=0;i<listNodeOutCtl.size();i++) {
281 QDomNode node = listNodeOutCtl.at(i);
282 QDomElement eltOutput = node.toElement();
283 nameStr = eltOutput.attribute("iface","none");
284 AbstractInterface* dataIface = getIfaceFromName(nameStr);
285 if (dataIface == NULL) throw (Exception(BLOCKFILE_CORRUPTED));
286 nameStr = dataIface->getName()+"_enb";
287 inter = new ReferenceInterface(this,nameStr,"boolean","1",AbstractInterface::Output, AbstractInterface::Control, 1);
288 if (!inter->setAssociatedIface(dataIface)) {
289 throw (Exception(BLOCKFILE_CORRUPTED));
291 cout << "created a control output named " << qPrintable(inter->getName()) << endl;
292 outputs.append(inter);
295 QDomElement eltBidirs = eltInputs.nextSiblingElement("bidirs");
296 QDomNodeList listNodeBidirs = eltBidirs.elementsByTagName("bidir");
297 for(int i=0;i<listNodeBidirs.size();i++) {
298 QDomNode node = listNodeBidirs.at(i);
299 QDomElement eltBidir = node.toElement();
300 nameStr = eltBidir.attribute("name","none");
301 typeStr = eltBidir.attribute("type","none");
302 widthStr = eltBidir.attribute("width","none");
303 purposeStr = eltBidir.attribute("purpose","none");
304 purpose = ReferenceInterface::translatePurpose(purposeStr);
305 multStr = eltBidir.attribute("multiplicity","none");
306 mult = ReferenceInterface::translateMultiplicity(multStr);
308 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::InOut, purpose, mult);
309 bidirs.append(inter);
313 void ReferenceBlock::createInterfaceForParameters() throw(Exception){
314 ReferenceInterface* iface = NULL;
315 foreach(BlockParameter* param, params) {
317 if (param->isWishboneParameter()) {
319 BlockParameterWishbone* p = (BlockParameterWishbone*)param;
320 cout << "creating interface for parameter wb " << qPrintable(p->getName()) << endl;
322 if (p->getWBAccess() == BlockParameter::Read) {
323 iface = new ReferenceInterface(this,p->getName(),p->getTypeString(),p->getWidth(), AbstractInterface::Output, AbstractInterface::Wishbone,1);
324 outputs.append(iface);
326 else if (p->getWBAccess() == BlockParameter::Write) {
327 iface = new ReferenceInterface(this,p->getName(),p->getTypeString(),p->getWidth(), AbstractInterface::Input, AbstractInterface::Wishbone,1);
328 inputs.append(iface);
331 throw (Exception(BLOCKFILE_CORRUPTED));
337 void ReferenceBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
342 only used to save all ReferenceBlock in a library in binary format, so that reference blocks
343 are read very fast at application startup.
346 QDataStream& operator<<(QDataStream &out, const ReferenceBlock &b) {
348 out.setVersion(QDataStream::Qt_5_0);
350 QByteArray blockData;
351 QDataStream toWrite(&blockData, QIODevice::WriteOnly);
354 toWrite << b.xmlFile;
355 toWrite << b.descriptionBrief;
356 toWrite << b.descriptionDetail;
357 toWrite << b.categories;
358 toWrite << b.hashMd5;
359 toWrite << b.params.size();
361 for(int i=0;i<b.params.size();i++) {
363 toWrite << p->getContext();
364 toWrite << p->getName();
365 toWrite << p->getTypeString();
366 toWrite << p->getValue().toString();
367 if (p->isPortParameter()) {
368 toWrite << ((BlockParameterPort*)p)->getIfaceName();
370 else if (p->isWishboneParameter()) {
371 BlockParameterWishbone* pwb = (BlockParameterWishbone*)p;
372 toWrite << pwb->getWidth();
373 toWrite << pwb->getWBAccess();
374 toWrite << pwb->getWBValue();
375 toWrite << pwb->getWBDuration();
380 toWrite << b.inputs.size();
381 // firstly write control ifaces
382 for(int i=0; i<b.inputs.size(); i++){
383 ReferenceInterface *iface = (ReferenceInterface *)(b.inputs.at(i));
384 if (iface->getPurpose() == AbstractInterface::Control) {
385 toWrite << iface->getName();
386 toWrite << iface->getWidth();
387 toWrite << iface->getPurpose();
388 toWrite << iface->getDirection();
389 toWrite << iface->getMultiplicity();
392 // secondly, write other ifaces
393 for(int i=0; i<b.inputs.size(); i++){
394 ReferenceInterface *iface = (ReferenceInterface *)(b.inputs.at(i));
395 if (iface->getPurpose() != AbstractInterface::Control) {
396 toWrite << iface->getName();
397 toWrite << iface->getWidth();
398 toWrite << iface->getPurpose();
399 toWrite << iface->getDirection();
400 toWrite << iface->getMultiplicity();
403 toWrite << b.outputs.size();
404 // firstly write control ifaces
405 for(int i=0; i<b.outputs.size(); i++){
406 ReferenceInterface *iface = (ReferenceInterface *)(b.outputs.at(i));
407 if (iface->getPurpose() == AbstractInterface::Control) {
408 toWrite << iface->getName();
409 toWrite << iface->getWidth();
410 toWrite << iface->getPurpose();
411 toWrite << iface->getDirection();
412 toWrite << iface->getMultiplicity();
415 // secondly, write other ifaces
416 for(int i=0; i<b.outputs.size(); i++){
417 ReferenceInterface *iface = (ReferenceInterface *)(b.outputs.at(i));
418 if (iface->getPurpose() != AbstractInterface::Control) {
419 toWrite << iface->getName();
420 toWrite << iface->getWidth();
421 toWrite << iface->getPurpose();
422 toWrite << iface->getDirection();
423 toWrite << iface->getMultiplicity();
426 toWrite << b.bidirs.size();
427 for(int i=0; i<b.bidirs.size(); i++){
428 ReferenceInterface *iface = (ReferenceInterface *)(b.bidirs.at(i));
429 toWrite << iface->getName();
430 toWrite << iface->getWidth();
431 toWrite << iface->getPurpose();
432 toWrite << iface->getDirection();
433 toWrite << iface->getMultiplicity();
441 QDataStream& operator>>(QDataStream &in, ReferenceBlock &b) {
444 ReferenceInterface* iface;
449 in.setVersion(QDataStream::Qt_5_0);
455 in >> b.descriptionBrief;
456 in >> b.descriptionDetail;
462 cout << qPrintable(b.name) << " has " << nb << " parameters" << endl;
463 for(int i=0;i<nb;i++) {
464 QString contextStr = "";
466 QString typeStr = "";
467 QString valueStr = "";
473 if (contextStr == "user") {
474 p = new BlockParameterUser(&b,nameStr,typeStr,valueStr);
476 else if (contextStr == "generic") {
477 p = new BlockParameterGeneric(&b,nameStr,typeStr,valueStr);
479 else if (contextStr == "port") {
480 QString ifaceStr = "";
482 p = new BlockParameterPort(&b,nameStr,valueStr,ifaceStr);
484 else if (contextStr == "wb") {
485 QString widthStr = "";
493 p = new BlockParameterWishbone(&b,nameStr,typeStr,widthStr,valueStr,wbAccess,wbValue,wbDuration);
500 for(int i=0;i<nb;i++) {
501 iface = new ReferenceInterface(&b);
505 iface->setWidth(txt);
507 iface->setPurpose(val);
509 iface->setDirection(val);
511 iface->setMultiplicity(val);
512 b.inputs.append(iface);
513 if (iface->getPurpose() == AbstractInterface::Data) {
514 QString ctlRefName = iface->getName()+"_enb";
515 ReferenceInterface* ctlRefIface = AI_TO_REF(b.getIfaceFromName(ctlRefName));
516 if (ctlRefIface != NULL) {
517 if (! ctlRefIface->setAssociatedIface(iface)) {
518 cerr << "Abnormal case while reading a reference block in library" << endl;
526 for(int i=0;i<nb;i++) {
527 iface = new ReferenceInterface(&b);
531 iface->setWidth(txt);
533 iface->setPurpose(val);
535 iface->setDirection(val);
537 iface->setMultiplicity(val);
538 b.outputs.append(iface);
539 if (iface->getPurpose() == AbstractInterface::Data) {
540 QString ctlRefName = iface->getName()+"_enb";
541 ReferenceInterface* ctlRefIface = AI_TO_REF(b.getIfaceFromName(ctlRefName));
542 if (ctlRefIface != NULL) {
543 if (! ctlRefIface->setAssociatedIface(iface)) {
544 cerr << "Abnormal case while reading a reference block in library" << endl;
552 for(int i=0;i<nb;i++) {
553 iface = new ReferenceInterface(&b);
557 iface->setWidth(txt);
559 iface->setPurpose(val);
561 iface->setDirection(val);
563 iface->setMultiplicity(val);
564 b.bidirs.append(iface);
570 bool ReferenceBlock::computeOutputPattern(int nbExec) {
571 // does strictly nothing