Belle II Software  release-05-01-25
FTDConverter.cc
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Trigger Utility program
5 //-----------------------------------------------------------------------------
6 // Filename : FTDConverter.cc
7 // Section : TRG GDL
8 // Owner : Yoshi Iwasaki
9 // Email : yoshihito.iwasaki@kek.jp
10 //-----------------------------------------------------------------------------
11 // Description : To generate FTD algorithm file from verilog source
12 //-----------------------------------------------------------------------------
13 // Version | Date | Explanation of changes
14 // 00.00 | 16-Jan-2014 | Creation
15 //-----------------------------------------------------------------------------
16 
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 #include <map>
22 #include <string.h>
23 #include "trg/trg/Utilities.h"
24 
25 unsigned DebugLevel = 1;
26 
27 using namespace std;
28 
29 vector<string> inputs;
30 vector<unsigned> inputSizes;
31 vector<string> inputBits;
32 vector<string> inputNames;
33 vector<string> outputs;
34 map<string, string> logics;
35 vector<string> expanded;
36 vector<string> expanded2;
37 vector<string> expandedcc;
38 
39 void shrink(char b[800]);
40 void shrink(string&);
41 void cosmetic(string&);
42 void tocc(string&);
43 void chopWord(char b[800], char c[800]);
44 void chopWord(string&, string&);
45 void removeComments(char b[800]);
46 void getInput(char b[800]);
47 void getOutput(char b[800]);
48 void getLogic(char b[800]);
49 void getLogicGND(string&);
50 void tofunc(string&);
51 string breakup(const string& logic);
52 string decodeLogic(const string& logic);
53 void kumacOutput(ofstream&, string logic, string ftd);
54 
55 class Term {
56 public:
57  Term(const string&);
58  virtual ~Term();
59 
60 public:
61 // unsigned size(void) const;
62  string sentence(void) const;
63  void expand(void);
64  string expanded(void);
65 
66 private:
67  string _sentence;
68  vector<string> _words;
69  vector<Term> _terms;
70  string _expanded;
71 };
72 
73 int
74 main(int argc, char* argv[])
75 {
76  cout << "ftd: version 1.01 : 2007/09/21 Y.Iwasaki" << endl;
77 
78  //...Check arguments...
79  if (argc != 2) {
80  cout << "usage : ftd.v" << endl;
81  cout << " ftd.v : ftd logic file" << endl;
82 
83  return -1;
84  }
85 
86  //...Open file...
87  cout << "ftd ... reading verilog file : "
88  << argv[1] << endl;
89  ifstream file(argv[1], ios::in);
90  if (! file.is_open()) {
91  cout << " !!! can not open file : " << argv[1] << endl;
92  exit(-1);
93  }
94 
95  //...Read source...
96  char b[800];
97  unsigned state = 0;
98  while (! file.eof()) {
99  file.getline(b, 800);
100 
101  //...Remove unnecessary spaces...
102  shrink(b);
103 
104  if (DebugLevel) {
105  string tmp = b;
106  shrink(tmp);
107  bool tmpWrong = false;
108  for (unsigned i = 0; i < tmp.size(); i++) {
109  if (b[i] != tmp[i])
110  tmpWrong = true;
111  }
112  cout << "state=" << state << ",b0:[" << b << "], tmpWrong="
113  << tmpWrong << endl;
114  }
115 
116  //...Skip blank line...
117  if (b[0] == 0) continue;
118 
119  //...Input definitions...
120  if (state == 0) {
121  if (strstr(b, "Inputs"))
122  state = 1;
123  } else if (state == 1) {
124  getInput(b);
125  if (strstr(b, "Outputs"))
126  state = 2;
127  } else if (state == 2) {
128  getOutput(b);
129  if (strstr(b, "FTD logics"))
130  state = 3;
131  } else if (state == 3) {
132  string tmp = b;
133  if (tmp.find("end") != string::npos)
134  break;
135  getLogic(b);
136  }
137  }
138  file.close();
139 
140  //...Logic decoding...
141  for (unsigned i = 0; i < outputs.size(); i++) {
142  string logic = decodeLogic(logics[outputs[i]]);
143  shrink(logic);
144  expanded.push_back(logic);
145  string logic_cc = logic;
146 
147  //...For algorithm file...
148  for (unsigned j = 0; j < inputBits.size(); j++) {
149  while (1) {
150  char crep[10];
151  sprintf(crep, "%d", (int)j);
152  string inp = inputBits[j];
153  string rep = crep;
154  string::size_type p = logic.find(inp);
155  if (p != string::npos)
156  logic.replace(p, inp.size(), rep, 0, rep.size());
157  else
158  break;
159  }
160  }
161  shrink(logic);
162  cosmetic(logic);
163  expanded2.push_back(logic);
164 
165  //...For cc file...
166  for (unsigned j = 0; j < inputBits.size(); j++) {
167  while (1) {
168  char crep[10];
169  sprintf(crep, "i[%d]", (int)j);
170  string inp = inputBits[j];
171  string rep = crep;
172  string::size_type p = logic_cc.find(inp);
173  if (p != string::npos)
174  logic_cc.replace(p, inp.size(), rep, 0, rep.size());
175  else
176  break;
177  }
178  }
179 
180  shrink(logic_cc);
181  cosmetic(logic_cc);
182  tocc(logic_cc);
183  string right_cc = "b[" + Belle2::TRGUtilities::itostring(i) + "] = (";
184  logic_cc = right_cc + logic_cc + ");";
185 
186  expandedcc.push_back(logic_cc);
187 
188  if (DebugLevel) {
189  cout << "->" << logic << "<-" << endl;
190  cout << "->" << logic_cc << "<-" << endl;
191 
192  }
193  }
194 
195  //...Otain file name...
196  string filename = argv[1];
197  string::size_type s = filename.find_last_of("/");
198  if (s != string::npos)
199  filename = filename.substr(s + 1);
200  s = filename.find_last_of(".");
201  if (s != string::npos)
202  filename = filename.substr(0, s);
203 
204  //...Summary for text file...
205  string tFilename = filename + ".txt";
206  string iFilename = filename + ".inp";
207  string oFilename = filename + ".oup";
208  cout << " Text file :" << tFilename << endl;
209  ofstream tFile(tFilename.c_str(), ios::out);
210  if (! tFile.is_open()) {
211  cout << " !!! can not open file : " << tFilename << endl;
212  exit(-2);
213  }
214  cout << " Input file :" << iFilename << endl;
215  ofstream iFile(iFilename.c_str(), ios::out);
216  if (! iFile.is_open()) {
217  cout << " !!! can not open file : " << iFilename << endl;
218  exit(-2);
219  }
220  cout << " Output file:" << oFilename << endl;
221  ofstream oFile(oFilename.c_str(), ios::out);
222  if (! oFile.is_open()) {
223  cout << " !!! can not open file : " << oFilename << endl;
224  exit(-2);
225  }
226  tFile << "----- relations -----------------------" << endl;
227  for (unsigned i = 0; i < outputs.size(); i++) {
228  tFile << i << " : " << outputs[i] << " = " << logics[outputs[i]]
229  << endl;
230  }
231  tFile << "----- inputs -------------------------" << endl;
232  for (unsigned i = 0; i < inputBits.size(); i++) {
233  tFile << i << " : " << inputBits[i] << endl;
234  iFile << i << " " << inputNames[i] << endl;
235  }
236  tFile << "----- outputs ------------------------" << endl;
237  for (unsigned i = 0; i < outputs.size(); i++) {
238  tFile << i << " : " << outputs[i] << endl;
239  tFile << " " << expanded[i] << endl;
240  tFile << " " << expanded2[i] << endl;
241  oFile << i << " " << outputs[i] << endl;
242  }
243  tFile << "----- logics ------------------------" << endl;
244  for (unsigned i = 0; i < outputs.size(); i++) {
245  tFile << i << " : " << outputs[i] << endl;
246  tFile << " " << logics[outputs[i]] << endl;
247  tFile << " " << expanded[i] << endl;
248  tFile << " " << expanded2[i] << endl;
249  }
250  tFile.close();
251  iFile.close();
252  oFile.close();
253 
254  //...Summary for alg file...
255  string aFilename = filename + ".alg";
256  cout << " alg file :" << aFilename << endl;
257  ofstream aFile(aFilename.c_str(), ios::out);
258  if (! aFile.is_open()) {
259  cout << " !!! can not open file : " << aFilename << endl;
260  exit(-3);
261  }
262  for (unsigned i = 0; i < expanded2.size(); i++) {
263  aFile << i << " : ( " << expanded2[i] << " )" << endl;
264  }
265  aFile.close();
266 
267  //...C++ alg file...
268  string cFilename = filename + ".cc";
269  string funcname = filename;
270  tofunc(funcname);
271  cout << " c++ file :" << cFilename << endl;
272  ofstream cFile(cFilename.c_str(), ios::out);
273  if (! cFile.is_open()) {
274  cout << " !!! can not open file : " << cFilename << endl;
275  exit(-3);
276  }
277  cFile << "namespace Belle2 {" << endl;
278  cFile << "void " << funcname << "(bool * b, bool * i) {" << endl;
279  for (unsigned i = 0; i < expandedcc.size(); i++) {
280  cFile << expandedcc[i] << endl;
281  }
282  cFile << "}" << endl;
283  cFile << "}" << endl;
284  cFile.close();
285 
286  //...Summary for paw...
287  string kFilename = filename + ".kumac";
288  cout << " kumac file :" << kFilename << endl;
289  ofstream kFile(kFilename.c_str(), ios::out);
290  if (! kFile.is_open()) {
291  cout << " !!! can not open file : " << kFilename << endl;
292  exit(-3);
293  }
294  kFile << "macro main wait=wait baseRise=197 baseFall=293" << endl;
295  for (unsigned i = 0; i < expanded.size(); i++) {
296  string logic = expanded[i];
297  kFile << "*...FTD " << i << "..." << endl;
298  kFile << "* " << logic << endl;
299  kumacOutput(kFile, logic, outputs[i]);
300  kFile << endl;
301  }
302  kFile << "return" << endl;
303  kFile.close();
304 
305  return 0;
306 }
307 
308 void
309 shrink(char b[800])
310 {
311  char c[800];
312  unsigned i = 0;
313  unsigned j = 0;
314  bool deleteSpace = true;
315  while (b[i]) {
316  char d = b[i++];
317  char e = b[i];
318  if (d == '\t') d = ' ';
319  if (d == ';') d = ' ';
320  if ((d == '|') && (e == '|')) d = ' ';
321 
322  const bool space = (d == ' ');
323  if (deleteSpace && space)
324  continue;
325  if (space)
326  deleteSpace = true;
327  else
328  deleteSpace = false;
329  c[j] = d;
330  ++j;
331  }
332  c[j] = 0;
333  strcpy(b, c);
334 }
335 
336 void
337 removeComments(char b[800])
338 {
339  if ((b[0] == 0) || (b[1] == 0)) return;
340  unsigned i = 1;
341  while (b[i]) {
342  char c = b[i - 1];
343  char d = b[i];
344 
345  if ((c == '/') && (d == '/')) {
346  b[i - 1] = 0;
347  return;
348  }
349  i++;
350  }
351 }
352 
353 void
354 chopWord(char b[800], char c[800])
355 {
356  c[0] = 0;
357  unsigned i = 0;
358  while (1) {
359  c[i] = b[i];
360  if ((b[i] == ' ') || (b[i] == 0)) {
361  c[i] = 0;
362  break;
363  }
364  ++i;
365  }
366 
367  if (b[i] == 0) {
368  b[0] = 0;
369  return;
370  }
371 
372 // for (unsigned j = i, k = 0; j++, k++; j < 800) {
373  for (unsigned j = i, k = 0; j < 800; j++, k++) {
374  b[k] = b[j];
375  ++k;
376  if (b[j] == 0)
377  return;
378  }
379 }
380 
381 void
382 getInput(char b[800])
383 {
384  char w[800];
385 
386  //...First word should be "reg"...
387  chopWord(b, w);
388  if (strcmp(w, "reg")) return;
389 
390  //...Next word should be the name of input, or bit width...
391  unsigned b0 = 0;
392  unsigned b1 = 0;
393  chopWord(b, w);
394  if (w[0] == '[') {
395 
396  //...One character...
397  if (w[2] == ':') {
398  b0 = w[3] - '0';
399  b1 = w[1] - '0';
400  } else {
401  b0 = w[4] - '0';
402  b1 = (w[1] - '0') * 10 + (w[2] - '0');
403  }
404 
405  chopWord(b, w);
406  }
407 
408  inputs.push_back(string(w));
409  inputSizes.push_back(b1 - b0);
410 
411  if (b0 != b1) {
412  if ((b1 - b0) < 10) {
413  for (unsigned i = b0; i <= b1; i++) {
414  char n[2];
415  n[0] = '0' + i;
416  n[1] = 0;
417  inputBits.push_back(string(w) + "[" + string(n) + "]");
418  inputNames.push_back(string(w) + string(n));
419  }
420  } else {
421  for (unsigned i = b0; i <= b1; i++) {
422  if (i < 10) {
423  char n[2];
424  n[0] = '0' + i;
425  n[1] = 0;
426  inputBits.push_back(string(w) + "[" + string(n) + "]");
427  inputNames.push_back(string(w) + string(n));
428  }
429  //...Assuming less than 20...
430  else {
431  char n[3];
432  n[0] = '1';
433  n[1] = '0' + i - 10;
434  n[2] = 0;
435  inputBits.push_back(string(w) + "[" + string(n) + "]");
436  inputNames.push_back(string(w) + string(n));
437  }
438  }
439  if (DebugLevel) {
440 // cout << inputNames.last() << endl;
441  }
442  }
443  } else {
444  inputBits.push_back(string(w));
445  inputNames.push_back(string(w));
446  }
447 }
448 
449 void
450 getOutput(char b[800])
451 {
452  char w[800];
453 
454  //...First word should be "reg"...
455  chopWord(b, w);
456  if (strcmp(w, "reg")) return;
457 
458  //...Next word should be the name...
459  chopWord(b, w);
460 
461  outputs.push_back(string(w));
462 }
463 
464 void
465 getLogic(char b[800])
466 {
467  char w[800];
468 
469  //...First word should be the output name...
470  string tmp = b;
471  getLogicGND(tmp);
472  strcpy(b, tmp.c_str());
473  removeComments(b);
474  chopWord(b, w);
475  string name = w;
476  chopWord(b, w); // This must be '='.
477 
478  for (unsigned i = 0; i < outputs.size(); i++) {
479  if (name == outputs[i]) {
480  string logic = b;
481  logics[name] = logic;
482  }
483  }
484 }
485 
486 string
487 breakup(const string& logic)
488 {
489  string s = logic;
490  string sp = " ";
491 
492  //...Instert spaces...
493 // for (string::size_type i = s.size(); i >= 0; i--) {
494  int len = int(s.size());
495  for (int i = len; i >= 0; i--) {
496  char c = s[i];
497  if ((c == '(') ||
498  (c == ')') ||
499  (c == '&') ||
500  (c == '|') ||
501  (c == '>') ||
502  (c == '<') ||
503  (c == '!') ||
504  (c == '=')) {
505  string d = s.substr(i, 1);
506 
507  if (i == 0) {
508  s.insert(i + 1, sp);
509  } else if (i == int(s.size())) {
510  s.insert(i, sp);
511  } else if (c == '=') {
512  // assuming only "=="
513  s.insert(i - 1, sp);
514  s.insert(i + 2, sp);
515  } else {
516  s.insert(i, sp);
517  s.insert(i + 2, sp);
518  }
519 
520  if (DebugLevel) {
521  cout << "i=" << i << ",d=[" << d << "]" << endl;
522  cout << s << endl;
523  }
524  }
525  }
526 
527  char w[800];
528  strcpy(w, s.c_str());
529  shrink(w);
530 
531  if (DebugLevel) {
532  cout << " breakup : in=[" << logic << "]" << endl;
533  cout << " out=[" << string(w) << "]" << endl;
534  }
535 
536  return string(w);
537 }
538 
539 string
540 decodeLogic(const string& logic)
541 {
542  string s = logic;
543  s = breakup(s);
544  char c[800];
545  strcpy(c, s.c_str());
546 
547  string f;
548  char p0[800] = "";
549  char p1[800] = "";
550  while (1) {
551  char cw[800];
552  chopWord(c, cw);
553  if (! cw[0]) {
554  if (f.size() && (p1[0] != 0)) f += " ";
555  f += p1;
556  if (f.size() && (p0[0] != 0)) f += " ";
557  f += p0;
558  break;
559  }
560 
561  if (DebugLevel) {
562  cout << "p0=" << p0 << ",p1=" << p1 << endl;
563  }
564 
565  //...'>'...
566  if (p0[0] == '>') {
567 
568  //...Search for width...
569  string name = p1;
570  unsigned n = 999;
571  for (unsigned i = 0; i < inputs.size(); i++) {
572  if (inputs[i] == p1) {
573  n = i;
574  break;
575  }
576  }
577  unsigned width = inputSizes[n] + 1;
578  string x = "( ";
579  if (cw[0] == '0') {
580  char b = '0';
581  for (unsigned i = 0; i < width; i++) {
582  if (i) x += " | ";
583  x += name + "[" + char(b + i) + "]";
584  }
585  } else if (cw[0] == '1') {
586  char b = '0';
587  for (unsigned i = 1; i < width; i++) {
588  if (i > 1) x += " | ";
589  x += name + "[" + char(b + i) + "]";
590  }
591  } else if (cw[0] == '2') {
592  char b = '0';
593  x = "(" + name + "[0] & " + name + "[1] )";
594  for (unsigned i = 2; i < width; i++) {
595  x += " | ";
596  x += name + "[" + char(b + i) + "]";
597  }
598  } else if (cw[0] == '3') {
599  char b = '0';
600  for (unsigned i = 2; i < width; i++) {
601  if (i > 2) x += " | ";
602  x += name + "[" + char(b + i) + "]";
603  }
604  } else if (cw[0] == '4') {
605  char b = '0';
606  x = "(" + name + "[1] & " + name + "[2] )";
607  x += "| (" + name + "[0] & " + name + "[2] )";
608  for (unsigned i = 3; i < width; i++) {
609  x += " | ";
610  x += name + "[" + char(b + i) + "]";
611  }
612  } else {
613  cout << "decodeLogic !!! [> with 4] is not supported"
614  << endl;
615  cout << " " << logic << endl;
616  }
617 
618  x += " )";
619 
620  if (DebugLevel) {
621  cout << " s=" << p1 << " " << p0 << " " << cw
622  << endl;
623  cout << " name=" << name << ",width=" << width
624  << endl;
625  cout << " x={" << x << "}" << endl;
626  }
627 
628  strcpy(p1, x.c_str());
629  strcpy(p0, "");
630  strcpy(cw, "");
631  }
632 
633  //...'<'...
634  else if (p0[0] == '<') {
635  cout << "decodeLogic !!! [> with 4] is not supported"
636  << endl;
637  }
638 
639  //...'='...
640  else if (p0[0] == '=') {
641 
642  //...Search for width...
643  string name = p1;
644  unsigned n = 999;
645  for (unsigned i = 0; i < inputs.size(); i++) {
646  if (inputs[i] == p1) {
647  n = i;
648  break;
649  }
650  }
651  unsigned width = inputSizes[n] + 1;
652  string x = "( ";
653  if (cw[0] == '0') {
654  char b = '0';
655  for (unsigned i = 0; i < width; i++) {
656  if (i) x += " & ";
657  x += "!" + name + "[" + char(b + i) + "]";
658  }
659  } else {
660  cout << "decodeLogic !!! '== n' is not supported"
661  << endl;
662  cout << " " << logic << endl;
663  }
664 
665  if (DebugLevel) {
666  cout << " s=" << p1 << " " << p0 << " " << cw
667  << endl;
668  cout << " name=" << name << ",width=" << width
669  << endl;
670  cout << " x={" << x << "}" << endl;
671  }
672 
673  x += " )";
674 
675  strcpy(p1, x.c_str());
676  strcpy(p0, "");
677  strcpy(cw, "");
678  }
679 
680  if (f.size() && (p1[0] != 0)) f += " ";
681  f += p1;
682  strcpy(p1, p0);
683  strcpy(p0, cw);
684 
685  }
686 
687  Term t(f);
688  t.expand();
689 
690  return t.expanded();
691 }
692 
693 Term::Term(const string& a)
694  : _sentence(a)
695 {
696  char b[800];
697  strcpy(b, _sentence.c_str());
698 
699  char w[800];
700  while (1) {
701  chopWord(b, w);
702  if (w[0] == 0) break;
703  _words.push_back(string(w));
704  }
705 
706  //...Create term candidates...
707  unsigned inP = 0;
708  vector<string> termCandidates;
709  string t;
710  for (unsigned i = 0; i < _sentence.size(); i++) {
711  char c = _sentence[i];
712 
713  if (c == '(') {
714  t += c;
715  ++inP;
716  continue;
717  } else if (c == ')') {
718  t += c;
719  --inP;
720  continue;
721  }
722 
723  if (inP) {
724  t += c;
725  continue;
726  }
727 
728  if (c == '|') {
729  termCandidates.push_back(t);
730  t = "";
731  continue;
732  }
733 
734  t += c;
735  }
736  termCandidates.push_back(t);
737 
738  //...Check term candidates...
739  if (termCandidates.size() > 1) {
740  for (unsigned i = 0; i < termCandidates.size(); i++) {
741  Term x(termCandidates[i]);
742  _terms.push_back(x);
743  }
744 
745 // cout << "Term list:#=" << _terms.size() << endl;
746 // for (unsigned i = 0; i < _terms.size(); i++) {
747 // cout << i << ":" << _terms[i].sentence() << endl;
748 // }
749  }
750 }
751 
752 Term::~Term()
753 {
754 }
755 
756 string
757 Term::sentence(void) const
758 {
759  return _sentence;
760 }
761 
762 void
763 Term::expand(void)
764 {
765  _expanded = "";
766 
767  //...Have sub-terms...
768  if (_terms.size() > 1) {
769  for (unsigned i = 0; i < _terms.size(); i++) {
770  _terms[i].expand();
771  if (i)
772  _expanded += " | ";
773  _expanded += _terms[i].expanded();
774  }
775  return;
776  }
777 
778  //...Expand...
779  _expanded = _sentence;
780  while (1) {
781  unsigned a = 0;
782  unsigned b = 0;
783  for (unsigned i = 0; i < _expanded.size(); i++) {
784  char c = _expanded[i];
785  if (c == '(') {
786  a = i;
787  } else if (c == ')') {
788  b = i;
789  break;
790  }
791  }
792 
793  //...No ()...
794  if (a == b) return;
795 
796  //...necessary to expand...
797  string s0 = _expanded.substr(0, a - 1);
798  if (a == 0) s0 = "";
799  string p = _expanded.substr(a + 1, b - a - 1);
800  string s1 = _expanded.substr(b + 1);
801 
802 // cout << "a,b=" << a << "," << b << endl;
803 // cout << " s0=" << s0 << endl;
804 // cout << " p =" << p << endl;
805 // cout << " s1=" << s1 << endl;
806 
807  //...divide p into two...
808  unsigned a1 = 0;
809  for (unsigned i = 0; i < p.size(); i++) {
810  char c = p[i];
811  if (c == '|') {
812  a1 = i;
813  break;
814  }
815  }
816  if (a1) {
817  string p0 = p.substr(0, a1 - 1);
818  string p1 = p.substr(a1 + 1);
819  _expanded = s0 + p0 + s1 + " | " + s0 + " (" + p1 + ") " + s1;
820 
821  Term tmp(_expanded);
822  tmp.expand();
823  _expanded = tmp.expanded();
824  } else {
825  _expanded = s0 + p + s1;
826  }
827 
828  // cout << "--->" << endl;
829  // cout << _expanded << endl;
830  // cout << "<---" << endl;
831  }
832 }
833 
834 string
835 Term::expanded(void)
836 {
837  return _expanded;
838 }
839 
840 void
841 shrink(string& s)
842 {
843  string t;
844  bool deleteSpace = true;
845  for (unsigned i = 0; i < s.size(); i++) {
846  char d = s[i];
847  char e = 0;
848  if ((i + 1) < s.size())
849  e = s[i + 1];
850 
851  if (d == '\t') d = ' ';
852  if (d == ';') d = ' ';
853  if ((d == '|') && (e == '|')) d = ' ';
854 
855  const bool space = (d == ' ');
856  if (deleteSpace && space)
857  continue;
858  if (space)
859  deleteSpace = true;
860  else
861  deleteSpace = false;
862 
863  t += d;
864  }
865 
866  s = t;
867 }
868 
869 void
870 cosmetic(string& s)
871 {
872  for (unsigned i = 0; i < s.size(); i++) {
873  char d = s[i];
874  char e = 0;
875  if ((i + 1) < s.size())
876  e = s[i + 1];
877 
878  if (d == '&')
879  s[i] = '*';
880  else if (d == '|')
881  s[i] = '+';
882  else if ((d == '!') && (e == ' '))
883  s.erase(i + 1, 1);
884  }
885 }
886 
887 void
888 getLogicGND(string& s)
889 {
890 // cout << "s0=" << s << endl;
891  string g = "= 0";
892  string::size_type p = s.find(g);
893  if (p != string::npos)
894  s.replace(p, 3, "= GND", 0, 5);
895 // cout << "s1=" << s << endl;
896 }
897 
898 void
899 kumacOutput(ofstream& f, string logic, string ftd)
900 {
901  const string tab = " ";
902  bool first = true;
903 
904  //...Check how many ORs...
905  unsigned nO = 0;
906  for (unsigned i = 0; i < logic.size(); i++)
907  if (logic[i] == '|')
908  ++nO;
909 
910  unsigned n = 0;
911  while (1) {
912  string t;
913  chopWord(logic, t);
914  if (t.empty()) break;
915 
916  if (t == "|")
917  first = true;
918 
919  if (first) {
920  f << "[wait]" << endl;
921  f << tab << "zone 1 6 1" << endl;
922  f << tab << "title '" << ftd << " " << n + 1 << "/" << nO + 1 <<
923  "'" << endl;
924  first = false;
925  ++n;
926  }
927 
928  for (unsigned i = 0; i < inputBits.size(); i++) {
929  if (t == inputBits[i]) {
930  f << tab << "set hcol 2" << endl;
931  f << tab << "id=[baseRise]+" << i << ";"
932  << "hi/pl [id]" << endl;
933  f << tab << "set hcol 4" << endl;
934  f << tab << "id=[baseFall]+" << i << ";"
935  << "hi/pl [id] s" << endl;
936  f << tab << "atit '" << t << "' ' '" << endl;
937  first = false;
938  break;
939  }
940  }
941  }
942 }
943 
944 void
945 chopWord(string& b, string& c)
946 {
947  c = "";
948  unsigned i = 0;
949  while (1) {
950  if ((b[i] == ' ') || (b[i] == 0))
951  break;
952 
953  c += b[i];
954  ++i;
955  }
956 
957  int l = b.size() - i - 1;
958  if (l > 0)
959  b = b.substr(i + 1, l);
960  else
961  b = "";
962 }
963 
964 void
965 tocc(string& s)
966 {
967  while (s.find("*") != string::npos) {
968  const unsigned n = s.size();
969  for (unsigned i = 0; i < n; i++)
970  if (s[i] == '*')
971  s.replace(i, 2, "&& ", 0, 3);
972  }
973  while (s.find("+") != string::npos) {
974  const unsigned n = s.size();
975  for (unsigned i = 0; i < n; i++)
976  if (s[i] == '+')
977  s.replace(i, 2, ") || ( ", 0, 6);
978  }
979 }
980 
981 void
982 tofunc(string& s)
983 {
984  const unsigned n = s.size();
985  for (unsigned i = 0; i < n; i++)
986  if (s[i] == '.')
987  s.replace(i, 1, "_", 0, 1);
988 }
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Term
Definition: FTDConverter.cc:55
Belle2::TRGUtilities::itostring
static std::string itostring(int i)
converts int to string. (Use boost::lexical_cast)
alignment.constraints_generator.filename
filename
File name.
Definition: constraints_generator.py:224