ct_lib.hpp
Go to the documentation of this file.
1 /**********************************************************************
2  * ct_lib.hpp -- a library of useful processes in the CT MoC *
3  * *
4  * Authors: Hosein Attarzadeh (shan2@kth.se) *
5  * *
6  * Purpose: Enriching the CT library. *
7  * *
8  * Usage: *
9  * *
10  * License: BSD3 *
11  *******************************************************************/
12 
13 #ifndef CTLIB_H
14 #define CTLIB_H
15 
16 #include "ct_moc.hpp"
17 #include "dde_moc.hpp"
18 #include "mis.hpp"
19 
27 namespace ForSyDe
28 {
29 
30 namespace CT
31 {
32 
34 
37 class sine : public source
38 {
39 public:
40  sine(sc_module_name name_,
41  const sc_time& endT,
42  const sc_time& period,
43  const CTTYPE& ampl
44  ) : source(name_, [=](CTTYPE& out, const sc_time& t)
45  {
46  out = ampl*sin(2*M_PI*t/period);
47  }, endT) {}
48 
50  std::string forsyde_kind() const {return "CT::sine";}
51 };
52 
54 
60 template <class OIf>
61 inline sine* make_sine(std::string pName,
62  const sc_time& endT,
63  const sc_time& period,
64  const CTTYPE& ampl,
65  OIf& outS
66  )
67 {
68  auto p = new sine(pName.c_str(), endT, period, ampl);
69 
70  (*p).oport1(outS);
71 
72  return p;
73 }
74 
76 
79 class cosine : public source
80 {
81 public:
82  cosine(sc_module_name name_,
83  const sc_time& endT,
84  const sc_time& period,
85  const CTTYPE& ampl
86  ) : source(name_, [=](CTTYPE& out, const sc_time& t)
87  {
88  out = ampl*cos(2*M_PI*t/period);
89  }, endT) {}
90 
92  std::string forsyde_kind() const {return "CT::cosine";}
93 };
94 
96 
102 template <class OIf>
103 inline cosine* make_cosine(std::string pName,
104  const sc_time& endT,
105  const sc_time& period,
106  const CTTYPE& ampl,
107  OIf& outS
108  )
109 {
110  auto p = new cosine(pName.c_str(), endT, period, ampl);
111 
112  (*p).oport1(outS);
113 
114  return p;
115 }
116 
118 
121 class square : public source
122 {
123 public:
124  square(sc_module_name name_,
125  const sc_time& endT,
126  const sc_time& period,
127  const CTTYPE& highS,
128  const CTTYPE& lowS,
129  const double& dutyCycle = 0.5
130  ) : source(name_, [=](CTTYPE& out, const sc_time& t)
131  {
132  double tmp = (t/period);
133  out = tmp-(long)tmp < dutyCycle ? highS : lowS;
134  }, endT) {}
135 
137  std::string forsyde_kind() const {return "CT::square";}
138 };
139 
141 
147 template <class OIf>
148 inline square* make_square(std::string pName,
149  const sc_time& endT,
150  const sc_time& period,
151  const CTTYPE& highS,
152  const CTTYPE& lowS,
153  const double& dutyCycle,
154  OIf& outS
155  )
156 {
157  auto p = new square(pName.c_str(), endT, period, highS, lowS, dutyCycle);
158 
159  (*p).oport1(outS);
160 
161  return p;
162 }
163 
165 
169 class scale : public comb
170 {
171 public:
172  scale(sc_module_name name_,
173  const CTTYPE& scaling_factor
174  ) : comb(name_, [=](CTTYPE& out1, const CTTYPE& inp1)
175  {
176  out1 = scaling_factor * inp1;
177  }) {}
178 
180  std::string forsyde_kind() const {return "CT::scale";}
181 };
182 
184 
190 template <class OIf, class IIf>
191 inline scale* make_scale(std::string pName,
192  const CTTYPE& scaling_factor,
193  OIf& outS,
194  IIf& inpS
195  )
196 {
197  auto p = new scale(pName.c_str(), scaling_factor);
198 
199  (*p).iport1(inpS);
200  (*p).oport1(outS);
201 
202  return p;
203 }
204 
206 
209 class add : public comb2
210 {
211 public:
212  add(sc_module_name name_
213  ) : comb2(name_, [=](CTTYPE& out1, const CTTYPE& inp1, const CTTYPE& inp2)
214  {
215  out1 = inp1 + inp2;
216  }) {}
217 
219  std::string forsyde_kind() const {return "CT::add";}
220 };
221 
223 
229 template <class OIf, class IIf1, class IIf2>
230 inline add* make_add(std::string pName,
231  OIf& outS,
232  IIf1& inp1S,
233  IIf2& inp2S
234  )
235 {
236  auto p = new add(pName.c_str());
237 
238  (*p).iport1(inp1S);
239  (*p).iport2(inp2S);
240  (*p).oport1(outS);
241 
242  return p;
243 }
244 
246 
250 class sub : public comb2
251 {
252 public:
253  sub(sc_module_name name_
254  ) : comb2(name_, [=](CTTYPE& out1, const CTTYPE& inp1, const CTTYPE& inp2)
255  {
256  out1 = inp1 - inp2;
257  }) {}
258 
260  std::string forsyde_kind() const {return "CT::sub";}
261 };
262 
264 
270 template <class OIf, class IIf1, class IIf2>
271 inline sub* make_sub(std::string pName,
272  OIf& outS,
273  IIf1& inp1S,
274  IIf2& inp2S
275  )
276 {
277  auto p = new sub(pName.c_str());
278 
279  (*p).iport1(inp1S);
280  (*p).iport2(inp2S);
281  (*p).oport1(outS);
282 
283  return p;
284 }
285 
287 
290 class mul : public comb2
291 {
292 public:
293  mul(sc_module_name name_
294  ) : comb2(name_, [=](CTTYPE& out1, const CTTYPE& inp1, const CTTYPE& inp2)
295  {
296  out1 = inp1 * inp2;
297  }) {}
298 
300  std::string forsyde_kind() const {return "CT::mul";}
301 };
302 
304 
310 template <class OIf, class IIf1, class IIf2>
311 inline mul* make_mul(std::string pName,
312  OIf& outS,
313  IIf1& inp1S,
314  IIf2& inp2S
315  )
316 {
317  auto p = new mul(pName.c_str());
318 
319  (*p).iport1(inp1S);
320  (*p).iport2(inp2S);
321  (*p).oport1(outS);
322 
323  return p;
324 }
325 
327 
330 SC_MODULE(gaussian)
331 {
332  CT_out oport1;
333 
334  SY::gaussian gaussian1;
335  SY2CT sy2ct1;
336 
337  SY::SY2SY<CTTYPE> out_sig;
338 
340 
342  gaussian(sc_module_name _name,
343  const double& gaussVar,
344  const double& gaussMean,
345  sc_time sample_period
346  ) : sc_module(_name), gaussian1("gaussian1", gaussVar, gaussMean),
347  sy2ct1("sy2ct1", sample_period, HOLD)
348  {
349  gaussian1.oport1(out_sig);
350 
351  sy2ct1.iport1(out_sig);
352  sy2ct1.oport1(oport1);
353  }
354 };
355 
357 
363 template <class OIf>
364 inline gaussian* make_gaussian(std::string pName,
365  const double& gaussVar,
366  const double& gaussMean,
367  const sc_time sample_period,
368  OIf& outS
369  )
370 {
371  auto p = new gaussian(pName.c_str(), gaussVar, gaussMean, sample_period);
372 
373  (*p).oport1(outS);
374 
375  return p;
376 }
377 
379 
384 SC_MODULE(filter)
385 {
386  CT_in iport1;
387  CT_out oport1;
388 
389  CT2DDE<CTTYPE> ct2de1;
390  DDE::filter<CTTYPE> filter1;
391  DDE2CT<CTTYPE> de2ct1;
392 
393  DDE::DDE2DDE<CTTYPE> inp_sig, out_sig;
395 
397 
399  filter(sc_module_name _name,
400  std::vector<CTTYPE> numerators,
401  std::vector<CTTYPE> denominators,
402  sc_time sample_period,
403  sc_time min_step=sc_time(0.05,SC_NS),
404  double tol_error=1e-5
405  ) : sc_module(_name), ct2de1("ct2de1"),
406  filter1("filter1", numerators, denominators, sample_period, min_step, tol_error),
407  de2ct1("de2ct1", HOLD)
408  {
409  ct2de1.iport1(iport1);
410  ct2de1.iport2(smp_sig);
411  ct2de1.oport1(inp_sig);
412 
413  filter1.iport1(inp_sig);
414  filter1.oport1(out_sig);
415  filter1.oport2(smp_sig);
416 
417  de2ct1.iport1(out_sig);
418  de2ct1.oport1(oport1);
419  }
420 };
421 
423 
429 template <class OIf, class I1If>
430 inline filter* make_filter(std::string pName,
431  const std::vector<CTTYPE> numerators,
432  const std::vector<CTTYPE> denominators,
433  const sc_time sample_period,
434  OIf& outS,
435  I1If& inp1S
436  )
437 {
438  auto p = new filter(pName.c_str(), numerators, denominators, sample_period);
439 
440  (*p).iport1(inp1S);
441  (*p).oport1(outS);
442 
443  return p;
444 }
445 
447 
453 SC_MODULE(filterf)
454 {
455  CT_in iport1;
456  CT_out oport1;
457 
458  CT2DDEf<CTTYPE> ct2de1;
459  DDE::filterf<CTTYPE> filter1;
460  DDE2CT<CTTYPE> de2ct1;
461 
462  DDE::DDE2DDE<CTTYPE> inp_sig, out_sig;
463 
465 
467  filterf(sc_module_name _name,
468  std::vector<CTTYPE> numerators,
469  std::vector<CTTYPE> denominators,
470  sc_time sample_period
471  ) : sc_module(_name), ct2de1("ct2de1", sample_period),
472  filter1("filter1", numerators, denominators),
473  de2ct1("de2ct1", HOLD)
474  {
475  ct2de1.iport1(iport1);
476  ct2de1.oport1(inp_sig);
477 
478  filter1.iport1(inp_sig);
479  filter1.oport1(out_sig);
480 
481  de2ct1.iport1(out_sig);
482  de2ct1.oport1(oport1);
483  }
484 };
485 
487 
493 template <class OIf, class I1If>
494 inline filterf* make_filterf(std::string pName,
495  const std::vector<CTTYPE> numerators,
496  const std::vector<CTTYPE> denominators,
497  const sc_time sample_period,
498  OIf& outS,
499  I1If& inp1S
500  )
501 {
502  auto p = new filterf(pName.c_str(), numerators, denominators, sample_period);
503 
504  (*p).iport1(inp1S);
505  (*p).oport1(outS);
506 
507  return p;
508 }
509 
511 
517 template <class OIf, class I1If>
518 inline filter* make_integrator(std::string pName,
519  const sc_time sample_period,
520  OIf& outS,
521  I1If& inp1S
522  )
523 {
524  std::vector<CTTYPE> numerators = {1.0};
525  std::vector<CTTYPE> denominators = {1.0, 0.0};
526 
527  auto p = new filter(pName.c_str(), numerators, denominators, sample_period);
528 
529  (*p).iport1(inp1S);
530  (*p).oport1(outS);
531 
532  return p;
533 }
534 
536 
542 template <class OIf, class I1If>
543 inline filterf* make_integratorf(std::string pName,
544  const sc_time sample_period,
545  OIf& outS,
546  I1If& inp1S
547  )
548 {
549  std::vector<CTTYPE> numerators = {1.0};
550  std::vector<CTTYPE> denominators = {1.0, 0.0};
551 
552  auto p = new filterf(pName.c_str(), numerators, denominators, sample_period);
553 
554  (*p).iport1(inp1S);
555  (*p).oport1(outS);
556 
557  return p;
558 }
559 
561 
566 {
567  CT_in iport1;
568  CT_out oport1;
569 
570  fanout fanout1;
571  scale scale1;
572  filterf integrator1;
573  add add1;
574 
575  signal fan2p, fan2i, p2add, i2add;
576 
578 
580  pif(sc_module_name _name,
581  const CTTYPE& kp,
582  const CTTYPE& ki,
583  sc_time sample_period
584  ) : sc_module(_name), fanout1("fanout1"), scale1("scale1", kp),
585  integrator1("integrator1", {ki}, {1,0}, sample_period),
586  add1("add1")
587  {
588  fanout1.iport1(iport1);
589  fanout1.oport1(fan2p);
590  fanout1.oport1(fan2i);
591 
592  scale1.iport1(fan2p);
593  scale1.oport1(p2add);
594 
595  integrator1.iport1(fan2i);
596  integrator1.oport1(i2add);
597 
598  add1.iport1(p2add);
599  add1.iport2(i2add);
600  add1.oport1(oport1);
601  }
602 };
603 
605 
611 template <class OIf, class I1If>
612 inline pif* make_pif(std::string pName,
613  const CTTYPE& kp,
614  const CTTYPE& ki,
615  const sc_time sample_period,
616  OIf& outS,
617  I1If& inp1S
618  )
619 {
620  auto p = new pif(pName.c_str(), kp, ki, sample_period);
621 
622  (*p).iport1(inp1S);
623  (*p).oport1(outS);
624 
625  return p;
626 }
627 
628 }
629 }
630 #endif
Process constructor for a continuous-time process which adds its inputs.
Definition: ct_lib.hpp:209
cosine(sc_module_name name_, const sc_time &endT, const sc_time &period, const CTTYPE &ampl)
Definition: ct_lib.hpp:82
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_lib.hpp:50
mul(sc_module_name name_)
Definition: ct_lib.hpp:293
double CTTYPE
Type of the values used in the CT MoC (currently fixed)
Definition: sub_signal.hpp:27
DDE_in< T > iport1
port for the input channel
Definition: dde_process_constructors.hpp:887
The namespace for ForSyDe.
Definition: abssemantics.hpp:30
CT_in iport2
port for the input channel 2
Definition: ct_process_constructors.hpp:129
CT_in iport1
port for the input channel
Definition: ct_process_constructors.hpp:48
Process constructor for a DDE2CT MoC interfaces.
Definition: mis.hpp:437
CT_out oport1
port for the output channel
Definition: ct_process_constructors.hpp:818
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_lib.hpp:260
pif * make_pif(std::string pName, const CTTYPE &kp, const CTTYPE &ki, const sc_time sample_period, OIf &outS, I1If &inp1S)
Helper function to construct a PI controller with fixed step size.
Definition: ct_lib.hpp:612
filter * make_integrator(std::string pName, const sc_time sample_period, OIf &outS, I1If &inp1S)
Helper function to construct an integrator.
Definition: ct_lib.hpp:518
DDE_in< T > iport1
port for the input channel
Definition: dde_process_constructors.hpp:614
DDE_out< T > oport1
port for the output channel
Definition: dde_process_constructors.hpp:888
Process constructor for a combinational process with one input and one output.
Definition: ct_process_constructors.hpp:45
Helper function to construct a coasine source.
Definition: ct_lib.hpp:37
Process constructor for a continuous-time process which multiplies its inputs.
Definition: ct_lib.hpp:290
CT::CT_in iport1
port for the input channel
Definition: mis.hpp:365
Implements the continuous-time Model of Computation.
CT_out oport1
port for the output channel
Definition: ct_process_constructors.hpp:583
add(sc_module_name name_)
Definition: ct_lib.hpp:212
square(sc_module_name name_, const sc_time &endT, const sc_time &period, const CTTYPE &highS, const CTTYPE &lowS, const double &dutyCycle=0.5)
Definition: ct_lib.hpp:124
Implements the MoC interfaces between different MoCs.
filterf * make_integratorf(std::string pName, const sc_time sample_period, OIf &outS, I1If &inp1S)
Helper function to construct an integrator with fixed step size.
Definition: ct_lib.hpp:543
The CT2CT signal used to inter-connect CT processes.
Definition: ct_process.hpp:35
The CT_out port is used for output ports of CT processes.
Definition: ct_process.hpp:64
The SY2SY signal used to inter-connect SY processes.
Definition: sy_process.hpp:36
Process constructor for implementing a linear filter with fixed step size.
Definition: dde_process_constructors.hpp:884
sine(sc_module_name name_, const sc_time &endT, const sc_time &period, const CTTYPE &ampl)
Definition: ct_lib.hpp:40
Process constructor for a continuous-time process which scales the input.
Definition: ct_lib.hpp:169
CT_in iport1
port for the input channel
Definition: ct_process_constructors.hpp:817
sub * make_sub(std::string pName, OIf &outS, IIf1 &inp1S, IIf2 &inp2S)
Helper function to construct a sub process.
Definition: ct_lib.hpp:271
Process constructor for a source process.
Definition: ct_process_constructors.hpp:580
The DDE2DDE signal used to inter-connect DDE processes.
Definition: dde_process.hpp:36
cosine * make_cosine(std::string pName, const sc_time &endT, const sc_time &period, const CTTYPE &ampl, OIf &outS)
Helper function to construct a cosine source process.
Definition: ct_lib.hpp:103
sub(sc_module_name name_)
Definition: ct_lib.hpp:253
add * make_add(std::string pName, OIf &outS, IIf1 &inp1S, IIf2 &inp2S)
Helper function to construct an add process.
Definition: ct_lib.hpp:230
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_lib.hpp:219
Helper function to construct a square source.
Definition: ct_lib.hpp:121
CT::CT_out oport1
port for the output channel
Definition: mis.hpp:44
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_lib.hpp:300
Process constructor for a CT2DDE MoC interface.
Definition: mis.hpp:222
gaussian * make_gaussian(std::string pName, const double &gaussVar, const double &gaussMean, const sc_time sample_period, OIf &outS)
Helper function to construct a gaussian process.
Definition: ct_lib.hpp:364
DDE::DDE_out< T > oport1
port for the output channel
Definition: mis.hpp:227
scale * make_scale(std::string pName, const CTTYPE &scaling_factor, OIf &outS, IIf &inpS)
Helper function to construct a scale process.
Definition: ct_lib.hpp:191
sine * make_sine(std::string pName, const sc_time &endT, const sc_time &period, const CTTYPE &ampl, OIf &outS)
Helper function to construct a sine source process.
Definition: ct_lib.hpp:61
mul * make_mul(std::string pName, OIf &outS, IIf1 &inp1S, IIf2 &inp2S)
Helper function to construct a mul process.
Definition: ct_lib.hpp:311
Helper function to construct a coasine source.
Definition: ct_lib.hpp:79
DDE_out< T > oport1
port for the output channel
Definition: dde_process_constructors.hpp:615
CT::CT_in iport1
port for the input channel
Definition: mis.hpp:225
Process constructor for a Gaussian randome wave generator.
Definition: sy_lib.hpp:35
square * make_square(std::string pName, const sc_time &endT, const sc_time &period, const CTTYPE &highS, const CTTYPE &lowS, const double &dutyCycle, OIf &outS)
Helper function to construct a square source process.
Definition: ct_lib.hpp:148
Process constructor for a CT2DDEf MoC interface.
Definition: mis.hpp:362
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_lib.hpp:180
The CT_in port is used for input ports of CT processes.
Definition: ct_process.hpp:53
filterf * make_filterf(std::string pName, const std::vector< CTTYPE > numerators, const std::vector< CTTYPE > denominators, const sc_time sample_period, OIf &outS, I1If &inp1S)
Helper function to construct a linear process with fixed step size.
Definition: ct_lib.hpp:494
Process constructor for a fan-out process with one input and one output.
Definition: ct_process_constructors.hpp:814
DDE::DDE_out< T > oport1
port for the output channel
Definition: mis.hpp:366
DDE::DDE_in< unsigned int > iport2
port for the sampling channel
Definition: mis.hpp:226
SY_out< T > oport1
port for the output channel
Definition: sy_process_constructors.hpp:1095
CT_in iport1
port for the input channel 1
Definition: ct_process_constructors.hpp:128
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_lib.hpp:92
Process constructor for a SY2CT MoC interfaces.
Definition: mis.hpp:40
Process constructor for a combinational process with two inputs and one output.
Definition: ct_process_constructors.hpp:125
filter * make_filter(std::string pName, const std::vector< CTTYPE > numerators, const std::vector< CTTYPE > denominators, const sc_time sample_period, OIf &outS, I1If &inp1S)
Helper function to construct a linear process.
Definition: ct_lib.hpp:430
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_lib.hpp:137
scale(sc_module_name name_, const CTTYPE &scaling_factor)
Definition: ct_lib.hpp:172
CT_out oport1
port for the output channel
Definition: ct_process_constructors.hpp:49
Process constructor for a continuous-time process which subtracts its inputs.
Definition: ct_lib.hpp:250
DDE::DDE_in< T > iport1
port for the input channel
Definition: mis.hpp:440
SC_MODULE(gaussian)
Process constructor for a Gaussian randome wave generator.
Definition: ct_lib.hpp:330
SY::SY_in< CTTYPE > iport1
port for the input channel
Definition: mis.hpp:43
CT::CT_out oport1
port for the output channel
Definition: mis.hpp:441
CT_out oport1
port for the output channel
Definition: ct_process_constructors.hpp:130
DDE_out< unsigned int > oport2
port for the sampling signal
Definition: dde_process_constructors.hpp:616
Process constructor for implementing a linear filter.
Definition: dde_process_constructors.hpp:611