13 #define TRG_SHORT_NAMES
14 #define TRGCDC_SHORT_NAMES
17 #include "trg/cdc/JSignal.h"
18 #include "trg/cdc/FpgaUtility.h"
19 #include "trg/cdc/JSignalData.h"
22 #include "FpgaUtility.h"
23 #include "JSignalData.h"
40 using std::stringstream;
84 m_toReal = 1 / ((pow(2, bitwidth - 1) - 0.5) / max(maxValue, fabs(minValue)));
86 m_toReal = 1 / ((pow(2, bitwidth) - 0.5) / maxValue);
100 checkInt(
"TRGCDCJSignal(int, int, double, double)");
106 (*this) =
TRGCDCJSignal(bitwidth, value, minValue, maxValue, 0, commonData);
110 signed long long const& maxInt,
double const& actual,
double const& minActual,
double const& maxActual,
int const& finishClock,
129 checkInt(
"TRGCDCJSignal(int, double, signed long long, signed long long, double, double, double)");
149 checkInt(
"TRGCDCJSignal(double, double)");
179 if (!(t_calFinishClock == -2 || t_calFinishClock == -1)) {
181 if (targetClock == -3) t_rhs.
m_finishClock = t_calFinishClock + 1;
183 if (targetClock < t_calFinishClock) cout <<
"[Error] TRGCDCJSignal::assign() => targetClock is too small." << endl;
190 if ((*this).m_bitsize != -1 || (*this).m_name !=
"")
194 if ((*this).m_bitsize == 1) {
196 if ((*this).m_bitsize != t_rhs.
m_bitsize) {
197 cout <<
"[Warning] TRGCDCJSignal::operator<=() => rhs and lhs do not have same bitsize." << endl;
200 if ((*this).m_type != t_rhs.
m_type) {
201 cout <<
"[Warning] TRGCDCJSignal::operator<=() => rhs and lhs do not have same type." << endl;
205 cout <<
"[Warning] TRGCDCJSignal::operator<=() => rhs and lhs do not have same clock." << endl;
240 string t_finalCode(finalCode);
244 if (t_finalCode ==
"printAssignVhdlCode") {
268 string finalCode =
"printAssignVhdlCode";
269 return assignTo(rhs, targetClock, finalCode);
274 string finalCode =
"printAssignVhdlCode";
275 return assignTo(rhs, -3, finalCode);
290 t_signal.
m_int *= -1;
291 t_signal.
m_minInt = -(*this).m_maxInt;
292 t_signal.
m_maxInt = -(*this).m_minInt;
323 cout <<
"TRGCDCJSignal::operator+=()" << endl;
324 cout <<
"<<<Input lhs>>>" << endl;
326 cout <<
"<<<Input lhs>>>" << endl;
327 cout <<
"<<<Input rhs>>>" << endl;
329 cout <<
"<<<Input rhs>>>" << endl;
357 t_result.
checkInt(
"TRGCDCJSignal::operator+");
359 cout <<
"<<<Output>>>" << endl;
361 cout <<
"<<<Output>>>" << endl;
362 cout <<
"TRGCDCJSignal::operator+=()" << endl;
385 cout <<
"TRGCDCJSignal::operator-=()" << endl;
386 cout <<
"<<<Input lhs>>>" << endl;
388 cout <<
"<<<Input lhs>>>" << endl;
389 cout <<
"<<<Input rhs>>>" << endl;
391 cout <<
"<<<Input rhs>>>" << endl;
419 t_result.
checkInt(
"TRGCDCJSignal::operator-");
421 cout <<
"<<<Output>>>" << endl;
423 cout <<
"<<<Output>>>" << endl;
424 cout <<
"TRGCDCJSignal::operator-=()" << endl;
448 cout <<
"TRGCDCJSignal::operator*=()" << endl;
449 cout <<
"<<<Input lhs>>>" << endl;
451 cout <<
"<<<Input lhs>>>" << endl;
452 cout <<
"<<<Input rhs>>>" << endl;
454 cout <<
"<<<Input rhs>>>" << endl;
457 int nBitsLargeShift = 0;
458 int nBitsSmallShift = 0;
460 if (t_large.
m_type == 1) {
462 nBitsLargeShift = t_large.
m_bitsize - 24;
463 t_large = t_large.
shift(nBitsLargeShift, 0);
467 nBitsLargeShift = t_large.
m_bitsize - 25;
468 t_large = t_large.
shift(nBitsLargeShift, 0);
471 if (t_small.
m_type == 1) {
473 nBitsSmallShift = t_small.
m_bitsize - 17;
474 t_small = t_small.
shift(nBitsSmallShift, 0);
478 nBitsSmallShift = t_small.
m_bitsize - 18;
479 t_small = t_small.
shift(nBitsSmallShift, 0);
484 if (orderSwap) cout << nBitsSmallShift <<
" bit right shifted input lhs>>>" << endl;
485 else cout << nBitsLargeShift <<
" bit right shifted input lhs>>>" << endl;
486 if (orderSwap) t_small.
dump();
488 cout <<
"<<<Shifted input lhs>>>" << endl;
490 if (orderSwap) cout << nBitsLargeShift <<
" bit right shifted input rhs>>>" << endl;
491 else cout << nBitsSmallShift <<
" bit right shifted input rhs>>>" << endl;
492 if (orderSwap) t_large.
dump();
494 cout <<
"<<<Shifted input rhs>>>" << endl;
526 t_result.
checkInt(
"TRGCDCJSignal::operator*");
528 cout <<
"<<<Output>>>" << endl;
530 cout <<
"<<<Output>>>" << endl;
531 cout <<
"TRGCDCJSignal::operator*=()" << endl;
541 cout <<
"-----------------------------------" << endl;
542 cout <<
"name: " <<
m_name << endl;
543 cout <<
"type: " <<
m_type << endl;
544 cout <<
"bitsize: " <<
m_bitsize << endl;
545 int t_org = cout.precision();
547 cout <<
"actual: " <<
m_actual << endl;
548 cout.precision(t_org);
549 cout <<
"int: " <<
m_int << endl;
550 cout <<
"minInt: " <<
m_minInt << endl;
551 cout <<
"maxInt: " <<
m_maxInt << endl;
555 cout <<
"toReal: " <<
m_toReal << endl;
558 cout <<
"-----------------------------------" << endl;
568 t_signal.
m_int = floor(this->
m_int / pow(2, nBits));
579 t_signal.
checkInt(
"TRGCDCJSignal::shift()");
584 }
else if (nBits != 0) {
597 if (t_offset.
calType() != 1) cout <<
"[Error] TRGCDCJSignal::offset() => t_offset min value below 0. " << endl;
600 t_offset.
checkInt(
"TRGCDCJSignal::offset()");
614 t_result.
checkInt(
"TRGCDCJSignal::invOffset()");
622 TRGCDCJSignal const& reference, std::vector<std::vector<TRGCDCJSignal> > data,
int targetClock)
629 if (data.size() == 0 || data.size() == 1) {
630 cout <<
"[Error] TRGCDCJSignal::choose() => There is not enough data." << endl;
633 if ((*(data.end() - 1)).size() != 1) {
634 cout <<
"[Error] TRGCDCJSignal::choose() => Last data doesn't contain 1 TRGCDCJSignal." << endl;
640 for (
unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
645 if (targetClock == -3) target.m_finishClock = t_maxClock + 1;
647 if (targetClock < t_maxClock) cout <<
"[Error] TRGCDCJSignal::choose() => targetClock is too small." << endl;
648 else target.m_finishClock = targetClock;
653 signed long long t_minInt = targetMin.
m_minInt;
654 signed long long t_maxInt = targetMax.
m_maxInt;
657 for (
unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
684 if (data[iCondition].size() != 1) {
686 from = &(data[iCondition][1]);
687 to = &(data[iCondition][2]);
706 for (
unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
709 if (data[iCondition].size() == 1) {
710 string t_string =
"";
711 target.assignTo(*assign, target.m_finishClock, t_string);
717 string t_string =
"";
718 target.assignTo(*assign, target.m_finishClock, t_string);
724 for (
unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
727 if (data[iCondition].size() == 1) {
728 target.m_actual = (*assign).m_actual;
733 assign = &(data[iCondition][0]);
735 target.m_actual = (*assign).m_actual;
741 target.checkInt(
"TRGCDCJSignal::choose()");
746 target.m_commonData->m_vhdlInProcess += t_finalCode +
"\n";
753 TRGCDCJSignal const& reference,
const std::vector<std::vector<TRGCDCJSignal> >& data)
755 choose(target, targetMin, targetMax, reference, data, -3);
759 const std::vector<std::vector<TRGCDCJSignal> >& data,
763 signed long long t_minInt;
764 signed long long t_maxInt;
769 for (
unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
771 if (iCondition == 0) {
782 if (t_toReal != assign->
getToReal()) cout <<
"[Error] TRGCDCJSignal::choose() => toReal are not same between assignments in data."
787 TRGCDCJSignal t_targetMax(t_maxInt, t_toReal, t_maxInt, t_maxInt, t_maxActual, t_maxActual, t_maxActual, -1, target.m_commonData);
788 TRGCDCJSignal t_targetMin(t_minInt, t_toReal, t_minInt, t_minInt, t_minActual, t_minActual, t_minActual, -1, target.m_commonData);
790 choose(target, t_targetMin, t_targetMax, reference, data, targetClock);
794 const std::vector<std::vector<TRGCDCJSignal> >& data)
796 choose(target, reference, data, -3);
804 if (data.size() == 0) {
805 cout <<
"[Error] TRGCDCJSignal::ifElse() => There is not enough data." << endl;
812 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
814 if (iCase != (data.size() - 1)) {
815 if (
comp.
m_argumentSignals.size() == 0) cout <<
"[Error] TRGCDCJSignal::ifElse() => comp signal has no m_vhdl." << endl;
817 ||
comp.
m_maxInt != 1) cout <<
"[Error] TRGCDCJSignal::ifElse() => comp signal m_minInt or m_maxInt is not correct." << endl;
820 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
829 int t_maxClock = data[0].first.calFinishClock();
830 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
831 int t_clock = data[iCase].first.calFinishClock();
832 if (t_maxClock < t_clock) t_maxClock = t_clock;
834 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
835 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
837 if (t_maxClock < t_clock) t_maxClock = t_clock;
840 int t_finishClock = 0;
841 if (targetClock == -3) {
842 if (t_maxClock < 0) cout <<
"[Error] TRGCDCJSignal::ifElse() => t_maxClock is negative. Did not consider this case." << endl;
843 t_finishClock = t_maxClock + 1;
845 if (targetClock < t_maxClock) cout <<
"[Error] TRGCDCJSignal::ifElse() => targetClock is too small." << endl;
846 else t_finishClock = targetClock;
850 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
853 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
861 map<TRGCDCJSignal*, pair<vector<long long int>, vector<double> > > t_signalsRange;
862 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
863 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
865 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
866 if (t_signalsRange.find(&lhs) == t_signalsRange.end()) {
869 t_signalsRange[&lhs] = make_pair(t_intRange, t_actualRange);
871 long long int& t_minInt = t_signalsRange[&lhs].first[0];
872 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
873 double& t_minActual = t_signalsRange[&lhs].second[0];
874 double& t_maxActual = t_signalsRange[&lhs].second[1];
883 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
884 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
886 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
888 long long int& t_minInt = t_signalsRange[&lhs].first[0];
889 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
890 double& t_minActual = t_signalsRange[&lhs].second[0];
891 double& t_maxActual = t_signalsRange[&lhs].second[1];
903 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
907 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
908 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
910 string t_string =
"";
911 lhs.
assignTo(rhs, t_finishClock, t_string);
913 lhs.
checkInt(
"TRGCDCJSignal::ifElse()");
920 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
924 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
925 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
934 if (data[0].first.getPrintVhdl() == 1) {
936 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
937 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
938 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
945 data[0].first.m_commonData->m_vhdlInProcess += t_finalCode +
"\n";
958 double maxActual)
const
986 return limit(min.m_int, max.m_int, min.m_actual, max.m_actual);
991 if (this->
m_bitsize == bitwidth)
return (*
this);
998 t_result.
checkInt(
"TRGCDCJSignal::resize()");
1041 signed long long const& maxInt,
double const& actual,
double const& minActual,
double const& maxActual,
int const& finishClock)
1044 vector<bool>
const& slv = in.
m_slv;
1047 if (in.
m_type != 2) cout <<
"[Error] TRGCDCJSignal::slvToSigned() => type is not slv." << endl;
1048 if (slv.size() > 63) cout <<
"[Error] TRGCDCJSignal::slvToSigned() => bitwidth is too large." << endl;
1054 for (
unsigned iBit = 0; iBit < slv.size() - 1; iBit++) {
1055 result.m_int += slv[iBit] * pow(2, iBit);
1057 result.m_int -= slv[slv.size() - 1] * pow(2, slv.size() - 1);
1059 result.m_minInt = minInt;
1060 result.m_maxInt = maxInt;
1061 result.m_actual = actual;
1062 result.m_minActual = minActual;
1063 result.m_maxActual = maxActual;
1064 result.m_toReal = toReal;
1066 result.m_type = result.calType();
1067 result.m_bitsize = result.calBitwidth();
1068 result.m_finishClock = finishClock;
1070 result.checkInt(
"slvToSigned");
1082 signed long long const& maxInt,
double const& actual,
double const& minActual,
double const& maxActual,
int const& finishClock)
1085 vector<bool>
const& slv = in.
m_slv;
1088 if (in.
m_type != 2) cout <<
"[Error] TRGCDCJSignal::slvToUnsigned() => type is not slv." << endl;
1089 if (slv.size() > 63) cout <<
"[Error] TRGCDCJSignal::slvToUnsigned() => bitwidth is too large." << endl;
1095 for (
unsigned iBit = 0; iBit < slv.size(); iBit++) {
1096 result.m_int += slv[iBit] * pow(2, iBit);
1099 result.m_minInt = minInt;
1100 result.m_maxInt = maxInt;
1101 result.m_actual = actual;
1102 result.m_minActual = minActual;
1103 result.m_maxActual = maxActual;
1104 result.m_toReal = toReal;
1106 result.m_type = result.calType();
1107 result.m_bitsize = result.calBitwidth();
1108 result.m_finishClock = finishClock;
1110 result.checkInt(
"slvToSigned");
1128 signed long long t_int = in.
m_int;
1133 result.m_slv.clear();
1135 for (
int iBit = 0; iBit < in.
m_bitsize; iBit++) {
1136 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1138 }
else if (in.
m_type == 1) {
1140 result.m_slv.clear();
1142 for (
int iBit = 0; iBit < in.
m_bitsize; iBit++) {
1143 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1146 cout <<
"[Error] TRGCDCJSignal::toSlv() => m_type is not 1 or -1." << endl;
1154 if (in.
m_type == 2)
return (in);
1157 cout <<
"[Error] TRGCDCJSignal::toSlv() => Slv is not same as unsigned anymore." << endl;
1180 bool t_bool =
false;
1181 if (operate ==
"=") {
1182 t_bool = (t_lhs.
m_int == t_rhs.
m_int ? 1 : 0);
1183 }
else if (operate ==
"/=") {
1184 t_bool = (t_lhs.
m_int != t_rhs.
m_int ? 1 : 0);
1185 }
else if (operate ==
"<") {
1186 t_bool = (t_lhs.
m_int < t_rhs.
m_int ? 1 : 0);
1187 }
else if (operate ==
"<=") {
1188 t_bool = (t_lhs.
m_int <= t_rhs.
m_int ? 1 : 0);
1189 }
else if (operate ==
">") {
1190 t_bool = (t_lhs.
m_int > t_rhs.
m_int ? 1 : 0);
1191 }
else if (operate ==
">=") {
1192 t_bool = (t_lhs.
m_int >= t_rhs.
m_int ? 1 : 0);
1193 }
else if (operate ==
"and") {
1194 t_bool = (t_lhs.
m_int && t_rhs.
m_int ? 1 : 0);
1195 }
else if (operate ==
"or") {
1196 t_bool = (t_lhs.
m_int || t_rhs.
m_int ? 1 : 0);
1198 cout <<
"[Error] TRGCDCJSignal::comp() => No operator named " << operate <<
"." << endl;
1201 if (operate ==
"=") {
1203 }
else if (operate ==
"/=") {
1205 }
else if (operate ==
"<") {
1207 }
else if (operate ==
"<=") {
1209 }
else if (operate ==
">") {
1211 }
else if (operate ==
">=") {
1213 }
else if (operate ==
"and") {
1215 }
else if (operate ==
"or") {
1218 cout <<
"[Error] TRGCDCJSignal::comp() => No operator named " << operate <<
"." << endl;
1243 std::swap(first.m_name, second.m_name);
1244 std::swap(first.m_type, second.m_type);
1245 std::swap(first.m_bitsize, second.m_bitsize);
1246 std::swap(first.m_int, second.m_int);
1247 std::swap(first.m_minInt, second.m_minInt);
1248 std::swap(first.m_maxInt, second.m_maxInt);
1249 std::swap(first.m_actual, second.m_actual);
1250 std::swap(first.m_minActual, second.m_minActual);
1251 std::swap(first.m_maxActual, second.m_maxActual);
1252 std::swap(first.m_toReal, second.m_toReal);
1253 std::swap(first.m_debug, second.m_debug);
1254 std::swap(first.m_vhdlCode, second.m_vhdlCode);
1255 std::swap(first.m_argumentSignals, second.m_argumentSignals);
1256 std::swap(first.m_finishClock, second.m_finishClock);
1257 std::swap(first.m_commonData, second.m_commonData);
1262 if (first.m_toReal != second.m_toReal) {
1264 double t_nBits = log(first.m_toReal / second.m_toReal) / log(2);
1267 t_nBits = int(t_nBits + 0.5);
1269 t_nBits = int(t_nBits - 0.5);
1273 first = first.shift(-t_nBits, 0);
1276 }
else if (t_nBits < 0) {
1277 second = second.shift(t_nBits, 0);
1282 if (fabs(first.m_toReal - second.m_toReal) > 0.00001 * first.m_toReal) {
1283 cout <<
"[Warning] TRGCDCJSignal::matchUnit() => in1 and in2 units are still different." << endl;
1284 cout << first.m_toReal <<
" " << second.m_toReal << endl;
1288 cout <<
"TRGCDCJSignal::matchUnit()" << endl;
1289 if (t_nBits > 0) cout <<
"lhs was shifted to left by " << t_nBits << endl;
1290 else if (t_nBits < 0) cout <<
"rhs was shifted to left by " << -t_nBits << endl;
1291 cout <<
"TRGCDCJSignal::matchUnit()" << endl;
1294 first.checkInt(
"TRGCDCJSignal::matchUnit()::first");
1295 second.checkInt(
"TRGCDCJSignal::matchUnit()::second");
1328 checkInt(
"TRGCDCJSignal::setBitsize()");
1345 cout <<
"[Error] TRGCDCJSignal::setPrintVhdl() => m_commonData does not exist." << endl;
1353 cout <<
"[Error] TRGCDCJSignal::setVhdlOutputFile() => m_commonData does not exist." << endl;
1441 cout <<
"[Error] TRGCDCJSignal::getCommonData() => m_commonData does not exist." << endl;
1457 if (minValue < 0)
return -1;
1465 if (
calType() != 1) t_bitwidth++;
1472 int t_bitwidth = ceil(log1p(max(maxInt, llabs(minInt))) / log(2));
1473 if (type != 1) t_bitwidth++;
1501 cout <<
"[Error] TRGCDCJSignal::getPrintVhdl() => m_commonData does not exist." << endl;
1510 cout <<
"[Error] TRGCDCJSignal::getVhdlOutputFile() => m_commonData does not exist." << endl;
1522 if (large.
m_type == 1) {;}
1537 signed long long t_maxValue;
1551 signed long long t_minValue;
1554 }
else if (
m_type == -1) {
1555 t_minValue = -pow(2,
m_bitsize - 1) + 1;
1557 t_minValue = -pow(2,
m_bitsize - 1) + 1;
1569 if (result.m_argumentSignals.size() == 0) result.initArgumentSignals();
1571 if (result.m_type != 1) {
1573 result.m_int = llabs(first.m_int);
1574 if (first.m_minInt < 0 && first.m_maxInt > 0) result.m_minInt = 0;
1575 else result.m_minInt = min(llabs(first.m_maxInt), llabs(first.m_minInt));
1576 result.m_maxInt = max(llabs(first.m_maxInt), llabs(first.m_minInt));
1577 result.m_actual = fabs(first.m_actual);
1578 if (first.m_minActual < 0 && first.m_maxActual > 0) result.m_minActual = 0;
1579 else result.m_minActual = min(fabs(first.m_maxActual), fabs(first.m_minActual));
1580 result.m_maxActual = max(fabs(first.m_maxActual), fabs(first.m_minActual));
1581 result.m_bitsize = first.m_bitsize - 1;
1584 result.checkInt(
"TRGCDCJSignal::absolute()");
1587 if (first.getPrintVhdl() == 1)
vhdlCode(
"abs", first, result, result.m_vhdlCode);
1671 if (first.m_type != second.m_type) {
1672 cout <<
"[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => first and second type are different." << endl;
1673 cout << first.m_name <<
" " << second.m_name << endl;
1674 cout << first.m_type <<
" " << second.m_type << endl;
1677 int firstBitwidth = first.m_bitsize;
1678 int secondBitwidth = second.m_bitsize;
1680 if (operation ==
"+" || operation ==
"-") {
1681 if (first.m_type != 1 || second.m_type != 1) {
1683 bitwidth = max(firstBitwidth, secondBitwidth);
1686 bitwidth = max(first.m_bitsize, second.m_bitsize);
1688 }
else if (operation ==
"*") {
1689 if (first.m_type != 1 || second.m_type != 1) {
1691 bitwidth = firstBitwidth + secondBitwidth;
1694 bitwidth = first.m_bitsize + second.m_bitsize;
1696 }
else if (operation ==
"=" || operation ==
"/=" || operation ==
"<" || operation ==
"<=" ||
1697 operation ==
">" || operation ==
">=" || operation ==
"and" || operation ==
"or") {
1701 cout <<
"[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => No operation named " << operation <<
"." << endl;
1712 t_name =
"decimal_string_to_unsigned(\"" + to_string(
m_int) +
"\"," + to_string(
m_bitsize) +
")";
1715 t_name =
"decimal_string_to_signed(\"" + to_string(
m_int) +
"\"," + to_string(
m_bitsize) +
")";
1718 }
else if (
m_name ==
"") {
1719 t_name =
"INPUT(" + to_string(
m_int) +
")";
1728 cout <<
"[Error] TRGCDCJSignal::initArgumentSignals() => m_argumentSignals.size() is not 0." << endl;
1734 result.m_argumentSignals.clear();
1735 result.m_argumentSignals.insert(result.m_argumentSignals.end(), first.m_argumentSignals.begin(), first.m_argumentSignals.end());
1736 result.m_argumentSignals.insert(result.m_argumentSignals.end(), second.m_argumentSignals.begin(), second.m_argumentSignals.end());
1742 cout <<
"[Error] TRGCDCJSignal::initVhdlCode() => m_vhdlCode is not \"\"." << endl;
1750 if (
m_type == -1)
return;
1753 if (
calType() == 1) t_bitwidth++;
1757 }
else if (t_bitwidth - 1 ==
m_bitsize) {
1761 cout <<
"[Error] TRGCDCJSignal::toSignedVhdlCode() => m_bitsize is too small." << endl;
1771 cout <<
"[Warning] TRGCDCJSignal::toUnsignedVhdlCode() => signal can be minus." << endl;
1795 if (t_first.
m_type * t_second.
m_type < 0 || (t_first.
m_type != -1 && result.m_type == -1)) {
1796 if (t_first.
m_type != -1) {
1799 if (t_second.
m_type != -1) {
1808 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1809 logicType = result.m_type;
1810 logicBitwidth = result.m_bitsize;
1816 if (operation ==
"=" || operation ==
"/=" || operation ==
"<" || operation ==
"<=" ||
1817 operation ==
">" || operation ==
">=" || operation ==
"and" || operation ==
"or") {
1822 if (logicType > 0 && vhdlType == -1) {
1823 if (logicBitwidth + 1 > vhdlBitwidth) t_first.
m_vhdlCode =
"resize(" + t_first.
m_vhdlCode +
"," + to_string(
1824 logicBitwidth + 1) +
")";
1826 resultCode =
"resize(unsigned(" + resultCode +
")," + to_string(logicBitwidth) +
")";
1829 if (logicBitwidth > vhdlBitwidth) {
1834 if (logicBitwidth < vhdlBitwidth) {
1835 resultCode =
"resize(" + resultCode +
"," + to_string(logicBitwidth) +
")";
1839 resultVhdlCode = resultCode;
1843 string& resultVhdlCode)
1853 if (t_first.
m_type != -1 && result.m_type == -1) {
1860 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1861 logicType = result.m_type;
1862 logicBitwidth = result.m_bitsize;
1864 vhdlType = t_first.
m_type;
1870 if (logicType == 1 && vhdlType == -1) {
1871 if (logicBitwidth + 1 > vhdlBitwidth) t_first.
m_vhdlCode =
"resize(" + t_first.
m_vhdlCode +
"," + to_string(
1872 logicBitwidth + 1) +
")";
1873 if (operation ==
"shift_left"
1874 || operation ==
"shift_right") resultCode = operation +
"(" + t_first.
m_vhdlCode +
"," + to_string(abs(
1875 first.m_bitsize - result.m_bitsize)) +
")";
1876 else if (operation ==
"unsigned" || operation ==
"none") resultCode = t_first.
m_vhdlCode;
1877 else resultCode = operation +
"(" + t_first.
m_vhdlCode +
")";
1878 resultCode =
"resize(unsigned(" + resultCode +
")," + to_string(logicBitwidth) +
")";
1881 if (logicBitwidth > vhdlBitwidth) {
1884 if (operation ==
"shift_left"
1885 || operation ==
"shift_right") resultCode = operation +
"(" + t_first.
m_vhdlCode +
"," + to_string(abs(
1886 first.m_bitsize - result.m_bitsize)) +
")";
1887 else if (operation ==
"resize" || operation ==
"signed" || operation ==
"none") resultCode = t_first.
m_vhdlCode;
1888 else resultCode = operation +
"(" + t_first.
m_vhdlCode +
")";
1890 if (logicBitwidth < vhdlBitwidth) {
1891 resultCode =
"resize(" + resultCode +
"," + to_string(logicBitwidth) +
")";
1894 resultVhdlCode = resultCode;
1898 std::vector<pair<std::string, vector<int> > >
const& fromArgumentSignals,
int const& finishClock,
1899 std::map<std::string, vector<int> >& buffers)
1902 string t_vhdlCode =
"";
1904 size_t end = fromVhdlCode.find(
" ");
1905 int nIndex = (int)fromArgumentSignals.size();
1907 while (end != string::npos) {
1909 if (nIndex - 1 < index) {
1910 cout <<
"[Error] TRGCDCJSignal::replaceWithSignalNames() => Not enough signal names in m_argumentSignals." << endl;
1915 int t_buffer = (finishClock - 1) - fromArgumentSignals[index].second[2] - 1;
1917 if (t_buffer == -1 || t_buffer == -2
1918 || fromArgumentSignals[index].second[2] == -1) t_vhdlCode += fromVhdlCode.substr(start,
1919 end - start) + fromArgumentSignals[index].first;
1920 else if (t_buffer > -1) {
1921 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first +
"_b(" + to_string(t_buffer) +
")";
1923 if (buffers.find(fromArgumentSignals[index].first) == buffers.end()) {
1924 buffers[fromArgumentSignals[index].first] = {fromArgumentSignals[index].second[0], fromArgumentSignals[index].second[1], t_buffer};
1926 if (buffers[fromArgumentSignals[index].first][2] < t_buffer) buffers[fromArgumentSignals[index].first][2] = t_buffer;
1929 cout <<
"[Error] TRGCDCJSignal::replaceWithSignalNames() => finishClock is too small." << endl;
1930 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first;
1935 end = fromVhdlCode.find(
" ", start);
1937 t_vhdlCode += fromVhdlCode.substr(start, end - start);
1939 if (nIndex != index) {
1940 cout <<
"[Error] TRGCDCJSignal::replaceWithSignalNames() => Number of signal names and number of " " do not match." << endl;
1950 if (target.m_commonData == 0) {
1951 cout <<
"[Error] TRGCDCJSignal::assignVhdlCode() => target.m_commonData is not set." << endl;
1964 if (target.m_name ==
"") cout <<
"[Error] TRGCDCJSignal::assignVhdlCode() => target.m_name is \"\" " << endl;
1965 map<string, vector<int> >& t_signals = target.m_commonData->m_signals;
1966 if (t_signals.find(target.m_name) == t_signals.end()) {
1978 return target.m_name +
"<=" + t_vhdlCode +
";";
2003 std::vector<std::vector<TRGCDCJSignal> >
const& data)
2006 if (target.m_name ==
"") {
2007 cout <<
"[Error] TRGCDCJSignal::chooseVhdlCode() => target has no name." << endl;
2012 cout <<
"[Error] TRGCDCJSignal::cooseVhdlCode() => reference has no name or vhdlCode." << endl;
2027 stringstream t_vhdlCode;
2054 target.m_commonData->m_buffers);
2056 target.m_commonData->m_buffers);
2057 string t_fromPrefixSigned;
2058 string t_fromPostfixSigned;
2059 string t_toPrefixSigned;
2060 string t_toPostfixSigned;
2062 t_fromPrefixSigned =
"signed('0'&";
2063 t_fromPostfixSigned =
")";
2065 t_fromPrefixSigned =
"";
2066 t_fromPostfixSigned =
"";
2069 t_toPrefixSigned =
"signed('0'&";
2070 t_toPostfixSigned =
")";
2072 t_toPrefixSigned =
"";
2073 t_toPostfixSigned =
"";
2075 t_vhdlCode <<
"if (" << t_referenceVhdlCode <<
" >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned <<
") and (" <<
2076 t_referenceVhdlCode <<
" <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned <<
") then" << endl;
2080 for (
unsigned iCondition = 1; iCondition < data.size() - 1; iCondition++) {
2087 t_fromPrefixSigned =
"signed('0'&";
2088 t_fromPostfixSigned =
")";
2090 t_fromPrefixSigned =
"";
2091 t_fromPostfixSigned =
"";
2094 t_toPrefixSigned =
"signed('0'&";
2095 t_toPostfixSigned =
")";
2097 t_toPrefixSigned =
"";
2098 t_toPostfixSigned =
"";
2101 target.m_commonData->m_buffers);
2103 t_vhdlCode <<
"elsif (" << t_referenceVhdlCode <<
" >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned <<
") and ("
2104 << t_referenceVhdlCode <<
" <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned <<
") then" << endl;
2107 t_vhdlCode <<
"else" << endl;
2108 t_vhdlCode <<
" " <<
assignVhdlCode(target, (*(data.end() - 1))[0]) << endl;
2109 t_vhdlCode <<
"end if;";
2110 return t_vhdlCode.str();
2114 std::vector<std::pair<
TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > >
const& data)
2117 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
2119 if (iCase != data.size() - 1) {
2121 cout <<
"[Error] TRGCDCJSignal::ifElseVhdlCode() => comp has no name." << endl;
2125 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2126 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2128 cout <<
"[Error] TRGCDCJSignal::ifElseVhdlCode() => lhs has no name." << endl;
2136 stringstream t_vhdlCode;
2137 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
2142 if (iCase == 0) t_vhdlCode <<
"if " << t_compVhdlCode <<
" then" << endl;
2143 else if (
comp.
m_bitsize == -1) t_vhdlCode <<
"else" << endl;
2144 else t_vhdlCode <<
"elsif " << t_compVhdlCode <<
" then" << endl;
2145 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2146 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2147 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
2151 t_vhdlCode <<
"end if;";
2153 return t_vhdlCode.str();
2162 if (outFile.is_open()) {
2167 cout <<
"[Error] TRGCDCJSignal::printVhdl() => m_commonData doesn't exist." << endl;
2174 cout <<
"[Error] " << name <<
" => m_int overflow" << endl;
2175 cout <<
" Start dumping" << endl;
2177 cout <<
" End dumping" << endl;
2180 cout <<
"[Error] " << name <<
" => m_int underflow" << endl;
2181 cout <<
" Start dumping" << endl;
2183 cout <<
" End dumping" << endl;
2186 cout <<
"[Error] " << name <<
" => m_maxInt overflow" << endl;
2187 cout <<
" Start dumping" << endl;
2189 cout <<
" End dumping" << endl;
2192 cout <<
"[Error] " << name <<
" => m_minInt underflow" << endl;
2193 cout <<
" Start dumping" << endl;
2195 cout <<
" End dumping" << endl;
2210 for (
unsigned iSignals = 0; iSignals < inValues.size(); iSignals++) {
2211 string const& t_name = get<0>(inValues[iSignals]);
2212 double const& t_value = get<1>(inValues[iSignals]);
2213 int const& t_bitWidth = get<2>(inValues[iSignals]);
2214 double const& t_min = get<3>(inValues[iSignals]);
2215 double const& t_max = get<4>(inValues[iSignals]);
2216 int const& t_clock = get<5>(inValues[iSignals]);
2225 std::vector<std::pair<std::string, int> >
const& inChoose,
2226 std::vector<std::tuple<string, double, int, double, double, int> >& outValues)
2228 for (
unsigned iSignals = 0; iSignals < inChoose.size(); iSignals++) {
2229 int const& b_integerEffect = inChoose[iSignals].second;
2230 string const& t_name = inChoose[iSignals].first;
2232 double t_value = 0, t_min = 0, t_max = 0;
2234 if (b_integerEffect == 1) {
2244 tuple<string, double, int, double, double, int> outValue(t_name, t_value, t_bitwidth, t_min, t_max, t_clock);
2245 outValues.push_back(outValue);
A class to hold common data for JSignals.
bool m_printVhdl
Status if code should be printed.
std::string m_vhdlInProcess
Holds VHDL process code.
std::string m_vhdlOutputFile
Memebers.
std::map< std::string, std::vector< int > > m_buffers
vector<int> is {type, bitwidth, buffer} Holds all the requried VHDL buffers.
A class to use Signals for TRGCDC 3D tracker.
std::vector< std::pair< std::string, std::vector< int > > > m_argumentSignals
vector<int> is {type, bitwidth, clock} Holds the arguments that made the signal.
int m_finishClock
Holds the clock tick when the signal is valid.
double m_toReal
The toReal value.
double m_minActual
The minimum float value.
bool m_debug
The debug flag.
signed long long m_minInt
The minimum integer value.
double m_maxActual
The maximum float value.
int m_bitsize
+1: unsigned.
int m_type
Type of signal.
signed long long m_int
integer value of signal.
TRGCDCJSignalData * m_commonData
The common JSignalData.
signed long long m_maxInt
The maximum integer value.
std::string m_name
Memebers.
std::string m_vhdlCode
Holds the vhdl code.
double m_actual
The float value.
std::vector< bool > m_slv
storage for std_logic_vector. (Will be deprecated.)
static double roundInt(double value)
Round double value.
signed long long calMaxInteger() const
Calculate max value for a binary number.
int calType() const
Calculated what type the signal is.
double getActual() const
Get float value of signal.
TRGCDCJSignal const invOffset(TRGCDCJSignal const &valueMin) const
Outputs a signal that is not offset.
void printVhdl(const std::string &vhdlCode) const
Prints vhdl code.
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.
void setBitsize(int bitsize)
Sets bitwidth of signal.
TRGCDCJSignal const operator-() const
Unary operator.
static void vhdlCode(TRGCDCJSignal const &first, const std::string &operation, TRGCDCJSignal const &second, TRGCDCJSignal const &result, std::string &targtVhdlCode)
Writes vhdl code using two signals.
static void valuesToMapSignals(std::vector< std::tuple< std::string, double, int, double, double, int > > const &inValues, Belle2::TRGCDCJSignalData *inCommonData, std::map< std::string, Belle2::TRGCDCJSignal > &outMap)
Values => [name, value, bitwidth, min, max, clock] Changes values to signals.
TRGCDCJSignal const operator+(TRGCDCJSignal const &rhs) const
Arithmetic add operator.
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.
bool orderLargestBitsize(TRGCDCJSignal &large, TRGCDCJSignal &small) const
Orders the TRGCDCJSignals by bitsize. Next priority is unsigned, signed, minus unsigned....
static bool isSameSignal(TRGCDCJSignal const &lhs, TRGCDCJSignal const &rhs)
Checks if signal is same signal.
void setActual(double value)
Sets float value of signal.
static std::string replaceWithSignalNames(std::string const &fromVhdlCode, std::vector< std::pair< std::string, std::vector< int > > > const &fromVhdlSignals, int const &finishClock, std::map< std::string, std::vector< int > > &buffers)
Writes final vhdl code.
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.
double getMaxActual() const
Get maximum float value of signal.
static void choose(TRGCDCJSignal &target, TRGCDCJSignal const &targetMin, TRGCDCJSignal const &targetMax, TRGCDCJSignal const &reference, std::vector< std::vector< TRGCDCJSignal > > data, int targetClock)
Case method.
static TRGCDCJSignal const toSlv(TRGCDCJSignal const &in, int test)
Change signal to std_logic_vector using vector<bool>. (Will be deprecated.)
void initArgumentSignals()
Initializes the argument signals for the signal.
void setVhdlOutputFile(const std::string &filename)
Sets ouptut VHDL filename.
std::string getVhdlCode() const
Gets the vhdl code for the signal.
void setInt(signed long long value)
Sets integer value of signal.
int calBitwidth() const
Calculates the bit width.
bool getPrintVhdl() const
Gets the print vhdl flag from common JSignalData class.
static void ifElse(std::vector< std::pair< TRGCDCJSignal, std::vector< std::pair< TRGCDCJSignal *, TRGCDCJSignal > > > > &data, int targetClock)
If else implementation with target clock.
std::vector< std::pair< std::string, std::vector< int > > > const getArgumentSignals() const
Gets the signal names and values that made this signal.
static std::string assignVhdlCode(TRGCDCJSignal const &target, TRGCDCJSignal const &from)
Writes the assign vhdl code.
TRGCDCJSignal const resize(int bitwidth) const
Resizes signal.
double getRealMinInt() const
Get converted float value of minimum integer.
static void combineArguments(TRGCDCJSignal const &first, TRGCDCJSignal const &second, TRGCDCJSignal &result)
Combines arguments of signals.
static std::string chooseVhdlCode(TRGCDCJSignal const &target, TRGCDCJSignal const &reference, std::vector< std::vector< TRGCDCJSignal > > const &data)
Writes the choose vhdl code.
static TRGCDCJSignal const absolute(TRGCDCJSignal const &first)
Absolute TRGCDCJSignal. Removes 1 bit if signed or minus unsigned.
int getFinishClock() const
Gets clock tick where signal is make.
static signed long long calInt(double value, TRGCDCJSignal const &mother)
Calculates integer value with unit of a TRGCDCJSignal.
void setFinishClock(int finishClock)
Sets finish clock of signal.
signed long long getInt() const
Get integer value of signal.
double getToReal() const
Get toReal value of signal.
void matchUnit(TRGCDCJSignal &first, TRGCDCJSignal &second) const
Utilities.
void setDebug(bool debug)
Sets debug flag for signal.
TRGCDCJSignal const limit(signed long long minInt, signed long long maxInt, double minActual, double maxActual) const
Limits min and max values using specific integers. Use only for ifElse. Doesn't check overflow,...
static void calVhdlTypeBitwidth(TRGCDCJSignal const &first, const std::string &operation, TRGCDCJSignal const &second, int &type, int &bitwidth)
Calculates vhdl bitwidth and type for operation.
void setName(const std::string &name)
Setters, Getters Sets name of signal.
double getRealMaxInt() const
Get converted float value of maximum integer.
void checkInt(const std::string &name) const
Checks underflow or overflow for TRGCDCJSignal.
void swap(TRGCDCJSignal &first, TRGCDCJSignal &second) const
Swaps the TRGCDCJSignals.
TRGCDCJSignal const operator*(TRGCDCJSignal const &rhs) const
Arithmetic multiply operator.
double getBitsize() const
Get bitwidth of signal.
TRGCDCJSignal & operator<=(TRGCDCJSignal const &rhs)
Assign operator.
signed long long calMinInteger() const
Calculate min value for a binary number.
void initVhdlCode()
Initializes the vhdl code.
void setPrintVhdl(bool printVhdl)
Sets print flag for signal.
int getType() const
Get type of signal.
static std::string ifElseVhdlCode(std::vector< std::pair< TRGCDCJSignal, std::vector< std::pair< TRGCDCJSignal *, TRGCDCJSignal > > > > const &data)
Writes the ifElse vhdl code.
void toUnsignedVhdlCode()
Changes signal type to unsigned type in VHDL.
double getMinActual() const
Get minimum float value of signal.
signed long long getMaxInt() const
Get maximum integer value of signal.
int calFinishClock() const
Claculates the clock tick where the signal is valid.
static TRGCDCJSignal const slvToUnsigned(TRGCDCJSignal const &in, double const &toReal, signed long long const &minInt, signed long long const &maxInt, double const &actual, double const &minActual, double const &maxActual, int const &finishClock)
Change slv to unsigned with target clock.
void dump() const
Methods.
double getRealInt() const
Get converted float value of integer.
TRGCDCJSignal()
Constructor.
signed long long getMinInt() const
Get minimum integer value of signal.
void setToReal(double value)
Sets toReal factor of signal.
static void mapSignalsToValues(std::map< std::string, Belle2::TRGCDCJSignal >const &inMap, std::vector< std::pair< std::string, int > > const &inChoose, std::vector< std::tuple< std::string, double, int, double, double, int > > &outValues)
Choose => [signalName, FpgaEffects(=1)/NoFpgaEffects(=0)] Values => [name, value, bitwidth,...
void setType(int type)
Sets type of signal.
void toSignedVhdlCode()
Changes signal type to signed type in VHDL.
static TRGCDCJSignal comp(TRGCDCJSignal const &lhs, const std::string &operate, TRGCDCJSignal const &rhs)
Compare two signals.
static TRGCDCJSignal const slvToSigned(TRGCDCJSignal const &in, double const &toReal, signed long long const &minInt, signed long long const &maxInt, double const &actual, double const &minActual, double const &maxActual, int const &finishClock)
Change slv to signed with target clock.
Abstract base class for different kinds of events.