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 string path = getenv(ENV_PATH);
106 const string indir = path +
"/include/trg/cdc";
110 cout << NAME <<
" !!! can not open file" << endl
111 <<
" " << Out0name << endl;
115 cout << NAME <<
" !!! can not open file" << endl
116 <<
" " << Out1name << endl;
120 cout << NAME <<
" !!! can not open file" << endl
121 <<
" " << Out2name << endl;
125 cout << NAME <<
" !!! can not open file" << endl
126 <<
" " << Out3name << endl;
131 Ofs0 <<
"-- Generated by " << NAME <<
" " << VERSION << endl;
132 Ofs0 <<
"-- " << ts1 << endl;
133 Ofs0 <<
"--" << endl;
137 const string fename = indir + FRONT_END;
138 ifstream fefile(fename.c_str(), ios::in);
140 cout << NAME <<
" !!! can not open file" << endl
141 <<
" " << fename << endl;
146 Ofs0 <<
"-- Data file : " << endl;
147 Ofs0 <<
"-- " << fename << endl;
148 Ofs0 <<
"--" << endl;
150 Ofs0 <<
"library IEEE;" << endl;
151 Ofs0 <<
"use IEEE.STD_LOGIC_1164.all;" << endl << endl;
152 Ofs0 <<
"package CDCTRG_pkg is" << endl << endl;
154 Ofs1 <<
"package body CDCTRG_pkg is" << endl << endl;
157 Ofs2 << Tab <<
"-- TRGFrame" << endl;
158 Ofs2 << Tab <<
"function TRGFrame (" << endl;
161 Ofs3 << Tab <<
"begin" << endl;
162 Ofs3 << Tab <<
" return" << endl;
169 string tmp = Out2name +
".tmp";
170 string cmd =
"cp " + Out2name +
" " + tmp;
172 ofstream ofs2(Out2name.c_str(), ios::app);
175 string tmq = Out2name +
".tmp2";
176 cmd =
"sed -e 's/std_logic_vector;/std_logic_vector is/g' " + tmp
181 ofs2 <<
"end;" << endl << endl;
190 cmd =
"cat " + Out0name +
" " + Out2name +
" " + Out1name +
" "
191 + tmq +
" " + Out3name +
" > TRGCDCInterfaceBuilder.vhdl";
195 cout << NAME <<
" ... terminated" << endl;
204 bool beginConversion =
false;
205 bool endConversion =
false;
206 while (! ifs.eof()) {
211 string::size_type pos = l.find(MARKER_0);
212 if (pos != string::npos) {
213 beginConversion =
true;
216 pos = l.find(MARKER_1);
217 if (pos != string::npos) {
218 endConversion =
true;
223 if (beginConversion && (! endConversion)) {
226 for (
unsigned i = 0; i < N_KEYWORDS; i++) {
229 pos = l.find(Keys[i].c_str());
230 if (pos != string::npos) {
235 }
else if (endConversion) {
239 Ofs2 << Tab <<
" return std_logic_vector;" << endl << endl;
241 Ofs3 <<
");" << endl;
242 Ofs3 << Tab <<
"end TRGFrame;" << endl;
243 Ofs3 <<
"end;" << endl;
252Parser0(
const string& line)
256 l = TRGUtil::cdrstring(l);
257 const string car1 = TRGUtil::carstring(l);
258 l = TRGUtil::cdrstring(l);
259 const string car2 = TRGUtil::carstring(l);
260 l = TRGUtil::cdrstring(l);
261 Ofs0 <<
"-- Version : " << car1 <<
" " << car2 << endl;
267ParserTS(
const string& l)
273 GetName(l, name, none, array, size);
278 Ofs3 <<
" &" << endl;
282 char v =
'a' + LinesOfs2;
283 Ofs2 << Tab <<
" " << v <<
" : in " << name;
286 Ofs3 << Tab <<
" ToLogicVector(" << name <<
")";
293ParserTSV(
const string& line)
297 l = TRGUtil::cdrstring(l);
299 l = TRGUtil::cdrstring(l);
301 l = TRGUtil::cdrstring(l);
305 string m = TRGUtil::cdrstring(l);
306 string car4 = TRGUtil::carstring(m);
308 Definitions.push_back(car4);
314 car4 = TRGUtil::carstring(m);
315 m = TRGUtil::cdrstring(m);
316 if (car4 ==
"downto") {
323 for (
unsigned i = 0; i < top.size(); i++) {
329 unsigned arraySize = boost::lexical_cast<int>(dim) + 1;
332 TypeDefinition(vnv, l);
335 Functions(vnv, arraySize);
340ParserVTS(
const string& l)
347 GetName(l, name, base, array, size);
350 bool defined =
false;
351 for (
unsigned i = 0; i < Definitions.size(); i++) {
352 if (Definitions[i] == name) {
358 cout <<
"TRGCDCInterfaceBuilder::ParserVTS !!! " << name
359 <<
" is not defined" << endl;
362 TypeDefinition(name, size,
"std_logic");
365 Functions(name, size);
373ParserVTSV(
const string& l)
380 GetName(l, name, base, array, size);
383 bool defined =
false;
384 for (
unsigned i = 0; i < Definitions.size(); i++) {
385 if (Definitions[i] == name) {
391 cout <<
"TRGCDCInterfaceBuilder::ParserVTS !!! " << name
392 <<
" is not defined" << endl;
395 TypeDefinition(name, size, base);
398 Functions(name, size);
405TypeDefinition(
const string& name,
const string& l)
409 Ofs0 << Tab <<
"-- " << name << endl;
410 Ofs0 << Tab << l << endl;
415TypeDefinition(
const string& name,
unsigned size,
const string& base)
419 Ofs0 << Tab <<
"-- " << name << endl;
420 Ofs0 << Tab <<
"type " << name <<
" is array (" << size - 1
421 <<
" downto 0) of " << base <<
";" << endl;
426Functions(
const string& name,
unsigned arraySize)
430 Ofs0 << Tab <<
"-- " << name << endl;
431 Ofs0 << Tab <<
"function ToLogicVector (a : in " << name
432 <<
") return std_logic_vector;" << endl;
435 Ofs1 << Tab <<
"-- " << name <<
" to std_logic_vector" << endl;
436 Ofs1 << Tab <<
"function ToLogicVector (a : in " << name
437 <<
") return std_logic_vector is" << endl;
438 Ofs1 << Tab <<
"begin" << endl;
439 Ofs1 << Tab <<
" return" << endl;
440 for (
unsigned i = arraySize; i > 0; --i) {
442 Ofs1 << Tab <<
" a(" << i - 1 <<
") &"
445 Ofs1 << Tab <<
" a(" << i - 1 <<
");"
448 Ofs1 << Tab <<
"end ToLogicVector;" << endl;
452 Ofs0 << Tab <<
"functino To" << name
453 <<
" (a : in std_logic_vector) return " << name <<
";" << endl;
457 Ofs1 << Tab <<
"-- std_logic_vector to " << name << endl;
458 Ofs1 << Tab <<
"function To" << name
459 <<
" (a : in std_logic_vector) return " << name <<
" is" << endl;
460 Ofs1 << Tab <<
"begin" << endl;
461 Ofs1 << Tab <<
" return" << endl;
462 for (
unsigned i = arraySize; i > 0; --i) {
464 Ofs1 << Tab <<
" a(" << i - 1 <<
") &" << endl;
466 Ofs1 << Tab <<
" a(" << i - 1 <<
");" << endl;
468 Ofs1 << Tab <<
"end " << name <<
";" << endl;
473GetName(
const string& line,
484 string::size_type pos = l.find(
"std::vector");
485 if (pos != string::npos)
491 string car = TRGUtil::carstring(l);
492 l = TRGUtil::cdrstring(l);
501 string vnv = name.substr(1, name.size() - 2);
502 string vnf = vnv.substr(0, 1);
503 boost::to_upper(vnf);
514 for (
unsigned i = 0; i < vnv.size(); i++) {
515 if (isdigit(vnv[i])) {
522 size = boost::lexical_cast<int>(dim);
Abstract base class for different kinds of events.