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"
40using 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++) {
681 if (reference.getPrintVhdl() == 1)
vhdlCode(
"none", t_assign, *assign, assign->
m_vhdlCode);
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()");
744 if (reference.getPrintVhdl() == 1) {
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 if (data.size() == 0) {
770 cout <<
"[Error] TRGCDCJSignal::choose() => There is not enough data." << endl;
775 for (
unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
777 if (iCondition == 0) {
788 if (t_toReal != assign->
getToReal()) cout <<
"[Error] TRGCDCJSignal::choose() => toReal are not same between assignments in data."
793 TRGCDCJSignal t_targetMax(t_maxInt, t_toReal, t_maxInt, t_maxInt, t_maxActual, t_maxActual, t_maxActual, -1, target.m_commonData);
794 TRGCDCJSignal t_targetMin(t_minInt, t_toReal, t_minInt, t_minInt, t_minActual, t_minActual, t_minActual, -1, target.m_commonData);
796 choose(target, t_targetMin, t_targetMax, reference, data, targetClock);
800 const std::vector<std::vector<TRGCDCJSignal> >& data)
802 choose(target, reference, data, -3);
810 if (data.size() == 0) {
811 cout <<
"[Error] TRGCDCJSignal::ifElse() => There is not enough data." << endl;
818 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
820 if (iCase != (data.size() - 1)) {
821 if (
comp.m_argumentSignals.size() == 0) cout <<
"[Error] TRGCDCJSignal::ifElse() => comp signal has no m_vhdl." << endl;
822 if (
comp.m_minInt != 0
823 ||
comp.m_maxInt != 1) cout <<
"[Error] TRGCDCJSignal::ifElse() => comp signal m_minInt or m_maxInt is not correct." << endl;
826 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
835 int t_maxClock = data[0].first.calFinishClock();
836 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
837 int t_clock = data[iCase].first.calFinishClock();
838 if (t_maxClock < t_clock) t_maxClock = t_clock;
840 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
841 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
843 if (t_maxClock < t_clock) t_maxClock = t_clock;
846 int t_finishClock = 0;
847 if (targetClock == -3) {
848 if (t_maxClock < 0) cout <<
"[Error] TRGCDCJSignal::ifElse() => t_maxClock is negative. Did not consider this case." << endl;
849 t_finishClock = t_maxClock + 1;
851 if (targetClock < t_maxClock) cout <<
"[Error] TRGCDCJSignal::ifElse() => targetClock is too small." << endl;
852 else t_finishClock = targetClock;
856 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
858 comp.m_finishClock = t_finishClock;
859 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
867 map<TRGCDCJSignal*, pair<vector<long long int>, vector<double> > > t_signalsRange;
868 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
869 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
871 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
872 if (t_signalsRange.find(&lhs) == t_signalsRange.end()) {
875 t_signalsRange[&lhs] = make_pair(t_intRange, t_actualRange);
877 long long int& t_minInt = t_signalsRange[&lhs].first[0];
878 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
879 double& t_minActual = t_signalsRange[&lhs].second[0];
880 double& t_maxActual = t_signalsRange[&lhs].second[1];
889 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
890 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
892 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
894 long long int& t_minInt = t_signalsRange[&lhs].first[0];
895 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
896 double& t_minActual = t_signalsRange[&lhs].second[0];
897 double& t_maxActual = t_signalsRange[&lhs].second[1];
909 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
912 if (
comp.m_int == 1 ||
comp.m_bitsize == -1) {
913 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
914 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
916 string t_string =
"";
917 lhs.
assignTo(rhs, t_finishClock, t_string);
919 lhs.
checkInt(
"TRGCDCJSignal::ifElse()");
926 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
929 if (
comp.m_actual == 1 ||
comp.m_bitsize == -1) {
930 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
931 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
940 if (data[0].first.getPrintVhdl() == 1) {
942 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
943 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
944 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
951 data[0].first.m_commonData->m_vhdlInProcess += t_finalCode +
"\n";
964 double maxActual)
const
992 return limit(min.m_int, max.m_int, min.m_actual, max.m_actual);
997 if (this->
m_bitsize == bitwidth)
return (*
this);
1004 t_result.
checkInt(
"TRGCDCJSignal::resize()");
1047 signed long long const& maxInt,
double const& actual,
double const& minActual,
double const& maxActual,
int const& finishClock)
1050 vector<bool>
const& slv = in.
m_slv;
1053 if (in.
m_type != 2) cout <<
"[Error] TRGCDCJSignal::slvToSigned() => type is not slv." << endl;
1054 if (slv.size() > 63) cout <<
"[Error] TRGCDCJSignal::slvToSigned() => bitwidth is too large." << endl;
1060 for (
unsigned iBit = 0; iBit < slv.size() - 1; iBit++) {
1061 result.m_int += slv[iBit] * pow(2, iBit);
1063 result.m_int -= slv[slv.size() - 1] * pow(2, slv.size() - 1);
1065 result.m_minInt = minInt;
1066 result.m_maxInt = maxInt;
1067 result.m_actual = actual;
1068 result.m_minActual = minActual;
1069 result.m_maxActual = maxActual;
1070 result.m_toReal = toReal;
1072 result.m_type = result.calType();
1073 result.m_bitsize = result.calBitwidth();
1074 result.m_finishClock = finishClock;
1076 result.checkInt(
"slvToSigned");
1088 signed long long const& maxInt,
double const& actual,
double const& minActual,
double const& maxActual,
int const& finishClock)
1091 vector<bool>
const& slv = in.
m_slv;
1094 if (in.
m_type != 2) cout <<
"[Error] TRGCDCJSignal::slvToUnsigned() => type is not slv." << endl;
1095 if (slv.size() > 63) cout <<
"[Error] TRGCDCJSignal::slvToUnsigned() => bitwidth is too large." << endl;
1101 for (
unsigned iBit = 0; iBit < slv.size(); iBit++) {
1102 result.m_int += slv[iBit] * pow(2, iBit);
1105 result.m_minInt = minInt;
1106 result.m_maxInt = maxInt;
1107 result.m_actual = actual;
1108 result.m_minActual = minActual;
1109 result.m_maxActual = maxActual;
1110 result.m_toReal = toReal;
1112 result.m_type = result.calType();
1113 result.m_bitsize = result.calBitwidth();
1114 result.m_finishClock = finishClock;
1116 result.checkInt(
"slvToSigned");
1134 signed long long t_int = in.
m_int;
1139 result.m_slv.clear();
1141 for (
int iBit = 0; iBit < in.
m_bitsize; iBit++) {
1142 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1144 }
else if (in.
m_type == 1) {
1146 result.m_slv.clear();
1148 for (
int iBit = 0; iBit < in.
m_bitsize; iBit++) {
1149 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1152 cout <<
"[Error] TRGCDCJSignal::toSlv() => m_type is not 1 or -1." << endl;
1160 if (in.
m_type == 2)
return (in);
1163 cout <<
"[Error] TRGCDCJSignal::toSlv() => Slv is not same as unsigned anymore." << endl;
1186 bool t_bool =
false;
1187 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 ==
"<=") {
1194 t_bool = (t_lhs.
m_int <= t_rhs.
m_int ? 1 : 0);
1195 }
else if (operate ==
">") {
1196 t_bool = (t_lhs.
m_int > t_rhs.
m_int ? 1 : 0);
1197 }
else if (operate ==
">=") {
1198 t_bool = (t_lhs.
m_int >= t_rhs.
m_int ? 1 : 0);
1199 }
else if (operate ==
"and") {
1200 t_bool = (t_lhs.
m_int && t_rhs.
m_int ? 1 : 0);
1201 }
else if (operate ==
"or") {
1202 t_bool = (t_lhs.
m_int || t_rhs.
m_int ? 1 : 0);
1204 cout <<
"[Error] TRGCDCJSignal::comp() => No operator named " << operate <<
"." << endl;
1207 if (operate ==
"=") {
1209 }
else if (operate ==
"/=") {
1211 }
else if (operate ==
"<") {
1213 }
else if (operate ==
"<=") {
1215 }
else if (operate ==
">") {
1217 }
else if (operate ==
">=") {
1219 }
else if (operate ==
"and") {
1221 }
else if (operate ==
"or") {
1224 cout <<
"[Error] TRGCDCJSignal::comp() => No operator named " << operate <<
"." << endl;
1249 std::swap(first.m_name, second.m_name);
1250 std::swap(first.m_type, second.m_type);
1251 std::swap(first.m_bitsize, second.m_bitsize);
1252 std::swap(first.m_int, second.m_int);
1253 std::swap(first.m_minInt, second.m_minInt);
1254 std::swap(first.m_maxInt, second.m_maxInt);
1255 std::swap(first.m_actual, second.m_actual);
1256 std::swap(first.m_minActual, second.m_minActual);
1257 std::swap(first.m_maxActual, second.m_maxActual);
1258 std::swap(first.m_toReal, second.m_toReal);
1259 std::swap(first.m_debug, second.m_debug);
1260 std::swap(first.m_vhdlCode, second.m_vhdlCode);
1261 std::swap(first.m_argumentSignals, second.m_argumentSignals);
1262 std::swap(first.m_finishClock, second.m_finishClock);
1263 std::swap(first.m_commonData, second.m_commonData);
1268 if (first.m_toReal != second.m_toReal) {
1270 double t_nBits = log(first.m_toReal / second.m_toReal) / log(2);
1273 t_nBits = int(t_nBits + 0.5);
1275 t_nBits = int(t_nBits - 0.5);
1279 first = first.shift(-t_nBits, 0);
1282 }
else if (t_nBits < 0) {
1283 second = second.shift(t_nBits, 0);
1288 if (fabs(first.m_toReal - second.m_toReal) > 0.00001 * first.m_toReal) {
1289 cout <<
"[Warning] TRGCDCJSignal::matchUnit() => in1 and in2 units are still different." << endl;
1290 cout << first.m_toReal <<
" " << second.m_toReal << endl;
1294 cout <<
"TRGCDCJSignal::matchUnit()" << endl;
1295 if (t_nBits > 0) cout <<
"lhs was shifted to left by " << t_nBits << endl;
1296 else if (t_nBits < 0) cout <<
"rhs was shifted to left by " << -t_nBits << endl;
1297 cout <<
"TRGCDCJSignal::matchUnit()" << endl;
1300 first.checkInt(
"TRGCDCJSignal::matchUnit()::first");
1301 second.checkInt(
"TRGCDCJSignal::matchUnit()::second");
1334 checkInt(
"TRGCDCJSignal::setBitsize()");
1351 cout <<
"[Error] TRGCDCJSignal::setPrintVhdl() => m_commonData does not exist." << endl;
1359 cout <<
"[Error] TRGCDCJSignal::setVhdlOutputFile() => m_commonData does not exist." << endl;
1447 cout <<
"[Error] TRGCDCJSignal::getCommonData() => m_commonData does not exist." << endl;
1463 if (minValue < 0)
return -1;
1471 if (
calType() != 1) t_bitwidth++;
1478 int t_bitwidth = ceil(log1p(max(maxInt, llabs(minInt))) / log(2));
1479 if (type != 1) t_bitwidth++;
1507 cout <<
"[Error] TRGCDCJSignal::getPrintVhdl() => m_commonData does not exist." << endl;
1516 cout <<
"[Error] TRGCDCJSignal::getVhdlOutputFile() => m_commonData does not exist." << endl;
1528 if (large.
m_type == 1) {;}
1543 signed long long t_maxValue;
1557 signed long long t_minValue;
1560 }
else if (
m_type == -1) {
1561 t_minValue = -pow(2,
m_bitsize - 1) + 1;
1563 t_minValue = -pow(2,
m_bitsize - 1) + 1;
1575 if (result.m_argumentSignals.size() == 0) result.initArgumentSignals();
1577 if (result.m_type != 1) {
1579 result.m_int = llabs(first.m_int);
1580 if (first.m_minInt < 0 && first.m_maxInt > 0) result.m_minInt = 0;
1581 else result.m_minInt = min(llabs(first.m_maxInt), llabs(first.m_minInt));
1582 result.m_maxInt = max(llabs(first.m_maxInt), llabs(first.m_minInt));
1583 result.m_actual = fabs(first.m_actual);
1584 if (first.m_minActual < 0 && first.m_maxActual > 0) result.m_minActual = 0;
1585 else result.m_minActual = min(fabs(first.m_maxActual), fabs(first.m_minActual));
1586 result.m_maxActual = max(fabs(first.m_maxActual), fabs(first.m_minActual));
1587 result.m_bitsize = first.m_bitsize - 1;
1590 result.checkInt(
"TRGCDCJSignal::absolute()");
1593 if (first.getPrintVhdl() == 1)
vhdlCode(
"abs", first, result, result.m_vhdlCode);
1677 if (first.m_type != second.m_type) {
1678 cout <<
"[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => first and second type are different." << endl;
1679 cout << first.m_name <<
" " << second.m_name << endl;
1680 cout << first.m_type <<
" " << second.m_type << endl;
1683 int firstBitwidth = first.m_bitsize;
1684 int secondBitwidth = second.m_bitsize;
1686 if (operation ==
"+" || operation ==
"-") {
1688 if (first.m_type != 1 || second.m_type != 1) {
1690 bitwidth = max(firstBitwidth, secondBitwidth);
1693 bitwidth = max(first.m_bitsize, second.m_bitsize);
1695 }
else if (operation ==
"*") {
1697 if (first.m_type != 1 || second.m_type != 1) {
1699 bitwidth = firstBitwidth + secondBitwidth;
1702 bitwidth = first.m_bitsize + second.m_bitsize;
1704 }
else if (operation ==
"=" || operation ==
"/=" || operation ==
"<" || operation ==
"<=" ||
1705 operation ==
">" || operation ==
">=" || operation ==
"and" || operation ==
"or") {
1709 cout <<
"[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => No operation named " << operation <<
"." << endl;
1720 t_name =
"decimal_string_to_unsigned(\"" + to_string(
m_int) +
"\"," + to_string(
m_bitsize) +
")";
1723 t_name =
"decimal_string_to_signed(\"" + to_string(
m_int) +
"\"," + to_string(
m_bitsize) +
")";
1726 }
else if (
m_name ==
"") {
1727 t_name =
"INPUT(" + to_string(
m_int) +
")";
1736 cout <<
"[Error] TRGCDCJSignal::initArgumentSignals() => m_argumentSignals.size() is not 0." << endl;
1742 result.m_argumentSignals.clear();
1743 result.m_argumentSignals.insert(result.m_argumentSignals.end(), first.m_argumentSignals.begin(), first.m_argumentSignals.end());
1744 result.m_argumentSignals.insert(result.m_argumentSignals.end(), second.m_argumentSignals.begin(), second.m_argumentSignals.end());
1750 cout <<
"[Error] TRGCDCJSignal::initVhdlCode() => m_vhdlCode is not \"\"." << endl;
1758 if (
m_type == -1)
return;
1761 if (
calType() == 1) t_bitwidth++;
1765 }
else if (t_bitwidth - 1 ==
m_bitsize) {
1769 cout <<
"[Error] TRGCDCJSignal::toSignedVhdlCode() => m_bitsize is too small." << endl;
1779 cout <<
"[Warning] TRGCDCJSignal::toUnsignedVhdlCode() => signal can be minus." << endl;
1803 if (t_first.
m_type * t_second.
m_type < 0 || (t_first.
m_type != -1 && result.m_type == -1)) {
1804 if (t_first.
m_type != -1) {
1807 if (t_second.
m_type != -1) {
1816 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1817 logicType = result.m_type;
1818 logicBitwidth = result.m_bitsize;
1824 if (operation ==
"=" || operation ==
"/=" || operation ==
"<" || operation ==
"<=" ||
1825 operation ==
">" || operation ==
">=" || operation ==
"and" || operation ==
"or") {
1830 if (logicType > 0 && vhdlType == -1) {
1831 if (logicBitwidth + 1 > vhdlBitwidth) t_first.
m_vhdlCode =
"resize(" + t_first.
m_vhdlCode +
"," + to_string(
1832 logicBitwidth + 1) +
")";
1834 resultCode =
"resize(unsigned(" + resultCode +
")," + to_string(logicBitwidth) +
")";
1837 if (logicBitwidth > vhdlBitwidth) {
1842 if (logicBitwidth < vhdlBitwidth) {
1843 resultCode =
"resize(" + resultCode +
"," + to_string(logicBitwidth) +
")";
1847 resultVhdlCode = resultCode;
1851 string& resultVhdlCode)
1861 if (t_first.
m_type != -1 && result.m_type == -1) {
1868 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1869 logicType = result.m_type;
1870 logicBitwidth = result.m_bitsize;
1872 vhdlType = t_first.
m_type;
1878 if (logicType == 1 && vhdlType == -1) {
1879 if (logicBitwidth + 1 > vhdlBitwidth) t_first.
m_vhdlCode =
"resize(" + t_first.
m_vhdlCode +
"," + to_string(
1880 logicBitwidth + 1) +
")";
1881 if (operation ==
"shift_left"
1882 || operation ==
"shift_right") resultCode = operation +
"(" + t_first.
m_vhdlCode +
"," + to_string(abs(
1883 first.m_bitsize - result.m_bitsize)) +
")";
1884 else if (operation ==
"unsigned" || operation ==
"none") resultCode = t_first.
m_vhdlCode;
1885 else resultCode = operation +
"(" + t_first.
m_vhdlCode +
")";
1886 resultCode =
"resize(unsigned(" + resultCode +
")," + to_string(logicBitwidth) +
")";
1889 if (logicBitwidth > vhdlBitwidth) {
1892 if (operation ==
"shift_left"
1893 || operation ==
"shift_right") resultCode = operation +
"(" + t_first.
m_vhdlCode +
"," + to_string(abs(
1894 first.m_bitsize - result.m_bitsize)) +
")";
1895 else if (operation ==
"resize" || operation ==
"signed" || operation ==
"none") resultCode = t_first.
m_vhdlCode;
1896 else resultCode = operation +
"(" + t_first.
m_vhdlCode +
")";
1898 if (logicBitwidth < vhdlBitwidth) {
1899 resultCode =
"resize(" + resultCode +
"," + to_string(logicBitwidth) +
")";
1902 resultVhdlCode = resultCode;
1906 std::vector<pair<std::string, vector<int> > >
const& fromArgumentSignals,
int const& finishClock,
1907 std::map<std::string, vector<int> >& buffers)
1910 string t_vhdlCode =
"";
1912 size_t end = fromVhdlCode.find(
" ");
1913 int nIndex = (int)fromArgumentSignals.size();
1915 while (end != string::npos) {
1917 if (nIndex - 1 < index) {
1918 cout <<
"[Error] TRGCDCJSignal::replaceWithSignalNames() => Not enough signal names in m_argumentSignals." << endl;
1923 int t_buffer = (finishClock - 1) - fromArgumentSignals[index].second[2] - 1;
1925 if (t_buffer == -1 || t_buffer == -2
1926 || fromArgumentSignals[index].second[2] == -1) t_vhdlCode += fromVhdlCode.substr(start,
1927 end - start) + fromArgumentSignals[index].first;
1928 else if (t_buffer > -1) {
1929 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first +
"_b(" + to_string(t_buffer) +
")";
1931 if (buffers.find(fromArgumentSignals[index].first) == buffers.end()) {
1932 buffers[fromArgumentSignals[index].first] = {fromArgumentSignals[index].second[0], fromArgumentSignals[index].second[1], t_buffer};
1934 if (buffers[fromArgumentSignals[index].first][2] < t_buffer) buffers[fromArgumentSignals[index].first][2] = t_buffer;
1937 cout <<
"[Error] TRGCDCJSignal::replaceWithSignalNames() => finishClock is too small." << endl;
1938 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first;
1943 end = fromVhdlCode.find(
" ", start);
1945 t_vhdlCode += fromVhdlCode.substr(start, end - start);
1947 if (nIndex != index) {
1948 cout <<
"[Error] TRGCDCJSignal::replaceWithSignalNames() => Number of signal names and number of " " do not match." << endl;
1958 if (target.m_commonData == 0) {
1959 cout <<
"[Error] TRGCDCJSignal::assignVhdlCode() => target.m_commonData is not set." << endl;
1972 if (target.m_name ==
"") cout <<
"[Error] TRGCDCJSignal::assignVhdlCode() => target.m_name is \"\" " << endl;
1973 map<string, vector<int> >& t_signals = target.m_commonData->m_signals;
1974 if (t_signals.find(target.m_name) == t_signals.end()) {
1986 return target.m_name +
"<=" + t_vhdlCode +
";";
2011 std::vector<std::vector<TRGCDCJSignal> >
const& data)
2014 if (target.m_name ==
"") {
2015 cout <<
"[Error] TRGCDCJSignal::chooseVhdlCode() => target has no name." << endl;
2019 if (reference.m_name ==
"" && reference.m_vhdlCode ==
"") {
2020 cout <<
"[Error] TRGCDCJSignal::cooseVhdlCode() => reference has no name or vhdlCode." << endl;
2035 stringstream t_vhdlCode;
2062 target.m_commonData->m_buffers);
2064 target.m_commonData->m_buffers);
2065 string t_fromPrefixSigned;
2066 string t_fromPostfixSigned;
2067 string t_toPrefixSigned;
2068 string t_toPostfixSigned;
2069 if (reference.m_type == -1 && from.
m_type == 1) {
2070 t_fromPrefixSigned =
"signed('0'&";
2071 t_fromPostfixSigned =
")";
2073 t_fromPrefixSigned =
"";
2074 t_fromPostfixSigned =
"";
2076 if (reference.m_type == -1 && to.
m_type == 1) {
2077 t_toPrefixSigned =
"signed('0'&";
2078 t_toPostfixSigned =
")";
2080 t_toPrefixSigned =
"";
2081 t_toPostfixSigned =
"";
2083 t_vhdlCode <<
"if (" << t_referenceVhdlCode <<
" >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned <<
") and (" <<
2084 t_referenceVhdlCode <<
" <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned <<
") then" << endl;
2088 for (
unsigned iCondition = 1; iCondition < data.size() - 1; iCondition++) {
2094 if (reference.m_type == -1 && from1.
m_type == 1) {
2095 t_fromPrefixSigned =
"signed('0'&";
2096 t_fromPostfixSigned =
")";
2098 t_fromPrefixSigned =
"";
2099 t_fromPostfixSigned =
"";
2101 if (reference.m_type == -1 && to1.
m_type == 1) {
2102 t_toPrefixSigned =
"signed('0'&";
2103 t_toPostfixSigned =
")";
2105 t_toPrefixSigned =
"";
2106 t_toPostfixSigned =
"";
2109 target.m_commonData->m_buffers);
2111 t_vhdlCode <<
"elsif (" << t_referenceVhdlCode <<
" >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned <<
") and ("
2112 << t_referenceVhdlCode <<
" <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned <<
") then" << endl;
2115 t_vhdlCode <<
"else" << endl;
2116 t_vhdlCode <<
" " <<
assignVhdlCode(target, (*(data.end() - 1))[0]) << endl;
2117 t_vhdlCode <<
"end if;";
2118 return t_vhdlCode.str();
2122 std::vector<std::pair<
TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > >
const& data)
2125 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
2127 if (iCase != data.size() - 1) {
2128 if (
comp.m_name ==
"") {
2129 cout <<
"[Error] TRGCDCJSignal::ifElseVhdlCode() => comp has no name." << endl;
2133 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2134 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2136 cout <<
"[Error] TRGCDCJSignal::ifElseVhdlCode() => lhs has no name." << endl;
2144 stringstream t_vhdlCode;
2145 for (
unsigned iCase = 0; iCase < data.size(); iCase++) {
2148 comp.m_commonData->m_buffers);
2150 if (iCase == 0) t_vhdlCode <<
"if " << t_compVhdlCode <<
" then" << endl;
2151 else if (
comp.m_bitsize == -1) t_vhdlCode <<
"else" << endl;
2152 else t_vhdlCode <<
"elsif " << t_compVhdlCode <<
" then" << endl;
2153 for (
unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2154 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2155 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
2159 t_vhdlCode <<
"end if;";
2161 return t_vhdlCode.str();
2169 outFile.open(
m_commonData->m_vhdlOutputFile, ios::app);
2170 if (outFile.is_open()) {
2175 cout <<
"[Error] TRGCDCJSignal::printVhdl() => m_commonData doesn't exist." << endl;
2182 cout <<
"[Error] " << name <<
" => m_int overflow" << endl;
2183 cout <<
" Start dumping" << endl;
2185 cout <<
" End dumping" << endl;
2188 cout <<
"[Error] " << name <<
" => m_int underflow" << endl;
2189 cout <<
" Start dumping" << endl;
2191 cout <<
" End dumping" << endl;
2194 cout <<
"[Error] " << name <<
" => m_maxInt overflow" << endl;
2195 cout <<
" Start dumping" << endl;
2197 cout <<
" End dumping" << endl;
2200 cout <<
"[Error] " << name <<
" => m_minInt underflow" << endl;
2201 cout <<
" Start dumping" << endl;
2203 cout <<
" End dumping" << endl;
2217 for (
unsigned iSignals = 0; iSignals < inValues.size(); iSignals++) {
2218 string const& t_name = get<0>(inValues[iSignals]);
2219 double const& t_value = get<1>(inValues[iSignals]);
2220 int const& t_bitWidth = get<2>(inValues[iSignals]);
2221 double const& t_min = get<3>(inValues[iSignals]);
2222 double const& t_max = get<4>(inValues[iSignals]);
2223 int const& t_clock = get<5>(inValues[iSignals]);
2232 std::vector<std::pair<std::string, int> >
const& inChoose,
2233 std::vector<std::tuple<string, double, int, double, double, int> >& outValues)
2235 for (
unsigned iSignals = 0; iSignals < inChoose.size(); iSignals++) {
2236 int const& b_integerEffect = inChoose[iSignals].second;
2237 string const& t_name = inChoose[iSignals].first;
2239 double t_value = 0, t_min = 0, t_max = 0;
2241 if (b_integerEffect == 1) {
2251 tuple<string, double, int, double, double, int> outValue(t_name, t_value, t_bitwidth, t_min, t_max, t_clock);
2252 outValues.push_back(outValue);
A class to hold common data for JSignals.
std::map< std::string, std::vector< int > > m_buffers
vector<int> is {type, bitwidth, buffer} Holds all the required 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
Members.
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 outputting 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 output 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
Calculates 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.