Belle II Software  release-08-01-10
JLUT.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 //-----------------------------------------------------------------------------
10 // Description : A class for LUT in TRGCDC
11 //-----------------------------------------------------------------------------
12 
13 #define TRG_SHORT_NAMES
14 #define TRGCDC_SHORT_NAMES
15 
16 #ifndef __EXTERNAL__
17 #include "trg/cdc/JLUT.h"
18 #include "trg/cdc/FpgaUtility.h"
19 #include "trg/cdc/JSignal.h"
20 #include "trg/cdc/JSignalData.h"
21 #else
22 #include "JLUT.h"
23 #include "FpgaUtility.h"
24 #include "JSignal.h"
25 #include "JSignalData.h"
26 #endif
27 #include <algorithm>
28 #include <iostream>
29 #include <sstream>
30 #include <fstream>
31 #include <cstdlib>
32 #include <cmath>
33 
34 using namespace std;
35 
36 namespace Belle2 {
42  std::string TRGCDCJLUT::version(void) const
43  {
44  return string("TRGCDCJLUT 0.00");
45  }
46 
47  TRGCDCJLUT::TRGCDCJLUT(const string& name)
48  : m_name(name),
49  m_outputFlag(0), m_inputBitsize(0), m_outputBitsize(0),
50  m_inputOffset(0), m_outputOffset(0), m_outputIntMax(0),
51  m_inputToReal(0), m_toReal(0), m_inputShiftBits(0),
52  m_outputType(0), m_outputNBitsWithOffset(0)
53  {
54  m_write = 0;
55  }
56 
58  : m_const(in.m_const),
59  m_name(in.m_name),
60  m_fileName(in.m_fileName),
61  m_function(in.m_function),
62  m_floatFunction(in.m_floatFunction),
63  m_inputMin(in.m_inputMin),
64  m_inputMax(in.m_inputMax),
65  m_shiftOutputMin(in.m_shiftOutputMin),
66  m_shiftOffsetOutputMax(in.m_shiftOffsetOutputMax)
67  {
76  m_toReal = in.m_toReal;
79  m_write = 0;
80  }
81 
83  {
84  }
85 
86  void TRGCDCJLUT::setData(int inputBitsize, int outputBitsize, const string& filename, bool twosComplement)
87  {
88  ifstream coeFile;
89  coeFile.open(filename.c_str());
90  if (coeFile.fail()) {
91  cout << "TRGCDCJLUT !!! can not open file : " << filename << endl;
92  cout << " LUT is not initialized yet" << endl;
93  return;
94  } else {
95  // Set values
96  setInputBitsize(inputBitsize);
97  setOutputBitsize(outputBitsize);
98  m_fileName = filename;
99  setOutputFlag(1);
100  int inputSize = pow(2, inputBitsize);
101  m_data.resize(inputSize);
102  // Read file
103  string t_line;
104  stringstream reformatCoeFile;
105  // Reformat file.
106  while (getline(coeFile, t_line)) {
107  // Remove all spaces
108  t_line.erase(remove_if(t_line.begin(), t_line.end(), ::isspace), t_line.end());
109  // Remove blank lines
110  if (t_line.size() == 0) continue;
111  // Remove all characters after ';'
112  size_t t_iFind = t_line.find(";");
113  if (t_iFind != string::npos) {
114  t_line.erase(t_iFind + 1, string::npos);
115  }
116  // Remove lines for comments
117  if (t_line == ";") continue;
118  // Combine lines until ';'
119  if (t_line.find(";") == string::npos) reformatCoeFile << t_line;
120  // Replace ';' with endl
121  else reformatCoeFile << t_line.substr(0, t_line.size() - 1) << endl;
122  }
123  // Process reformatted file.
124  // vector<string> keywords = {"memory_initialization_radix", "memory_initialization_vector"};
125  int t_radix = 0;
126  vector<string> t_rawData;
127  while (getline(reformatCoeFile, t_line)) {
128  //cout<<t_line<<endl;
129  // Find keywords
130  size_t t_iFind = t_line.find("=");
131  if (t_iFind != string::npos) {
132  // Find values for keywords
133  // Find radix
134  if (t_line.substr(0, t_iFind) == "memory_initialization_radix") {
135  t_radix = atoi(t_line.substr(t_iFind + 1, string::npos).c_str());
136  } else if (t_line.substr(0, t_iFind) == "memory_initialization_vector") {
137  // Find data values
138  string t_dataLine = t_line.substr(t_iFind + 1, string::npos);
139  while (1) {
140  t_iFind = t_dataLine.find(",");
141  if (t_iFind != string::npos) {
142  t_rawData.push_back(t_dataLine.substr(0, t_iFind));
143  t_dataLine.erase(0, t_iFind + 1);
144  } else {
145  // last entry
146  t_rawData.push_back(t_dataLine);
147  break;
148  }
149  } // Finding data values loop
150  } else {
151  cout << "[Error] TRGCDCJLUT::setData() => .coe format keyword is wrong. Aborting" << endl;
152  break;
153  } // End of finding values for keywords
154  } else {
155  cout << "[Error] TRGCDCJLUT::setData() => .coe format is wrong. Needs keywords. Aborting" << endl;
156  break;
157  } // End of finding keywords
158  }
160  //for(auto it=t_rawData.begin(); it!=t_rawData.end(); it++){
161  // cout<<(*it)<<endl;
162  //}
163  if (!twosComplement) {
164  for (int iData = 0; iData < int(t_rawData.size()); iData++) {
165  // Change string to float octal.
166  m_data[iData] = FpgaUtility::arbToDouble(t_rawData[iData], t_radix);
167  }
168  } else {
169  // Find number of bits in binary for data.
170  int nBits = t_rawData[0].size();
171  // Calculate max value.
172  int t_max = pow(t_radix, nBits) - 1;
173  // Calculate number of bits
174  nBits = floor(log(t_max) / log(2)) + 1;
175  for (int iData = 0; iData < int(t_rawData.size()); iData++) {
176  // Change to binary.
177  string t_binary = FpgaUtility::arbToArb(t_rawData[iData], t_radix, 2, nBits);
178  // Change to signed.
179  string t_signedBinary = FpgaUtility::twosComplementToSigned(t_binary);
180  // Change to float octal.
181  m_data[iData] = FpgaUtility::arbToDouble(t_signedBinary, 2);
182  }
183  } // End filling m_data.
184  // Print all values of LUT
185  cout << "LUT(" << m_name << ") data" << endl;
186  for (int iData = 0; iData < int(m_data.size()); iData++) {
187  cout << "[" << iData << "] " << m_data[iData] << endl;
188  }
189  cout << "TTRGCDCJLUT ... LUT(" << m_name << ") initilized with " << filename << endl;
190  }
191  coeFile.close();
192  }
193 
194  void TRGCDCJLUT::setFunction(function<int(int)> function)
195  {
196  m_function = function;
197  }
198 
199 
200  int TRGCDCJLUT::setFloatFunction(function<double(double)> function,
201  TRGCDCJSignal const& input,
202  //TRGCDCJSignal const & outputMin, TRGCDCJSignal const & outputMax,
203  TRGCDCJSignal const& minInv, TRGCDCJSignal const& maxInv, double outputToReal,
204  int lutInputBitwidth, int lutOutputBitwidth)
205  {
206 
207  signed long long t_int;
208  double t_toReal;
209  double t_actual;
210  TRGCDCJSignalData* t_commonData;
211 
212  // Save values.
213  m_inputBitsize = lutInputBitwidth;
214  m_outputBitsize = lutOutputBitwidth;
215  m_floatFunction = function;
216  // Create input min and max.
217  t_int = input.getMinInt();
218  t_toReal = input.getToReal();;
219  t_actual = input.getMinActual();
220  t_commonData = input.getCommonData();
221  m_inputMin = Belle2::TRGCDCJSignal(t_int, t_toReal, t_int, t_int, t_actual, t_actual, t_actual, -1, t_commonData);
222  t_int = input.getMaxInt();
223  t_actual = input.getMaxActual();
224  m_inputMax = Belle2::TRGCDCJSignal(t_int, t_toReal, t_int, t_int, t_actual, t_actual, t_actual, -1, t_commonData);
225 
226  //cout<<"<<<m_inputMin>>>"<<endl; m_inputMin.dump();
227  //cout<<"<<<m_inputMax>>>"<<endl; m_inputMin.dump();
228 
229  // Calculates number of bits to shift for input.
230  //int t_offsetNBits = m_inputMax.offset(m_inputMin, m_inputMax).getBitsize();
231  int t_offsetNBits = m_inputMax.offset(m_inputMin).getBitsize();
232  m_inputShiftBits = t_offsetNBits - lutInputBitwidth;
233  if (m_inputShiftBits < 0) {
234  m_inputShiftBits = 0;
235  //cout<<"[Warning] TRGCDCJLUT::setFloatFunction() => Changing m_inputBitsize from "<<lutInputBitwidth<<" to optimal bitwidth "<<t_offsetNBits<<". "<<endl;
236  m_inputBitsize = t_offsetNBits;
237  }
238 
239  // Calculates number of bits to shift for lut output bitwidth to be lutOutputBitwidth.
240  //cout<<"<<<minInv>>>"<<endl; minInv.dump();
241  //cout<<"<<<maxInv>>>"<<endl; maxInv.dump();
242  double t_outputRealMinInt = function(minInv.getRealInt());
243  double t_outputRealMaxInt = function(maxInv.getRealInt());
244  if (std::isnan(t_outputRealMinInt)) {
245  cout << "[Error] TRGCDCJLUT::setFloatFunction() => t_outputRealMinInt is nan. Please change minInv signal so that function(minInv.getRealInt()) does not give nan."
246  << endl;
247  }
248  if (std::isnan(t_outputRealMaxInt)) {
249  cout << "[Error] TRGCDCJLUT::setFloatFunction() => t_outputRealMaxInt is nan. Please change maxInv so that function(maxInv.getRealInt()) does not give nan"
250  << endl;
251  }
252  if (t_outputRealMaxInt < t_outputRealMinInt) {
253  cout << "[Error] TRGCDCJLUT::setFloatFunction() => t_outputRealMaxInt is smaller than t_outputRealMinInt." << endl;
254  return 0;
255  }
256  double t_outputMinActual = function(minInv.getActual());
257  double t_outputMaxActual = function(maxInv.getActual());
258  double t_outputToReal;
259  t_outputToReal = outputToReal;
260  double tt_factor = log((max(t_outputRealMaxInt, abs(t_outputRealMinInt)) - t_outputRealMinInt) / ((pow(2,
261  lutOutputBitwidth) - 0.5) * t_outputToReal)) / log(2);
262  tt_factor = ceil(tt_factor);
263  // Create outputMin.
264  //cout<<"t_outputRealMinInt:"<<t_outputRealMinInt<<" t_outputToReal:"<<t_outputToReal<<" tt_factor:"<<tt_factor<<endl;
265  //cout<<"rountInt:"<<t_outputRealMinInt / t_outputToReal / pow(2, tt_factor)<<endl;
266  t_int = FpgaUtility::roundInt(t_outputRealMinInt / t_outputToReal / pow(2, tt_factor));
267  t_toReal = t_outputToReal * pow(2, tt_factor);
268  t_actual = t_outputMinActual;
269  //cout<<"t_int:"<<t_int<<" t_toReal:"<<t_toReal<<" t_actual:"<<t_actual<<endl;
270  Belle2::TRGCDCJSignal t_shiftOutputMin = Belle2::TRGCDCJSignal(t_int, t_toReal, t_int, t_int, t_actual, t_actual, t_actual, -1,
271  t_commonData);
272  //cout<<"<<<t_shiftOutputMin>>>"<<endl; t_shiftOutputMin.dump();
273  //cout<<"minInvRealInt: "<<minInv.getRealInt()<<" RealMinInt: "<<t_outputRealMinInt<<" int: "<<t_int<<endl;
274  //cout<<"minInvActual: "<<minInv.getActual()<<" minActual: "<<t_outputMinActual<<endl;
275  // Create outputMax.
276  t_int = FpgaUtility::roundInt(t_outputRealMaxInt / t_shiftOutputMin.getToReal()) - t_shiftOutputMin.getInt();
277  t_toReal = t_shiftOutputMin.getToReal();
278  t_actual = t_outputMaxActual - t_shiftOutputMin.getActual();
279  Belle2::TRGCDCJSignal t_shiftOffsetOutputMax = Belle2::TRGCDCJSignal(t_int, t_toReal, t_int, t_int, t_actual, t_actual, t_actual,
280  -1, t_commonData);
281  //cout<<"maxInv: "<<maxInv.getRealInt()<<" RealMaxInt: "<<t_outputRealMaxInt<<" int: "<<FpgaUtility::roundInt(t_outputRealMaxInt/t_shiftOutputMin.getToReal())<<endl;
282  //cout<<"maxInvActual: "<<maxInv.getActual()<<" maxActual: "<<t_outputMaxActual<<endl;
283  //cout<<"<<<t_shiftOffsetOutputMax>>>"<<endl; t_shiftOffsetOutputMax.dump();
284 
286  m_shiftOutputMin = t_shiftOutputMin;
287  m_shiftOffsetOutputMax = t_shiftOffsetOutputMax;
288 
289  // Save value for operator function.
290  //cout<<"actual_range: "<<max(outputActualMax, abs(outputActualMin)) - outputActualMin<<endl;
291  //cout<<"int_range: "<<(pow(2,requiredNBits)-0.5)<<endl;
292  //cout<<"intReal_range: "<<(pow(2,requiredNBits)-0.5)*outputToReal<<endl;
293  //cout<<"outputToReal: "<<outputToReal<<endl;
294  //cout<<"actual/int: "<<(max(outputActualMax, abs(outputActualMin)) - outputActualMin)/((pow(2,requiredNBits)-0.5)*outputToReal)<<endl;
295  //cout<<"t_factor_raw: "<<log((max(outputActualMax, abs(outputActualMin)) - outputActualMin)/((pow(2,requiredNBits)-0.5)*outputToReal))/log(2)<<endl;
296  //cout<<"t_factor: "<<t_factor<<endl;
297  //cout<<"m_outputOffset: "<<m_outputOffset<<endl;
298  //cout<<"m_outputType: "<<m_outputType<<endl;
299  //cout<<"m_outputNBits: "<<m_outputNBitsWithOffset<<endl;
300  //cout<<"m_toReal: "<<m_toReal<<endl;
301  // Set integer function.
302  m_function = [&](int anInt) ->
303  int{
304  // Limit input
305  if (anInt < 0)
306  {
307  if (m_write != 1) cout << "[Warning] TRGCDCJLUT::m_function => input is smaller then 0. Changed to 0." << endl;
308  anInt = 0;
309  }
310  double t_input = (anInt + m_inputMin.shift(m_inputShiftBits, 0).getInt()) * m_inputMin.shift(m_inputShiftBits, 0).getToReal();
311  double t_realResult = m_floatFunction(t_input);
312  double result = FpgaUtility::roundInt(t_realResult / m_shiftOutputMin.getToReal() - m_shiftOutputMin.getInt());
313  // Limit output according to requiredNBits
314  if (result < 0)
315  {
316  result = 0;
317  }
318  if (result > m_shiftOffsetOutputMax.getInt())
319  {
320  result = m_shiftOffsetOutputMax.getInt();
321  }
322  // Warnings. Use ideal result for checking output.
323  double t_inputActual = anInt * m_inputMin.shift(m_inputShiftBits, 0).getToReal() + m_inputMin.getActual();
324  double t_outputActual = m_floatFunction(t_inputActual);
325  if (t_outputActual < m_shiftOutputMin.getActual())
326  {
327  if (m_write != 1) {
328  cout << "[Warning] TRGCDCJLUT::m_function => output is smaller then 0. Changed to 0." << endl;
329  cout << "Could happen if invY_min and invY_max are inside x range." << endl;
330  }
331  }
332  if (t_outputActual > m_shiftOffsetOutputMax.getActual() + m_shiftOutputMin.getActual())
333  {
334  if (m_write != 1) {
335  cout << "[Warning] TRGCDCJLUT::m_function => output is larger then allowed max value. Changed to " <<
336  m_shiftOffsetOutputMax.getInt() << "." << endl;
337  cout << "Could happen if invY_min and invY_max are inside x range." << endl;
338  }
339  }
340  return result;
341  };
342 
343  return 1;
344  }
345 
346 
347  void TRGCDCJLUT::setBitsize(int inputBitsize, int outputBitsize)
348  {
349  setInputBitsize(inputBitsize);
350  setOutputBitsize(outputBitsize);
351  }
352 
353  void TRGCDCJLUT::setOutputFlag(int outputFlag)
354  {
355  m_outputFlag = outputFlag;
356  }
357 
358  void TRGCDCJLUT::setInputBitsize(int inputBitsize)
359  {
360  m_inputBitsize = inputBitsize;
361  }
362 
363  void TRGCDCJLUT::setOutputBitsize(int outputBitsize)
364  {
365  m_outputBitsize = outputBitsize;
366  }
367 
368  string TRGCDCJLUT::getFileName() const
369  {
370  return m_fileName;
371  }
372 
374  {
375  return m_inputBitsize;
376  }
377 
379  {
380  return m_outputBitsize;
381  }
382 
383  function<int(int)> TRGCDCJLUT::getFunction()
384  {
385  return m_function;
386  }
387 
388  function<double(double)> TRGCDCJLUT::getFloatFunction()
389  {
390  return m_floatFunction;
391  }
392 
394  {
395  return m_outputFlag;
396  }
397 
399  {
400  return m_outputType;
401  }
402 
403  int TRGCDCJLUT::getOutput(int input) const
404  {
405  return m_function(input);
406  }
407 
408  double TRGCDCJLUT::getFloatOutput(double input) const
409  {
410  return m_floatFunction(input);
411  }
412 
414  {
415  cout << "<<<[LUT] " << m_name << ">>>" << endl;
416  if (m_fileName != "") cout << "LUT filename: " << m_fileName << endl;
417  cout << "In bitsize: " << m_inputBitsize << " Out bitsize: " << m_outputBitsize << endl;
418  cout << "[input] offset: " << m_inputOffset << " toReal: " << m_inputToReal << endl;
419  cout << "[output] offset: " << m_outputOffset << " toReal: " << m_toReal << endl;
420  cout << "[output] type: " << m_outputType << " NBitsWithOffset: " << m_outputNBitsWithOffset << endl;
421  cout << "<<<[LUT] " << m_name << ">>>" << endl;
422  }
423 
425  void TRGCDCJLUT::makeCOE(const string& fileName)
426  {
427  m_write = 1;
428  // Check if function is set.
429  if (!m_function.operator bool()) {
430  cout << "[Error] TRGCDCJLUT::makeCOE() => m_function is not set. Aborting." << endl;
431  return;
432  }
433  // Check if m_inputBitsize is set.
434  if (m_inputBitsize == 0) {
435  cout << "[Error] TRGCDCJLUT::makeCOE() => inputBitSize is not set. Aborting." << endl;
436  return;
437  }
438  // Set file name.
439  string t_fileName;
440  if (fileName != "") t_fileName = fileName;
441  else if (name() != "") {
442  t_fileName = name();
443  t_fileName += ".coe";
444  } else t_fileName = "untitled.coe";
445  // Create file.
446  ofstream coeFile;
447  coeFile.open(t_fileName.c_str());
448 
449  coeFile << "* [Information for COE " << fileName << " ]" << endl;
450  coeFile << "* in_real = in * " << m_inputMin.getToReal() << endl;
451  coeFile << "* LUT_in = (in - " << m_inputMin.getInt() << ") * 2^" << m_inputShiftBits << endl;
452  coeFile << "* x = (LUT_in + " << m_inputMin.getInt() << " * 2^" << m_inputShiftBits << ") * " << m_inputMin.shift(m_inputShiftBits,
453  0).getToReal() << endl;
454  coeFile << "* y_int = round( f(x) / " << m_shiftOutputMin.getToReal() << " - " << m_shiftOutputMin.getInt() << " )" << endl;
455  coeFile << "* LUT_out = Limit to 0 < y_int < " << m_shiftOffsetOutputMax.getInt() << endl;
456  coeFile << "* out = LUT_out + " << m_shiftOutputMin.getInt() << endl;
457  coeFile << "* out_Real = out * " << m_shiftOutputMin.getToReal() << endl;
458 
459  // Meta data for file.
460  coeFile << "memory_initialization_radix=10;" << endl;
461  coeFile << "memory_initialization_vector=" << endl;
462  // Fill content of file.
463  for (unsigned index = 0; index < pow(2, m_inputBitsize) - 1; index++) {
464  //cout<<"Directly: "<<m_function(index)<<endl;
465  //cout<<"Indirect: "<<getOutput(index)<<endl;
466  coeFile << m_function(index) << "," << endl;
467  }
468  coeFile << m_function(pow(2, m_inputBitsize) - 1) << ";" << endl;
469  // Close file.
470  coeFile.close();
471  m_write = 0;
472  }
473 
474  // Return name of offset.
475  string TRGCDCJLUT::operate(const std::string out, const std::string in,
476  std::map<std::string, std::map<std::string, double>* >& m_intStorage)
477  {
478  double floatInput = (*m_intStorage["int"])[in] + m_inputOffset;
479  floatInput *= m_inputToReal;
480  // Make offset value.
481  stringstream t_offsetName;
482  t_offsetName << out << "_o" << m_outputOffset;
483  (*m_intStorage["type"])[t_offsetName.str()] = 1;
484  (*m_intStorage["bitSize"])[t_offsetName.str()] = m_outputBitsize;
485  (*m_intStorage["toReal"])[t_offsetName.str()] = m_toReal;
486  (*m_intStorage["real"])[t_offsetName.str()] = getFloatOutput(floatInput) - m_outputOffset * m_toReal;
487  (*m_intStorage["int"])[t_offsetName.str()] = getOutput((*m_intStorage["int"])[in]);
488  (*m_intStorage["realInt"])[t_offsetName.str()] = (*m_intStorage["int"])[t_offsetName.str()] * m_toReal;
489  // Make no offset value.
490  (*m_intStorage["type"])[out] = m_outputType;
491  (*m_intStorage["bitSize"])[out] = m_outputNBitsWithOffset;
492  (*m_intStorage["toReal"])[out] = m_toReal;
493  (*m_intStorage["real"])[out] = getFloatOutput(floatInput);
494  (*m_intStorage["int"])[out] = getOutput((*m_intStorage["int"])[in]) + m_outputOffset;
495  (*m_intStorage["realInt"])[out] = (*m_intStorage["int"])[out] * m_toReal;;
496 
497  return t_offsetName.str();
498  }
499 
501  {
502 
503  //cout<<"Operate start"<<endl;
504 
505  Belle2::TRGCDCJSignal t_offsetInput;
506  // Offset input. (If needed)
507  if (m_inputMin.getInt() != 0) {
508  //t_offsetInput = in.offset(m_inputMin, m_inputMax);
509  t_offsetInput = in.offset(m_inputMin);
510  } else {
511  t_offsetInput = in;
512  }
513  if (t_offsetInput.getMinInt() != 0) {
514  cout << "[Warning] TRGCDCJLUT::operate() => Offsetted input minInt is not 0." << endl;
515  }
516 
517  //cout<<"in"<<endl;
518  //in.dump();
523  //cout<<"m_inputMin"<<endl;
524  //m_inputMin.dump();
525  //cout<<"t_offsetInput"<<endl;
526  //t_offsetInput.dump();
527 
528  // Set m_shiftOffsetInput.
529  // [TODO] Change to constructor?
530  m_shiftOffsetInput.setName("lut_" + out.getName() + "_in");
531  // To remove warning of no m_commonData.
533 
534  // Shift input (If needed)
535  string t_shiftOffsetInputCode = "";
537  t_offsetInput.calFinishClock(), t_shiftOffsetInputCode);
538  else m_shiftOffsetInput.assignTo(Belle2::TRGCDCJSignal::toSlv(t_offsetInput), t_offsetInput.calFinishClock(),
539  t_shiftOffsetInputCode);
542 
543  //cout<<"m_inputShiftBits"<<endl;
544  //cout<<m_inputShiftBits<<endl;
545  //cout<<"m_offsetShiftInput"<<endl;
546  //m_shiftOffsetInput.dump();
547 
548  // Get output of LUT and invOffset it.
549  signed long long intOutput = getOutput(m_shiftOffsetInput.getInt());
550  //signed long long intOutput = getOutput(in.getInt());
551  int t_finishClock;
552  // Create offsetted output signal.
553  double t_actual = getFloatOutput(in.getActual()) - m_shiftOutputMin.getActual();
554  TRGCDCJSignalData* t_commonData = in.getCommonData();
555  t_finishClock = in.getFinishClock() + 1;
556  Belle2::TRGCDCJSignal t_offsetOutput(intOutput, m_shiftOutputMin.getToReal(), 0, m_shiftOffsetOutputMax.getInt(), t_actual, 0,
557  m_shiftOffsetOutputMax.getActual(), t_finishClock, t_commonData, 1);
558  t_offsetOutput.setName("lut_" + out.getName() + "_out");
559 
561  //cout<<"t_offsetOutput"<<endl;
562  //t_offsetOutput.dump();
568 
569  //out <= t_offsetOutput.invOffset(m_shiftOutputMin,m_shiftOutputMin+m_shiftOffsetOutputMax);
570  out <= t_offsetOutput.invOffset(m_shiftOutputMin);
571 
572  //cout<<"m_shiftOutputMin"<<endl;
573  //m_shiftOutputMin.dump();
574  //cout<<"out"<<endl;
575  //out.dump();
576 
577  // Print Vhdl code.
578  if (out.getPrintVhdl() == 1) {
579  string t_finalCode = lutVhdlCode(t_shiftOffsetInputCode);
580  out.getCommonData()->setVhdlOutProcess(out.getCommonData()->getVhdlOutProcess() + t_finalCode);
581  // Fill signal map for definition.
582  {
583  map<string, vector<int> >& t_signals = out.getCommonData()->m_signals;
584  if (!t_signals.count(t_offsetOutput.getName())) {
585  t_signals[t_offsetOutput.getName()] = {t_offsetOutput.getType(), (int)t_offsetOutput.getBitsize()};
586  }
587  }
588  }
589 
590  //cout<<"Operate end"<<endl;
591 
592  // LUT result value
593  return t_offsetOutput;
594  }
595 
596  std::string const TRGCDCJLUT::lutVhdlCode(string const& shiftOffsetInput) const
597  {
598  //cout<<shiftOffsetInput.getFinalCode()<<endl;
599  //cout<<out.getFinalCode()<<endl;
600  stringstream t_vhdlCode;
601  t_vhdlCode << "lut_" << m_name << "_i: entity work.lut_" << m_name << endl;
602  t_vhdlCode << " port map(" << endl;
603  t_vhdlCode << " clka=>CLKIN, " << endl;
604  t_vhdlCode << " addra=>lut_" << m_name << "_in," << endl;
605  t_vhdlCode << " douta=>lut_" << m_name << "_out" << endl;
606  t_vhdlCode << ");" << endl;
607  t_vhdlCode << shiftOffsetInput << endl;
608  return t_vhdlCode.str();
609  }
610 
612 }
A class to use LUTs for TRGCDC.
Definition: JLUT.h:35
double m_outputIntMax
The integer offset of the output for LUT.
Definition: JLUT.h:167
bool m_write
VHDL write flag for signals. 1: Write is on. 0: Write is off.
Definition: JLUT.h:158
int m_outputFlag
Flag for output. 0 is from function. 1 is from LUT file.
Definition: JLUT.h:149
TRGCDCJSignal m_shiftOffsetOutputMax
The maximum output value that is bit shifted.
Definition: JLUT.h:194
double m_toReal
The factor to change integer to float value.
Definition: JLUT.h:173
TRGCDCJSignal m_shiftOutputMin
The minimum output signal that is bit shifted.
Definition: JLUT.h:191
std::function< int(int)> m_function
LUT function.
Definition: JLUT.h:143
double m_outputOffset
The float offset of the output for LUT.
Definition: JLUT.h:164
int m_inputShiftBits
The number of bits to shift to make input the wanted bit width.
Definition: JLUT.h:176
TRGCDCJSignal m_inputMin
The minimum input signal.
Definition: JLUT.h:185
std::string m_fileName
LUT filename.
Definition: JLUT.h:140
double m_outputType
The output type of the LUT. (Will be deprecated.)
Definition: JLUT.h:179
int m_inputBitsize
input bitsize. Number of bits.
Definition: JLUT.h:152
std::vector< int > m_data
LUT data.
Definition: JLUT.h:137
TRGCDCJSignal m_shiftOffsetInput
The bt shifted offset signal of input.
Definition: JLUT.h:197
int m_outputBitsize
output bitsize. Number of bits.
Definition: JLUT.h:155
std::function< double(double)> m_floatFunction
Float LUT function.
Definition: JLUT.h:146
double m_inputOffset
Changing float function to int function variables.
Definition: JLUT.h:161
TRGCDCJSignal m_inputMax
The maximum input signal.
Definition: JLUT.h:188
double m_inputToReal
The factor of the input integer to change to float value.
Definition: JLUT.h:170
std::string m_name
Name.
Definition: JLUT.h:134
double m_outputNBitsWithOffset
The output bit width with offset. (Will be deprecated.)
Definition: JLUT.h:182
A class to hold common data for JSignals.
Definition: JSignalData.h:33
A class to use Signals for TRGCDC 3D tracker.
Definition: JSignal.h:35
static double arbToDouble(std::string in, int inRadix)
Radix changing functions.
Definition: FpgaUtility.cc:60
static std::string arbToArb(const std::string &in, int inRadix, int outRadix, int numberOfDigits=-1)
Change string number to another string number depending on radix.
Definition: FpgaUtility.cc:114
static std::string twosComplementToSigned(std::string in)
Changes string two complements to string signed binary(-sign).
Definition: FpgaUtility.cc:163
static double roundInt(double value)
Round double value.
Definition: FpgaUtility.cc:38
double getActual() const
Get float value of signal.
Definition: JSignal.cc:1392
TRGCDCJSignal const invOffset(TRGCDCJSignal const &valueMin) const
Outputs a signal that is not offset.
Definition: JSignal.cc:606
TRGCDCJLUT(const std::string &name="no_name")
Contructor.
Definition: JLUT.cc:47
std::string getName() const
Get name of signal.
Definition: JSignal.cc:1362
std::string getVhdlOutputFile() const
Gets the vhdl output file name.
Definition: JSignal.cc:1506
void setCommonData(TRGCDCJSignalData *)
Sets JSignalData to signal.
Definition: JSignal.cc:1357
virtual ~TRGCDCJLUT()
Destructor.
Definition: JLUT.cc:82
TRGCDCJSignal & assignTo(TRGCDCJSignal const &rhs, int targetClock, std::string &finalCode)
Assign operator with setting target clock with outputing final code.
Definition: JSignal.cc:172
TRGCDCJSignal const offset(TRGCDCJSignal const &valueMin) const
Outputs an offset signal which is an unsigned signal.
Definition: JSignal.cc:592
void setData(int inputBitsize, int outputBitSize, const std::string &filename, bool twoscomplement=0)
set LookUpTable(LUT) data.
Definition: JLUT.cc:86
void setOutputBitsize(int outputBitsize)
set output bitsize.
Definition: JLUT.cc:363
TRGCDCJSignal const shift(int nBits, int operate=1) const
Shift signal. Shift direction is right. operate=0 to change m_toReal(unit).
Definition: JSignal.cc:561
TRGCDCJSignalData * getCommonData() const
Get the sharged JSignalData.
Definition: JSignal.cc:1437
static TRGCDCJSignal const toSlv(TRGCDCJSignal const &in, int test)
Change signal to std_logic_vector using vector<bool>. (Will be deprecated.)
Definition: JSignal.cc:1121
void setVhdlOutputFile(const std::string &filename)
Sets ouptut VHDL filename.
Definition: JSignal.cc:1349
bool getPrintVhdl() const
Gets the print vhdl flag from common JSignalData class.
Definition: JSignal.cc:1497
int setFloatFunction(std::function< double(double)> function, TRGCDCJSignal const &input, TRGCDCJSignal const &minInv, TRGCDCJSignal const &maxInv, double outputToReal, int lutInputBitwidth, int lutOutputBitwidth)
set LUT function using JSignals.
Definition: JLUT.cc:200
double getFloatOutput(double) const
get output using float values.
Definition: JLUT.cc:408
int getFinishClock() const
Gets clock tick where signal is make.
Definition: JSignal.cc:1427
void setBitsize(int inputBitsize, int outputBitsize)
set input & output bitsize.
Definition: JLUT.cc:347
std::function< int(int)> getFunction()
get LUT function using int values.
Definition: JLUT.cc:383
std::string getFileName() const
get LUT filename
Definition: JLUT.cc:368
signed long long getInt() const
Get integer value of signal.
Definition: JSignal.cc:1377
void makeCOE(const std::string &fileName="")
make COE file.
Definition: JLUT.cc:425
double getToReal() const
Get toReal value of signal.
Definition: JSignal.cc:1422
void setFunction(std::function< int(int)> function)
set LUT function directly using int values.
Definition: JLUT.cc:194
int getInputBitsize() const
get input bitsize
Definition: JLUT.cc:373
void setName(const std::string &name)
Setters, Getters Sets name of signal.
Definition: JSignal.cc:1298
std::string operate(std::string out, std::string in, std::map< std::string, std::map< std::string, double > * > &m_intStorage)
Operate on LUT. (Will be deprecated.)
Definition: JLUT.cc:475
int getOutputType() const
get output type.
Definition: JLUT.cc:398
std::string name(void) const
Operators.
Definition: JLUT.h:205
int getOutput(int) const
get output using int values.
Definition: JLUT.cc:403
std::function< double(double)> getFloatFunction()
get LUT function using float values.
Definition: JLUT.cc:388
double getBitsize() const
Get bitwidth of signal.
Definition: JSignal.cc:1372
void dump()
Print variables for LUT.
Definition: JLUT.cc:413
void setOutputFlag(int outputFlag)
set output flag.
Definition: JLUT.cc:353
int getOutputBitsize() const
get output bitsize
Definition: JLUT.cc:378
void setPrintVhdl(bool printVhdl)
Sets print flag for signal.
Definition: JSignal.cc:1341
int getType() const
Get type of signal.
Definition: JSignal.cc:1367
std::string const lutVhdlCode(std::string const &shiftOffsetInput) const
Creates vhdlCode for lut.
Definition: JLUT.cc:596
int calFinishClock() const
Claculates the clock tick where the signal is valid.
Definition: JSignal.cc:1477
double getRealInt() const
Get converted float value of integer.
Definition: JSignal.cc:1407
void setInputBitsize(int inputBitsize)
set input bitsize.
Definition: JLUT.cc:358
int getOutputFlag() const
get output type.
Definition: JLUT.cc:393
signed long long getMinInt() const
Get minimum integer value of signal.
Definition: JSignal.cc:1382
Abstract base class for different kinds of events.