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 QDomNodeList listNodeInputs = eltInputs.elementsByTagName("input");
228 for(int i=0;i<listNodeInputs.size();i++) {
229 QDomNode node = listNodeInputs.at(i);
230 QDomElement eltInput = node.toElement();
231 nameStr = eltInput.attribute("name","none");
232 typeStr = eltInput.attribute("type","none");
233 widthStr = eltInput.attribute("width","none");
234 purposeStr = eltInput.attribute("purpose","none");
235 cout << "block : " << this->getName().toStdString() << endl;
236 cout << "purpose for " << nameStr.toStdString() << " : " << purposeStr.toStdString() << endl;
237 purpose = ReferenceInterface::translatePurpose(purposeStr);
238 cout << "translated purpose : " << purpose << endl;
239 multStr = eltInput.attribute("multiplicity","none");
240 mult = ReferenceInterface::translateMultiplicity(multStr);
242 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Input, purpose, mult);
243 inputs.append(inter);
246 QDomElement eltOutputs = eltInputs.nextSiblingElement("outputs");
247 QDomNodeList listNodeOutputs = eltOutputs.elementsByTagName("output");
248 for(int i=0;i<listNodeOutputs.size();i++) {
249 QDomNode node = listNodeOutputs.at(i);
250 QDomElement eltOutput = node.toElement();
251 nameStr = eltOutput.attribute("name","none");
252 typeStr = eltOutput.attribute("type","none");
253 widthStr = eltOutput.attribute("width","none");
254 purposeStr = eltOutput.attribute("purpose","none");
255 purpose = ReferenceInterface::translatePurpose(purposeStr);
256 multStr = eltOutput.attribute("multiplicity","none");
257 mult = ReferenceInterface::translateMultiplicity(multStr);
259 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::Output, purpose, mult);
260 outputs.append(inter);
263 QDomElement eltBidirs = eltInputs.nextSiblingElement("bidirs");
264 QDomNodeList listNodeBidirs = eltBidirs.elementsByTagName("bidir");
265 for(int i=0;i<listNodeBidirs.size();i++) {
266 QDomNode node = listNodeBidirs.at(i);
267 QDomElement eltBidir = node.toElement();
268 nameStr = eltBidir.attribute("name","none");
269 typeStr = eltBidir.attribute("type","none");
270 widthStr = eltBidir.attribute("width","none");
271 purposeStr = eltBidir.attribute("purpose","none");
272 purpose = ReferenceInterface::translatePurpose(purposeStr);
273 multStr = eltBidir.attribute("multiplicity","none");
274 mult = ReferenceInterface::translateMultiplicity(multStr);
276 inter = new ReferenceInterface(this,nameStr,typeStr,widthStr,AbstractInterface::InOut, purpose, mult);
277 bidirs.append(inter);
281 void ReferenceBlock::createInterfaceForParameters() throw(Exception){
282 ReferenceInterface* iface = NULL;
283 foreach(BlockParameter* param, params) {
285 if (param->isWishboneParameter()) {
287 BlockParameterWishbone* p = (BlockParameterWishbone*)param;
288 cout << "creating interface for parameter wb " << qPrintable(p->getName()) << endl;
290 if (p->getWBAccess() == BlockParameter::Read) {
291 iface = new ReferenceInterface(this,p->getName(),p->getTypeString(),p->getWidth(), AbstractInterface::Output, AbstractInterface::Wishbone,1);
292 outputs.append(iface);
294 else if (p->getWBAccess() == BlockParameter::Write) {
295 iface = new ReferenceInterface(this,p->getName(),p->getTypeString(),p->getWidth(), AbstractInterface::Input, AbstractInterface::Wishbone,1);
296 inputs.append(iface);
299 throw (Exception(BLOCKFILE_CORRUPTED));
305 void ReferenceBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
310 only used to save all ReferenceBlock in a library in binary format, so that reference blocks
311 are read very fast at application startup.
314 QDataStream& operator<<(QDataStream &out, const ReferenceBlock &b) {
316 out.setVersion(QDataStream::Qt_5_0);
318 QByteArray blockData;
319 QDataStream toWrite(&blockData, QIODevice::WriteOnly);
322 toWrite << b.xmlFile;
323 toWrite << b.descriptionBrief;
324 toWrite << b.descriptionDetail;
325 toWrite << b.categories;
326 toWrite << b.hashMd5;
327 toWrite << b.params.size();
329 for(int i=0;i<b.params.size();i++) {
331 toWrite << p->getContext();
332 toWrite << p->getName();
333 toWrite << p->getTypeString();
334 toWrite << p->getValue().toString();
335 if (p->isPortParameter()) {
336 toWrite << ((BlockParameterPort*)p)->getIfaceName();
338 else if (p->isWishboneParameter()) {
339 BlockParameterWishbone* pwb = (BlockParameterWishbone*)p;
340 toWrite << pwb->getWidth();
341 toWrite << pwb->getWBAccess();
342 toWrite << pwb->getWBValue();
343 toWrite << pwb->getWBDuration();
348 toWrite << b.inputs.size();
349 for(int i=0; i<b.inputs.size(); i++){
350 ReferenceInterface *iface = (ReferenceInterface *)(b.inputs.at(i));
351 toWrite << iface->getName();
352 toWrite << iface->getWidth();
353 toWrite << iface->getPurpose();
354 toWrite << iface->getDirection();
355 toWrite << iface->getMultiplicity();
357 toWrite << b.outputs.size();
358 for(int i=0; i<b.outputs.size(); i++){
359 ReferenceInterface *iface = (ReferenceInterface *)(b.outputs.at(i));
360 toWrite << iface->getName();
361 toWrite << iface->getWidth();
362 toWrite << iface->getPurpose();
363 toWrite << iface->getDirection();
364 toWrite << iface->getMultiplicity();
366 toWrite << b.bidirs.size();
367 for(int i=0; i<b.bidirs.size(); i++){
368 ReferenceInterface *iface = (ReferenceInterface *)(b.bidirs.at(i));
369 toWrite << iface->getName();
370 toWrite << iface->getWidth();
371 toWrite << iface->getPurpose();
372 toWrite << iface->getDirection();
373 toWrite << iface->getMultiplicity();
381 QDataStream& operator>>(QDataStream &in, ReferenceBlock &b) {
384 ReferenceInterface* iface;
389 in.setVersion(QDataStream::Qt_5_0);
395 in >> b.descriptionBrief;
396 in >> b.descriptionDetail;
402 cout << qPrintable(b.name) << " has " << nb << " parameters" << endl;
403 for(int i=0;i<nb;i++) {
404 QString contextStr = "";
406 QString typeStr = "";
407 QString valueStr = "";
413 if (contextStr == "user") {
414 p = new BlockParameterUser(&b,nameStr,valueStr);
416 else if (contextStr == "generic") {
417 p = new BlockParameterGeneric(&b,nameStr,typeStr,valueStr);
419 else if (contextStr == "port") {
420 QString ifaceStr = "";
422 p = new BlockParameterPort(&b,nameStr,valueStr,ifaceStr);
424 else if (contextStr == "wb") {
425 QString widthStr = "";
433 p = new BlockParameterWishbone(&b,nameStr,typeStr,widthStr,valueStr,wbAccess,wbValue,wbDuration);
440 for(int i=0;i<nb;i++) {
441 iface = new ReferenceInterface(&b);
445 iface->setWidth(txt);
447 iface->setPurpose(val);
449 iface->setDirection(val);
451 iface->setMultiplicity(val);
452 b.inputs.append(iface);
457 for(int i=0;i<nb;i++) {
458 iface = new ReferenceInterface(&b);
462 iface->setWidth(txt);
464 iface->setPurpose(val);
466 iface->setDirection(val);
468 iface->setMultiplicity(val);
469 b.outputs.append(iface);
474 for(int i=0;i<nb;i++) {
475 iface = new ReferenceInterface(&b);
479 iface->setWidth(txt);
481 iface->setPurpose(val);
483 iface->setDirection(val);
485 iface->setMultiplicity(val);
486 b.bidirs.append(iface);