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) {
213 if (isReferenceInterface()) throw(Exception(IFACE_INVALID_TYPE));
218 cout << "iface " << qPrintable(name) << " must be evaluated to vhdl :" << qPrintable(msb) << " with type = " << qPrintable(getTypeString()) << endl;
220 if ((context == BlockParameter::Entity) || (context == BlockParameter::Component)) {
222 QString formatBool = "%1 : %2 std_logic";
223 QString formatVector = "";
224 if (endianess == LittleEndian) formatVector = "%1 : %2 std_logic_vector(%3 downto %4)";
225 else formatVector = "%1 : %2 std_logic_vector(%4 to %3)";
227 if ((flags & BlockParameter::NoComma) == 0) {
228 formatBool.append(";");
229 formatVector.append(";");
231 QString orientation="";
232 if (direction == Input) {
235 else if (direction == Output) {
239 orientation = "inout";
241 if (type == Boolean) {
242 ret = formatBool.arg(name).arg(orientation);
244 else if (type == Natural) {
245 int w = width.toInt(&ok);
247 throw(Exception(INVALID_VALUE));
251 ret = formatVector.arg(name).arg(orientation).arg(w).arg("0");
254 else if (type == Expression) {
257 /* must check the following conditions :
258 - if it contains user/port parameters : must evaluate their numeric value
259 - if it contains generic parameters : just remove the $ -> the expression is not arithmetically evaluated.
261 QList<BlockParameter*> listGenerics = owner->getGenericParameters();
262 QList<BlockParameter*> listUsers = owner->getUserParameters();
263 QList<BlockParameter*> listPorts = owner->getPortParameters();
264 foreach(BlockParameter* p, listUsers) {
267 if (width.contains(var)) {
268 int w = p->getValue().toInt(&ok);
269 if (!ok) throw(Exception(INVALID_VALUE));
270 msb.replace(var,p->getValue().toString());
273 foreach(BlockParameter* p, listPorts) {
276 if (width.contains(var)) {
277 msb.replace(var,p->toVHDL(0,0));
280 foreach(BlockParameter* p, listGenerics) {
283 if (width.contains(var)) {
284 msb.replace(var,p->getName());
288 cout << "iface size :" << qPrintable(msb) << endl;
289 ret = formatVector.arg(name).arg(orientation).arg(msb).arg("0");