1 #include "AbstractInterface.h"
2 #include "BlockParameterPort.h"
3 #include "AbstractBlock.h"
4 #include "Parameters.h"
6 AbstractInterface::AbstractInterface(AbstractBlock* _owner) {
14 endianess = LittleEndian;
15 associatedIface = NULL;
19 AbstractInterface::AbstractInterface(AbstractBlock* _owner, const QString& _name, int _direction, int _purpose, const QString& _type, const QString& _width, int _endianess) {
22 name = Parameters::normalizeName(_name);
24 direction = _direction;
26 type = typeFromString(_type);
27 endianess = _endianess;
28 associatedIface = NULL;
31 AbstractInterface::AbstractInterface(AbstractInterface* other) {
33 name = Parameters::normalizeName(other->name);
36 direction = other->direction;
37 purpose = other->purpose;
38 endianess = LittleEndian;
39 associatedIface = NULL;
42 void AbstractInterface::setName(const QString& _name) {
43 name = Parameters::normalizeName(_name);
46 AbstractInterface::~AbstractInterface() {
50 bool AbstractInterface::isReferenceInterface() {
54 bool AbstractInterface::isFunctionalInterface() {
58 bool AbstractInterface::isGroupInterface() {
62 QString AbstractInterface::getEndianessString() {
63 QString str="unknown";
65 case AbstractInterface::LittleEndian:
66 str = QString("little");
68 case AbstractInterface::BigEndian:
75 QString AbstractInterface::getPurposeString() {
78 case AbstractInterface::AnyPurpose:
81 case AbstractInterface::Data:
82 str = QString("data");
84 case AbstractInterface::Control:
85 str = QString("control");
87 case AbstractInterface::Clock:
88 str = QString("clock");
90 case AbstractInterface::Reset:
91 str = QString("reset");
93 case AbstractInterface::Wishbone:
94 str = QString("wishbone");
100 QString AbstractInterface::getDirectionString() {
103 case AbstractInterface::Input:
104 str = QString("input");
106 case AbstractInterface::Output:
107 str = QString("output");
109 case AbstractInterface::InOut:
110 str = QString("inout");
116 double AbstractInterface::getDoubleWidth() throw(QException) {
118 static QString fctName = "AbstractInterface::getDoubleWidth()";
120 cout << "call to " << qPrintable(fctName) << endl;
124 cout << "start AbstractInterface::getDoubleWidth()" << endl;
126 double width = getWidth().toDouble(&ok);
129 ArithmeticEvaluator *evaluator = new ArithmeticEvaluator;
130 cout << "evaluator created!" << endl;
131 evaluator->setExpression(getWidth());
132 cout << "expression defined!" << endl;
133 foreach(BlockParameter *param, getOwner()->getParameters()){
134 evaluator->setVariableValue(param->getName(), param->getIntValue());
135 cout << "param : " << param->getName().toStdString() << " evaluated!" << endl;
137 width = evaluator->evaluate();
138 cout << "expression evaluated succefully!" << endl;
140 cout << "real width : " << width << endl;
147 void AbstractInterface::setPurpose(int _purpose) {
148 if ((_purpose>=Data) && (_purpose <= Wishbone)) {
153 void AbstractInterface::setDirection(int _direction) {
154 if ((_direction > Input) && (_direction <= InOut)) {
155 direction = _direction;
159 bool AbstractInterface::setAssociatedIface(AbstractInterface* iface) {
160 if (purpose != Control) return false;
161 if (iface->purpose != Data) return false;
162 associatedIface = iface;
163 iface->associatedIface = this;
168 int AbstractInterface::getIntDirection(QString str) {
169 if(str == "input") return Input;
170 if(str == "output") return Output;
171 if(str == "inout") return InOut;
175 int AbstractInterface::getIntPurpose(QString str) {
176 if(str == "data") return Data;
177 else if(str == "clock") return Clock;
178 else if(str == "reset") return Reset;
179 else if(str == "wishbone") return Wishbone;
183 QString AbstractInterface::getTypeString() {
185 if (type == Boolean) {
188 else if (type == Natural) {
191 else if (type == Expression) {
194 return "invalid_type";
197 int AbstractInterface::typeFromString(const QString &_type) {
199 int ret = Expression; // default type
200 if (_type == "expression") {
203 else if (_type == "boolean") {
206 else if (_type == "natural") {
209 else if (_type == "inherited") {
215 QString AbstractInterface::toVHDL(int context, int flags) throw(Exception) {
218 if (isReferenceInterface()) throw(Exception(IFACE_INVALID_TYPE));
223 cout << "iface " << qPrintable(name) << " must be evaluated to vhdl :" << qPrintable(msb) << " with type = " << qPrintable(getTypeString()) << endl;
225 if ((context == BlockParameter::Entity) || (context == BlockParameter::Component)) {
227 QString formatBool = "%1 : %2 std_logic";
228 QString formatVector = "";
229 if (endianess == LittleEndian) formatVector = "%1 : %2 std_logic_vector(%3 downto %4)";
230 else formatVector = "%1 : %2 std_logic_vector(%4 to %3)";
232 if ((flags & BlockParameter::NoComma) == 0) {
233 formatBool.append(";");
234 formatVector.append(";");
236 QString orientation="";
237 if (direction == Input) {
240 else if (direction == Output) {
244 orientation = "inout";
246 if (type == Boolean) {
247 ret = formatBool.arg(name).arg(orientation);
249 else if (type == Natural) {
250 int w = width.toInt(&ok);
252 throw(Exception(INVALID_VALUE));
256 ret = formatVector.arg(name).arg(orientation).arg(w).arg("0");
259 else if (type == Expression) {
262 /* must check the following conditions :
263 - if it contains user/port parameters : must evaluate their numeric value
264 - if it contains generic parameters : just remove the $ -> the expression is not arithmetically evaluated.
266 QList<BlockParameter*> listGenerics = owner->getGenericParameters();
267 QList<BlockParameter*> listUsers = owner->getUserParameters();
268 QList<BlockParameter*> listPorts = owner->getPortParameters();
269 foreach(BlockParameter* p, listUsers) {
272 if (width.contains(var)) {
273 int w = p->getValue().toInt(&ok);
274 if (!ok) throw(Exception(INVALID_VALUE));
275 msb.replace(var,p->getValue().toString());
278 foreach(BlockParameter* p, listPorts) {
281 if (width.contains(var)) {
282 msb.replace(var,p->toVHDL(0,0));
285 foreach(BlockParameter* p, listGenerics) {
288 if (width.contains(var)) {
289 msb.replace(var,p->getName());
293 cout << "iface size :" << qPrintable(msb) << endl;
294 ret = formatVector.arg(name).arg(orientation).arg(msb).arg("0");