13#define TRG_SHORT_NAMES
20#include <boost/algorithm/string.hpp>
21#include <boost/lexical_cast.hpp>
22#include "trg/trg/Utilities.h"
28#define NAME "TRGCDCInterfaceBuilder"
29#define VERSION string("0.00")
30#define ENV_PATH "BELLE2_LOCAL_DIR"
31#define FRONT_END "/OpticalLinkFrontEnd.h"
32#define MARKER_0 "VHDL : begin"
33#define MARKER_1 "VHDL : end"
35#define KEYWORD_0 "TRGOpticalLinkVersion"
36#define KEYWORD_1 "VHDL : type"
37#define KEYWORD_2 "vector<const TRGSignalVector"
38#define KEYWORD_3 "vector<const TRGSignal"
39#define KEYWORD_4 "TRGSignal"
41const string Keys[] = {
48vector<string> Definitions;
49unsigned LinesOfs2 = 0;
50const string Out0name =
".TRGCDCInterfaceBuilder0.vhdl";
51const string Out1name =
".TRGCDCInterfaceBuilder1.vhdl";
52const string Out2name =
".TRGCDCInterfaceBuilder2.vhdl";
53const string Out3name =
".TRGCDCInterfaceBuilder3.vhdl";
54ofstream Ofs0(Out0name.c_str(), ios::out);
55ofstream Ofs1(Out1name.c_str(), ios::out);
56ofstream Ofs2(Out2name.c_str(), ios::out);
57ofstream Ofs3(Out3name.c_str(), ios::out);
58const string Tab =
" ";
60void Parser(ifstream&);
61void Parser0(
const string&);
62void ParserTS(
const string&);
63void ParserVTS(
const string&);
64void ParserVTS2(
const string&);
65void ParserTSV(
const string&);
66void ParserVTSV(
const string&);
68void TypeDefinition(
const string& name,
const string& line);
69void TypeDefinition(
const string& name,
unsigned size,
const string& base);
70void Functions(
const string& name,
unsigned size);
71void GetName(
const string& line,
79void (* ParserN[N_KEYWORDS])(
const string&) = {
92 cout << NAME <<
" ... " << VERSION << endl;
96 cout << NAME <<
" !!! no argument is necessary" << endl;
102 const string ts1 = TRGUtil::dateString();
105 const char* envPath = getenv(ENV_PATH);
106 const string path = (envPath !=
nullptr ? envPath :
"");
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;
205 bool beginConversion =
false;
206 bool endConversion =
false;
207 while (! ifs.eof()) {
212 string::size_type pos = l.find(MARKER_0);
213 if (pos != string::npos) {
214 beginConversion =
true;
217 pos = l.find(MARKER_1);
218 if (pos != string::npos) {
219 endConversion =
true;
224 if (beginConversion && (! endConversion)) {
227 for (
unsigned i = 0; i < N_KEYWORDS; i++) {
230 pos = l.find(Keys[i].c_str());
231 if (pos != string::npos) {
236 }
else if (endConversion) {
240 Ofs2 << Tab <<
" return std_logic_vector;" << endl << endl;
242 Ofs3 <<
");" << endl;
243 Ofs3 << Tab <<
"end TRGFrame;" << endl;
244 Ofs3 <<
"end;" << endl;
253Parser0(
const string& line)
257 l = TRGUtil::cdrstring(l);
258 const string car1 = TRGUtil::carstring(l);
259 l = TRGUtil::cdrstring(l);
260 const string car2 = TRGUtil::carstring(l);
261 l = TRGUtil::cdrstring(l);
262 Ofs0 <<
"-- Version : " << car1 <<
" " << car2 << endl;
268ParserTS(
const string& l)
274 GetName(l, name, none, array, size);
279 Ofs3 <<
" &" << endl;
283 char v =
'a' + LinesOfs2;
284 Ofs2 << Tab <<
" " << v <<
" : in " << name;
287 Ofs3 << Tab <<
" ToLogicVector(" << name <<
")";
294ParserTSV(
const string& line)
298 l = TRGUtil::cdrstring(l);
300 l = TRGUtil::cdrstring(l);
302 l = TRGUtil::cdrstring(l);
306 string m = TRGUtil::cdrstring(l);
307 string car4 = TRGUtil::carstring(m);
309 Definitions.push_back(car4);
315 car4 = TRGUtil::carstring(m);
316 m = TRGUtil::cdrstring(m);
317 if (car4 ==
"downto") {
324 for (
unsigned i = 0; i < top.size(); i++) {
330 unsigned arraySize = boost::lexical_cast<int>(dim) + 1;
333 TypeDefinition(vnv, l);
336 Functions(vnv, arraySize);
341ParserVTS(
const string& l)
348 GetName(l, name, base, array, size);
351 bool defined =
false;
352 for (
unsigned i = 0; i < Definitions.size(); i++) {
353 if (Definitions[i] == name) {
359 cout <<
"TRGCDCInterfaceBuilder::ParserVTS !!! " << name
360 <<
" is not defined" << endl;
363 TypeDefinition(name, size,
"std_logic");
366 Functions(name, size);
374ParserVTSV(
const string& l)
381 GetName(l, name, base, array, size);
384 bool defined =
false;
385 for (
unsigned i = 0; i < Definitions.size(); i++) {
386 if (Definitions[i] == name) {
392 cout <<
"TRGCDCInterfaceBuilder::ParserVTS !!! " << name
393 <<
" is not defined" << endl;
396 TypeDefinition(name, size, base);
399 Functions(name, size);
406TypeDefinition(
const string& name,
const string& l)
410 Ofs0 << Tab <<
"-- " << name << endl;
411 Ofs0 << Tab << l << endl;
416TypeDefinition(
const string& name,
unsigned size,
const string& base)
420 Ofs0 << Tab <<
"-- " << name << endl;
421 Ofs0 << Tab <<
"type " << name <<
" is array (" << size - 1
422 <<
" downto 0) of " << base <<
";" << endl;
427Functions(
const string& name,
unsigned arraySize)
431 Ofs0 << Tab <<
"-- " << name << endl;
432 Ofs0 << Tab <<
"function ToLogicVector (a : in " << name
433 <<
") return std_logic_vector;" << endl;
436 Ofs1 << Tab <<
"-- " << name <<
" to std_logic_vector" << endl;
437 Ofs1 << Tab <<
"function ToLogicVector (a : in " << name
438 <<
") return std_logic_vector is" << endl;
439 Ofs1 << Tab <<
"begin" << endl;
440 Ofs1 << Tab <<
" return" << endl;
441 for (
unsigned i = arraySize; i > 0; --i) {
443 Ofs1 << Tab <<
" a(" << i - 1 <<
") &"
446 Ofs1 << Tab <<
" a(" << i - 1 <<
");"
449 Ofs1 << Tab <<
"end ToLogicVector;" << endl;
453 Ofs0 << Tab <<
"function To" << name
454 <<
" (a : in std_logic_vector) return " << name <<
";" << endl;
458 Ofs1 << Tab <<
"-- std_logic_vector to " << name << endl;
459 Ofs1 << Tab <<
"function To" << name
460 <<
" (a : in std_logic_vector) return " << name <<
" is" << endl;
461 Ofs1 << Tab <<
"begin" << endl;
462 Ofs1 << Tab <<
" return" << endl;
463 for (
unsigned i = arraySize; i > 0; --i) {
465 Ofs1 << Tab <<
" a(" << i - 1 <<
") &" << endl;
467 Ofs1 << Tab <<
" a(" << i - 1 <<
");" << endl;
469 Ofs1 << Tab <<
"end " << name <<
";" << endl;
474GetName(
const string& line,
485 string::size_type pos = l.find(
"std::vector");
486 if (pos != string::npos)
492 string car = TRGUtil::carstring(l);
493 l = TRGUtil::cdrstring(l);
502 string vnv = name.substr(1, name.size() - 2);
503 string vnf = vnv.substr(0, 1);
504 boost::to_upper(vnf);
515 for (
unsigned i = 0; i < vnv.size(); i++) {
516 if (isdigit(vnv[i])) {
523 size = boost::lexical_cast<int>(dim);
Abstract base class for different kinds of events.