CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
vlib.cc
Go to the documentation of this file.
1 // Alexander Madorsky, University of Florida/Physics.
2 
3 
5 
7 
8 char const* obnames[] =
9 {
10  "none " ,
11  "reg " ,
12  "wire " ,
13  "input " ,
14  "output " ,
15  "inout " ,
16  "num " ,
17  "temp "
18 };
19 
20 
21 #define dbgmsg(a) std::cerr << a << " Set breakpoint at " << __FILE__ << ":" << __LINE__ << "\n";
22 
23 // Signal class --------------------------------------------------------------------------
24 
26 {
27 #ifdef VGEN
28  name = "";
29  orname = "";
30  obname = "";
31  lb = "(";
32  rb = ")";
33 #endif
34 
35  outhost = host = outreg = ca1 = ca2 = NULL;
36  inited = printable = r = rc = l = pedge = nedge = change = alwaysn = mode = 0;
37  h = 8*Sizeofrval - 1;
38  mask = (rval)(- 1);
39  hostl = -1;
40 
41 }
42 
44 {
45  create();
46 }
47 
49 {
50  create();
51 #ifdef VGEN
52  ostringstream sval;
53  sval << dec << bits << "'d" << value;
54  Signal::init(bits - 1, 0, sval.str().c_str());
55 #else
56  Signal::init(bits - 1, 0, "");
57 #endif
58  r = rc = value & mask;
59 }
60 
61 Signal::Signal(const char* sval)
62 {
63  create();
64  mode = mnum;
65  std::string val = sval;
66  int bits;
67  unsigned int i;
68  char radix;
69  rval value = 0;
70  int dig;
71 
72  sscanf(val.c_str(), "%d'%c", &bits, &radix);
73  switch (radix)
74  {
75  case 'h':
76  case 'H':
77 // sscanf (val.c_str(), "%d'%c%x", &bits, &radix, &value);
78  for (i = 0; val[i] != 'h' && val[i] != 'H'; ++i);
79  for (; i < val.length(); ++i)
80  {
81  switch (val[i])
82  {
83  case '0':
84  case '1':
85  case '2':
86  case '3':
87  case '4':
88  case '5':
89  case '6':
90  case '7':
91  case '8':
92  case '9':
93  dig = val[i] - '0';
94  break;
95  case 'a':
96  case 'b':
97  case 'c':
98  case 'd':
99  case 'e':
100  case 'f':
101  dig = val[i] - 'a' + 10;
102  break;
103  case 'A':
104  case 'B':
105  case 'C':
106  case 'D':
107  case 'E':
108  case 'F':
109  dig = val[i] - 'A' + 10;
110  break;
111  default:
112  dig = -1;
113  break;
114  }
115  if (dig >= 0)
116  {
117  value = value << 4;
118  value = value | dig;
119  }
120  }
121 
122 
123  break;
124  case 'd':
125  case 'D':
126  sscanf (val.c_str(), "%d'%c%d", &bits, &radix, reinterpret_cast<int *>(&value));
127  break;
128  case 'o':
129  case 'O':
130 // sscanf (val.c_str(), "%d'%c%o", &bits, &radix, &value);
131  for (i = 0; val[i] != 'o' && val[i] != 'O'; ++i);
132  for (; i < val.length(); ++i)
133  {
134  switch (val[i])
135  {
136  case '0':
137  case '1':
138  case '2':
139  case '3':
140  case '4':
141  case '5':
142  case '6':
143  case '7':
144  dig = val[i] - '0';
145  break;
146  default:
147  dig = -1;
148  break;
149  }
150  if (dig >= 0)
151  {
152  value = value << 3;
153  value = value | dig;
154  }
155  }
156 
157  break;
158 
159  case 'b':
160  case 'B':
161 
162  for (i = 0; val[i] != 'b' && val[i] != 'B'; ++i);
163  for (; i < val.length(); ++i)
164  {
165  switch (val[i])
166  {
167  case '0':
168  value = value << 1;
169  break;
170  case '1':
171  value = value << 1;
172  value = value | 1;
173  break;
174  }
175  }
176  break;
177  }
178  Signal::init(bits - 1, 0, val.c_str());
179  rc = value & mask;
180  r = rc;
181 
182 }
183 
185 {
186  create();
187  mode = mnum;
188 #ifdef VGEN
189  ostringstream ln;
190  ln << dec << n;
191  init (8*Sizeofrval - 1, 0, ln.str().c_str());
192 #else
193  init (8*Sizeofrval - 1, 0, "");
194 #endif
195  inited = 0;
196  rc = n;
197  r = n;
198 }
199 
201 {
202  create();
203  mode = mnum;
204 #ifdef VGEN
205  ostringstream ln;
206  ln << dec << n;
207  init (Sizeofrval * 8 - 1, 0, ln.str().c_str());
208 #else
209  init (Sizeofrval * 8 - 1, 0, "");
210 #endif
211  inited = 0;
212  r = (rval)n;
213  rc = r;
214 }
215 
216 Signal::Signal(unsigned int n)
217 {
218  create();
219  mode = mnum;
220 #ifdef VGEN
221  ostringstream ln;
222  ln << dec << n;
223  init (Sizeofrval * 8 - 1, 0, ln.str().c_str());
224 #else
225  init (Sizeofrval * 8 - 1, 0, "");
226 #endif
227  inited = 0;
228  r = (rval)n;
229  rc = r;
230 }
231 
233 {
234  mask = (1LL << (h - l + 1)) - 1LL;
235  if (mask == 0LL) mask = ~mask;
236 }
237 
238 
239 void Signal::init (int high, int low, const char* rname)
240 {
241 
242 #ifdef VGEN
243  name = rname;
244  orname = rname;
245  lb = "";
246  rb = "";
247 #endif
248  if (!inited)
249  {
250  h = high;
251  l = low;
252  makemask();
253  inited = 1;
254  }
255  source = this;
256 }
257 
258 void Signal::init(Signal* shost, int high, int low, const char* rname)
259 {
260  host = shost;
261 #ifdef VGEN
262  name = rname;
263  orname = rname;
264  lb = "";
265  rb = "";
266 #endif
267  h = high;
268  l = low;
269  makemask();
270  source = this;
271  if (host)
272  {
273  rc = (host->getr() >> (l - host->getl())) & mask;
274  r = rc;
275  }
276  change = pedge = nedge = 0;
277 }
278 
279 #ifdef VGEN
280 string& Signal::getcatname()
281 {
282  if (lb[0] == '{')
283  catname = lb + name + rb;
284  else
285  catname = name;
286  return catname;
287 }
288 #endif
289 
290 // unary operators -------------------------------------
291 
292 #ifdef VGEN
293 #define unop(op,cop) \
294 Signal Signal::op () \
295 { \
296  Signal t; \
297  t.name = #cop + lb + name + rb; \
298  printable = 0; \
299  t.r = mask & (cop getval()); \
300  return t; \
301 }
302 #else
303 #define unop(op,cop) \
304 Signal Signal::op () \
305 { \
306  Signal t; \
307  t.init(h, l, ""); \
308  t.r = mask & (cop getval()); \
309  return t; \
310 }
311 #endif
312 
313 unop (operator!,!)
315 
316 Signal* Signal::operator& ()
317 {
318 #ifdef VGEN
319  printable = 0;
320 #endif
321  return this;
322 }
323 
324 
325 // binary operators ------------------------------------
326 #ifdef VGEN
327 #define binop(op,cop) \
328 Signal Signal::op (Signal arg) \
329 { \
330  Signal t; \
331  t.name = lb + name + rb + " " + #cop + " " + arg.lb + arg.name + arg.rb; \
332  printable = arg.printable = 0; \
333  t.r = (getval() cop arg.getval()); \
334  return t; \
335 }
336 #else
337 #define binop(op,cop) \
338 Signal Signal::op (Signal arg) \
339 { \
340  Signal t; \
341  int ln = h - l; \
342  int aln = arg.h - arg.l; \
343  t.init((ln > aln) ? ln : aln, 0, ""); \
344  t.r = t.mask & (getval() cop arg.getval()); \
345  return t; \
346 }
347 #endif
348 
349 binop (operator+,+)
351 binop (operator*,*)
352 binop (operator/,/)
353 binop (operator%,%)
354 binop (operator^,^)
355 binop (operator<<,<<)
356 binop (operator>>,>>)
357 binop (operator&,&)
358 binop (operator&&,&&)
359 binop (operator|,|)
360 
361 
362 Signal Signal::operator|| (Signal arg)
363 {
364  Signal t;
365 #ifdef VGEN
366  t.name = lb + name + rb + " || " + arg.lb + arg.name + arg.rb;
367  t.orname = orname + " or " + arg.orname;
368  printable = arg.printable = 0;
369 #else
370  int ln = h - l;
371  int aln = arg.h - arg.l;
372  t.init((ln > aln) ? ln : aln, 0, "");
373 #endif
374  t.r = t.mask & (getval() || arg.getval());
375  t.change = change || arg.change;
376  return t;
377 }
378 
380 {
381  Signal t;
382 #ifdef VGEN
383  t.name = name + ", " + arg.name;
384  t.lb = "{";
385  t.rb = "}";
386  printable = arg.printable = 0;
387 #else
388  t.ca1 = this;
389  t.ca2 = arg.source;
390  t.init(h - l + arg.h - arg.l + 1, 0, "");
391 #endif
392  t.r = (((getval() << (arg.h - arg.l + 1)) & (~(arg.mask))) | arg.getval()) & t.mask;
393  return t;
394 }
395 
396 // comparison operators ---------------------------------------------------
397 
398 #ifdef VGEN
399 #define compop(op,cop) \
400 Signal Signal::op (Signal arg) \
401 { \
402  Signal t; \
403  t.name = lb + name + rb + " " + #cop + " " + arg.lb + arg.name + arg.rb; \
404  printable = arg.printable = 0; \
405  t.r = (getval() cop arg.getval()); \
406  return t; \
407 }
408 #else
409 #define compop(op,cop) \
410 Signal Signal::op (Signal arg) \
411 { \
412  Signal t; \
413  t.init(""); \
414  t.r = (getval() cop arg.getval()); \
415  return t; \
416 }
417 #endif
418 
419 compop (operator>,>)
421 compop (operator<=,<=)
422 compop (operator>=,>=)
423 compop (operator==,==)
424 compop (operator!=,!=)
425 
426 // reduction operators --------------------------------------------------------------
428 {
429  Signal t;
430  rval tr;
431 #ifdef VGEN
432  t.name = "|" + arg.lb + arg.name + arg.rb;
433  arg.printable = 0;
434 #else
435  t.init("");
436 #endif
437  tr = (arg.getval()) & arg.mask;
438  t.r = (tr != 0) ? 1 : 0;
439  return t;
440 }
441 
443 {
444  Signal t;
445  rval tr;
446 #ifdef VGEN
447  t.name = "&" + arg.lb + arg.name + arg.rb;
448  arg.printable = 0;
449 #else
450  t.init("");
451 #endif
452  tr = (arg.getval()) & arg.mask;
453  t.r = (tr == arg.mask) ? 1 : 0;
454  return t;
455 }
456 
458 {
459  Signal t;
460  rval tr;
461  int i;
462 #ifdef VGEN
463  t.name = "^" + arg.lb + arg.name + arg.rb;
464  arg.printable = 0;
465 #else
466  t.init("");
467 #endif
468  tr = (arg.getval()) & arg.mask;
469  t.r = 0;
470  for (i = 0; i < arg.h-arg.l+1; ++i)
471  {
472  t.r = ((tr & 1) != 0) ? !t.r : t.r;
473  tr = tr >> 1;
474  }
475  t.r = t.r & 1;
476  return t;
477 }
478 
479 // blocking assignment operator -----------------------------------------------------
480 
482 {
483  return (getalwaysn() == glc.getalwaysn()) ? rc : r;
484 }
485 
487 {
488 
489 #ifndef VGEN
490 #ifdef _VDEBUG
491  switch (mode)
492  {
493  case moutput:
494  if (glc.getalwaysn() == -1 && outreg != NULL)
495  dbgmsg("Assigning output-reg outside always block.");
496  if (glc.getalwaysn() != -1 && outreg == NULL)
497  dbgmsg("Assigning non-reg output inside always block.");
498  break;
499 
500  case minout:
501  if (glc.getalwaysn() != -1)
502  dbgmsg("Assigning inout inside always block.");
503  break;
504 
505  case minput:
506  dbgmsg("Assigning to input is not allowed.");
507  return other;
508 
509  case mreg:
510  if (glc.getalwaysn() == -1)
511  dbgmsg("Assigning reg outside always block.");
512  break;
513 
514  case mwire:
515  if (glc.getalwaysn() != -1)
516  dbgmsg("Assigning wire inside always block.");
517  break;
518  }
519 #endif
520 #endif
521  return asgn(other);
522 
523 }
524 
526 {
527 #ifdef VGEN
528  glc.setprintassign(0);
529 #endif
530  return asgn(other);
531 }
532 
534 {
535  Signal t;
536 
537 #ifdef VGEN
538  t.name = lb + name + rb + " = " + other.getcatname();
539  if (glc.printassign()) std::cout << glc.getmargin() << t.name << ";\n" << flush;
540 #endif
541  rval hr, portionr, portionmask, otr;
542  int shn;
543 
544  otr = other.getval() & mask;
545 
546  if (otr != r)
547  {
548  setalwaysn (glc.getalwaysn());
549  glc.setchange(1);
550  }
551 
552  rc = otr;
553  if (host) // is this a portion of the other register?
554  {
555  hr = host->rc;
556  if (hostl > 0) shn = (hostl - host->getl());
557  else shn = (l - host->getl());
558  portionr = rc << shn;
559  portionmask = mask << shn;
560  host->set((hr & (~portionmask)) | portionr);
561  }
562 
563  if (ca1 != NULL && ca2 != NULL) // is this a concatenation of the two registers?
564  {
565  ca2->set(other);
566  ca1->set(other >> (ca2->h - ca2->l + 1));
567  }
568  if (outhost != NULL && outhost != this && (mode == moutput || mode == minout))
569  {
570  outhost->set(other);
571  }
572  if (outreg)
573  {
574  outreg->set(other);
575  }
576 
577  t.h = h;
578  t.l = l;
579  t.mask = mask;
580  t.pedge = pedge;
581  t.nedge = nedge;
582  t.change = change;
583  t.r = rc;
584 #ifdef VGEN
585  glc.setprintassign(1);
586 #endif
587  return t;
588 }
589 
590 
591 // bit selection operator -----------------------------------------------
592 
594 {
595  Signal t;
596 #ifdef VGEN
597  string bname;
598  if (hn.getname().compare(ln.getname()) != 0) bname = name + "[" + hn.getname() + ":" + ln.getname() + "]";
599  else bname = name + "[" + hn.getname() + "]";
600  hn.printable = ln.printable = 0;
601  t.init(this, 0, 0, bname.c_str());
602 #else
603  t.init(this, (unsigned)hn.getval(), (unsigned)ln.getval(), "");
604 #endif
605  t.rc = (getval() >> (t.l - l)) & t.mask;
606  t.r = t.rc;
607  return t;
608 }
609 
611 {
612  return (*this)(n, n);
613 }
614 
615 // insertion operator ---------------------------------------------------
616 std::ostream &operator<< (std::ostream &stream, Signal s)
617 {
618  stream << s.getr();
619  s.setprintable(0);
620  return stream;
621 }
622 
623 
624 // input -----------------------------------------------------------------------------
625 void Signal::input(int high, int low, const char* rname)
626 {
627 #ifdef VGEN
628  if (lb == "{") glc.AddIO(lb + name + rb);
629  else glc.AddIO(name);
630 #else
631 #ifdef _VDEBUG
632  if (h-l != high-low && inited) // inited is analysed in case the passed parameter is a temp var. Actually, every operator should return temp with the proper mask,h,l inherited from operands
633  {
634  dbgmsg("Different port length for input argument: declared [" << high << ":" << low << "], passed: [" << h << ":" << l <<"]. ");
635  }
636 #endif
637 #endif
638  Signal::init(high, low, rname);
639  if (high >= low)
640  {
641  h = high;
642  l = low;
643  }
644  else
645  {
646  h = low;
647  l = high;
648  }
649  mode = minput;
650 #ifdef VGEN
651  obname = obnames[mode];
652  ostringstream ln;
653  glc.AddParameter(name);
654  if (h == l) ln << obname << name << ";\n";
655  else ln << obname << "[" << dec << h << ":" << l << "] " << name << ";\n";
656  glc.AddDeclarator(ln.str());
657  printable = 0;
658 #else
659  outreg = glc.getparent()->AddOutReg((Signal)(*this));
660  r = r & outreg->getmask();
661  if (outreg->getr() != r)
662  {
663  change = 1;
664  glc.getparent()->setchange(1);
665  if (r == 1 && outreg->getr() == 0) pedge = 1; else pedge = 0;
666  if (r == 0 && outreg->getr() == 1) nedge = 1; else nedge = 0;
667  }
668  else change = pedge = nedge = 0;
669  outreg->setr(r);
670  outreg->setrc(r);
671  outhost = host = NULL;
672 #endif
673 
674 }
675 
676 // clock input --------------------------------------------------------------
677 void Signal::clock(const char* rname)
678 {
679 #ifdef VGEN
680  if (lb == "{") glc.AddIO(lb + name + rb);
681  else glc.AddIO(name);
682 #else
683 #ifdef _VDEBUG
684  if (h-l != 0 && inited) // inited is analysed in case the passed parameter is a temp var. Actually, every operator should return temp with the proper mask,h,l inherited from operands
685  {
686  dbgmsg("Different port length for clock argument: declared [" << 0 << ":" << 0 << "], passed: [" << h << ":" << l <<"]. ");
687  }
688 #endif
689 #endif
690  Signal::init(0, 0, rname);
691  h = l = 0;
692  mode = minput;
693 #ifdef VGEN
694  obname = obnames[mode];
695  ostringstream ln;
696  glc.AddParameter(name);
697  if (h == l) ln << obname << name << ";\n";
698  else ln << obname << "[" << dec << h << ":" << l << "] " << name << ";\n";
699  glc.AddDeclarator(ln.str());
700  printable = 0;
701 #else
702  outreg = glc.getparent()->AddOutReg((Signal)(*this));
703  if (rc != r)
704  {
705  change = 1;
706  glc.getparent()->setchange(1);
707  if (rc == 1 && r == 0) pedge = 1; else pedge = 0;
708  if (rc == 0 && r == 1) nedge = 1; else nedge = 0;
709  }
710  else change = pedge = nedge = 0;
711  outreg->setr(r);
712  outreg->setrc(r);
713  outhost = host = NULL;
714 #endif
715 
716 }
717 
718 
719 
720 // output -----------------------------------------------------------------------------
721 
722 void Signal::output(int high, int low, const char* rname)
723 {
724 #ifdef VGEN
725  if (lb == "{") glc.AddIO(lb + name + rb);
726  else glc.AddIO(name);
727 #else
728 #ifdef _VDEBUG
729  if (h-l != high-low)
730  {
731  dbgmsg("Different port length for output argument: declared [" << high << ":" << low << "], passed: [" << h << ":" << l <<"]. ");
732  }
733  if (mode == mreg)
734  {
735  dbgmsg("Using reg as output.");
736  }
737 #endif
738 #endif
739  hostl = l;
740  Signal::init(high, low, rname);
741  if (high >= low)
742  {
743  h = high;
744  l = low;
745  }
746  else
747  {
748  h = low;
749  l = high;
750  }
751  mode = moutput;
752 #ifdef VGEN
753  obname = obnames[mode];
754  ostringstream ln;
755  glc.AddParameter(name);
756  if (h == l) ln << obname << name << ";\n";
757  else ln << obname << "[" << dec << h << ":" << l << "] " << name << ";\n";
758  glc.AddDeclarator(ln.str());
759  printable = 0;
760 #endif
761 
762 }
763 
764 void Signal::output(int high, int low, const char* rname, module* parent)
765 {
766  output(high, low, rname);
767 #ifdef VGEN
768  ostringstream ln;
769  if (h == l) ln << "reg " << name << ";\n";
770  else ln << "reg [" << dec << h << ":" << l << "] " << name << ";\n";
771  glc.AddDeclarator(ln.str());
772 #else
773  outreg = parent->AddOutReg((Signal)(*this));
774  setr(outreg->getr());
778 #endif
779 }
780 
781 void Signal::output(const char* rname, module* parent)
782 {
783  output(0, 0, rname, parent);
784 }
785 
786 // inout -----------------------------------------------------------------------------
787 
788 void Signal::inout(int high, int low, const char* rname)
789 {
790 #ifdef VGEN
791  if (lb == "{") glc.AddIO(lb + name + rb);
792  else glc.AddIO(name);
793 #else
794 #ifdef _VDEBUG
795  if (h-l != high-low)
796  {
797  dbgmsg("Different port length for inout argument: declared [" << high << ":" << low << "], passed: [" << h << ":" << l <<"]. ");
798  }
799 #endif
800 #endif
801  hostl = l;
802  Signal::init(high, low, rname);
803  if (high >= low)
804  {
805  h = high;
806  l = low;
807  }
808  else
809  {
810  h = low;
811  l = high;
812  }
813  mode = minout;
814 #ifdef VGEN
815  obname = obnames[mode];
816  ostringstream ln;
817  glc.AddParameter(name);
818  if (h == l) ln << obname << name << ";\n";
819  else ln << obname << "[" << dec << h << ":" << l << "] " << name << ";\n";
820  glc.AddDeclarator(ln.str());
821  printable = 0;
822 #endif
823 
824 }
825 
826 // reg -------------------------------------------------------------------------------
827 
828 void Signal::reg(int high, int low, const char* rname)
829 {
830  initreg(high, low, rname);
831 #ifdef VGEN
832  if (h == l) cout << glc.getmargin() << obname << name << ";\n" << flush;
833  else std::cout << glc.getmargin() << obname << "[" << dec << h << ":" << l << "] "<< name << ";\n" << flush;
834 #endif
835 }
836 
837 void Signal::initreg(int high, int low, const char* rname)
838 {
839  Signal::init(high, low, rname);
840  mode = mreg;
841 #ifdef VGEN
842  obname = obnames[mode];
843 #endif
844  change = (r != rc);
845  if (change) glc.getparent()->setchange(1);
846  pedge = (rc == 1 && r == 0);
847  nedge = (rc == 0 && r == 1);
848  r = rc;
849 }
850 
851 // parameter class -------------------------------------------------------------------------------
852 
853 parameter::parameter (const char* rname, Signal arg) : Signal()
854 {
855 #ifdef VGEN
856  obname = "parameter ";
857 #endif
858  init(Sizeofrval * 8 - 1, 0, rname);
859  operator=(arg);
860 }
861 
862 
863 void parameter::init (int h, int l, const char* rname)
864 {
865  Signal::init(h, l, rname);
866  change = pedge = nedge = 0;
867 
868 #ifdef VGEN
869  cout << glc.getmargin() << obname << name << flush;
870 #endif
871 }
872 
874 {
875  r = arg.getr();
876 #ifdef VGEN
877  cout << " = " << arg.getname() << ";\n" << flush;
878 #endif
879 }
880 
881 
882 
883 // wire -------------------------------------------------------------------------------
884 
885 void Signal::wire(int high, int low, const char* rname)
886 {
887  Signal::init(high, low, rname);
888  mode = mwire;
889  outhost = this;
890  change = (r != rc);
891  if (change && glc.getparent()) glc.getparent()->setchange(1);
892  pedge = (rc == 1 && r == 0);
893  nedge = (rc == 0 && r == 1);
894  r = rc;
895 #ifdef VGEN
896  obname = obnames[mode];
897  if (h == l) cout << glc.getmargin() << obname << name << ";\n" << flush;
898  else std::cout << glc.getmargin() << obname << "[" << dec << h << ":" << l << "] "<< name << ";\n" << flush;
899 #endif
900 }
901 
902 void Signal::wire(int high, int low, const char* rname, int i)
903 {
904 #ifdef VGEN
905  ostringstream instnamestream;
906  instnamestream << rname << dec << i;
907 // init(high, low, instnamestream.str().c_str());
908  wire(high, low, instnamestream.str().c_str());
909 #else
910  wire(high, low, rname);
911 #endif
912 }
913 
914 
915 // memory class -----------------------------------------------------------------------------
916 
917 void memory::reg (int high, int low, int nup, int ndown, const char* rname)
918 {
919  int i;
920  if (nup > ndown)
921  {
922  up = nup;
923  down = ndown;
924  }
925  else
926  {
927  up = ndown;
928  down = nup;
929  }
930  if (r == NULL)
931  {
932  r = new Signal [up - down + 1];
933  for (i = 0; i <= up - down; ++i)
934  {
935  r[i].initreg(high, low, "");
936  r[i].setr(0);
937  }
938  }
939  else
940  {
941  for (i = 0; i <= up - down; ++i)
942  {
943  r[i].initreg(high, low, "");
944  }
945  }
946 #ifdef VGEN
947  name = rname;
948  if (high == low) std::cout << glc.getmargin() << "reg " << name << " [" << dec << up << ":" << down << "]" << ";\n" << flush;
949  else std::cout << glc.getmargin() << "reg " << "[" << dec << high << ":" << low << "] "<< name << " [" << dec << up << ":" << down << "]" << ";\n" << flush;
950 #endif
951 }
952 
954 {
955  if (r != NULL) delete [] r;
956  r = NULL;
957 }
958 
959 #ifdef VGEN
961 #else
963 #endif
964 {
965 #ifdef VGEN
966  string ln;
967  ln = name + "[" + i.getname() + "]";
968  r[0].setname(ln);
969  r[0].setorname(ln);
970  return r[0];
971 #else
972  rval ind = i.getval();
973 #ifdef _VDEBUG
974  if (ind < down || ind > up)
975  {
976  dbgmsg("Memory index out of range: index: " << ind << ", range: [" << up << ":" << down << "]. ");
977  return r[down];
978  }
979  else
980 #endif
981  return r[ind - down];
982 #endif
983 }
984 
985 // module class -------------------------------------------------------------------------
986 
988 {
989  for (unsigned int i = 0; i < sizeof(outreg)/sizeof(Signal*); ++i) outreg[i] = NULL;
990  outregn = 0;
991  runperiod = NULL;
992 }
993 
995 {
996  create();
997 }
998 
1000 {
1001  for (unsigned int i = 0; i < sizeof(outreg)/sizeof(Signal*); ++i)
1002  {
1003  if (outreg[i] != NULL)
1004  delete outreg[i];
1005  }
1006 
1007 }
1008 
1009 void module::init (const char* mname, const char* iname)
1010 {
1011 #ifdef VGEN
1012  name = mname;
1013  instname = iname;
1014 #endif
1015 }
1016 
1017 void module::init (const char* mname, const char* iname, module* fixt)
1018 {
1019 #ifdef VGEN
1020  name = mname;
1021  instname = iname;
1022 #endif
1023  tfixt = fixt;
1024 }
1025 
1026 
1027 void module::init (const char* mname, const char* iname, int index)
1028 {
1029 #ifdef VGEN
1030  name = mname;
1031  ostringstream instnamestream;
1032  instnamestream << iname << dec << index;
1033  instname = instnamestream.str().c_str();
1034 #endif
1035 }
1036 
1037 #ifdef VGEN
1038 void module::PrintHeader()
1039 {
1040  char *username = NULL;
1041  struct tm *newtime;
1042  time_t aclock;
1043  time( &aclock );
1044  newtime = localtime( &aclock );
1045  username = getenv("USER");
1046  if (username == NULL || strlen(username) < 2) username = getenv("USERNAME");
1047  if (username == NULL || strlen(username) < 2) username = getenv("LOGNAME");
1048  cout << "// This Verilog HDL source file was automatically generated" << std::endl;
1049  cout << "// by C++ model based on VPP library. Modification of this file" << std::endl;
1050  cout << "// is possible, but if you want to keep it in sync with the C++" << std::endl;
1051  cout << "// model, please modify the model and re-generate this file." << std::endl;
1052  cout << "// VPP library web-page: http://www.phys.ufl.edu/~madorsky/vpp/" << std::endl;
1053  cout << std::endl;
1054  if (username != NULL)
1055  cout << "// Author : " << username << std::endl;
1056  cout << "// File name : " << name << ".v" << std::endl;
1057  cout << "// Timestamp : " << asctime(newtime) << std::endl << flush;
1058 
1059 }
1060 #endif
1061 
1063 {
1064  switchn = 0;
1065 #ifdef VGEN
1066  string filename = name + ".v";
1067  cout << glc.getmargin() << name << " " << instname << std::endl << flush;
1068  cout << glc.getmargin() << "(" << glc.PrintIO(true).c_str() << std::endl << flush;
1069  cout << glc.getmargin() << ");\n" << flush;
1070  vfile.open (filename.c_str());
1071  outbuf = std::cout.rdbuf(vfile.rdbuf());
1072  OuterIndPos = glc.getpos();
1073  oldenmarg = glc.getenablemargin();
1074  glc.enablemargin(1);
1075  glc.setpos(0);
1076  PrintHeader();
1077  cout << glc.getmargin() << "module " << name << std::endl << glc.getmargin() << "(" << flush;
1078  glc.setfunction(0);
1079  glc.Print();
1080  glc.setFileOpen(1);
1081 #endif
1082 }
1083 
1085 {
1086 #ifdef VGEN
1087  glc.Outdent();
1088  cout << glc.getmargin() << "endmodule\n" << flush;
1089  glc.setpos(OuterIndPos);
1090  cout.rdbuf(outbuf);
1091  vfile.close();
1092  glc.enablemargin(oldenmarg);
1093 #endif
1094  outregn = 0;
1095 }
1096 
1098 {
1099  Signal t;
1100 #ifdef VGEN
1101  string ln = "";
1102  ln = "posedge " + arg.getname();
1103  t.init(NULL, 0, 0, ln.c_str());
1104 #else
1105  t.init(NULL, 0, 0, "");
1106 #endif
1107  if (arg.getposedge()) glc.setce(0);
1108  t.setchange(arg.getposedge());
1109  return t;
1110 }
1111 
1113 {
1114  Signal t;
1115 #ifdef VGEN
1116  string ln = "";
1117  ln = "negedge " + arg.getname();
1118  t.init(NULL, 0, 0, ln.c_str());
1119 #else
1120  t.init(NULL, 0, 0, "");
1121 #endif
1122  if (arg.getnegedge()) glc.setce(0);
1123  t.setchange(arg.getnegedge());
1124  return t;
1125 }
1126 
1127 
1129 {
1130  if (outreg[outregn] == NULL)
1131  {
1132  outreg[outregn] = new Signal;
1133  outreg[outregn]->setr(0);
1134  }
1135  outreg[outregn]->reg(arg.geth(), arg.getl(), "");
1136  outregn++;
1137  return outreg[outregn - 1];
1138 }
1139 
1140 Signal module::ifelse(Signal condition, Signal iftrue, Signal iffalse)
1141 {
1142 
1143 #ifdef VGEN
1144  Signal t;
1145  string ln;
1146  ln = "(" + condition.getname() + ") ? " + iftrue.getname() + " : " + iffalse.getname();
1147  t.setname(ln);
1148  return t;
1149 #else
1150  if (condition.getbool()) return iftrue;
1151  else return iffalse;
1152 #endif
1153 
1154 }
1155 
1156 // function class -------------------------------------------------------------
1157 
1158 void function::makemask(int hpar, int lpar)
1159 {
1160  //int i;
1161  unsigned int lng = hpar - lpar + 1;
1162 
1163  if (lng < Sizeofrval * 8)
1164  mask = (1LL << lng) - 1;
1165  else
1166  mask = (rval)(-1);
1167 }
1168 
1169 void function::init(int high, int low, const char* rname)
1170 {
1171 #ifdef VGEN
1172  name = rname;
1173  cout << "`include " << '"' <<name << ".v" << '"' << "\n" << flush;
1174 #endif
1175  if (high >= low)
1176  {
1177  h = high;
1178  l = low;
1179  }
1180  else
1181  {
1182  h = low;
1183  l = high;
1184  }
1185  makemask(h, l);
1186 }
1187 
1189 {
1190 #ifdef VGEN
1191  string filename = name + ".v";
1192  retname = name + "(" + glc.PrintIO(false) + ")";
1193  vfile.open (filename.c_str());
1194  outbuf = std::cout.rdbuf(vfile.rdbuf());
1195  OuterIndPos = glc.getpos();
1196  oldenmarg = glc.getenablemargin();
1197  glc.enablemargin(1);
1198  glc.setpos(0);
1199  PrintHeader();
1200  cout << glc.getmargin() << "function [" << dec << h << ":" << l << "] " << name << ";\n" << flush;
1201  glc.setfunction(1);
1202  glc.Print();
1203  glc.setFileOpen(1);
1204  result.setname(name);
1205  result.setbrackets("", "");
1206 #endif
1207  switchn = 0;
1208  OldChange = glc.getchange();
1209 }
1210 
1212 {
1213 
1214  result.sethlmask(h, l, mask);
1215  result.setr(result.getr() & mask);
1216  glc.setchange(OldChange);
1217 #ifdef VGEN
1218  glc.Outdent();
1219  cout << glc.getmargin() << "endfunction\n" << flush;
1220  glc.setpos(OuterIndPos);
1221  cout.rdbuf(outbuf);
1222  result.setname(retname);
1223  result.setbrackets("", "");
1224  vfile.close();
1225  glc.enablemargin(oldenmarg);
1226 #endif
1227  outregn = 0;
1228 }
1229 
1230 // globcontrol class --------------------------------------------------------------------
1231 
1233 {
1234 #ifdef VGEN
1235  nomargin = 0;
1236  ndio = 0;
1237  npar = 0;
1238  ndecl = 0;
1239  indpos = 0;
1240  pa = 1;
1241  zeromargin = "";
1242  VFileOpen = 0;
1243 #endif
1244  alwayscnt = -1;
1245  alwaysn = 1;
1246  change = 0;
1247 }
1248 
1249 #ifdef VGEN
1250 void globcontrol::Print()
1251 {
1252  int i;
1253 
1254 
1255  if (functiondecl == 0)
1256  {
1257  Indent();
1258  for (i = 0; i < npar; ++i)
1259  {
1260  cout << std::endl;
1261  cout << getmargin();
1262  cout << pars[i];
1263  if (i != npar-1) std::cout << ",";
1264  }
1265  Outdent();
1266  cout << std::endl << getmargin() << ");\n";
1267  }
1268  Indent();
1269  cout << "\n";
1270 
1271  for (i = 0; i < ndecl; ++i)
1272  {
1273  cout << glc.getmargin() << decls[i];
1274  }
1275  npar = ndecl = 0;
1276  cout << "\n" << flush;
1277 
1278 }
1279 
1280 
1281 string& globcontrol::PrintIO(bool col)
1282 {
1283  int i;
1284 
1285  if (col) Indent();
1286  outln = "";
1287  if (ndio > 0)
1288  {
1289  for (i = 0; i < ndio; ++i)
1290  {
1291  if (col)
1292  {
1293  outln += "\n";
1294  outln += getmargin();
1295  }
1296  outln += dios[i];
1297  if (i < ndio - 1) outln += ",";
1298  }
1299  }
1300  ndio = 0;
1301  if (col) Outdent();
1302  return outln;
1303 }
1304 
1305 void globcontrol::PrepMargin()
1306 {
1307 
1308  int i;
1309  margin = "";
1310  for (i = 0; i < indpos; ++i)
1311  {
1312  margin += " ";
1313  }
1314 }
1315 
1316 
1317 void globcontrol::AddIO(std::string ln)
1318 {
1319  dios[ndio] = ln;
1320  ndio++;
1321 }
1322 #endif
1323 
1325 {
1326  change = i;
1327 }
1328 
1330 {
1331  sprintf(constring, "%d%s", bits, value);
1332  return constring;
1333 }
1334 
1336 {
1337  sprintf(constring, "%d'd%u", bits, (unsigned)value);
1338  return constring;
1339 }
Definition: BitonicSort.h:8
int i
Definition: DBlmapReader.cc:9
globcontrol()
Definition: vlib.cc:1232
#define unop(op, cop)
Definition: vlib.cc:303
rval mask
Definition: vlib.h:152
rval r
Definition: vlib.h:144
void init(int, int, const char *)
Definition: vlib.cc:239
void reg(int, int, const char *)
Definition: vlib.cc:828
Signal * outhost
Definition: vlib.h:165
int l
Definition: vlib.h:268
void setr(rval rv)
Definition: vlib.h:66
int pedge
Definition: vlib.h:163
void reg(int, int, int, int, const char *)
Definition: vlib.cc:917
void makemask(int hpar, int lpar)
Definition: vlib.cc:1158
module()
Definition: vlib.cc:994
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
void sethlmask(int high, int low, rval imask)
Definition: vlib.h:68
void setnegedge(int ch)
Definition: vlib.h:76
Signal operator=(Signal)
Definition: vlib.cc:486
Signal set(Signal)
Definition: vlib.cc:525
int change
Definition: vlib.h:342
Definition: vlib.h:34
parent
Definition: confdb.py:1052
int down
Definition: vlib.h:204
rval getval()
Definition: vlib.cc:481
Signal operator()(Signal, Signal)
Definition: vlib.cc:593
Signal rxor(Signal arg)
Definition: vlib.cc:457
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision bits
Signal * ca1
Definition: vlib.h:161
void wire(int, int, const char *)
Definition: vlib.cc:885
int getalwaysn()
Definition: vlib.h:303
#define NULL
Definition: scimark2.h:8
Signal result
Definition: vlib.h:271
int hostl
Definition: vlib.h:151
Signal negedge(Signal)
Definition: vlib.cc:1112
void makemask()
Definition: vlib.cc:232
char const * obnames[]
Definition: vlib.cc:8
susybsm::HSCParticle pa
Definition: classes.h:8
int h
Definition: vlib.h:268
int functiondecl
Definition: vlib.h:337
~memory()
Definition: vlib.cc:953
rval getr()
Definition: vlib.h:61
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
int inited
Definition: vlib.h:168
A arg
Definition: Factorize.h:36
void setchange(int ch)
Definition: vlib.h:74
void setrc(rval rv)
Definition: vlib.h:67
void setposedge(int ch)
Definition: vlib.h:75
void init(int, int, const char *)
Definition: vlib.cc:863
int switchn
Definition: vlib.h:243
void setprintable(int p)
Definition: vlib.h:79
void create()
Definition: vlib.cc:987
int oldenmarg
Definition: vlib.h:244
rval mask
Definition: vlib.h:269
void setchange(int c)
Definition: vlib.h:229
#define dbgmsg(a)
Definition: vlib.cc:21
Definition: vlib.h:35
int VFileOpen
Definition: vlib.h:345
int OldChange
Definition: vlib.h:270
int up
Definition: vlib.h:204
int getposedge()
Definition: vlib.h:71
void vbeginmodule()
Definition: vlib.cc:1062
int h
Definition: vlib.h:150
void init(const char *, const char *)
Definition: vlib.cc:1009
int nedge
Definition: vlib.h:163
#define Sizeofrval
Definition: vlib.h:23
int getalwaysn()
Definition: vlib.h:78
#define Signal(s)
Definition: vpp_tools.h:6
Definition: vlib.h:31
void vendfunction()
Definition: vlib.cc:1211
Definition: vlib.h:32
susybsm::HSCParticleRef hr
Definition: classes.h:26
int getchange()
Definition: vlib.h:306
void init(int, int, const char *)
Definition: vlib.cc:1169
void(* runperiod)()
Definition: vlib.h:247
void initreg(int, int, const char *)
Definition: vlib.cc:837
void create()
Definition: vlib.cc:25
Double_t margin
Signal asgn(Signal)
Definition: vlib.cc:533
int geth()
Definition: vlib.h:64
Definition: vlib.h:33
Signal ifelse(Signal, Signal, Signal)
Definition: vlib.cc:1140
#define binop(op, cop)
Definition: vlib.cc:337
int alwaysn
Definition: vlib.h:340
void vendmodule()
Definition: vlib.cc:1084
void clock(const char *rname)
Definition: vlib.cc:677
void setchange(int i)
Definition: vlib.cc:1324
char * constant(int bits, char *val)
Definition: vlib.cc:1329
globcontrol glc
Definition: vlib.cc:6
Signal operator,(Signal)
Definition: vlib.cc:379
void operator=(Signal arg)
Definition: vlib.cc:873
Signal * AddOutReg(Signal arg)
Definition: vlib.cc:1128
int printable
Definition: vlib.h:169
int l
Definition: vlib.h:150
int getnegedge()
Definition: vlib.h:72
unsigned long long int rval
Definition: vlib.h:22
int OuterIndPos
Definition: vlib.h:241
void setalwaysn(int n)
Definition: vlib.h:77
virtual ~module()
Definition: vlib.cc:999
Signal ror(Signal arg)
Definition: vlib.cc:427
int alwayscnt
Definition: vlib.h:339
int getchange()
Definition: vlib.h:73
void setfunction(int i)
Definition: vlib.h:309
void vbeginfunction()
Definition: vlib.cc:1188
module * tfixt
Definition: vlib.h:248
Signal * source
Definition: vlib.h:164
Signal * host
Definition: vlib.h:160
Signal & operator[](Signal)
Definition: vlib.cc:962
char constring[2 *32+32]
Definition: vlib.h:347
bool getbool()
Definition: vlib.h:70
module * getparent()
Definition: vlib.h:308
int setce(int c)
Definition: vlib.h:312
Definition: vlib.h:30
rval rc
Definition: vlib.h:149
void inout(int, int, const char *)
Definition: vlib.cc:788
std::string instname
Definition: vlib.h:240
Signal()
Definition: vlib.cc:43
tuple filename
Definition: lut2db_cfg.py:20
Signal * r
Definition: vlib.h:203
Signal rand(Signal arg)
Definition: vlib.cc:442
Signal * outreg[1000]
Definition: vlib.h:245
Signal * outreg
Definition: vlib.h:166
int outregn
Definition: vlib.h:246
tuple cout
Definition: gather_cfg.py:145
int alwaysn
Definition: vlib.h:167
int mode
Definition: vlib.h:170
Signal * ca2
Definition: vlib.h:162
int getl()
Definition: vlib.h:63
Signal posedge(Signal)
Definition: vlib.cc:1097
int col
Definition: cuy.py:1008
void output(int, int, const char *)
Definition: vlib.cc:722
for(const auto &isodef:isoDefs)
rval getmask()
Definition: vlib.h:65
int change
Definition: vlib.h:163
Definition: vlib.h:208
parameter(const char *rname, Signal arg)
Definition: vlib.cc:853
#define compop(op, cop)
Definition: vlib.cc:409
void input(int, int, const char *)
Definition: vlib.cc:625
Definition: vlib.h:39