23 #include "trg/trg/Utilities.h"
25 unsigned DebugLevel = 1;
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;
39 void shrink(
char b[800]);
41 void cosmetic(
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&);
51 string breakup(
const string& logic);
52 string decodeLogic(
const string& logic);
53 void kumacOutput(ofstream&,
string logic,
string ftd);
62 string sentence(
void)
const;
64 string expanded(
void);
68 vector<string> _words;
74 main(
int argc,
char* argv[])
76 cout <<
"ftd: version 1.01 : 2007/09/21 Y.Iwasaki" << endl;
80 cout <<
"usage : ftd.v" << endl;
81 cout <<
" ftd.v : ftd logic file" << endl;
87 cout <<
"ftd ... reading verilog file : "
89 ifstream file(argv[1], ios::in);
90 if (! file.is_open()) {
91 cout <<
" !!! can not open file : " << argv[1] << endl;
98 while (! file.eof()) {
107 bool tmpWrong =
false;
108 for (
unsigned i = 0; i < tmp.size(); i++) {
112 cout <<
"state=" << state <<
",b0:[" << b <<
"], tmpWrong="
117 if (b[0] == 0)
continue;
121 if (strstr(b,
"Inputs"))
123 }
else if (state == 1) {
125 if (strstr(b,
"Outputs"))
127 }
else if (state == 2) {
129 if (strstr(b,
"FTD logics"))
131 }
else if (state == 3) {
133 if (tmp.find(
"end") != string::npos)
141 for (
unsigned i = 0; i < outputs.size(); i++) {
142 string logic = decodeLogic(logics[outputs[i]]);
144 expanded.push_back(logic);
145 string logic_cc = logic;
148 for (
unsigned j = 0; j < inputBits.size(); j++) {
151 sprintf(crep,
"%d", (
int)j);
152 string inp = inputBits[j];
154 string::size_type p = logic.find(inp);
155 if (p != string::npos)
156 logic.replace(p, inp.size(), rep, 0, rep.size());
163 expanded2.push_back(logic);
166 for (
unsigned j = 0; j < inputBits.size(); j++) {
169 sprintf(crep,
"i[%d]", (
int)j);
170 string inp = inputBits[j];
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());
184 logic_cc = right_cc + logic_cc +
");";
186 expandedcc.push_back(logic_cc);
189 cout <<
"->" << logic <<
"<-" << endl;
190 cout <<
"->" << logic_cc <<
"<-" << endl;
197 string::size_type s =
filename.find_last_of(
"/");
198 if (s != string::npos)
201 if (s != string::npos)
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;
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;
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;
226 tFile <<
"----- relations -----------------------" << endl;
227 for (
unsigned i = 0; i < outputs.size(); i++) {
228 tFile << i <<
" : " << outputs[i] <<
" = " << logics[outputs[i]]
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;
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;
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;
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;
262 for (
unsigned i = 0; i < expanded2.size(); i++) {
263 aFile << i <<
" : ( " << expanded2[i] <<
" )" << endl;
268 string cFilename =
filename +
".cc";
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;
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;
282 cFile <<
"}" << endl;
283 cFile <<
"}" << endl;
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;
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]);
302 kFile <<
"return" << endl;
314 bool deleteSpace =
true;
318 if (d ==
'\t') d =
' ';
319 if (d ==
';') d =
' ';
320 if ((d ==
'|') && (e ==
'|')) d =
' ';
322 const bool space = (d ==
' ');
323 if (deleteSpace && space)
337 removeComments(
char b[800])
339 if ((b[0] == 0) || (b[1] == 0))
return;
345 if ((c ==
'/') && (d ==
'/')) {
354 chopWord(
char b[800],
char c[800])
360 if ((b[i] ==
' ') || (b[i] == 0)) {
373 for (
unsigned j = i, k = 0; j < 800; j++, k++) {
382 getInput(
char b[800])
388 if (strcmp(w,
"reg"))
return;
402 b1 = (w[1] -
'0') * 10 + (w[2] -
'0');
408 inputs.push_back(
string(w));
409 inputSizes.push_back(b1 - b0);
412 if ((b1 - b0) < 10) {
413 for (
unsigned i = b0; i <= b1; i++) {
417 inputBits.push_back(
string(w) +
"[" +
string(n) +
"]");
418 inputNames.push_back(
string(w) +
string(n));
421 for (
unsigned i = b0; i <= b1; i++) {
426 inputBits.push_back(
string(w) +
"[" +
string(n) +
"]");
427 inputNames.push_back(
string(w) +
string(n));
435 inputBits.push_back(
string(w) +
"[" +
string(n) +
"]");
436 inputNames.push_back(
string(w) +
string(n));
444 inputBits.push_back(
string(w));
445 inputNames.push_back(
string(w));
450 getOutput(
char b[800])
456 if (strcmp(w,
"reg"))
return;
461 outputs.push_back(
string(w));
465 getLogic(
char b[800])
472 strcpy(b, tmp.c_str());
478 for (
unsigned i = 0; i < outputs.size(); i++) {
479 if (name == outputs[i]) {
481 logics[name] = logic;
487 breakup(
const string& logic)
494 int len = int(s.size());
495 for (
int i = len; i >= 0; i--) {
505 string d = s.substr(i, 1);
509 }
else if (i ==
int(s.size())) {
511 }
else if (c ==
'=') {
521 cout <<
"i=" << i <<
",d=[" << d <<
"]" << endl;
528 strcpy(w, s.c_str());
532 cout <<
" breakup : in=[" << logic <<
"]" << endl;
533 cout <<
" out=[" << string(w) <<
"]" << endl;
540 decodeLogic(
const string& logic)
545 strcpy(c, s.c_str());
554 if (f.size() && (p1[0] != 0)) f +=
" ";
556 if (f.size() && (p0[0] != 0)) f +=
" ";
562 cout <<
"p0=" << p0 <<
",p1=" << p1 << endl;
571 for (
unsigned i = 0; i < inputs.size(); i++) {
572 if (inputs[i] == p1) {
577 unsigned width = inputSizes[n] + 1;
581 for (
unsigned i = 0; i < width; i++) {
583 x += name +
"[" + char(b + i) +
"]";
585 }
else if (cw[0] ==
'1') {
587 for (
unsigned i = 1; i < width; i++) {
588 if (i > 1) x +=
" | ";
589 x += name +
"[" + char(b + i) +
"]";
591 }
else if (cw[0] ==
'2') {
593 x =
"(" + name +
"[0] & " + name +
"[1] )";
594 for (
unsigned i = 2; i < width; i++) {
596 x += name +
"[" + char(b + i) +
"]";
598 }
else if (cw[0] ==
'3') {
600 for (
unsigned i = 2; i < width; i++) {
601 if (i > 2) x +=
" | ";
602 x += name +
"[" + char(b + i) +
"]";
604 }
else if (cw[0] ==
'4') {
606 x =
"(" + name +
"[1] & " + name +
"[2] )";
607 x +=
"| (" + name +
"[0] & " + name +
"[2] )";
608 for (
unsigned i = 3; i < width; i++) {
610 x += name +
"[" + char(b + i) +
"]";
613 cout <<
"decodeLogic !!! [> with 4] is not supported"
615 cout <<
" " << logic << endl;
621 cout <<
" s=" << p1 <<
" " << p0 <<
" " << cw
623 cout <<
" name=" << name <<
",width=" << width
625 cout <<
" x={" << x <<
"}" << endl;
628 strcpy(p1, x.c_str());
634 else if (p0[0] ==
'<') {
635 cout <<
"decodeLogic !!! [> with 4] is not supported"
640 else if (p0[0] ==
'=') {
645 for (
unsigned i = 0; i < inputs.size(); i++) {
646 if (inputs[i] == p1) {
651 unsigned width = inputSizes[n] + 1;
655 for (
unsigned i = 0; i < width; i++) {
657 x +=
"!" + name +
"[" + char(b + i) +
"]";
660 cout <<
"decodeLogic !!! '== n' is not supported"
662 cout <<
" " << logic << endl;
666 cout <<
" s=" << p1 <<
" " << p0 <<
" " << cw
668 cout <<
" name=" << name <<
",width=" << width
670 cout <<
" x={" << x <<
"}" << endl;
675 strcpy(p1, x.c_str());
680 if (f.size() && (p1[0] != 0)) f +=
" ";
693 Term::Term(
const string& a)
697 strcpy(b, _sentence.c_str());
702 if (w[0] == 0)
break;
703 _words.push_back(
string(w));
708 vector<string> termCandidates;
710 for (
unsigned i = 0; i < _sentence.size(); i++) {
711 char c = _sentence[i];
717 }
else if (c ==
')') {
729 termCandidates.push_back(t);
736 termCandidates.push_back(t);
739 if (termCandidates.size() > 1) {
740 for (
unsigned i = 0; i < termCandidates.size(); i++) {
741 Term x(termCandidates[i]);
757 Term::sentence(
void)
const
768 if (_terms.size() > 1) {
769 for (
unsigned i = 0; i < _terms.size(); i++) {
773 _expanded += _terms[i].expanded();
779 _expanded = _sentence;
783 for (
unsigned i = 0; i < _expanded.size(); i++) {
784 char c = _expanded[i];
787 }
else if (c ==
')') {
797 string s0 = _expanded.substr(0, a - 1);
799 string p = _expanded.substr(a + 1, b - a - 1);
800 string s1 = _expanded.substr(b + 1);
809 for (
unsigned i = 0; i < p.size(); i++) {
817 string p0 = p.substr(0, a1 - 1);
818 string p1 = p.substr(a1 + 1);
819 _expanded = s0 + p0 + s1 +
" | " + s0 +
" (" + p1 +
") " + s1;
823 _expanded = tmp.expanded();
825 _expanded = s0 + p + s1;
844 bool deleteSpace =
true;
845 for (
unsigned i = 0; i < s.size(); i++) {
848 if ((i + 1) < s.size())
851 if (d ==
'\t') d =
' ';
852 if (d ==
';') d =
' ';
853 if ((d ==
'|') && (e ==
'|')) d =
' ';
855 const bool space = (d ==
' ');
856 if (deleteSpace && space)
872 for (
unsigned i = 0; i < s.size(); i++) {
875 if ((i + 1) < s.size())
882 else if ((d ==
'!') && (e ==
' '))
888 getLogicGND(
string& s)
892 string::size_type p = s.find(g);
893 if (p != string::npos)
894 s.replace(p, 3,
"= GND", 0, 5);
899 kumacOutput(ofstream& f,
string logic,
string ftd)
901 const string tab =
" ";
906 for (
unsigned i = 0; i < logic.size(); i++)
914 if (t.empty())
break;
920 f <<
"[wait]" << endl;
921 f << tab <<
"zone 1 6 1" << endl;
922 f << tab <<
"title '" << ftd <<
" " << n + 1 <<
"/" << nO + 1 <<
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;
945 chopWord(
string& b,
string& c)
950 if ((b[i] ==
' ') || (b[i] == 0))
957 int l = b.size() - i - 1;
959 b = b.substr(i + 1, l);
967 while (s.find(
"*") != string::npos) {
968 const unsigned n = s.size();
969 for (
unsigned i = 0; i < n; i++)
971 s.replace(i, 2,
"&& ", 0, 3);
973 while (s.find(
"+") != string::npos) {
974 const unsigned n = s.size();
975 for (
unsigned i = 0; i < n; i++)
977 s.replace(i, 2,
") || ( ", 0, 6);
984 const unsigned n = s.size();
985 for (
unsigned i = 0; i < n; i++)
987 s.replace(i, 1,
"_", 0, 1);