]> AND Private Git Repository - blast.git/blob - AbstractInterface.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
debugged clk/rst auto conn
[blast.git] / AbstractInterface.cpp
1 #include "AbstractInterface.h"
2 #include "BlockParameterPort.h"
3 #include "AbstractBlock.h"
4
5 AbstractInterface::AbstractInterface(AbstractBlock* _owner) {
6
7   owner = _owner;
8   name = "";
9   width = "1";
10   direction = Input;
11   purpose = Data;  
12   type = Boolean;
13   endianess = LittleEndian;
14   associatedIface = NULL;
15
16 }
17
18 AbstractInterface::AbstractInterface(AbstractBlock* _owner, const QString& _name, int _direction, int _purpose, const QString& _type, const QString& _width, int _endianess) {
19
20   owner = _owner;  
21   name = AbstractBlock::normalizeName(_name);
22   width = _width;
23   direction = _direction;
24   purpose = _purpose;
25   type = typeFromString(_type);
26   endianess = _endianess;
27   associatedIface = NULL;
28 }
29
30 AbstractInterface::AbstractInterface(AbstractInterface* other) {
31   owner = NULL;
32   name = AbstractBlock::normalizeName(other->name);
33   type = other->type;
34   width = other->width;
35   direction = other->direction;
36   purpose = other->purpose;
37   endianess = LittleEndian;
38   associatedIface = NULL;
39 }
40
41 void AbstractInterface::setName(const QString& _name) {
42   name = AbstractBlock::normalizeName(_name);
43 }
44
45 AbstractInterface::~AbstractInterface() {
46
47 }
48
49 bool AbstractInterface::isReferenceInterface() {
50   return false;
51 }
52
53 bool AbstractInterface::isFunctionalInterface() {
54   return false;
55 }
56
57 bool AbstractInterface::isGroupInterface() {
58   return false;
59 }
60
61 QString AbstractInterface::getEndianessString() {
62   QString str="unknown";
63   switch(endianess){
64   case AbstractInterface::LittleEndian:
65     str = QString("little");
66     break;
67   case AbstractInterface::BigEndian:
68     str = QString("big");
69     break;
70   }
71   return str;
72 }
73
74 QString AbstractInterface::getPurposeString() {
75   QString str;
76   switch(purpose){
77   case AbstractInterface::AnyPurpose:
78     str = QString("any");
79     break;
80   case AbstractInterface::Data:
81     str = QString("data");
82     break;
83   case AbstractInterface::Control:
84     str = QString("control");
85     break;
86   case AbstractInterface::Clock:
87     str = QString("clock");
88     break;
89   case AbstractInterface::Reset:
90     str = QString("reset");
91     break;
92   case AbstractInterface::Wishbone:
93     str = QString("wishbone");
94     break;
95   }
96   return str;
97 }
98
99 QString AbstractInterface::getDirectionString() {
100     QString str;
101     switch(direction){
102         case AbstractInterface::Input:
103             str = QString("input");
104             break;
105         case AbstractInterface::Output:
106             str = QString("output");
107             break;
108         case AbstractInterface::InOut:
109             str = QString("inout");
110             break;
111     }
112     return str;
113 }
114
115 double AbstractInterface::getDoubleWidth() throw(QException) {
116
117   static QString fctName = "AbstractInterface::getDoubleWidth()";
118  #ifdef DEBUG_FCTNAME
119    cout << "call to " << qPrintable(fctName) << endl;
120  #endif
121
122    /*
123     cout << "start AbstractInterface::getDoubleWidth()" << endl;
124     bool ok;
125     double width = getWidth().toDouble(&ok);
126
127     if(!ok){
128       ArithmeticEvaluator *evaluator = new ArithmeticEvaluator;
129       cout << "evaluator created!" << endl;
130       evaluator->setExpression(getWidth());
131       cout << "expression defined!" << endl;
132       foreach(BlockParameter *param, getOwner()->getParameters()){
133         evaluator->setVariableValue(param->getName(), param->getIntValue());
134         cout << "param : " << param->getName().toStdString() << " evaluated!" << endl;
135       }
136       width = evaluator->evaluate();
137       cout << "expression evaluated succefully!" << endl;
138     }
139     cout << "real width : " << width << endl;
140     return width;
141     */
142
143    return 1.0;
144 }
145
146 void AbstractInterface::setPurpose(int _purpose) {
147   if ((_purpose>=Data) && (_purpose <= Wishbone)) {
148     purpose = _purpose;
149   }
150 }
151
152 void AbstractInterface::setDirection(int _direction) {
153   if ((_direction > Input) && (_direction <= InOut)) {
154     direction = _direction;
155   }
156 }
157
158 bool AbstractInterface::setAssociatedIface(AbstractInterface* iface) {
159   if (purpose != Control) return false;
160   if (iface->purpose != Data) return false;
161   associatedIface = iface;
162   iface->associatedIface = this;  
163   return true;
164 }
165
166
167 int AbstractInterface::getIntDirection(QString str) {
168     if(str == "input") return Input;
169     if(str == "output") return Output;
170     if(str == "inout") return InOut;
171     return -1;
172 }
173
174 int AbstractInterface::getIntPurpose(QString str) {
175     if(str == "data") return Data;
176     else if(str == "clock") return Clock;
177     else if(str == "reset") return Reset;
178     else if(str == "wishbone") return Wishbone;
179     return -1;
180 }
181
182 QString AbstractInterface::getTypeString() {
183
184   if (type == Boolean) {
185     return "boolean";
186   }
187   else if (type == Natural) {
188     return "natural";
189   }
190   else if (type == Expression) {
191     return "expression";
192   }
193   return "invalid_type";
194 }
195
196 int AbstractInterface::typeFromString(const QString &_type) {
197
198   int ret = Expression; // default type
199   if (_type == "expression") {
200     ret = Expression;
201   }
202   else if (_type == "boolean") {
203     ret = Boolean;
204   }
205   else if (_type == "natural") {
206     ret = Natural;
207   }
208   else if (_type == "inherited") {
209     ret = Inherited;
210   }
211   return ret;
212 }
213
214 QString AbstractInterface::toVHDL(int context, int flags) throw(Exception) {
215
216
217   if (isReferenceInterface()) throw(Exception(IFACE_INVALID_TYPE));
218
219   QString msb = width;
220   QString ret="";
221   bool ok;
222     cout << "iface " << qPrintable(name) << " must be evaluated to vhdl :" << qPrintable(msb) << " with type = " << qPrintable(getTypeString()) << endl;
223
224   if ((context == BlockParameter::Entity) || (context == BlockParameter::Component)) {
225
226     QString formatBool = "%1 : %2 std_logic";
227     QString formatVector = "";
228     if (endianess == LittleEndian) formatVector = "%1 : %2 std_logic_vector(%3 downto %4)";
229     else formatVector = "%1 : %2 std_logic_vector(%4 to %3)";
230
231     if ((flags & BlockParameter::NoComma) == 0) {
232       formatBool.append(";");
233       formatVector.append(";");
234     }
235     QString orientation="";
236     if (direction == Input) {
237       orientation = "in";
238     }
239     else if (direction == Output) {
240       orientation = "out";
241     }
242     else {
243       orientation = "inout";
244     }
245     if (type == Boolean) {
246       ret = formatBool.arg(name).arg(orientation);
247     }
248     else if (type == Natural) {
249       int w = width.toInt(&ok);
250       if (!ok) {
251         throw(Exception(INVALID_VALUE));
252       }
253       else {
254         w -= 1;
255         ret = formatVector.arg(name).arg(orientation).arg(w).arg("0");
256       }
257     }
258     else if (type == Expression) {
259
260
261       /* must check the following conditions :
262            - if it contains user/port parameters : must evaluate their numeric value
263            - if it contains generic parameters : just remove the $ -> the expression is not arithmetically evaluated.
264       */
265       QList<BlockParameter*> listGenerics = owner->getGenericParameters();
266       QList<BlockParameter*> listUsers = owner->getUserParameters();
267       QList<BlockParameter*> listPorts = owner->getPortParameters();
268       foreach(BlockParameter* p, listUsers) {
269         QString var = "$";
270         var += p->getName();
271         if (width.contains(var)) {
272           int w = p->getValue().toInt(&ok);
273           if (!ok) throw(Exception(INVALID_VALUE));
274           msb.replace(var,p->getValue().toString());
275         }
276       }
277       foreach(BlockParameter* p, listPorts) {
278         QString var = "$";
279         var += p->getName();
280         if (width.contains(var)) {          
281           msb.replace(var,p->toVHDL(0,0));
282         }
283       }
284       foreach(BlockParameter* p, listGenerics) {
285         QString var = "$";
286         var += p->getName();
287         if (width.contains(var)) {
288           msb.replace(var,p->getName());
289         }
290       }
291       msb += "-1";
292       cout << "iface size :" << qPrintable(msb) << endl;
293       ret = formatVector.arg(name).arg(orientation).arg(msb).arg("0");
294     }
295   }
296   return ret;
297 }
298