15#include "trg/trg/Utilities.h"
17unsigned DebugLevel = 1;
22vector<unsigned> inputSizes;
23vector<string> inputBits;
24vector<string> inputNames;
25vector<string> outputs;
26map<string, string> logics;
27vector<string> expanded;
28vector<string> expanded2;
29vector<string> expandedcc;
31void shrink(
char b[800]);
33void cosmetic(
string&);
35void chopWord(
char b[800],
char c[800]);
36void chopWord(
string&,
string&);
37void removeComments(
char b[800]);
38void getInput(
char b[800]);
39void getOutput(
char b[800]);
40void getLogic(
char b[800]);
41void getLogicGND(
string&);
43string breakup(
const string& logic);
44string decodeLogic(
const string& logic);
45void kumacOutput(ofstream&,
string logic,
const string& ftd);
49 explicit Term(
const string&);
54 string sentence(
void)
const;
56 string expanded(
void);
60 vector<string> _words;
66main(
int argc,
char* argv[])
68 cout <<
"ftd: version 1.01 : 2007/09/21 Y.Iwasaki" << endl;
72 cout <<
"usage : ftd.v" << endl;
73 cout <<
" ftd.v : ftd logic file" << endl;
79 cout <<
"ftd ... reading verilog file : "
81 ifstream file(argv[1], ios::in);
82 if (! file.is_open()) {
83 cout <<
" !!! can not open file : " << argv[1] << endl;
90 while (! file.eof()) {
99 bool tmpWrong =
false;
100 for (
unsigned i = 0; i < tmp.size(); i++) {
104 cout <<
"state=" << state <<
",b0:[" << b <<
"], tmpWrong="
109 if (b[0] == 0)
continue;
113 if (strstr(b,
"Inputs"))
115 }
else if (state == 1) {
117 if (strstr(b,
"Outputs"))
119 }
else if (state == 2) {
121 if (strstr(b,
"FTD logics"))
123 }
else if (state == 3) {
125 if (tmp.find(
"end") != string::npos)
133 for (
unsigned i = 0; i < outputs.size(); i++) {
134 string logic = decodeLogic(logics[outputs[i]]);
136 expanded.push_back(logic);
137 string logic_cc = logic;
140 for (
unsigned j = 0; j < inputBits.size(); j++) {
143 sprintf(crep,
"%d", (
int)j);
144 string inp = inputBits[j];
146 string::size_type p = logic.find(inp);
147 if (p != string::npos)
148 logic.replace(p, inp.size(), rep, 0, rep.size());
155 expanded2.push_back(logic);
158 for (
unsigned j = 0; j < inputBits.size(); j++) {
161 sprintf(crep,
"i[%d]", (
int)j);
162 string inp = inputBits[j];
164 string::size_type p = logic_cc.find(inp);
165 if (p != string::npos)
166 logic_cc.replace(p, inp.size(), rep, 0, rep.size());
176 logic_cc = right_cc + logic_cc +
");";
178 expandedcc.push_back(logic_cc);
181 cout <<
"->" << logic <<
"<-" << endl;
182 cout <<
"->" << logic_cc <<
"<-" << endl;
188 string filename = argv[1];
189 string::size_type s = filename.find_last_of(
"/");
190 if (s != string::npos)
191 filename = filename.substr(s + 1);
192 s = filename.find_last_of(
".");
193 if (s != string::npos)
194 filename = filename.substr(0, s);
197 string tFilename = filename +
".txt";
198 string iFilename = filename +
".inp";
199 string oFilename = filename +
".oup";
200 cout <<
" Text file :" << tFilename << endl;
201 ofstream tFile(tFilename.c_str(), ios::out);
202 if (! tFile.is_open()) {
203 cout <<
" !!! can not open file : " << tFilename << endl;
206 cout <<
" Input file :" << iFilename << endl;
207 ofstream iFile(iFilename.c_str(), ios::out);
208 if (! iFile.is_open()) {
209 cout <<
" !!! can not open file : " << iFilename << endl;
212 cout <<
" Output file:" << oFilename << endl;
213 ofstream oFile(oFilename.c_str(), ios::out);
214 if (! oFile.is_open()) {
215 cout <<
" !!! can not open file : " << oFilename << endl;
218 tFile <<
"----- relations -----------------------" << endl;
219 for (
unsigned i = 0; i < outputs.size(); i++) {
220 tFile << i <<
" : " << outputs[i] <<
" = " << logics[outputs[i]]
223 tFile <<
"----- inputs -------------------------" << endl;
224 for (
unsigned i = 0; i < inputBits.size(); i++) {
225 tFile << i <<
" : " << inputBits[i] << endl;
226 iFile << i <<
" " << inputNames[i] << endl;
228 tFile <<
"----- outputs ------------------------" << endl;
229 for (
unsigned i = 0; i < outputs.size(); i++) {
230 tFile << i <<
" : " << outputs[i] << endl;
231 tFile <<
" " << expanded[i] << endl;
232 tFile <<
" " << expanded2[i] << endl;
233 oFile << i <<
" " << outputs[i] << endl;
235 tFile <<
"----- logics ------------------------" << endl;
236 for (
unsigned i = 0; i < outputs.size(); i++) {
237 tFile << i <<
" : " << outputs[i] << endl;
238 tFile <<
" " << logics[outputs[i]] << endl;
239 tFile <<
" " << expanded[i] << endl;
240 tFile <<
" " << expanded2[i] << endl;
247 string aFilename = filename +
".alg";
248 cout <<
" alg file :" << aFilename << endl;
249 ofstream aFile(aFilename.c_str(), ios::out);
250 if (! aFile.is_open()) {
251 cout <<
" !!! can not open file : " << aFilename << endl;
254 for (
unsigned i = 0; i < expanded2.size(); i++) {
255 aFile << i <<
" : ( " << expanded2[i] <<
" )" << endl;
260 string cFilename = filename +
".cc";
261 string funcname = filename;
263 cout <<
" c++ file :" << cFilename << endl;
264 ofstream cFile(cFilename.c_str(), ios::out);
265 if (! cFile.is_open()) {
266 cout <<
" !!! can not open file : " << cFilename << endl;
269 cFile <<
"namespace Belle2 {" << endl;
270 cFile <<
"void " << funcname <<
"(bool * b, bool * i) {" << endl;
271 for (
unsigned i = 0; i < expandedcc.size(); i++) {
272 cFile << expandedcc[i] << endl;
274 cFile <<
"}" << endl;
275 cFile <<
"}" << endl;
279 string kFilename = filename +
".kumac";
280 cout <<
" kumac file :" << kFilename << endl;
281 ofstream kFile(kFilename.c_str(), ios::out);
282 if (! kFile.is_open()) {
283 cout <<
" !!! can not open file : " << kFilename << endl;
286 kFile <<
"macro main wait=wait baseRise=197 baseFall=293" << endl;
287 for (
unsigned i = 0; i < expanded.size(); i++) {
288 string logic = expanded[i];
289 kFile <<
"*...FTD " << i <<
"..." << endl;
290 kFile <<
"* " << logic << endl;
291 kumacOutput(kFile, logic, outputs[i]);
294 kFile <<
"return" << endl;
306 bool deleteSpace =
true;
310 if (d ==
'\t') d =
' ';
311 if (d ==
';') d =
' ';
312 if ((d ==
'|') && (e ==
'|')) d =
' ';
314 const bool space = (d ==
' ');
315 if (deleteSpace && space)
329removeComments(
char b[800])
331 if ((b[0] == 0) || (b[1] == 0))
return;
337 if ((c ==
'/') && (d ==
'/')) {
346chopWord(
char b[800],
char c[800])
352 if ((b[i] ==
' ') || (b[i] == 0)) {
365 for (
unsigned j = i, k = 0; j < 800; j++, k++) {
380 if (strcmp(w,
"reg"))
return;
394 b1 = (w[1] -
'0') * 10 + (w[2] -
'0');
400 inputs.push_back(
string(w));
401 inputSizes.push_back(b1 - b0);
404 if ((b1 - b0) < 10) {
405 for (
unsigned i = b0; i <= b1; i++) {
409 inputBits.push_back(
string(w) +
"[" +
string(n) +
"]");
410 inputNames.push_back(
string(w) +
string(n));
413 for (
unsigned i = b0; i <= b1; i++) {
418 inputBits.push_back(
string(w) +
"[" +
string(n) +
"]");
419 inputNames.push_back(
string(w) +
string(n));
427 inputBits.push_back(
string(w) +
"[" +
string(n) +
"]");
428 inputNames.push_back(
string(w) +
string(n));
436 inputBits.push_back(
string(w));
437 inputNames.push_back(
string(w));
442getOutput(
char b[800])
448 if (strcmp(w,
"reg"))
return;
453 outputs.push_back(
string(w));
464 strcpy(b, tmp.c_str());
470 for (
unsigned i = 0; i < outputs.size(); i++) {
471 if (name == outputs[i]) {
473 logics[name] = logic;
479breakup(
const string& logic)
486 int len = int(s.size());
487 for (
int i = len; i >= 0; i--) {
497 string d = s.substr(i, 1);
501 }
else if (i ==
int(s.size())) {
503 }
else if (c ==
'=') {
513 cout <<
"i=" << i <<
",d=[" << d <<
"]" << endl;
520 strcpy(w, s.c_str());
524 cout <<
" breakup : in=[" << logic <<
"]" << endl;
525 cout <<
" out=[" << string(w) <<
"]" << endl;
532decodeLogic(
const string& logic)
537 strcpy(c, s.c_str());
546 if (f.size() && (p1[0] != 0)) f +=
" ";
548 if (f.size() && (p0[0] != 0)) f +=
" ";
554 cout <<
"p0=" << p0 <<
",p1=" << p1 << endl;
563 for (
unsigned i = 0; i < inputs.size(); i++) {
564 if (inputs[i] == p1) {
569 unsigned width = inputSizes[n] + 1;
573 for (
unsigned i = 0; i < width; i++) {
575 x += name +
"[" + char(b + i) +
"]";
577 }
else if (cw[0] ==
'1') {
579 for (
unsigned i = 1; i < width; i++) {
580 if (i > 1) x +=
" | ";
581 x += name +
"[" + char(b + i) +
"]";
583 }
else if (cw[0] ==
'2') {
585 x =
"(" + name +
"[0] & " + name +
"[1] )";
586 for (
unsigned i = 2; i < width; i++) {
588 x += name +
"[" + char(b + i) +
"]";
590 }
else if (cw[0] ==
'3') {
592 for (
unsigned i = 2; i < width; i++) {
593 if (i > 2) x +=
" | ";
594 x += name +
"[" + char(b + i) +
"]";
596 }
else if (cw[0] ==
'4') {
598 x =
"(" + name +
"[1] & " + name +
"[2] )";
599 x +=
"| (" + name +
"[0] & " + name +
"[2] )";
600 for (
unsigned i = 3; i < width; i++) {
602 x += name +
"[" + char(b + i) +
"]";
605 cout <<
"decodeLogic !!! [> with 4] is not supported"
607 cout <<
" " << logic << endl;
613 cout <<
" s=" << p1 <<
" " << p0 <<
" " << cw
615 cout <<
" name=" << name <<
",width=" << width
617 cout <<
" x={" << x <<
"}" << endl;
620 strcpy(p1, x.c_str());
626 else if (p0[0] ==
'<') {
627 cout <<
"decodeLogic !!! [> with 4] is not supported"
632 else if (p0[0] ==
'=') {
637 for (
unsigned i = 0; i < inputs.size(); i++) {
638 if (inputs[i] == p1) {
643 unsigned width = inputSizes[n] + 1;
647 for (
unsigned i = 0; i < width; i++) {
649 x +=
"!" + name +
"[" + char(b + i) +
"]";
652 cout <<
"decodeLogic !!! '== n' is not supported"
654 cout <<
" " << logic << endl;
658 cout <<
" s=" << p1 <<
" " << p0 <<
" " << cw
660 cout <<
" name=" << name <<
",width=" << width
662 cout <<
" x={" << x <<
"}" << endl;
667 strcpy(p1, x.c_str());
672 if (f.size() && (p1[0] != 0)) f +=
" ";
685Term::Term(
const string& a)
689 strcpy(b, _sentence.c_str());
694 if (w[0] == 0)
break;
695 _words.push_back(
string(w));
700 vector<string> termCandidates;
702 for (
unsigned i = 0; i < _sentence.size(); i++) {
703 char c = _sentence[i];
709 }
else if (c ==
')') {
721 termCandidates.push_back(t);
728 termCandidates.push_back(t);
731 if (termCandidates.size() > 1) {
732 for (
unsigned i = 0; i < termCandidates.size(); i++) {
733 Term x(termCandidates[i]);
749Term::sentence(
void)
const
760 if (_terms.size() > 1) {
761 for (
unsigned i = 0; i < _terms.size(); i++) {
765 _expanded += _terms[i].expanded();
771 _expanded = _sentence;
775 for (
unsigned i = 0; i < _expanded.size(); i++) {
776 char c = _expanded[i];
779 }
else if (c ==
')') {
789 string s0 = _expanded.substr(0, a - 1);
791 string p = _expanded.substr(a + 1, b - a - 1);
792 string s1 = _expanded.substr(b + 1);
801 for (
unsigned i = 0; i < p.size(); i++) {
809 string p0 = p.substr(0, a1 - 1);
810 string p1 = p.substr(a1 + 1);
811 _expanded = s0 + p0 + s1 +
" | " + s0 +
" (" + p1 +
") " + s1;
815 _expanded = tmp.expanded();
817 _expanded = s0 + p + s1;
836 bool deleteSpace =
true;
837 for (
unsigned i = 0; i < s.size(); i++) {
840 if ((i + 1) < s.size())
843 if (d ==
'\t') d =
' ';
844 if (d ==
';') d =
' ';
845 if ((d ==
'|') && (e ==
'|')) d =
' ';
847 const bool space = (d ==
' ');
848 if (deleteSpace && space)
864 for (
unsigned i = 0; i < s.size(); i++) {
867 if ((i + 1) < s.size())
874 else if ((d ==
'!') && (e ==
' '))
880getLogicGND(
string& s)
884 string::size_type p = s.find(g);
885 if (p != string::npos)
886 s.replace(p, 3,
"= GND", 0, 5);
891kumacOutput(ofstream& f,
string logic,
const string& ftd)
893 const string tab =
" ";
898 for (
unsigned i = 0; i < logic.size(); i++)
906 if (t.empty())
break;
912 f <<
"[wait]" << endl;
913 f << tab <<
"zone 1 6 1" << endl;
914 f << tab <<
"title '" << ftd <<
" " << n + 1 <<
"/" << nO + 1 <<
920 for (
unsigned i = 0; i < inputBits.size(); i++) {
921 if (t == inputBits[i]) {
922 f << tab <<
"set hcol 2" << endl;
923 f << tab <<
"id=[baseRise]+" << i <<
";"
924 <<
"hi/pl [id]" << endl;
925 f << tab <<
"set hcol 4" << endl;
926 f << tab <<
"id=[baseFall]+" << i <<
";"
927 <<
"hi/pl [id] s" << endl;
928 f << tab <<
"atit '" << t <<
"' ' '" << endl;
937chopWord(
string& b,
string& c)
942 if ((b[i] ==
' ') || (b[i] == 0))
949 int l = b.size() - i - 1;
951 b = b.substr(i + 1, l);
959 while (s.find(
"*") != string::npos) {
960 const unsigned n = s.size();
961 for (
unsigned i = 0; i < n; i++)
963 s.replace(i, 2,
"&& ", 0, 3);
965 while (s.find(
"+") != string::npos) {
966 const unsigned n = s.size();
967 for (
unsigned i = 0; i < n; i++)
969 s.replace(i, 2,
") || ( ", 0, 6);
976 const unsigned n = s.size();
977 for (
unsigned i = 0; i < n; i++)
979 s.replace(i, 1,
"_", 0, 1);
static std::string itostring(int i)
converts int to string. (Use boost::lexical_cast)