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,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()+"_ctl";
255 inter = new ReferenceInterface(this,nameStr,"boolean","1",AbstractInterface::Input, AbstractInterface::Control, 1);
256 if (!inter->setAssociatedIface(dataIface)) {
257 throw (Exception(BLOCKFILE_CORRUPTED));
259 inputs.append(inter);
261 QDomElement eltOutputs = eltInputs.nextSiblingElement("outputs");
262 QDomNodeList listNodeOutputs = eltOutputs.elementsByTagName("output");
263 for(int i=0;i<listNodeOutputs.size();i++) {
264 QDomNode node = listNodeOutputs.at(i);
265 QDomElement eltOutput = node.toElement();
266 nameStr = eltOutput.attribute("name","none");
267 typeStr = eltOutput.attribute("type","none");
268 widthStr = eltOutput.attribute("width","none");
269 purposeStr = eltOutput.attribute("purpose","none");
270 purpose = ReferenceInterface::translatePurpose(purposeStr);
271 multStr = eltOutput.attribute("multiplicity","none");
272 mult = ReferenceInterface::translateMultiplicity(multStr);
274 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Output, purpose, mult);
275 outputs.append(inter);
277 // getting each control
278 QDomNodeList listNodeOutCtl = eltOutputs.elementsByTagName("control");
279 for(int i=0;i<listNodeOutCtl.size();i++) {
280 QDomNode node = listNodeOutCtl.at(i);
281 QDomElement eltOutput = node.toElement();
282 nameStr = eltOutput.attribute("iface","none");
283 AbstractInterface* dataIface = getIfaceFromName(nameStr);
284 if (dataIface == NULL) throw (Exception(BLOCKFILE_CORRUPTED));
285 nameStr = dataIface->getName()+"_ctl";
286 inter = new ReferenceInterface(this,nameStr,"boolean","1",AbstractInterface::Output, AbstractInterface::Control, 1);
287 if (!inter->setAssociatedIface(dataIface)) {
288 throw (Exception(BLOCKFILE_CORRUPTED));
290 outputs.append(inter);
293 QDomElement eltBidirs = eltInputs.nextSiblingElement("bidirs");
294 QDomNodeList listNodeBidirs = eltBidirs.elementsByTagName("bidir");
295 for(int i=0;i<listNodeBidirs.size();i++) {
296 QDomNode node = listNodeBidirs.at(i);
297 QDomElement eltBidir = node.toElement();
298 nameStr = eltBidir.attribute("name","none");
299 typeStr = eltBidir.attribute("type","none");
300 widthStr = eltBidir.attribute("width","none");
301 purposeStr = eltBidir.attribute("purpose","none");
302 purpose = ReferenceInterface::translatePurpose(purposeStr);
303 multStr = eltBidir.attribute("multiplicity","none");
304 mult = ReferenceInterface::translateMultiplicity(multStr);
306 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::InOut, purpose, mult);
307 bidirs.append(inter);
311 void ReferenceBlock::createInterfaceForParameters() throw(Exception){
312 ReferenceInterface* iface = NULL;
313 foreach(BlockParameter* param, params) {
315 if (param->isWishboneParameter()) {
317 BlockParameterWishbone* p = (BlockParameterWishbone*)param;
318 cout << "creating interface for parameter wb " << qPrintable(p->getName()) << endl;
320 if (p->getWBAccess() == BlockParameter::Read) {
321 iface = new ReferenceInterface(this,p->getName(),p->getTypeString(),p->getWidth(), AbstractInterface::Output, AbstractInterface::Wishbone,1);
322 outputs.append(iface);
324 else if (p->getWBAccess() == BlockParameter::Write) {
325 iface = new ReferenceInterface(this,p->getName(),p->getTypeString(),p->getWidth(), AbstractInterface::Input, AbstractInterface::Wishbone,1);
326 inputs.append(iface);
329 throw (Exception(BLOCKFILE_CORRUPTED));
335 void ReferenceBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
340 only used to save all ReferenceBlock in a library in binary format, so that reference blocks
341 are read very fast at application startup.
344 QDataStream& operator<<(QDataStream &out, const ReferenceBlock &b) {
346 out.setVersion(QDataStream::Qt_5_0);
348 QByteArray blockData;
349 QDataStream toWrite(&blockData, QIODevice::WriteOnly);
352 toWrite << b.xmlFile;
353 toWrite << b.descriptionBrief;
354 toWrite << b.descriptionDetail;
355 toWrite << b.categories;
356 toWrite << b.hashMd5;
357 toWrite << b.params.size();
359 for(int i=0;i<b.params.size();i++) {
361 toWrite << p->getContext();
362 toWrite << p->getName();
363 toWrite << p->getTypeString();
364 toWrite << p->getValue().toString();
365 if (p->isPortParameter()) {
366 toWrite << ((BlockParameterPort*)p)->getIfaceName();
368 else if (p->isWishboneParameter()) {
369 BlockParameterWishbone* pwb = (BlockParameterWishbone*)p;
370 toWrite << pwb->getWidth();
371 toWrite << pwb->getWBAccess();
372 toWrite << pwb->getWBValue();
373 toWrite << pwb->getWBDuration();
378 toWrite << b.inputs.size();
379 for(int i=0; i<b.inputs.size(); i++){
380 ReferenceInterface *iface = (ReferenceInterface *)(b.inputs.at(i));
381 toWrite << iface->getName();
382 toWrite << iface->getWidth();
383 toWrite << iface->getPurpose();
384 toWrite << iface->getDirection();
385 toWrite << iface->getMultiplicity();
387 toWrite << b.outputs.size();
388 for(int i=0; i<b.outputs.size(); i++){
389 ReferenceInterface *iface = (ReferenceInterface *)(b.outputs.at(i));
390 toWrite << iface->getName();
391 toWrite << iface->getWidth();
392 toWrite << iface->getPurpose();
393 toWrite << iface->getDirection();
394 toWrite << iface->getMultiplicity();
396 toWrite << b.bidirs.size();
397 for(int i=0; i<b.bidirs.size(); i++){
398 ReferenceInterface *iface = (ReferenceInterface *)(b.bidirs.at(i));
399 toWrite << iface->getName();
400 toWrite << iface->getWidth();
401 toWrite << iface->getPurpose();
402 toWrite << iface->getDirection();
403 toWrite << iface->getMultiplicity();
411 QDataStream& operator>>(QDataStream &in, ReferenceBlock &b) {
414 ReferenceInterface* iface;
419 in.setVersion(QDataStream::Qt_5_0);
425 in >> b.descriptionBrief;
426 in >> b.descriptionDetail;
432 cout << qPrintable(b.name) << " has " << nb << " parameters" << endl;
433 for(int i=0;i<nb;i++) {
434 QString contextStr = "";
436 QString typeStr = "";
437 QString valueStr = "";
443 if (contextStr == "user") {
444 p = new BlockParameterUser(&b,nameStr,valueStr);
446 else if (contextStr == "generic") {
447 p = new BlockParameterGeneric(&b,nameStr,typeStr,valueStr);
449 else if (contextStr == "port") {
450 QString ifaceStr = "";
452 p = new BlockParameterPort(&b,nameStr,valueStr,ifaceStr);
454 else if (contextStr == "wb") {
455 QString widthStr = "";
463 p = new BlockParameterWishbone(&b,nameStr,typeStr,widthStr,valueStr,wbAccess,wbValue,wbDuration);
470 for(int i=0;i<nb;i++) {
471 iface = new ReferenceInterface(&b);
475 iface->setWidth(txt);
477 iface->setPurpose(val);
479 iface->setDirection(val);
481 iface->setMultiplicity(val);
482 b.inputs.append(iface);
487 for(int i=0;i<nb;i++) {
488 iface = new ReferenceInterface(&b);
492 iface->setWidth(txt);
494 iface->setPurpose(val);
496 iface->setDirection(val);
498 iface->setMultiplicity(val);
499 b.outputs.append(iface);
504 for(int i=0;i<nb;i++) {
505 iface = new ReferenceInterface(&b);
509 iface->setWidth(txt);
511 iface->setPurpose(val);
513 iface->setDirection(val);
515 iface->setMultiplicity(val);
516 b.bidirs.append(iface);