14 #define TRG_SHORT_NAMES
21 #include <boost/algorithm/string.hpp>
22 #include <boost/lexical_cast.hpp>
23 #include "trg/trg/Utilities.h"
29 #define NAME "TRGCDCInterfaceBuilder"
30 #define VERSION string("0.00")
31 #define ENV_PATH "BELLE2_LOCAL_DIR"
32 #define FRONT_END "/OpticalLinkFrontEnd.h"
33 #define MARKER_0 "VHDL : begin"
34 #define MARKER_1 "VHDL : end"
36 #define KEYWORD_0 "TRGOpticalLinkVersion"
37 #define KEYWORD_1 "VHDL : type" // TRGSignalVector
38 #define KEYWORD_2 "vector<const TRGSignalVector" // vector<TRGSignalVector
39 #define KEYWORD_3 "vector<const TRGSignal" // vector<TRGSignal
40 #define KEYWORD_4 "TRGSignal" // TRGSignal
42 const string Keys[] = {
49 vector<string> Definitions;
50 unsigned LinesOfs2 = 0;
51 const string Out0name =
".TRGCDCInterfaceBuilder0.vhdl";
52 const string Out1name =
".TRGCDCInterfaceBuilder1.vhdl";
53 const string Out2name =
".TRGCDCInterfaceBuilder2.vhdl";
54 const string Out3name =
".TRGCDCInterfaceBuilder3.vhdl";
55 ofstream Ofs0(Out0name.c_str(), ios::out);
56 ofstream Ofs1(Out1name.c_str(), ios::out);
57 ofstream Ofs2(Out2name.c_str(), ios::out);
58 ofstream Ofs3(Out3name.c_str(), ios::out);
59 const string Tab =
" ";
61 void Parser(ifstream&);
62 void Parser0(
const string&);
63 void ParserTS(
const string&);
64 void ParserVTS(
const string&);
65 void ParserVTS2(
const string&);
66 void ParserTSV(
const string&);
67 void ParserVTSV(
const string&);
69 void TypeDefinition(
const string& name,
const string& line);
70 void TypeDefinition(
const string& name,
unsigned size,
const string& base);
71 void Functions(
const string& name,
unsigned size);
72 void GetName(
const string& line,
80 void (* ParserN[N_KEYWORDS])(
const string&) = {
89 main(
int argc,
char**)
93 cout << NAME <<
" ... " << VERSION << endl;
97 cout << NAME <<
" !!! no argument is necessary" << endl;
103 const string ts1 = TRGUtil::dateString();
106 const string path = getenv(ENV_PATH);
107 const string indir = path +
"/include/trg/cdc";
111 cout << NAME <<
" !!! can not open file" << endl
112 <<
" " << Out0name << endl;
116 cout << NAME <<
" !!! can not open file" << endl
117 <<
" " << Out1name << endl;
121 cout << NAME <<
" !!! can not open file" << endl
122 <<
" " << Out2name << endl;
126 cout << NAME <<
" !!! can not open file" << endl
127 <<
" " << Out3name << endl;
132 Ofs0 <<
"-- Generated by " << NAME <<
" " << VERSION << endl;
133 Ofs0 <<
"-- " << ts1 << endl;
134 Ofs0 <<
"--" << endl;
138 const string fename = indir + FRONT_END;
139 ifstream fefile(fename.c_str(), ios::in);
141 cout << NAME <<
" !!! can not open file" << endl
142 <<
" " << fename << endl;
147 Ofs0 <<
"-- Data file : " << endl;
148 Ofs0 <<
"-- " << fename << endl;
149 Ofs0 <<
"--" << endl;
151 Ofs0 <<
"library IEEE;" << endl;
152 Ofs0 <<
"use IEEE.STD_LOGIC_1164.all;" << endl << endl;
153 Ofs0 <<
"package CDCTRG_pkg is" << endl << endl;
155 Ofs1 <<
"package body CDCTRG_pkg is" << endl << endl;
158 Ofs2 << Tab <<
"-- TRGFrame" << endl;
159 Ofs2 << Tab <<
"function TRGFrame (" << endl;
162 Ofs3 << Tab <<
"begin" << endl;
163 Ofs3 << Tab <<
" return" << endl;
170 string tmp = Out2name +
".tmp";
171 string cmd =
"cp " + Out2name +
" " + tmp;
173 ofstream ofs2(Out2name.c_str(), ios::app);
176 string tmq = Out2name +
".tmp2";
177 cmd =
"sed -e 's/std_logic_vector;/std_logic_vector is/g' " + tmp
182 ofs2 <<
"end;" << endl << endl;
191 cmd =
"cat " + Out0name +
" " + Out2name +
" " + Out1name +
" "
192 + tmq +
" " + Out3name +
" > TRGCDCInterfaceBuilder.vhdl";
196 cout << NAME <<
" ... terminated" << endl;
200 Parser(ifstream& ifs)
206 bool beginConversion =
false;
207 bool endConversion =
false;
208 while (! ifs.eof()) {
214 string::size_type pos = l.find(MARKER_0);
215 if (pos != string::npos) {
216 beginConversion =
true;
219 pos = l.find(MARKER_1);
220 if (pos != string::npos) {
221 endConversion =
true;
226 if (beginConversion && (! endConversion)) {
229 for (
unsigned i = 0; i < N_KEYWORDS; i++) {
232 pos = l.find(Keys[i].c_str());
233 if (pos != string::npos) {
238 }
else if (endConversion) {
242 Ofs2 << Tab <<
" return std_logic_vector;" << endl << endl;
244 Ofs3 <<
");" << endl;
245 Ofs3 << Tab <<
"end TRGFrame;" << endl;
246 Ofs3 <<
"end;" << endl;
255 Parser0(
const string& line)
259 l = TRGUtil::cdrstring(l);
260 const string car1 = TRGUtil::carstring(l);
261 l = TRGUtil::cdrstring(l);
262 const string car2 = TRGUtil::carstring(l);
263 l = TRGUtil::cdrstring(l);
264 Ofs0 <<
"-- Version : " << car1 <<
" " << car2 << endl;
270 ParserTS(
const string& l)
276 GetName(l, name, none, array, size);
281 Ofs3 <<
" &" << endl;
285 char v =
'a' + LinesOfs2;
286 Ofs2 << Tab <<
" " << v <<
" : in " << name;
289 Ofs3 << Tab <<
" ToLogicVector(" << name <<
")";
296 ParserTSV(
const string& line)
300 l = TRGUtil::cdrstring(l);
302 l = TRGUtil::cdrstring(l);
304 l = TRGUtil::cdrstring(l);
308 string m = TRGUtil::cdrstring(l);
309 string car4 = TRGUtil::carstring(m);
311 Definitions.push_back(car4);
317 car4 = TRGUtil::carstring(m);
318 m = TRGUtil::cdrstring(m);
319 if (car4 ==
"downto") {
326 for (
unsigned i = 0; i < top.size(); i++) {
332 unsigned arraySize = boost::lexical_cast<int>(dim) + 1;
335 TypeDefinition(vnv, l);
338 Functions(vnv, arraySize);
343 ParserVTS(
const string& l)
350 GetName(l, name, base, array, size);
353 bool defined =
false;
354 for (
unsigned i = 0; i < Definitions.size(); i++) {
355 if (Definitions[i] == name) {
361 cout <<
"TRGCDCInterfaceBuilder::ParserVTS !!! " << name
362 <<
" is not defined" << endl;
365 TypeDefinition(name, size,
"std_logic");
368 Functions(name, size);
376 ParserVTSV(
const string& l)
383 GetName(l, name, base, array, size);
386 bool defined =
false;
387 for (
unsigned i = 0; i < Definitions.size(); i++) {
388 if (Definitions[i] == name) {
394 cout <<
"TRGCDCInterfaceBuilder::ParserVTS !!! " << name
395 <<
" is not defined" << endl;
398 TypeDefinition(name, size, base);
401 Functions(name, size);
408 TypeDefinition(
const string& name,
const string& l)
412 Ofs0 << Tab <<
"-- " << name << endl;
413 Ofs0 << Tab << l << endl;
418 TypeDefinition(
const string& name,
unsigned size,
const string& base)
422 Ofs0 << Tab <<
"-- " << name << endl;
423 Ofs0 << Tab <<
"type " << name <<
" is array (" << size - 1
424 <<
" downto 0) of " << base <<
";" << endl;
429 Functions(
const string& name,
unsigned arraySize)
433 Ofs0 << Tab <<
"-- " << name << endl;
434 Ofs0 << Tab <<
"function ToLogicVector (a : in " << name
435 <<
") return std_logic_vector;" << endl;
438 Ofs1 << Tab <<
"-- " << name <<
" to std_logic_vector" << endl;
439 Ofs1 << Tab <<
"function ToLogicVector (a : in " << name
440 <<
") return std_logic_vector is" << endl;
441 Ofs1 << Tab <<
"begin" << endl;
442 Ofs1 << Tab <<
" return" << endl;
443 for (
unsigned i = arraySize; i > 0; --i) {
445 Ofs1 << Tab <<
" a(" << i - 1 <<
") &"
448 Ofs1 << Tab <<
" a(" << i - 1 <<
");"
451 Ofs1 << Tab <<
"end ToLogicVector;" << endl;
455 Ofs0 << Tab <<
"functino To" << name
456 <<
" (a : in std_logic_vector) return " << name <<
";" << endl;
460 Ofs1 << Tab <<
"-- std_logic_vector to " << name << endl;
461 Ofs1 << Tab <<
"function To" << name
462 <<
" (a : in std_logic_vector) return " << name <<
" is" << endl;
463 Ofs1 << Tab <<
"begin" << endl;
464 Ofs1 << Tab <<
" return" << endl;
465 for (
unsigned i = arraySize; i > 0; --i) {
467 Ofs1 << Tab <<
" a(" << i - 1 <<
") &" << endl;
469 Ofs1 << Tab <<
" a(" << i - 1 <<
");" << endl;
471 Ofs1 << Tab <<
"end " << name <<
";" << endl;
476 GetName(
const string& line,
487 string::size_type pos = l.find(
"std::vector");
488 if (pos != string::npos)
494 string car = TRGUtil::carstring(l);
495 l = TRGUtil::cdrstring(l);
504 string vnv = name.substr(1, name.size() - 2);
505 string vnf = vnv.substr(0, 1);
506 boost::to_upper(vnf);
517 for (
unsigned i = 0; i < vnv.size(); i++) {
518 if (isdigit(vnv[i])) {
525 size = boost::lexical_cast<int>(dim);