1 #include "AbstractInterface.h"
2 #include "BlockParameterPort.h"
3 #include "AbstractBlock.h"
5 AbstractInterface::AbstractInterface(AbstractBlock* _owner) {
13 endianess = LittleEndian;
14 associatedIface = NULL;
18 AbstractInterface::AbstractInterface(AbstractBlock* _owner, const QString& _name, int _direction, int _purpose, const QString& _type, const QString& _width, int _endianess) {
23 direction = _direction;
25 type = typeFromString(_type);
26 endianess = _endianess;
27 associatedIface = NULL;
30 AbstractInterface::AbstractInterface(AbstractInterface* other) {
35 direction = other->direction;
36 purpose = other->purpose;
37 endianess = LittleEndian;
38 associatedIface = NULL;
41 AbstractInterface::~AbstractInterface() {
45 bool AbstractInterface::isReferenceInterface() {
49 bool AbstractInterface::isFunctionalInterface() {
53 bool AbstractInterface::isGroupInterface() {
57 QString AbstractInterface::getEndianessString() {
58 QString str="unknown";
60 case AbstractInterface::LittleEndian:
61 str = QString("little");
63 case AbstractInterface::BigEndian:
70 QString AbstractInterface::getPurposeString() {
73 case AbstractInterface::AnyPurpose:
76 case AbstractInterface::Data:
77 str = QString("data");
79 case AbstractInterface::Control:
80 str = QString("control");
82 case AbstractInterface::Clock:
83 str = QString("clock");
85 case AbstractInterface::Reset:
86 str = QString("reset");
88 case AbstractInterface::Wishbone:
89 str = QString("wishbone");
95 QString AbstractInterface::getDirectionString() {
98 case AbstractInterface::Input:
99 str = QString("input");
101 case AbstractInterface::Output:
102 str = QString("output");
104 case AbstractInterface::InOut:
105 str = QString("inout");
111 double AbstractInterface::getDoubleWidth() throw(QException) {
113 static QString fctName = "AbstractInterface::getDoubleWidth()";
115 cout << "call to " << qPrintable(fctName) << endl;
119 cout << "start AbstractInterface::getDoubleWidth()" << endl;
121 double width = getWidth().toDouble(&ok);
124 ArithmeticEvaluator *evaluator = new ArithmeticEvaluator;
125 cout << "evaluator created!" << endl;
126 evaluator->setExpression(getWidth());
127 cout << "expression defined!" << endl;
128 foreach(BlockParameter *param, getOwner()->getParameters()){
129 evaluator->setVariableValue(param->getName(), param->getIntValue());
130 cout << "param : " << param->getName().toStdString() << " evaluated!" << endl;
132 width = evaluator->evaluate();
133 cout << "expression evaluated succefully!" << endl;
135 cout << "real width : " << width << endl;
142 void AbstractInterface::setPurpose(int _purpose) {
143 if ((_purpose>=Data) && (_purpose <= Wishbone)) {
148 void AbstractInterface::setDirection(int _direction) {
149 if ((_direction > Input) && (_direction <= InOut)) {
150 direction = _direction;
154 bool AbstractInterface::setAssociatedIface(AbstractInterface* iface) {
155 if (purpose != Control) return false;
156 if (iface->purpose != Data) return false;
157 associatedIface = iface;
158 iface->associatedIface = this;
163 int AbstractInterface::getIntDirection(QString str) {
164 if(str == "input") return Input;
165 if(str == "output") return Output;
166 if(str == "inout") return InOut;
170 int AbstractInterface::getIntPurpose(QString str) {
171 if(str == "data") return Data;
172 else if(str == "clock") return Clock;
173 else if(str == "reset") return Reset;
174 else if(str == "wishbone") return Wishbone;
178 QString AbstractInterface::getTypeString() {
180 if (type == Boolean) {
183 else if (type == Natural) {
186 else if (type == Expression) {
189 return "invalid_type";
192 int AbstractInterface::typeFromString(const QString &_type) {
194 int ret = Expression; // default type
195 if (_type == "expression") {
198 else if (_type == "boolean") {
201 else if (_type == "natural") {
204 else if (_type == "inherited") {
210 QString AbstractInterface::toVHDL(int context, int flags) throw(Exception) {
212 if (isReferenceInterface()) throw(Exception(IFACE_INVALID_TYPE));
217 if ((context == BlockParameter::Entity) || (context == BlockParameter::Component)) {
219 QString formatBool = "%1 : %2 std_logic";
220 QString formatVector = "%1 : %2 std_logic_vector(%3 downto %4)";
221 if ((flags & BlockParameter::NoComma) == 0) {
222 formatBool.append(";");
223 formatVector.append(";");
225 QString orientation="";
226 if (direction == Input) {
229 else if (direction == Output) {
233 orientation = "inout";
235 if (type == Boolean) {
236 ret = formatVector.arg(name).arg(orientation);
238 else if (type == Natural) {
239 int w = width.toInt(&ok);
241 throw(Exception(INVALID_VALUE));
245 ret = formatVector.arg(name).arg(orientation).arg(w).arg("0");
248 else if (type == Expression) {
249 /* must check the following conditions :
250 - if it contains user/port parameters : must evaluate their numeric value
251 - if it contains generic parameters : just remove the $ -> the expression is not arithmetically evaluated.
253 QList<BlockParameter*> listGenerics = owner->getGenericParameters();
254 QList<BlockParameter*> listUsers = owner->getUserParameters();
255 QList<BlockParameter*> listPorts = owner->getPortParameters();
256 foreach(BlockParameter* p, listUsers) {
258 var.append(p->getName());
259 if (width.contains(var)) {
260 int w = p->getValue().toInt(&ok);
261 if (!ok) throw(Exception(INVALID_VALUE));
262 msb.replace(var,p->getValue().toString());
265 foreach(BlockParameter* p, listPorts) {
267 var.append(p->getName());
268 if (width.contains(var)) {
269 BlockParameterPort* pp = (BlockParameterPort*)p;
270 AbstractInterface* iface = owner->getIfaceFromName(pp->getIfaceName());
272 int w = p->getValue().toInt(&ok);
273 if (!ok) throw(Exception(INVALID_VALUE));
274 msb.replace(var,p->getValue().toString());
278 ret = formatVector.arg(name).arg(orientation).arg("toto").arg("0");