1 #include "AbstractInterface.h"
2 #include "BlockParameterPort.h"
3 #include "AbstractBlock.h"
5 AbstractInterface::AbstractInterface(AbstractBlock* _owner) {
13 associatedIface = NULL;
17 AbstractInterface::AbstractInterface(AbstractBlock* _owner, const QString& _name, const QString& _type, const QString& _width, int _direction, int _purpose) {
22 direction = _direction;
24 type = typeFromString(_type);
25 associatedIface = NULL;
28 AbstractInterface::AbstractInterface(AbstractInterface* other) {
33 direction = other->direction;
34 purpose = other->purpose;
35 associatedIface = NULL;
38 AbstractInterface::~AbstractInterface() {
42 bool AbstractInterface::isReferenceInterface() {
46 bool AbstractInterface::isFunctionalInterface() {
50 bool AbstractInterface::isGroupInterface() {
54 QString AbstractInterface::getPurposeString() {
57 case AbstractInterface::Data:
58 str = QString("data");
60 case AbstractInterface::Clock:
61 str = QString("clock");
63 case AbstractInterface::Reset:
64 str = QString("reset");
66 case AbstractInterface::Wishbone:
67 str = QString("wishbone");
73 QString AbstractInterface::getDirectionString() {
76 case AbstractInterface::Input:
77 str = QString("input");
79 case AbstractInterface::Output:
80 str = QString("output");
82 case AbstractInterface::InOut:
83 str = QString("inout");
89 double AbstractInterface::getDoubleWidth() throw(QException) {
91 static QString fctName = "AbstractInterface::getDoubleWidth()";
93 cout << "call to " << qPrintable(fctName) << endl;
97 cout << "start AbstractInterface::getDoubleWidth()" << endl;
99 double width = getWidth().toDouble(&ok);
102 ArithmeticEvaluator *evaluator = new ArithmeticEvaluator;
103 cout << "evaluator created!" << endl;
104 evaluator->setExpression(getWidth());
105 cout << "expression defined!" << endl;
106 foreach(BlockParameter *param, getOwner()->getParameters()){
107 evaluator->setVariableValue(param->getName(), param->getIntValue());
108 cout << "param : " << param->getName().toStdString() << " evaluated!" << endl;
110 width = evaluator->evaluate();
111 cout << "expression evaluated succefully!" << endl;
113 cout << "real width : " << width << endl;
120 void AbstractInterface::setPurpose(int _purpose) {
121 if ((_purpose>=Data) && (_purpose <= Wishbone)) {
126 void AbstractInterface::setDirection(int _direction) {
127 if ((_direction > Input) && (_direction <= InOut)) {
128 direction = _direction;
132 bool AbstractInterface::setAssociatedIface(AbstractInterface* iface) {
133 if (purpose != Control) return false;
134 if (iface->purpose != Data) return false;
135 associatedIface = iface;
136 iface->associatedIface = this;
141 int AbstractInterface::getIntDirection(QString str) {
142 if(str == "input") return Input;
143 if(str == "output") return Output;
144 if(str == "inout") return InOut;
148 int AbstractInterface::getIntPurpose(QString str) {
149 if(str == "data") return Data;
150 else if(str == "clock") return Clock;
151 else if(str == "reset") return Reset;
152 else if(str == "wishbone") return Wishbone;
156 QString AbstractInterface::getTypeString() {
158 if (type == Boolean) {
161 else if (type == Natural) {
164 else if (type == Expression) {
167 return "invalid_type";
170 int AbstractInterface::typeFromString(const QString &_type) {
173 if (_type == "expression") {
176 else if (_type == "boolean") {
179 else if (_type == "natural") {
182 else if (_type == "inherited") {
188 QString AbstractInterface::toVHDL(int context, int flags) throw(Exception) {
190 if (isReferenceInterface()) throw(Exception(IFACE_INVALID_TYPE));
195 if ((context == BlockParameter::Entity) || (context == BlockParameter::Component)) {
197 QString formatBool = "%1 : %2 std_logic";
198 QString formatVector = "%1 : %2 std_logic_vector(%3 downto %4)";
199 if ((flags & BlockParameter::NoComma) == 0) {
200 formatBool.append(";");
201 formatVector.append(";");
203 QString orientation="";
204 if (direction == Input) {
207 else if (direction == Output) {
211 orientation = "inout";
213 if (type == Boolean) {
214 ret = formatVector.arg(name).arg(orientation);
216 else if (type == Natural) {
217 int w = width.toInt(&ok);
219 throw(Exception(INVALID_VALUE));
223 ret = formatVector.arg(name).arg(orientation).arg(w).arg("0");
226 else if (type == Expression) {
227 /* must check the following conditions :
228 - if it contains user/port parameters : must evaluate their numeric value
229 - if it contains generic parameters : just remove the $ -> the expression is not arithmetically evaluated.
231 QList<BlockParameter*> listGenerics = owner->getGenericParameters();
232 QList<BlockParameter*> listUsers = owner->getUserParameters();
233 QList<BlockParameter*> listPorts = owner->getPortParameters();
234 foreach(BlockParameter* p, listUsers) {
236 var.append(p->getName());
237 if (width.contains(var)) {
238 int w = p->getValue().toInt(&ok);
239 if (!ok) throw(Exception(INVALID_VALUE));
240 msb.replace(var,p->getValue().toString());
243 foreach(BlockParameter* p, listPorts) {
245 var.append(p->getName());
246 if (width.contains(var)) {
247 BlockParameterPort* pp = (BlockParameterPort*)p;
248 AbstractInterface* iface = owner->getIfaceFromName(pp->getIfaceName());
250 int w = p->getValue().toInt(&ok);
251 if (!ok) throw(Exception(INVALID_VALUE));
252 msb.replace(var,p->getValue().toString());
256 ret = formatVector.arg(name).arg(orientation).arg("toto").arg("0");