13#define TRG_SHORT_NAMES
14#define TRGCDC_SHORT_NAMES
17#include "trg/cdc/JLUT.h"
18#include "trg/cdc/FpgaUtility.h"
19#include "trg/cdc/JSignal.h"
20#include "trg/cdc/JSignalData.h"
23#include "FpgaUtility.h"
25#include "JSignalData.h"
44 return string(
"TRGCDCJLUT 0.00");
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)
58 : m_const(in.m_const),
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)
86 void TRGCDCJLUT::setData(
int inputBitsize,
int outputBitsize,
const string& filename,
bool twosComplement)
89 coeFile.open(filename.c_str());
91 cout <<
"TRGCDCJLUT !!! can not open file : " << filename << endl;
92 cout <<
" LUT is not initialized yet" << endl;
100 int inputSize = pow(2, inputBitsize);
104 stringstream reformatCoeFile;
106 while (getline(coeFile, t_line)) {
108 t_line.erase(remove_if(t_line.begin(), t_line.end(), ::isspace), t_line.end());
110 if (t_line.size() == 0)
continue;
112 size_t t_iFind = t_line.find(
";");
113 if (t_iFind != string::npos) {
114 t_line.erase(t_iFind + 1, string::npos);
117 if (t_line ==
";")
continue;
119 if (t_line.find(
";") == string::npos) reformatCoeFile << t_line;
121 else reformatCoeFile << t_line.substr(0, t_line.size() - 1) << endl;
126 vector<string> t_rawData;
127 while (getline(reformatCoeFile, t_line)) {
130 size_t t_iFind = t_line.find(
"=");
131 if (t_iFind != string::npos) {
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") {
138 string t_dataLine = t_line.substr(t_iFind + 1, string::npos);
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);
146 t_rawData.push_back(t_dataLine);
151 cout <<
"[Error] TRGCDCJLUT::setData() => .coe format keyword is wrong. Aborting" << endl;
155 cout <<
"[Error] TRGCDCJLUT::setData() => .coe format is wrong. Needs keywords. Aborting" << endl;
163 if (!twosComplement) {
164 for (
int iData = 0; iData < int(t_rawData.size()); iData++) {
170 int nBits = t_rawData[0].size();
172 int t_max = pow(t_radix, nBits) - 1;
174 nBits = floor(log(t_max) / log(2)) + 1;
175 for (
int iData = 0; iData < int(t_rawData.size()); iData++) {
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;
189 cout <<
"TTRGCDCJLUT ... LUT(" <<
m_name <<
") initilized with " << filename << endl;
204 int lutInputBitwidth,
int lutOutputBitwidth)
207 signed long long t_int;
217 t_int = input.getMinInt();
218 t_toReal = input.getToReal();;
219 t_actual = input.getMinActual();
220 t_commonData = input.getCommonData();
222 t_int = input.getMaxInt();
223 t_actual = input.getMaxActual();
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."
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"
252 if (t_outputRealMaxInt < t_outputRealMinInt) {
253 cout <<
"[Error] TRGCDCJLUT::setFloatFunction() => t_outputRealMaxInt is smaller than t_outputRealMinInt." << endl;
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);
267 t_toReal = t_outputToReal * pow(2, tt_factor);
268 t_actual = t_outputMinActual;
278 t_actual = t_outputMaxActual - t_shiftOutputMin.
getActual();
307 if (
m_write != 1) cout <<
"[Warning] TRGCDCJLUT::m_function => input is smaller then 0. Changed to 0." << endl;
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;
335 cout <<
"[Warning] TRGCDCJLUT::m_function => output is larger then allowed max value. Changed to " <<
337 cout <<
"Could happen if invY_min and invY_max are inside x range." << endl;
415 cout <<
"<<<[LUT] " <<
m_name <<
">>>" << endl;
421 cout <<
"<<<[LUT] " <<
m_name <<
">>>" << endl;
430 cout <<
"[Error] TRGCDCJLUT::makeCOE() => m_function is not set. Aborting." << endl;
435 cout <<
"[Error] TRGCDCJLUT::makeCOE() => inputBitSize is not set. Aborting." << endl;
440 if (fileName !=
"") t_fileName = fileName;
441 else if (
name() !=
"") {
443 t_fileName +=
".coe";
444 }
else t_fileName =
"untitled.coe";
447 coeFile.open(t_fileName.c_str());
449 coeFile <<
"* [Information for COE " << fileName <<
" ]" << endl;
460 coeFile <<
"memory_initialization_radix=10;" << endl;
461 coeFile <<
"memory_initialization_vector=" << endl;
463 for (
unsigned index = 0; index < pow(2,
m_inputBitsize) - 1; index++) {
476 std::map<std::string, std::map<std::string, double>* >& m_intStorage)
478 double floatInput = (*m_intStorage[
"int"])[in] +
m_inputOffset;
481 stringstream t_offsetName;
483 (*m_intStorage[
"type"])[t_offsetName.str()] = 1;
485 (*m_intStorage[
"toReal"])[t_offsetName.str()] =
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;
492 (*m_intStorage[
"toReal"])[out] =
m_toReal;
495 (*m_intStorage[
"realInt"])[out] = (*m_intStorage[
"int"])[out] *
m_toReal;;
497 return t_offsetName.str();
514 cout <<
"[Warning] TRGCDCJLUT::operate() => Offsetted input minInt is not 0." << endl;
535 string t_shiftOffsetInputCode =
"";
539 t_shiftOffsetInputCode);
558 t_offsetOutput.
setName(
"lut_" + out.getName() +
"_out");
578 if (out.getPrintVhdl() == 1) {
579 string t_finalCode =
lutVhdlCode(t_shiftOffsetInputCode);
580 out.getCommonData()->setVhdlOutProcess(out.getCommonData()->getVhdlOutProcess() + t_finalCode);
583 map<string, vector<int> >& t_signals = out.getCommonData()->m_signals;
584 if (!t_signals.count(t_offsetOutput.
getName())) {
593 return t_offsetOutput;
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();
A class to use LUTs for TRGCDC.
double m_outputIntMax
The integer offset of the output for LUT.
bool m_write
VHDL write flag for signals. 1: Write is on. 0: Write is off.
int m_outputFlag
Flag for output. 0 is from function. 1 is from LUT file.
TRGCDCJSignal m_shiftOffsetOutputMax
The maximum output value that is bit shifted.
double m_toReal
The factor to change integer to float value.
TRGCDCJSignal m_shiftOutputMin
The minimum output signal that is bit shifted.
std::function< int(int)> m_function
LUT function.
double m_outputOffset
The float offset of the output for LUT.
int m_inputShiftBits
The number of bits to shift to make input the wanted bit width.
TRGCDCJSignal m_inputMin
The minimum input signal.
std::string m_fileName
LUT filename.
double m_outputType
The output type of the LUT. (Will be deprecated.)
int m_inputBitsize
input bitsize. Number of bits.
std::vector< int > m_data
LUT data.
TRGCDCJSignal m_shiftOffsetInput
The bt shifted offset signal of input.
int m_outputBitsize
output bitsize. Number of bits.
std::function< double(double)> m_floatFunction
Float LUT function.
double m_inputOffset
Changing float function to int function variables.
TRGCDCJSignal m_inputMax
The maximum input signal.
double m_inputToReal
The factor of the input integer to change to float value.
double m_outputNBitsWithOffset
The output bit width with offset. (Will be deprecated.)
A class to hold common data for JSignals.
A class to use Signals for TRGCDC 3D tracker.
static double arbToDouble(std::string in, int inRadix)
Radix changing functions.
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.
static std::string twosComplementToSigned(std::string in)
Changes string two complements to string signed binary(-sign).
static double roundInt(double value)
Round double value.
double getActual() const
Get float value of signal.
TRGCDCJSignal const invOffset(TRGCDCJSignal const &valueMin) const
Outputs a signal that is not offset.
TRGCDCJLUT(const std::string &name="no_name")
Contructor.
std::string getName() const
Get name of signal.
std::string getVhdlOutputFile() const
Gets the vhdl output file name.
void setCommonData(TRGCDCJSignalData *)
Sets JSignalData to signal.
virtual ~TRGCDCJLUT()
Destructor.
TRGCDCJSignal & assignTo(TRGCDCJSignal const &rhs, int targetClock, std::string &finalCode)
Assign operator with setting target clock with outputing final code.
TRGCDCJSignal const offset(TRGCDCJSignal const &valueMin) const
Outputs an offset signal which is an unsigned signal.
void setData(int inputBitsize, int outputBitSize, const std::string &filename, bool twoscomplement=0)
set LookUpTable(LUT) data.
void setOutputBitsize(int outputBitsize)
set output bitsize.
TRGCDCJSignal const shift(int nBits, int operate=1) const
Shift signal. Shift direction is right. operate=0 to change m_toReal(unit).
TRGCDCJSignalData * getCommonData() const
Get the sharged JSignalData.
static TRGCDCJSignal const toSlv(TRGCDCJSignal const &in, int test)
Change signal to std_logic_vector using vector<bool>. (Will be deprecated.)
void setVhdlOutputFile(const std::string &filename)
Sets ouptut VHDL filename.
std::string version(void) const
returns version.
bool getPrintVhdl() const
Gets the print vhdl flag from common JSignalData class.
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.
double getFloatOutput(double) const
get output using float values.
int getFinishClock() const
Gets clock tick where signal is make.
void setBitsize(int inputBitsize, int outputBitsize)
set input & output bitsize.
std::function< int(int)> getFunction()
get LUT function using int values.
std::string getFileName() const
get LUT filename
signed long long getInt() const
Get integer value of signal.
void makeCOE(const std::string &fileName="")
make COE file.
double getToReal() const
Get toReal value of signal.
void setFunction(std::function< int(int)> function)
set LUT function directly using int values.
int getInputBitsize() const
get input bitsize
void setName(const std::string &name)
Setters, Getters Sets name of signal.
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.)
int getOutputType() const
get output type.
std::string name(void) const
Operators.
int getOutput(int) const
get output using int values.
std::function< double(double)> getFloatFunction()
get LUT function using float values.
double getBitsize() const
Get bitwidth of signal.
void dump()
Print variables for LUT.
void setOutputFlag(int outputFlag)
set output flag.
int getOutputBitsize() const
get output bitsize
void setPrintVhdl(bool printVhdl)
Sets print flag for signal.
int getType() const
Get type of signal.
std::string const lutVhdlCode(std::string const &shiftOffsetInput) const
Creates vhdlCode for lut.
int calFinishClock() const
Claculates the clock tick where the signal is valid.
double getRealInt() const
Get converted float value of integer.
void setInputBitsize(int inputBitsize)
set input bitsize.
int getOutputFlag() const
get output type.
signed long long getMinInt() const
Get minimum integer value of signal.
Abstract base class for different kinds of events.