My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
digi_source.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  digi_source.cpp
3  -----------------
4  begin : Oct 3 2005
5  copyright : (C) 2005 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "digi_source.h"
19 #include "main.h"
20 #include "node.h"
21 
22 
24 {
25  Type = isComponent; // both analog and digital
26  Description = QObject::tr("digital source");
27 
28  Lines.append(new Line(-10, 0, 0, 0,QPen(QPen::darkGreen,2)));
29  Lines.append(new Line(-20,-10,-10, 0,QPen(QPen::darkGreen,2)));
30  Lines.append(new Line(-20, 10,-10, 0,QPen(QPen::darkGreen,2)));
31  Lines.append(new Line(-35,-10,-20,-10,QPen(QPen::darkGreen,2)));
32  Lines.append(new Line(-35, 10,-20, 10,QPen(QPen::darkGreen,2)));
33  Lines.append(new Line(-35,-10,-35, 10,QPen(QPen::darkGreen,2)));
34 
35  Lines.append(new Line(-32, 5,-28, 5,QPen(QPen::darkGreen,2)));
36  Lines.append(new Line(-28,-5,-24,-5,QPen(QPen::darkGreen,2)));
37  Lines.append(new Line(-24, 5,-20, 5,QPen(QPen::darkGreen,2)));
38  Lines.append(new Line(-28,-5,-28, 5,QPen(QPen::darkGreen,2)));
39  Lines.append(new Line(-24,-5,-24, 5,QPen(QPen::darkGreen,2)));
40 
41  Ports.append(new Port( 0, 0));
42 
43  x1 = -39; y1 = -14;
44  x2 = 0; y2 = 14;
45 
46  tx = x1+4;
47  ty = y2+2;
48  Model = "DigiSource";
49  Name = "S";
50 
51  // This property must stay in this order !
52  Props.append(new Property("Num", "1", true,
53  QObject::tr("number of the port")));
54  Props.append(new Property("init", "low", false,
55  QObject::tr("initial output value")+" [low, high]"));
56  Props.append(new Property("times", "1ns; 1ns", false,
57  QObject::tr("list of times for changing output value")));
58  Props.append(new Property("V", "1 V", false,
59  QObject::tr("voltage of high level")));
60 }
61 
62 // -------------------------------------------------------
64 {
65 }
66 
67 // -------------------------------------------------------
69 {
70  return new Digi_Source();
71 }
72 
73 // -------------------------------------------------------
74 Element* Digi_Source::info(QString& Name, char* &BitmapFile, bool getNewOne)
75 {
76  Name = QObject::tr("digital source");
77  BitmapFile = (char *) "digi_source";
78 
79  if(getNewOne) return new Digi_Source();
80  return 0;
81 }
82 
83 // -------------------------------------------------------
85 {
86  QString s = Model+":"+Name;
87 
88  // output node names
89  s += " "+Ports.getFirst()->Connection->Name;
90 
91  // output all properties
92  Props.first(); // first property not needed
93  Property *pp = Props.next();
94  s += " "+pp->Name+"=\""+pp->Value+"\"";
95  pp = Props.next();
96  s += " "+pp->Name+"=\"["+pp->Value+"]\"";
97  pp = Props.next();
98  s += " "+pp->Name+"=\""+pp->Value+"\"\n";
99 
100  return s;
101 }
102 
103 // -------------------------------------------------------
104 QString Digi_Source::vhdlCode(int NumPorts)
105 {
106  QString s, t;
107  QString Out(" " + Ports.getFirst()->Connection->Name + " <= '");
108 
109  s = "\n " + Name + ":process\n begin\n";
110 
111  int z = 0;
112  char State;
113  if(NumPorts <= 0) { // time table simulation ?
114  if(Props.at(1)->Value == "low")
115  State = '0';
116  else
117  State = '1';
118 
119  t = Props.next()->Value.section(';',z,z).stripWhiteSpace();
120  while(!t.isEmpty()) {
121  s += Out + State + "';"; // next value for signal
122 
123  if(!VHDL_Delay(t, Name))
124  return t; // time has not VHDL format
125 
126  s += t.replace("after","wait for") + ";\n";
127  State ^= 1;
128  z++;
129  t = Props.current()->Value.section(';',z,z).stripWhiteSpace();
130  }
131  }
132  else { // truth table simulation
133  State = '0';
134  int Num = Props.getFirst()->Value.toInt() - 1;
135 
136  s += Out + State + "';"; // first value for signal
137  s += " wait for "+QString::number(1 << Num)+" ns;\n";
138  State ^= 1;
139  s += Out + State + "';"; // next value for signal
140  s += " wait for "+QString::number(1 << Num)+" ns;\n";
141  }
142 
143  s += " end process;\n";
144  return s;
145 }
146 
147 // -------------------------------------------------------
148 QString Digi_Source::verilogCode(int NumPorts)
149 {
150  QString s, t, n, r;
151 
152  n = Ports.getFirst()->Connection->Name;
153  r = "net_src" + Name + n;
154  s = "\n // " + Name + " digital source\n";
155  s += " assign " + n + " = " + r + ";\n";
156  s += " reg " + r + ";\n";
157 
158  int z = 0;
159  char State;
160  if(NumPorts <= 0) { // time table simulation ?
161  if(Props.at(1)->Value == "low")
162  State = '0';
163  else
164  State = '1';
165  s += " always begin\n";
166 
167  t = Props.next()->Value.section(';',z,z).stripWhiteSpace();
168  while(!t.isEmpty()) {
169  if(!Verilog_Delay(t, Name))
170  return t; // time has not VHDL format
171  s += " " + r + " = " + State + ";\n";
172  s += " " + t + ";\n";
173  State ^= 1;
174  z++;
175  t = Props.current()->Value.section(';',z,z).stripWhiteSpace();
176  }
177  }
178  else { // truth table simulation
179  int Num = Props.getFirst()->Value.toInt() - 1;
180  s += " always begin\n";
181  s += " " + r + " = 0;\n";
182  s += " #"+ QString::number(1 << Num) + ";\n";
183  s += " " + r + " = !" + r + ";\n";
184  s += " #"+ QString::number(1 << Num) + ";\n";
185  }
186 
187  s += " end\n";
188  return s;
189 }