Bug Summary

File:trg/cdc/src/JSignal.cc
Warning:line 109, column 11
Assigned value is uninitialized

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name JSignal.cc -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/data/b2soft/buildbot/development/build -fcoverage-compilation-dir=/data/b2soft/buildbot/development/build -resource-dir /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/lib/clang/21 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++ -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++/x86_64-redhat-linux -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++/backward -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/python3.12 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/CLHEP -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/Geant4 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/root -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/belle_legacy -I include/ -D _PACKAGE_="trg" -D G4UI_USE_TCSH -D RaveDllExport= -D HAS_SQLITE -D HAS_CALLGRIND -I include -I /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/libxml2 -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++ -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++/x86_64-redhat-linux -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++/backward -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-missing-braces -Wno-unused-command-line-argument -std=c++20 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /scan_build/2026-05-31-004316-385593-1 -x c++ trg/cdc/src/JSignal.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 Signals in CDC Trigger for 3D tracker.
11//-----------------------------------------------------------------------------
12
13#define TRG_SHORT_NAMES
14#define TRGCDC_SHORT_NAMES
15
16#ifndef __EXTERNAL__
17#include "trg/cdc/JSignal.h"
18#include "trg/cdc/FpgaUtility.h"
19#include "trg/cdc/JSignalData.h"
20#else
21#include "JSignal.h"
22#include "FpgaUtility.h"
23#include "JSignalData.h"
24#endif
25#include <iostream>
26#include <sstream>
27#include <fstream>
28#include <cstdlib>
29#include <cmath>
30//#include <algorithm>
31
32using std::cout;
33using std::endl;
34using std::pair;
35using std::make_pair;
36using std::tuple;
37using std::vector;
38using std::map;
39using std::string;
40using std::stringstream;
41using std::to_string;
42using std::get;
43using std::max;
44using std::min;
45using std::ofstream;
46using std::ios;
47
48namespace Belle2 {
49
50 /// Constructor.
51 TRGCDCJSignal::TRGCDCJSignal()
52 {
53 (*this) = TRGCDCJSignal(0);
54 }
55
56 TRGCDCJSignal::TRGCDCJSignal(TRGCDCJSignalData* commonData)
57 : m_name(""),
58 m_vhdlCode("")
59 {
60 m_type = 1;
61 m_bitsize = -1;
62 m_int = 0;
63 m_minInt = 0;
64 m_maxInt = 0;
65 m_actual = 0;
66 m_minActual = 0;
67 m_maxActual = 0;
68 m_toReal = 0;
69 m_debug = 0;
70 m_finishClock = -2;
71 m_commonData = commonData;
72 }
73
74 TRGCDCJSignal::TRGCDCJSignal(int const& bitwidth, double const& value, double const& minValue, double const& maxValue,
75 int const& clock, TRGCDCJSignalData* commonData)
76 {
77 m_bitsize = bitwidth;
78 m_type = calType(minValue);
79 if (m_type == -1) {
80 m_toReal = 1 / ((pow(2, bitwidth - 1) - 0.5) / max(maxValue, fabs(minValue)));
81 } else if (m_type == 1) {
82 m_toReal = 1 / ((pow(2, bitwidth) - 0.5) / maxValue);
83 }
84 m_int = FpgaUtility::roundInt(value / m_toReal);
85 m_minInt = FpgaUtility::roundInt(minValue / m_toReal);
86 m_maxInt = FpgaUtility::roundInt(maxValue / m_toReal);
87 m_actual = value;
88 m_minActual = minValue;
89 m_maxActual = maxValue;
90 m_debug = 0;
91 m_name = "";
92 m_vhdlCode = "";
93 m_finishClock = clock;
94 m_commonData = commonData;
95 // Check if there is overflow or underflow.
96 checkInt("TRGCDCJSignal(int, int, double, double)");
97 }
98
99 TRGCDCJSignal::TRGCDCJSignal(int const& bitwidth, double const& value, double const& minValue, double const& maxValue,
100 TRGCDCJSignalData* commonData)
101 {
102 (*this) = TRGCDCJSignal(bitwidth, value, minValue, maxValue, 0, commonData);
103 }
104
105 TRGCDCJSignal::TRGCDCJSignal(signed long long const& intValue, double const& toReal, signed long long const& minInt,
106 signed long long const& maxInt, double const& actual, double const& minActual, double const& maxActual, int const& finishClock,
107 TRGCDCJSignalData* commonData, bool b_slv)
108 {
109 m_int = intValue;
7
Assigned value is uninitialized
110 m_minInt = minInt;
111 m_maxInt = maxInt;
112 m_actual = actual;
113 m_minActual = minActual;
114 m_maxActual = maxActual;
115 m_toReal = toReal;
116 if (b_slv == 0) m_type = calType();
117 else m_type = 2;
118 m_bitsize = calBitwidth();
119 m_finishClock = finishClock;
120 m_debug = 0;
121 m_name = "";
122 m_vhdlCode = "";
123 m_commonData = commonData;
124 // Check if there is overflow or underflow.
125 checkInt("TRGCDCJSignal(int, double, signed long long, signed long long, double, double, double)");
126 }
127
128 TRGCDCJSignal::TRGCDCJSignal(double const& value, double const& toReal, TRGCDCJSignalData* commonData)
129 {
130 m_type = calType(value);
131 m_toReal = toReal;
132 m_int = FpgaUtility::roundInt(value / m_toReal);
133 m_minInt = m_int;
134 m_maxInt = m_int;
135 m_actual = value;
136 m_minActual = value;
137 m_maxActual = value;
138 m_bitsize = calBitwidth();
139 m_debug = 0;
140 m_name = "";
141 m_vhdlCode = "";
142 m_finishClock = -1;
143 m_commonData = commonData;
144 // Check if there is overflow or underflow.
145 checkInt("TRGCDCJSignal(double, double)");
146 }
147
148 TRGCDCJSignal::TRGCDCJSignal(std::vector<bool> const& slvValue, int const& finishClock, TRGCDCJSignalData* commonData)
149 : m_name("")
150 {
151 m_type = 2;
152 m_bitsize = slvValue.size();
153 m_slv = slvValue;
154 m_int = 0;
155 m_minInt = 0;
156 m_maxInt = 0;
157 m_actual = 0;
158 m_minActual = 0;
159 m_maxActual = 0;
160 m_toReal = 0;
161 m_debug = 0;
162 m_vhdlCode = "";
163 m_finishClock = finishClock;
164 m_commonData = commonData;
165 }
166
167
168 TRGCDCJSignal& TRGCDCJSignal::assignTo(TRGCDCJSignal const& rhs, int targetClock, string& finalCode)
169 {
170
171 TRGCDCJSignal t_rhs(rhs);
172 // Calculate result final clock.
173 int t_calFinishClock = t_rhs.calFinishClock();
174 // If t_rhs finish clock is blank or constant, skip.
175 if (!(t_calFinishClock == -2 || t_calFinishClock == -1)) {
176 // If target clock is not set, add 1 to t_rhs finish clock.
177 if (targetClock == -3) t_rhs.m_finishClock = t_calFinishClock + 1;
178 else {
179 if (targetClock < t_calFinishClock) cout << "[Error] TRGCDCJSignal::assign() => targetClock is too small." << endl;
180 else t_rhs.m_finishClock = targetClock;
181 }
182 }
183 //if(m_name!="") cout<<"Signal name: "<<m_name<<" Clock: "<<t_rhs.m_finishClock<<endl;
184
185 //// If Signal is not from blank constructor, or has a name.
186 if ((*this).m_bitsize != -1 || (*this).m_name != "")
187 //if((*this).m_bitsize != -1)
188 {
189
190 if ((*this).m_bitsize == 1) {
191 // Check if rhs and lhs bitsize is the same.
192 if ((*this).m_bitsize != t_rhs.m_bitsize) {
193 cout << "[Warning] TRGCDCJSignal::operator<=() => rhs and lhs do not have same bitsize." << endl;
194 }
195 // Check if rhs and lhs is same type.
196 if ((*this).m_type != t_rhs.m_type) {
197 cout << "[Warning] TRGCDCJSignal::operator<=() => rhs and lhs do not have same type." << endl;
198 }
199 // Check if rhs and lhs clock is the same.
200 if (m_finishClock != t_rhs.m_finishClock) {
201 cout << "[Warning] TRGCDCJSignal::operator<=() => rhs and lhs do not have same clock." << endl;
202 cout << m_name << " lhs clock: " << m_finishClock << " rhs clock: " << t_rhs.m_finishClock << endl;
203 }
204 }
205
206 //int t_type;
207 //if(t_rhs.m_minInt<0) t_type = -1;
208 //else t_type = 1;
209 //int t_bitsize = ceil(log(max(t_rhs.m_maxInt, abs(t_rhs.m_minInt))+1)/log(2));
210 //if(t_type != 1) t_bitsize++;
211 //// Check if bitwidth and calculated bitwidth match.
212 //if(t_rhs.m_bitsize != t_bitsize){
213 // cout<<"Bitsize and calculated bitwidth don't match"<<endl;
214 // cout<<"Bitsize: "<<t_rhs.m_bitsize<<" calBitsize: "<<t_bitsize<<endl;
215 // cout<<"LHS"<<endl;
216 // dump();
217 // cout<<"RHS"<<endl;
218 // t_rhs.dump();
219 // cout<<"Bitsize and calculated bitwidth don't match"<<endl;
220 //}
221 //// Check if type and calculated type match.
222 //if(t_rhs.m_type != t_type){
223 // cout<<"Type and calculated type don't match"<<endl;
224 // cout<<"Type: "<<t_rhs.m_type<<" calType: "<<t_type<<endl;
225 // cout<<"LHS"<<endl;
226 // dump();
227 // cout<<"RHS"<<endl;
228 // t_rhs.dump();
229 // cout<<"Type and calculated type don't match"<<endl;
230 //}
231
232 // Set arguments if needed.
233 if (t_rhs.m_argumentSignals.size() == 0) t_rhs.initArgumentSignals();
234
235 // Print m_vhdlCode.
236 string t_finalCode(finalCode);
237
238 if (getPrintVhdl() == 1) {
239 finalCode = assignVhdlCode(*this, t_rhs);
240 if (t_finalCode == "printAssignVhdlCode") {
241 //printVhdl(finalCode);
242 m_commonData->m_vhdlInProcess += finalCode + "\n";
243 }
244 }
245
246 } // End of not blank constructor.
247
248 swap(*this, t_rhs);
249 // Operations due to swap.
250 // Keep original name
251 m_name = t_rhs.m_name;
252 // Clear code.
253 m_vhdlCode = "";
254 m_argumentSignals.clear();
255
256 // Don't propagate debug
257 m_debug = 0;
258
259 return *this;
260 }
261
262 TRGCDCJSignal& TRGCDCJSignal::assignTo(TRGCDCJSignal const& rhs, int targetClock)
263 {
264 string finalCode = "printAssignVhdlCode";
265 return assignTo(rhs, targetClock, finalCode);
266 }
267
268 TRGCDCJSignal& TRGCDCJSignal::operator<= (TRGCDCJSignal const& rhs)
269 {
270 string finalCode = "printAssignVhdlCode";
271 return assignTo(rhs, -3, finalCode);
272 }
273
274 /// Unary operator
275 TRGCDCJSignal const TRGCDCJSignal::operator- () const
276 {
277
278 TRGCDCJSignal t_signal(*this);
279
280 //if(t_signal.m_type==2) {
281 // cout<<"[Error] TRGCDCJSignal::operator-(void) => should not be used with slv type."<<endl;
282 //}
283
284 if (t_signal.m_argumentSignals.size() == 0) t_signal.initArgumentSignals();
285
286 t_signal.m_int *= -1;
287 t_signal.m_minInt = -(*this).m_maxInt;
288 t_signal.m_maxInt = -(*this).m_minInt;
289 t_signal.m_actual *= -1;
290 t_signal.m_minActual = -m_maxActual;
291 t_signal.m_maxActual = -m_minActual;
292 if (m_type == 1) {
293 t_signal.m_type = -1;
294 t_signal.m_bitsize += 1;
295 }
296
297 //t_signal.m_vhdlCode = vhdlCode("-", *this, t_signal);
298 if (getPrintVhdl() == 1) {
299 vhdlCode("-", *this, t_signal, t_signal.m_vhdlCode);
300 }
301
302 return t_signal;
303 }
304
305 /// Arithmetic operator.
306 TRGCDCJSignal const TRGCDCJSignal::operator+ (TRGCDCJSignal const& rhs) const
307 {
308 TRGCDCJSignal t_lhs(*this);
309 TRGCDCJSignal t_rhs(rhs);
310
311 //if(t_lhs.m_type==2 || t_rhs.m_type==2) {
312 // cout<<"[Error] TRGCDCJSignal::operator+() => should not be used with slv type."<<endl;
313 //}
314
315 if (t_lhs.m_argumentSignals.size() == 0) t_lhs.initArgumentSignals();
316 if (t_rhs.m_argumentSignals.size() == 0) t_rhs.initArgumentSignals();
317
318 if (m_debug == 1) {
319 cout << "TRGCDCJSignal::operator+=()" << endl;
320 cout << "<<<Input lhs>>>" << endl;
321 t_lhs.dump();
322 cout << "<<<Input lhs>>>" << endl;
323 cout << "<<<Input rhs>>>" << endl;
324 t_rhs.dump();
325 cout << "<<<Input rhs>>>" << endl;
326 }
327 // Match m_toReal(unit) by shifting bits.
328 matchUnit(t_lhs, t_rhs);
329
330 // To save toReal, debug
331 TRGCDCJSignal t_result(t_lhs);
332 combineArguments(t_lhs, t_rhs, t_result);
333
334 // Set name
335 t_result.m_name = t_lhs.m_name + "+" + t_rhs.m_name;
336 // Add values.
337 t_result.m_int = t_lhs.m_int + t_rhs.m_int;
338 t_result.m_minInt = t_lhs.m_minInt + t_rhs.m_minInt;
339 t_result.m_maxInt = t_lhs.m_maxInt + t_rhs.m_maxInt;
340 t_result.m_actual = t_lhs.m_actual + t_rhs.m_actual;
341 t_result.m_minActual = t_lhs.m_minActual + t_rhs.m_minActual;
342 t_result.m_maxActual = t_lhs.m_maxActual + t_rhs.m_maxActual;
343 // Calculate bitsize.
344 //int outputBitsize;
345 //int outputType;
346 //calTypeBitwidth(t_lhs, "+", t_rhs, outputType, outputBitsize);
347 //Set output bitsize and type.
348 //t_result.m_type = outputType;
349 //t_result.m_bitsize = outputBitsize;
350 t_result.m_type = t_result.calType();
351 t_result.m_bitsize = t_result.calBitwidth();
352 // Check for overflow underflow.
353 t_result.checkInt("TRGCDCJSignal::operator+");
354 if (m_debug == 1) {
355 cout << "<<<Output>>>" << endl;
356 t_result.dump();
357 cout << "<<<Output>>>" << endl;
358 cout << "TRGCDCJSignal::operator+=()" << endl;
359 }
360
361 //t_result.m_vhdlCode = vhdlCode(t_lhs, "+", t_rhs, t_result);
362 if (getPrintVhdl() == 1) vhdlCode(t_lhs, "+", t_rhs, t_result, t_result.m_vhdlCode);
363
364 return t_result;
365 }
366
367 TRGCDCJSignal const TRGCDCJSignal::operator- (TRGCDCJSignal const& rhs) const
368 {
369
370 TRGCDCJSignal t_lhs(*this);
371 TRGCDCJSignal t_rhs(rhs);
372
373 //if(t_lhs.m_type==2 || t_rhs.m_type==2) {
374 // cout<<"[Error] TRGCDCJSignal::operator-() => should not be used with slv type."<<endl;
375 //}
376
377 if (t_lhs.m_argumentSignals.size() == 0) t_lhs.initArgumentSignals();
378 if (t_rhs.m_argumentSignals.size() == 0) t_rhs.initArgumentSignals();
379
380 if (m_debug == 1) {
381 cout << "TRGCDCJSignal::operator-=()" << endl;
382 cout << "<<<Input lhs>>>" << endl;
383 t_lhs.dump();
384 cout << "<<<Input lhs>>>" << endl;
385 cout << "<<<Input rhs>>>" << endl;
386 t_rhs.dump();
387 cout << "<<<Input rhs>>>" << endl;
388 }
389 // Match m_toReal(unit) by shifting bits.
390 matchUnit(t_lhs, t_rhs);
391
392 // To save toReal, debug
393 TRGCDCJSignal t_result(t_lhs);
394 combineArguments(t_lhs, t_rhs, t_result);
395 // Set name
396 t_result.m_name = t_lhs.m_name + "-" + t_rhs.m_name;
397 // Subtract values.
398 t_result.m_int = t_lhs.m_int - t_rhs.m_int;
399 if (isSameSignal(t_lhs, t_rhs)) {
400 t_result.m_minInt = 0;
401 t_result.m_maxInt = 0;
402 t_result.m_minActual = 0;
403 t_result.m_maxActual = 0;
404 } else {
405 t_result.m_minInt = t_lhs.m_minInt - t_rhs.m_maxInt;
406 t_result.m_maxInt = t_lhs.m_maxInt - t_rhs.m_minInt;
407 t_result.m_minActual = t_lhs.m_minActual - t_rhs.m_maxActual;
408 t_result.m_maxActual = t_lhs.m_maxActual - t_rhs.m_minActual;
409 }
410 t_result.m_actual = t_lhs.m_actual - t_rhs.m_actual;
411 // Calculate bitsize.
412 t_result.m_type = t_result.calType();
413 t_result.m_bitsize = t_result.calBitwidth();
414 // Check for overflow underflow.
415 t_result.checkInt("TRGCDCJSignal::operator-");
416 if (m_debug == 1) {
417 cout << "<<<Output>>>" << endl;
418 t_result.dump();
419 cout << "<<<Output>>>" << endl;
420 cout << "TRGCDCJSignal::operator-=()" << endl;
421 }
422 //t_result.m_vhdlCode = vhdlCode(t_lhs, "-", t_rhs, t_result);
423 if (getPrintVhdl() == 1) vhdlCode(t_lhs, "-", t_rhs, t_result, t_result.m_vhdlCode);
424
425 return t_result;
426 }
427
428 TRGCDCJSignal const TRGCDCJSignal::operator* (TRGCDCJSignal const& rhs) const
429 {
430
431 // Shift values if input bit size is larger then signed 25 bits * signed 18 bits (unsigned 24 bits * unsigned 17 bits)
432 // Find largest and second bitsize.
433 TRGCDCJSignal t_large(*this);
434 TRGCDCJSignal t_small(rhs);
435
436 //if(t_large.m_type==2 || t_small.m_type==2) {
437 // cout<<"[Error] TRGCDCJSignal::operator*() => should not be used with slv type."<<endl;
438 //}
439
440 if (t_large.m_argumentSignals.size() == 0) t_large.initArgumentSignals();
441 if (t_small.m_argumentSignals.size() == 0) t_small.initArgumentSignals();
442
443 if (m_debug == 1) {
444 cout << "TRGCDCJSignal::operator*=()" << endl;
445 cout << "<<<Input lhs>>>" << endl;
446 t_large.dump();
447 cout << "<<<Input lhs>>>" << endl;
448 cout << "<<<Input rhs>>>" << endl;
449 t_small.dump();
450 cout << "<<<Input rhs>>>" << endl;
451 }
452 bool orderSwap = orderLargestBitsize(t_large, t_small);
453 int nBitsLargeShift = 0;
454 int nBitsSmallShift = 0;
455 // Shift if needed.
456 if (t_large.m_type == 1) {
457 if (t_large.m_bitsize > 24) {
458 nBitsLargeShift = t_large.m_bitsize - 24;
459 t_large = t_large.shift(nBitsLargeShift, 0);
460 }
461 } else {
462 if (t_large.m_bitsize > 25) {
463 nBitsLargeShift = t_large.m_bitsize - 25;
464 t_large = t_large.shift(nBitsLargeShift, 0);
465 }
466 }
467 if (t_small.m_type == 1) {
468 if (t_small.m_bitsize > 17) {
469 nBitsSmallShift = t_small.m_bitsize - 17;
470 t_small = t_small.shift(nBitsSmallShift, 0);
471 }
472 } else {
473 if (t_small.m_bitsize > 18) {
474 nBitsSmallShift = t_small.m_bitsize - 18;
475 t_small = t_small.shift(nBitsSmallShift, 0);
476 }
477 }
478 if (m_debug == 1) {
479 cout << "<<<";
480 if (orderSwap) cout << nBitsSmallShift << " bit right shifted input lhs>>>" << endl;
481 else cout << nBitsLargeShift << " bit right shifted input lhs>>>" << endl;
482 if (orderSwap) t_small.dump();
483 else t_large.dump();
484 cout << "<<<Shifted input lhs>>>" << endl;
485 cout << "<<<";
486 if (orderSwap) cout << nBitsLargeShift << " bit right shifted input rhs>>>" << endl;
487 else cout << nBitsSmallShift << " bit right shifted input rhs>>>" << endl;
488 if (orderSwap) t_large.dump();
489 else t_small.dump();
490 cout << "<<<Shifted input rhs>>>" << endl;
491 }
492
493 // To save debug
494 TRGCDCJSignal t_result(t_large);
495 combineArguments(t_large, t_small, t_result);
496 // Set name
497 t_result.m_name = t_large.m_name + "*" + t_small.m_name;
498 // Multiply values.
499 t_result.m_int = t_large.m_int * t_small.m_int;
500 t_result.m_actual = t_large.m_actual * t_small.m_actual;
501
502 if (isSameSignal(*this, rhs)) {
503 if (t_large.m_minInt >= 0 && t_small.m_minInt >= 0) t_result.m_minInt = t_large.m_minInt * t_small.m_minInt;
504 else t_result.m_minInt = 0;
505 if (t_large.m_minActual >= 0 && t_small.m_minActual >= 0) t_result.m_minActual = t_large.m_minActual * t_small.m_minActual;
506 else t_result.m_minActual = 0;
507 } else {
508 if (t_large.m_minInt >= 0 && t_small.m_minInt >= 0) t_result.m_minInt = t_large.m_minInt * t_small.m_minInt;
509 else t_result.m_minInt = min(t_large.m_minInt * t_small.m_maxInt, t_large.m_maxInt * t_small.m_minInt);
510 if (t_large.m_minActual >= 0 && t_small.m_minActual >= 0) t_result.m_minActual = t_large.m_minActual * t_small.m_minActual;
511 else t_result.m_minActual = min(t_large.m_minActual * t_small.m_maxActual, t_large.m_maxActual * t_small.m_minActual);
512 }
513 t_result.m_maxInt = max(t_large.m_maxInt * t_small.m_maxInt, t_large.m_minInt * t_small.m_minInt);
514 t_result.m_maxActual = max(t_large.m_maxActual * t_small.m_maxActual, t_large.m_minActual * t_small.m_minActual);
515
516 // Set unit
517 t_result.m_toReal = t_large.m_toReal * t_small.m_toReal;
518 // Calculate bitsize and type.
519 t_result.m_type = t_result.calType();
520 t_result.m_bitsize = t_result.calBitwidth();
521 // Check for overflow underflow.
522 t_result.checkInt("TRGCDCJSignal::operator*");
523 if (m_debug == 1) {
524 cout << "<<<Output>>>" << endl;
525 t_result.dump();
526 cout << "<<<Output>>>" << endl;
527 cout << "TRGCDCJSignal::operator*=()" << endl;
528 }
529
530 if (getPrintVhdl() == 1) vhdlCode(t_large, "*", t_small, t_result, t_result.m_vhdlCode);
531
532 return t_result;
533 }
534
535 void TRGCDCJSignal::dump() const
536 {
537 cout << "-----------------------------------" << endl;
538 cout << "name: " << m_name << endl;
539 cout << "type: " << m_type << endl;
540 cout << "bitsize: " << m_bitsize << endl;
541 int t_org = cout.precision();
542 cout.precision(15);
543 cout << "actual: " << m_actual << endl;
544 cout.precision(t_org);
545 cout << "int: " << m_int << endl;
546 cout << "minInt: " << m_minInt << endl;
547 cout << "maxInt: " << m_maxInt << endl;
548 cout << "minActual: " << m_minActual << endl;
549 cout << "maxActual: " << m_maxActual << endl;
550 cout << "realInt: " << m_int* m_toReal << endl;
551 cout << "toReal: " << m_toReal << endl;
552 cout << "clock: " << m_finishClock << endl;
553 cout << "debug|printVhdl: " << m_debug << "|" << getPrintVhdl() << endl;
554 cout << "-----------------------------------" << endl;
555 }
556
557 TRGCDCJSignal const TRGCDCJSignal::shift(int nBits, int operate) const
558 {
559 TRGCDCJSignal t_signal(*this);
560
561 if (t_signal.m_argumentSignals.size() == 0) t_signal.initArgumentSignals();
562
563 t_signal.m_bitsize -= nBits;
564 t_signal.m_int = floor(this->m_int / pow(2, nBits));
565 t_signal.m_minInt = floor(this->m_minInt / pow(2, nBits));
566 t_signal.m_maxInt = floor(this->m_maxInt / pow(2, nBits));
567 if (operate == 0) {
568 t_signal.m_toReal = this->m_toReal * pow(2, nBits);
569 } else {
570 t_signal.m_actual = this->m_actual / pow(2, nBits);
571 t_signal.m_minActual = this->m_minActual / pow(2, nBits);
572 t_signal.m_maxActual = this->m_maxActual / pow(2, nBits);
573 }
574
575 t_signal.checkInt("TRGCDCJSignal::shift()");
576
577 if (getPrintVhdl() == 1) {
578 if (nBits < 0) {
579 vhdlCode("shift_left", *this, t_signal, t_signal.m_vhdlCode);
580 } else if (nBits != 0) {
581 vhdlCode("shift_right", *this, t_signal, t_signal.m_vhdlCode);
582 }
583 }
584
585 return t_signal;
586 }
587
588 TRGCDCJSignal const TRGCDCJSignal::offset(TRGCDCJSignal const& valueMin) const
589 {
590 TRGCDCJSignal t_offset = *this - valueMin;
591 // Need to find minimum bitsize
592 t_offset.m_bitsize = t_offset.calBitwidth();
593 if (t_offset.calType() != 1) cout << "[Error] TRGCDCJSignal::offset() => t_offset min value below 0. " << endl;
594 t_offset.m_type = 1;
595 // Check if there is overflow or underflow
596 t_offset.checkInt("TRGCDCJSignal::offset()");
597 // Reset vhdlCode.
598 if (getPrintVhdl() == 1) vhdlCode(*this, "-", valueMin, t_offset, t_offset.m_vhdlCode);
599 return t_offset;
600 }
601
602 TRGCDCJSignal const TRGCDCJSignal::invOffset(TRGCDCJSignal const& valueMin) const
603 {
604 TRGCDCJSignal t_result = *this + valueMin;
605 // Set bitsize and type.
606 t_result.m_type = t_result.calType();
607 t_result.m_bitsize = t_result.calBitwidth();
608
609 // Check if there is overflow or underflow
610 t_result.checkInt("TRGCDCJSignal::invOffset()");
611 // Reset vhdlCode.
612 if (getPrintVhdl() == 1) vhdlCode(*this, "+", valueMin, t_result, t_result.m_vhdlCode);
613 return t_result;
614 }
615
616
617 void TRGCDCJSignal::choose(TRGCDCJSignal& target, TRGCDCJSignal const& targetMin, TRGCDCJSignal const& targetMax,
618 TRGCDCJSignal const& reference, std::vector<std::vector<TRGCDCJSignal> > data, int targetClock)
619 {
620
621 TRGCDCJSignal t_reference(reference);
622 if (t_reference.m_argumentSignals.size() == 0) t_reference.initArgumentSignals();
623
624 // Handle input errors.
625 if (data.size() == 0 || data.size() == 1) {
626 cout << "[Error] TRGCDCJSignal::choose() => There is not enough data." << endl;
627 return;
628 }
629 if ((*(data.end() - 1)).size() != 1) {
630 cout << "[Error] TRGCDCJSignal::choose() => Last data doesn't contain 1 TRGCDCJSignal." << endl;
631 return;
632 }
633
634 // Calculate result final clock.
635 int t_maxClock = t_reference.m_finishClock;
636 for (unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
637 const TRGCDCJSignal* assign = &(data[iCondition][0]);
638 if (t_maxClock < assign->m_finishClock) t_maxClock = assign->m_finishClock;
639 }
640 // Save final clock.
641 if (targetClock == -3) target.m_finishClock = t_maxClock + 1;
642 else {
643 if (targetClock < t_maxClock) cout << "[Error] TRGCDCJSignal::choose() => targetClock is too small." << endl;
644 else target.m_finishClock = targetClock;
645 }
646
647 // Change assignment black signal constructors to min and max.
648 // Change assignment min, max (int, actual).
649 signed long long t_minInt = targetMin.m_minInt;
650 signed long long t_maxInt = targetMax.m_maxInt;
651 double t_minActual = targetMin.m_minActual;
652 double t_maxActual = targetMax.m_maxActual;
653 for (unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
654 TRGCDCJSignal* from = 0;
655 TRGCDCJSignal* to = 0;
656 TRGCDCJSignal* assign = &(data[iCondition][0]);
657
658 // To keep properties.
659 TRGCDCJSignal t_assign(*assign);
660
661 // To prevent target clock to become constant.
662 //if(assign->m_minInt==assign->m_maxInt) assign->m_finishClock= target.m_finishClock-1;
663 //else assign->m_finishClock= target.m_finishClock;
664 // Change finish clock to target clock.
665 // Init arguments.
666 if (assign->m_argumentSignals.size() == 0) assign->initArgumentSignals();
667 assign->m_finishClock = target.m_finishClock;
668
669 // Change min, max (int, actual), type values.
670 assign->m_minInt = t_minInt;
671 assign->m_maxInt = t_maxInt;
672 assign->m_minActual = t_minActual;
673 assign->m_maxActual = t_maxActual;
674 assign->m_type = assign->calType();
675 assign->m_bitsize = assign->calBitwidth();
676 // Calculated vhdlCode due to above changes.
677 if (reference.getPrintVhdl() == 1) vhdlCode("none", t_assign, *assign, assign->m_vhdlCode);
678
679 // For non-default
680 if (data[iCondition].size() != 1) {
681 // Change blank signal constructors to min and max.
682 from = &(data[iCondition][1]);
683 to = &(data[iCondition][2]);
684 if (from->m_bitsize == -1) {
685 (*from) = TRGCDCJSignal(t_reference.m_minInt, t_reference.m_toReal, t_reference.m_minInt, t_reference.m_minInt,
686 t_reference.m_minActual, t_reference.m_minActual, t_reference.m_minActual, -1, t_reference.m_commonData);
687 }
688 if (to->m_bitsize == -1) {
689 (*to) = TRGCDCJSignal(t_reference.m_maxInt, t_reference.m_toReal, t_reference.m_maxInt, t_reference.m_maxInt,
690 t_reference.m_maxActual, t_reference.m_maxActual, t_reference.m_maxActual, -1, t_reference.m_commonData);
691 }
692 }
693
694 if (from) {
695 if (from->m_argumentSignals.size() == 0) from->initArgumentSignals();
696 }
697 if (to) {
698 if (to->m_argumentSignals.size() == 0) to->initArgumentSignals();
699 }
700 }
701 // For integer case.
702 for (unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
703 TRGCDCJSignal const* assign = &(data[iCondition][0]);
704 // For default case.
705 if (data[iCondition].size() == 1) {
706 string t_string = "";
707 target.assignTo(*assign, target.m_finishClock, t_string);
708 // For non-default cases.
709 } else {
710 TRGCDCJSignal const* from = &(data[iCondition][1]);
711 TRGCDCJSignal const* to = &(data[iCondition][2]);
712 if (t_reference.m_int >= from->m_int && t_reference.m_int <= to->m_int) {
713 string t_string = "";
714 target.assignTo(*assign, target.m_finishClock, t_string);
715 break;
716 }
717 }
718 }
719 // For actual case.
720 for (unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
721 TRGCDCJSignal const* assign = &(data[iCondition][0]);
722 // For default case.
723 if (data[iCondition].size() == 1) {
724 target.m_actual = (*assign).m_actual;
725 // For non-default case.
726 } else {
727 TRGCDCJSignal const* from = &(data[iCondition][1]);
728 TRGCDCJSignal const* to = &(data[iCondition][2]);
729 assign = &(data[iCondition][0]);
730 if (t_reference.m_actual >= from->m_actual && t_reference.m_actual <= to->m_actual) {
731 target.m_actual = (*assign).m_actual;
732 break;
733 }
734 }
735 }
736
737 target.checkInt("TRGCDCJSignal::choose()");
738
739 // Print Vhdl code.
740 if (reference.getPrintVhdl() == 1) {
741 string t_finalCode = chooseVhdlCode(target, t_reference, data);
742 target.m_commonData->m_vhdlInProcess += t_finalCode + "\n";
743 //target.printVhdl(t_finalCode);
744 }
745
746 }
747
748 void TRGCDCJSignal::choose(TRGCDCJSignal& target, TRGCDCJSignal const& targetMin, TRGCDCJSignal const& targetMax,
749 TRGCDCJSignal const& reference, const std::vector<std::vector<TRGCDCJSignal> >& data)
750 {
751 choose(target, targetMin, targetMax, reference, data, -3);
752 }
753
754 void TRGCDCJSignal::choose(TRGCDCJSignal& target, TRGCDCJSignal const& reference,
755 const std::vector<std::vector<TRGCDCJSignal> >& data,
756 int targetClock)
757 {
758
759 signed long long t_minInt;
760 signed long long t_maxInt;
2
't_maxInt' declared without an initial value
761 double t_minActual;
762 double t_maxActual;
763 double t_toReal;
764 // Find targetMin and targetMax between assignments.
765 for (unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
3
Assuming the condition is false
4
Loop condition is false. Execution continues on line 783
766 const TRGCDCJSignal* assign = &(data[iCondition][0]);
767 if (iCondition == 0) {
768 t_minInt = assign->getMinInt();
769 t_maxInt = assign->getMaxInt();
770 t_minActual = assign->getMinActual();
771 t_maxActual = assign->getMaxActual();
772 t_toReal = assign->getToReal();
773 } else {
774 if (t_minInt > assign->getMinInt()) t_minInt = assign->getMinInt();
775 if (t_maxInt < assign->getMaxInt()) t_maxInt = assign->getMaxInt();
776 if (t_minActual > assign->getMinActual()) t_minActual = assign->getMinActual();
777 if (t_maxActual < assign->getMaxActual()) t_maxActual = assign->getMaxActual();
778 if (t_toReal != assign->getToReal()) cout << "[Error] TRGCDCJSignal::choose() => toReal are not same between assignments in data."
779 << endl;
780 }
781 }
782
783 TRGCDCJSignal t_targetMax(t_maxInt, t_toReal, t_maxInt, t_maxInt, t_maxActual, t_maxActual, t_maxActual, -1, target.m_commonData);
5
Passing value via 1st parameter 'intValue'
6
Calling constructor for 'TRGCDCJSignal'
784 TRGCDCJSignal t_targetMin(t_minInt, t_toReal, t_minInt, t_minInt, t_minActual, t_minActual, t_minActual, -1, target.m_commonData);
785
786 choose(target, t_targetMin, t_targetMax, reference, data, targetClock);
787 }
788
789 void TRGCDCJSignal::choose(TRGCDCJSignal& target, TRGCDCJSignal const& reference,
790 const std::vector<std::vector<TRGCDCJSignal> >& data)
791 {
792 choose(target, reference, data, -3);
1
Calling 'TRGCDCJSignal::choose'
793 }
794
795
796 void TRGCDCJSignal::ifElse(std::vector<std::pair<TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > >& data,
797 int targetClock)
798 {
799 // Handle input errors.
800 if (data.size() == 0) {
801 cout << "[Error] TRGCDCJSignal::ifElse() => There is not enough data." << endl;
802 return;
803 }
804
805 // Init arguments of data.
806 // These errors prevent else case being in between data.
807 // Loop over all cases.
808 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
809 TRGCDCJSignal const& comp = data[iCase].first;
810 if (iCase != (data.size() - 1)) {
811 if (comp.m_argumentSignals.size() == 0) cout << "[Error] TRGCDCJSignal::ifElse() => comp signal has no m_vhdl." << endl;
812 if (comp.m_minInt != 0
813 || comp.m_maxInt != 1) cout << "[Error] TRGCDCJSignal::ifElse() => comp signal m_minInt or m_maxInt is not correct." << endl;
814 }
815 // Loop over all assignments.
816 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
817 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
818 //if(rhs.m_type==2) cout<<"[Error] TRGCDCJSignal::ifElse() => Cannot handle slv in assignments currently. Should be fixed later."<<endl;
819 if (rhs.m_argumentSignals.size() == 0) rhs.initArgumentSignals();
820 }
821 }
822
823 // Calculate final clock.
824 // Loop over all cases.
825 int t_maxClock = data[0].first.calFinishClock();
826 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
827 int t_clock = data[iCase].first.calFinishClock();
828 if (t_maxClock < t_clock) t_maxClock = t_clock;
829 // Loop over all assignments.
830 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
831 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
832 t_clock = rhs.calFinishClock();
833 if (t_maxClock < t_clock) t_maxClock = t_clock;
834 }
835 }
836 int t_finishClock = 0;
837 if (targetClock == -3) {
838 if (t_maxClock < 0) cout << "[Error] TRGCDCJSignal::ifElse() => t_maxClock is negative. Did not consider this case." << endl;
839 t_finishClock = t_maxClock + 1;
840 } else {
841 if (targetClock < t_maxClock) cout << "[Error] TRGCDCJSignal::ifElse() => targetClock is too small." << endl;
842 else t_finishClock = targetClock;
843 }
844
845 // Change rhs, comp clock to finish clock.
846 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
847 TRGCDCJSignal& comp = data[iCase].first;
848 comp.m_finishClock = t_finishClock;
849 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
850 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
851 rhs.m_finishClock = t_finishClock;
852 }
853 }
854
855 // Find min and max ranges between all rhs.
856 // <minInt, maxInt, minActual, maxActual>
857 map<TRGCDCJSignal*, pair<vector<long long int>, vector<double> > > t_signalsRange;
858 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
859 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
860 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
861 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
862 if (t_signalsRange.find(&lhs) == t_signalsRange.end()) {
863 vector<long long int> t_intRange = {rhs.m_minInt, rhs.m_maxInt};
864 vector<double> t_actualRange = {rhs.m_minActual, rhs.m_maxActual};
865 t_signalsRange[&lhs] = make_pair(t_intRange, t_actualRange);
866 } else {
867 long long int& t_minInt = t_signalsRange[&lhs].first[0];
868 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
869 double& t_minActual = t_signalsRange[&lhs].second[0];
870 double& t_maxActual = t_signalsRange[&lhs].second[1];
871 if (t_minInt > rhs.m_minInt) t_minInt = rhs.m_minInt;
872 if (t_maxInt < rhs.m_maxInt) t_maxInt = rhs.m_maxInt;
873 if (t_minActual > rhs.m_minActual) t_minActual = rhs.m_minActual;
874 if (t_maxActual < rhs.m_maxActual) t_maxActual = rhs.m_maxActual;
875 }
876 }
877 }
878 // Change min, max ranges and type, bitwidth for all rhs.
879 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
880 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
881 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
882 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
883 TRGCDCJSignal orgRhs(rhs);
884 long long int& t_minInt = t_signalsRange[&lhs].first[0];
885 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
886 double& t_minActual = t_signalsRange[&lhs].second[0];
887 double& t_maxActual = t_signalsRange[&lhs].second[1];
888 rhs.m_minInt = t_minInt;
889 rhs.m_maxInt = t_maxInt;
890 rhs.m_minActual = t_minActual;
891 rhs.m_maxActual = t_maxActual;
892 rhs.m_type = rhs.calType();
893 rhs.m_bitsize = rhs.calBitwidth();
894 if (rhs.getPrintVhdl() == 1) vhdlCode("none", orgRhs, rhs, rhs.m_vhdlCode);
895 }
896 }
897
898 // For integer case.
899 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
900 TRGCDCJSignal const& comp = data[iCase].first;
901 // If comp is 1 or else.
902 if (comp.m_int == 1 || comp.m_bitsize == -1) {
903 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
904 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
905 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
906 string t_string = "";
907 lhs.assignTo(rhs, t_finishClock, t_string);
908 // Check ranges.
909 lhs.checkInt("TRGCDCJSignal::ifElse()");
910 }
911 break;
912 }
913 }
914
915 // For actual case.
916 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
917 TRGCDCJSignal const& comp = data[iCase].first;
918 // If comp is 1 or else.
919 if (comp.m_actual == 1 || comp.m_bitsize == -1) {
920 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
921 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
922 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
923 lhs.m_actual = rhs.m_actual;
924 }
925 break;
926 }
927 }
928
929 // Print vhdl code.
930 if (data[0].first.getPrintVhdl() == 1) {
931 // Set m_commonData for all lhs signals.
932 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
933 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
934 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
935 /* cppcheck-suppress variableScope */
936 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
937 if (lhs.m_commonData == 0) lhs.m_commonData = rhs.m_commonData;
938 }
939 }
940 string t_finalCode = ifElseVhdlCode(data);
941 data[0].first.m_commonData->m_vhdlInProcess += t_finalCode + "\n";
942 }
943
944
945 }
946
947 void TRGCDCJSignal::ifElse(std::vector<std::pair<TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > >& data)
948 {
949 ifElse(data, -3);
950 }
951
952
953 TRGCDCJSignal const TRGCDCJSignal::limit(signed long long minInt, signed long long maxInt, double minActual,
954 double maxActual) const
955 {
956 TRGCDCJSignal t_result(*this);
957
958 //if(t_result.m_type==2) {
959 // cout<<"[Error] TRGCDCJSignal::limit() => should not be used with slv type."<<endl;
960 //}
961
962
963 if (t_result.m_argumentSignals.size() == 0) t_result.initArgumentSignals();
964
965 t_result.m_minInt = minInt;
966 t_result.m_maxInt = maxInt;
967 t_result.m_minActual = minActual;
968 t_result.m_maxActual = maxActual;
969 t_result.m_type = t_result.calType();
970 t_result.m_bitsize = t_result.calBitwidth();
971 //// Check if there is overflow or underflow.
972 //t_result.checkInt("TRGCDCJSignal::limit()");
973
974
975 if (getPrintVhdl() == 1) vhdlCode("none", *this, t_result, t_result.m_vhdlCode);
976 //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
977 return t_result;
978 }
979
980 TRGCDCJSignal const TRGCDCJSignal::limit(TRGCDCJSignal const& min, TRGCDCJSignal const& max) const
981 {
982 return limit(min.m_int, max.m_int, min.m_actual, max.m_actual);
983 }
984
985 TRGCDCJSignal const TRGCDCJSignal::resize(int bitwidth) const
986 {
987 if (this->m_bitsize == bitwidth) return (*this);
988
989 TRGCDCJSignal t_result(*this);
990 if (t_result.m_argumentSignals.size() == 0) t_result.initArgumentSignals();
991
992 t_result.m_bitsize = bitwidth;
993 // Check if there is overflow or underflow.
994 t_result.checkInt("TRGCDCJSignal::resize()");
995
996
997 if (getPrintVhdl() == 1) vhdlCode("resize", *this, t_result, t_result.m_vhdlCode);
998 //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
999 return t_result;
1000 }
1001
1002 //TRGCDCJSignal const TRGCDCJSignal::toSigned() const {
1003 // if(m_type==-1) return (*this);
1004
1005 // TRGCDCJSignal t_result(*this);
1006 // if(t_result.m_argumentSignals.size() ==0) t_result.initArgumentSignals();
1007
1008 // t_result.m_type = -1;
1009 // t_result.m_bitsize = m_bitsize+1;
1010 // // Check if there is overflow or underflow.
1011 // t_result.checkInt("TRGCDCJSignal::toSigned()");
1012 //
1013
1014 // if(getPrintVhdl()==1) vhdlCode("signed", *this, t_result, t_result.m_vhdlCode);
1015 // //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
1016 // return t_result;
1017 //}
1018
1019 //TRGCDCJSignal const TRGCDCJSignal::toUnsigned() const {
1020 // if(m_type==1) return (*this);
1021 // TRGCDCJSignal t_result(*this);
1022 // if(t_result.m_argumentSignals.size() ==0) t_result.initArgumentSignals();
1023
1024 // t_result.m_type = 1;
1025 // t_result.m_bitsize = m_bitsize-1;
1026 // // Check if there is overflow or underflow.
1027 // t_result.checkInt("TRGCDCJSignal::toUnsigned()");
1028 //
1029
1030 // if(getPrintVhdl()==1) vhdlCode("unsigned", *this, t_result, t_result.m_vhdlCode);
1031 // //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
1032 // return t_result;
1033 //}
1034
1035
1036 TRGCDCJSignal const TRGCDCJSignal::slvToSigned(TRGCDCJSignal const& in, double const& toReal, signed long long const& minInt,
1037 signed long long const& maxInt, double const& actual, double const& minActual, double const& maxActual, int const& finishClock)
1038 {
1039
1040 vector<bool> const& slv = in.m_slv;
1041
1042 // Check input
1043 if (in.m_type != 2) cout << "[Error] TRGCDCJSignal::slvToSigned() => type is not slv." << endl;
1044 if (slv.size() > 63) cout << "[Error] TRGCDCJSignal::slvToSigned() => bitwidth is too large." << endl;
1045
1046 TRGCDCJSignal result(in);
1047
1048 // Calculate value. [Index 0 is lsb]
1049 result.m_int = 0;
1050 for (unsigned iBit = 0; iBit < slv.size() - 1; iBit++) {
1051 result.m_int += slv[iBit] * pow(2, iBit);
1052 }
1053 result.m_int -= slv[slv.size() - 1] * pow(2, slv.size() - 1);
1054 // Set values.
1055 result.m_minInt = minInt;
1056 result.m_maxInt = maxInt;
1057 result.m_actual = actual;
1058 result.m_minActual = minActual;
1059 result.m_maxActual = maxActual;
1060 result.m_toReal = toReal;
1061 result.m_type = 0;
1062 result.m_type = result.calType();
1063 result.m_bitsize = result.calBitwidth();
1064 result.m_finishClock = finishClock;
1065 // Check if there is overflow or underflow.
1066 result.checkInt("slvToSigned");
1067 result.m_commonData = in.m_commonData;
1068
1069 return result;
1070 }
1071
1072 TRGCDCJSignal const TRGCDCJSignal::slvToSigned(TRGCDCJSignal const& in)
1073 {
1074 return slvToSigned(in, in.m_toReal, in.m_minInt, in.m_maxInt, in.m_actual, in.m_minActual, in.m_maxActual, in.m_finishClock);
1075 }
1076
1077 TRGCDCJSignal const TRGCDCJSignal::slvToUnsigned(TRGCDCJSignal const& in, double const& toReal, signed long long const& minInt,
1078 signed long long const& maxInt, double const& actual, double const& minActual, double const& maxActual, int const& finishClock)
1079 {
1080
1081 vector<bool> const& slv = in.m_slv;
1082
1083 // Check input
1084 if (in.m_type != 2) cout << "[Error] TRGCDCJSignal::slvToUnsigned() => type is not slv." << endl;
1085 if (slv.size() > 63) cout << "[Error] TRGCDCJSignal::slvToUnsigned() => bitwidth is too large." << endl;
1086
1087 TRGCDCJSignal result(in);
1088
1089 // Calculate value. [Index 0 is lsb]
1090 result.m_int = 0;
1091 for (unsigned iBit = 0; iBit < slv.size(); iBit++) {
1092 result.m_int += slv[iBit] * pow(2, iBit);
1093 }
1094 // Set values.
1095 result.m_minInt = minInt;
1096 result.m_maxInt = maxInt;
1097 result.m_actual = actual;
1098 result.m_minActual = minActual;
1099 result.m_maxActual = maxActual;
1100 result.m_toReal = toReal;
1101 result.m_type = 0;
1102 result.m_type = result.calType();
1103 result.m_bitsize = result.calBitwidth();
1104 result.m_finishClock = finishClock;
1105 // Check if there is overflow or underflow.
1106 result.checkInt("slvToSigned");
1107 result.m_commonData = in.m_commonData;
1108
1109 return result;
1110 }
1111
1112 TRGCDCJSignal const TRGCDCJSignal::slvToUnsigned(TRGCDCJSignal const& in)
1113 {
1114 return slvToUnsigned(in, in.m_toReal, in.m_minInt, in.m_maxInt, in.m_actual, in.m_minActual, in.m_maxActual, in.m_finishClock);
1115 }
1116
1117 TRGCDCJSignal const TRGCDCJSignal::toSlv(TRGCDCJSignal const& in, int /*test*/)
1118 {
1119
1120 TRGCDCJSignal result(in);
1121
1122 // Temporary signal.
1123 //test += 0; //TODO why?
1124 signed long long t_int = in.m_int;
1125 if (in.m_type == -1) {
1126 // Change signed m_int to vector<bool>
1127 t_int = ~t_int;
1128 t_int += 1;
1129 result.m_slv.clear();
1130 result.m_slv.resize(in.m_bitsize);
1131 for (int iBit = 0; iBit < in.m_bitsize; iBit++) {
1132 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1133 }
1134 } else if (in.m_type == 1) {
1135 // Change unsigned m_int to vector<bool>
1136 result.m_slv.clear();
1137 result.m_slv.resize(in.m_bitsize);
1138 for (int iBit = 0; iBit < in.m_bitsize; iBit++) {
1139 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1140 }
1141 } else {
1142 cout << "[Error] TRGCDCJSignal::toSlv() => m_type is not 1 or -1." << endl;
1143 }
1144
1145 return result;
1146 }
1147
1148 TRGCDCJSignal const TRGCDCJSignal::toSlv(TRGCDCJSignal const& in)
1149 {
1150 if (in.m_type == 2) return (in);
1151 // Don't think I need below but will keep it for now.
1152 if (in.m_type == -1) {
1153 cout << "[Error] TRGCDCJSignal::toSlv() => Slv is not same as unsigned anymore." << endl;
1154 }
1155 TRGCDCJSignal t_result(in);
1156 t_result.m_type = 2;
1157
1158 if (in.getPrintVhdl() == 1) vhdlCode("std_logic_vector", in, t_result, t_result.m_vhdlCode);
1159
1160 return t_result;
1161 }
1162
1163
1164
1165 TRGCDCJSignal TRGCDCJSignal::comp(TRGCDCJSignal const& lhs, const std::string& operate, TRGCDCJSignal const& rhs)
1166 {
1167 // Make copies for method.
1168 TRGCDCJSignal t_lhs(lhs);
1169 TRGCDCJSignal t_rhs(rhs);
1170 TRGCDCJSignal t_result(t_lhs);
1171 // Make arguments.
1172 if (t_lhs.m_argumentSignals.size() == 0) t_lhs.initArgumentSignals();
1173 if (t_rhs.m_argumentSignals.size() == 0) t_rhs.initArgumentSignals();
1174 combineArguments(t_lhs, t_rhs, t_result);
1175 // Int Operation.
1176 bool t_bool = false;
1177 if (operate == "=") {
1178 t_bool = (t_lhs.m_int == t_rhs.m_int ? 1 : 0);
1179 } else if (operate == "/=") {
1180 t_bool = (t_lhs.m_int != t_rhs.m_int ? 1 : 0);
1181 } else 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 == "and") {
1190 t_bool = (t_lhs.m_int && t_rhs.m_int ? 1 : 0);
1191 } else if (operate == "or") {
1192 t_bool = (t_lhs.m_int || t_rhs.m_int ? 1 : 0);
1193 } else {
1194 cout << "[Error] TRGCDCJSignal::comp() => No operator named " << operate << "." << endl;
1195 }
1196 // Actual Operation.
1197 if (operate == "=") {
1198 t_result.m_actual = (t_lhs.m_actual == t_rhs.m_actual ? 1 : 0);
1199 } else if (operate == "/=") {
1200 t_result.m_actual = (t_lhs.m_actual != t_rhs.m_actual ? 1 : 0);
1201 } else if (operate == "<") {
1202 t_result.m_actual = (t_lhs.m_actual < t_rhs.m_actual ? 1 : 0);
1203 } else if (operate == "<=") {
1204 t_result.m_actual = (t_lhs.m_actual <= t_rhs.m_actual ? 1 : 0);
1205 } else if (operate == ">") {
1206 t_result.m_actual = (t_lhs.m_actual > t_rhs.m_actual ? 1 : 0);
1207 } else if (operate == ">=") {
1208 t_result.m_actual = (t_lhs.m_actual >= t_rhs.m_actual ? 1 : 0);
1209 } else if (operate == "and") {
1210 t_result.m_actual = (t_lhs.m_actual && t_rhs.m_actual ? 1 : 0);
1211 } else if (operate == "or") {
1212 t_result.m_actual = (t_lhs.m_actual || t_rhs.m_actual ? 1 : 0);
1213 } else {
1214 cout << "[Error] TRGCDCJSignal::comp() => No operator named " << operate << "." << endl;
1215 }
1216 // Change values.
1217 t_result.m_name = t_lhs.m_name + operate + t_rhs.m_name;
1218 t_result.m_type = 1;
1219 t_result.m_bitsize = 1;
1220 t_result.m_minInt = 0;
1221 t_result.m_maxInt = 1;
1222 t_result.m_minActual = 0;
1223 t_result.m_maxActual = 1;
1224 t_result.m_toReal = 1;
1225 if (t_bool == 1) {
1226 t_result.m_int = 1;
1227 } else {
1228 t_result.m_int = 0;
1229 }
1230
1231 if (t_result.getPrintVhdl() == 1) vhdlCode(t_lhs, operate, t_rhs, t_result, t_result.m_vhdlCode);
1232
1233 return t_result;
1234
1235 }
1236
1237 void TRGCDCJSignal::swap(TRGCDCJSignal& first, TRGCDCJSignal& second) const
1238 {
1239 std::swap(first.m_name, second.m_name);
1240 std::swap(first.m_type, second.m_type);
1241 std::swap(first.m_bitsize, second.m_bitsize);
1242 std::swap(first.m_int, second.m_int);
1243 std::swap(first.m_minInt, second.m_minInt);
1244 std::swap(first.m_maxInt, second.m_maxInt);
1245 std::swap(first.m_actual, second.m_actual);
1246 std::swap(first.m_minActual, second.m_minActual);
1247 std::swap(first.m_maxActual, second.m_maxActual);
1248 std::swap(first.m_toReal, second.m_toReal);
1249 std::swap(first.m_debug, second.m_debug);
1250 std::swap(first.m_vhdlCode, second.m_vhdlCode);
1251 std::swap(first.m_argumentSignals, second.m_argumentSignals);
1252 std::swap(first.m_finishClock, second.m_finishClock);
1253 std::swap(first.m_commonData, second.m_commonData);
1254 }
1255
1256 void TRGCDCJSignal::matchUnit(TRGCDCJSignal& first, TRGCDCJSignal& second) const
1257 {
1258 if (first.m_toReal != second.m_toReal) {
1259 // Find number of bits to shift to match unit.
1260 double t_nBits = log(first.m_toReal / second.m_toReal) / log(2);
1261 // Round to integer
1262 if (t_nBits > 0) {
1263 t_nBits = int(t_nBits + 0.5);
1264 } else {
1265 t_nBits = int(t_nBits - 0.5);
1266 }
1267 // Shift bits to match unit
1268 if (t_nBits > 0) {
1269 first = first.shift(-t_nBits, 0);
1270 //cout<<"mu::shift_left(resize(" + first.m_name + "," + to_string(int(first.m_bitsize+t_nBits)) + ")," + to_string(int(t_nBits)) + ")"<<endl;
1271 //cout<<first.m_vhdlCode<<endl;
1272 } else if (t_nBits < 0) {
1273 second = second.shift(t_nBits, 0);
1274 //cout<<"mu::shift_left(resize(" + second.m_name + "," + to_string(int(second.m_bitsize-t_nBits)) + ")," + to_string(int(-t_nBits)) + ")"<<endl;
1275 //cout<<second.m_vhdlCode<<endl;
1276 }
1277
1278 if (fabs(first.m_toReal - second.m_toReal) > 0.00001 * first.m_toReal) {
1279 cout << "[Warning] TRGCDCJSignal::matchUnit() => in1 and in2 units are still different." << endl;
1280 cout << first.m_toReal << " " << second.m_toReal << endl;
1281 }
1282
1283 if (m_debug == 1) {
1284 cout << "TRGCDCJSignal::matchUnit()" << endl;
1285 if (t_nBits > 0) cout << "lhs was shifted to left by " << t_nBits << endl;
1286 else if (t_nBits < 0) cout << "rhs was shifted to left by " << -t_nBits << endl;
1287 cout << "TRGCDCJSignal::matchUnit()" << endl;
1288 }
1289 } // End of if units are different.
1290 first.checkInt("TRGCDCJSignal::matchUnit()::first");
1291 second.checkInt("TRGCDCJSignal::matchUnit()::second");
1292 }
1293
1294 void TRGCDCJSignal::setName(const string& name)
1295 {
1296 m_name = name;
1297 }
1298
1299 void TRGCDCJSignal::setInt(signed long long value)
1300 {
1301 m_int = value;
1302 }
1303
1304 void TRGCDCJSignal::setActual(double value)
1305 {
1306 m_actual = value;
1307 }
1308
1309 void TRGCDCJSignal::setToReal(double value)
1310 {
1311 m_toReal = value;
1312 }
1313
1314 void TRGCDCJSignal::setType(int type)
1315 {
1316 m_type = type;
1317 }
1318
1319 void TRGCDCJSignal::setBitsize(int bitsize)
1320 {
1321 m_bitsize = bitsize;
1322
1323 // Check if there is overflow or underflow.
1324 checkInt("TRGCDCJSignal::setBitsize()");
1325 }
1326
1327 void TRGCDCJSignal::setFinishClock(int finishClock)
1328 {
1329 m_finishClock = finishClock;
1330 }
1331
1332 void TRGCDCJSignal::setDebug(bool debug)
1333 {
1334 m_debug = debug;
1335 }
1336
1337 void TRGCDCJSignal::setPrintVhdl(bool printVhdl)
1338 {
1339 if (m_commonData) m_commonData->m_printVhdl = printVhdl;
1340 else {
1341 cout << "[Error] TRGCDCJSignal::setPrintVhdl() => m_commonData does not exist." << endl;
1342 }
1343 }
1344
1345 void TRGCDCJSignal::setVhdlOutputFile(const string& filename)
1346 {
1347 if (m_commonData) m_commonData->m_vhdlOutputFile = filename;
1348 else {
1349 cout << "[Error] TRGCDCJSignal::setVhdlOutputFile() => m_commonData does not exist." << endl;
1350 }
1351 }
1352
1353 void TRGCDCJSignal::setCommonData(TRGCDCJSignalData* commonData)
1354 {
1355 m_commonData = commonData;
1356 }
1357
1358 string TRGCDCJSignal::getName() const
1359 {
1360 return m_name;
1361 }
1362
1363 int TRGCDCJSignal::getType() const
1364 {
1365 return m_type;
1366 }
1367
1368 double TRGCDCJSignal::getBitsize() const
1369 {
1370 return m_bitsize;
1371 }
1372
1373 signed long long TRGCDCJSignal::getInt() const
1374 {
1375 return m_int;
1376 }
1377
1378 signed long long TRGCDCJSignal::getMinInt() const
1379 {
1380 return m_minInt;
1381 }
1382
1383 signed long long TRGCDCJSignal::getMaxInt() const
1384 {
1385 return m_maxInt;
1386 }
1387
1388 double TRGCDCJSignal::getActual() const
1389 {
1390 return m_actual;
1391 }
1392
1393 double TRGCDCJSignal::getMinActual() const
1394 {
1395 return m_minActual;
1396 }
1397
1398 double TRGCDCJSignal::getMaxActual() const
1399 {
1400 return m_maxActual;
1401 }
1402
1403 double TRGCDCJSignal::getRealInt() const
1404 {
1405 return m_int * m_toReal;
1406 }
1407
1408 double TRGCDCJSignal::getRealMinInt() const
1409 {
1410 return m_minInt * m_toReal;
1411 }
1412
1413 double TRGCDCJSignal::getRealMaxInt() const
1414 {
1415 return m_maxInt * m_toReal;
1416 }
1417
1418 double TRGCDCJSignal::getToReal() const
1419 {
1420 return m_toReal;
1421 }
1422
1423 int TRGCDCJSignal::getFinishClock() const
1424 {
1425 return m_finishClock;
1426 }
1427
1428 std::vector<std::pair<std::string, vector<int> > > const TRGCDCJSignal::getArgumentSignals() const
1429 {
1430 return m_argumentSignals;
1431 }
1432
1433 TRGCDCJSignalData* TRGCDCJSignal::getCommonData() const
1434 {
1435 if (m_commonData) return m_commonData;
1436 else {
1437 cout << "[Error] TRGCDCJSignal::getCommonData() => m_commonData does not exist." << endl;
1438 return 0;
1439 }
1440 }
1441
1442
1443 int TRGCDCJSignal::calType() const
1444 {
1445 //if(m_type==2) cout<<"[Error] TRGCDCJSignal::calType() => m_type should not be slv."<<endl;
1446 if (m_minInt < 0) return -1;
1447 else return 1;
1448 }
1449
1450 int TRGCDCJSignal::calType(double minValue) const
1451 {
1452 //if(m_type==2) cout<<"[Error] TRGCDCJSignal::calType() => m_type should not be slv."<<endl;
1453 if (minValue < 0) return -1;
1454 else return 1;
1455 }
1456
1457 int TRGCDCJSignal::calBitwidth() const
1458 {
1459 //int t_bitwidth = ceil(log(max(m_maxInt, llabs(m_minInt))+1)/log(2));
1460 int t_bitwidth = ceil(log1p(max(m_maxInt, llabs(m_minInt))) / log(2));
1461 if (calType() != 1) t_bitwidth++;
1462 return t_bitwidth;
1463 }
1464
1465 int TRGCDCJSignal::calBitwidth(int type, signed long long minInt, signed long long maxInt)
1466 {
1467 //int t_bitwidth = ceil(log(max(maxInt, llabs(minInt))+1)/log(2));
1468 int t_bitwidth = ceil(log1p(max(maxInt, llabs(minInt))) / log(2));
1469 if (type != 1) t_bitwidth++;
1470 return t_bitwidth;
1471 }
1472
1473 int TRGCDCJSignal::calFinishClock() const
1474 {
1475 // Combined signal?
1476 if (m_argumentSignals.size() != 0) {
1477 int maxClock = -1;
1478 // Find max clock between signals.
1479 for (unsigned iSignals = 0; iSignals < m_argumentSignals.size(); iSignals++) {
1480 if (maxClock < m_argumentSignals[iSignals].second[2]) maxClock = m_argumentSignals[iSignals].second[2];
1481 }
1482 return maxClock;
1483 } else {
1484 return m_finishClock;
1485 }
1486 }
1487
1488 string TRGCDCJSignal::getVhdlCode() const
1489 {
1490 return m_vhdlCode;
1491 }
1492
1493 bool TRGCDCJSignal::getPrintVhdl() const
1494 {
1495 if (m_commonData) return m_commonData->m_printVhdl;
1496 else {
1497 cout << "[Error] TRGCDCJSignal::getPrintVhdl() => m_commonData does not exist." << endl;
1498 return 0;
1499 }
1500 }
1501
1502 string TRGCDCJSignal::getVhdlOutputFile() const
1503 {
1504 if (m_commonData) return m_commonData->m_vhdlOutputFile;
1505 else {
1506 cout << "[Error] TRGCDCJSignal::getVhdlOutputFile() => m_commonData does not exist." << endl;
1507 return "";
1508 }
1509 }
1510
1511 bool TRGCDCJSignal::orderLargestBitsize(TRGCDCJSignal& large, TRGCDCJSignal& small) const
1512 {
1513 bool result = 0;
1514 if (large.m_bitsize < small.m_bitsize) {
1515 swap(large, small);
1516 result = 1;
1517 } else if (large.m_bitsize == small.m_bitsize) {
1518 if (large.m_type == 1) {;}
1519 else if (large.m_type == -1 && small.m_type == 1) {
1520 swap(large, small);
1521 result = 1;
1522 } else if (large.m_type == 0 && small.m_type != 0) {
1523 swap(large, small);
1524 result = 1;
1525 }
1526 }
1527
1528 return result;
1529 }
1530
1531 signed long long TRGCDCJSignal::calMaxInteger() const
1532 {
1533 signed long long t_maxValue;
1534 if (calType() == 1) {
1535 t_maxValue = pow(2, m_bitsize) - 1;
1536 } else if (calType() == -1) {
1537 t_maxValue = pow(2, m_bitsize - 1) - 1;
1538 } else {
1539 t_maxValue = 0;
1540 }
1541 return t_maxValue;
1542 }
1543
1544 signed long long TRGCDCJSignal::calMinInteger() const
1545 {
1546 //if(m_type==2) cout<<"[Error] TRGCDCJSignal::calMinInteger() => m_type should not be slv"<<endl;
1547 signed long long t_minValue;
1548 if (m_type == 1) {
1549 t_minValue = 0;
1550 } else if (m_type == -1) {
1551 t_minValue = -pow(2, m_bitsize - 1) + 1;
1552 } else {
1553 t_minValue = -pow(2, m_bitsize - 1) + 1;
1554 }
1555 return t_minValue;
1556 }
1557
1558 TRGCDCJSignal const TRGCDCJSignal::absolute(TRGCDCJSignal const& first)
1559 {
1560
1561 TRGCDCJSignal result(first);
1562
1563 //if(result.m_type==2) cout<<"[Error] TRGCDCJSignal::absolute() => m_type should not be slv"<<endl;
1564
1565 if (result.m_argumentSignals.size() == 0) result.initArgumentSignals();
1566
1567 if (result.m_type != 1) {
1568 result.m_type = 1;
1569 result.m_int = llabs(first.m_int);
1570 if (first.m_minInt < 0 && first.m_maxInt > 0) result.m_minInt = 0;
1571 else result.m_minInt = min(llabs(first.m_maxInt), llabs(first.m_minInt));
1572 result.m_maxInt = max(llabs(first.m_maxInt), llabs(first.m_minInt));
1573 result.m_actual = fabs(first.m_actual);
1574 if (first.m_minActual < 0 && first.m_maxActual > 0) result.m_minActual = 0;
1575 else result.m_minActual = min(fabs(first.m_maxActual), fabs(first.m_minActual));
1576 result.m_maxActual = max(fabs(first.m_maxActual), fabs(first.m_minActual));
1577 result.m_bitsize = first.m_bitsize - 1;
1578 }
1579
1580 result.checkInt("TRGCDCJSignal::absolute()");
1581
1582 // For vhdl code.
1583 if (first.getPrintVhdl() == 1) vhdlCode("abs", first, result, result.m_vhdlCode);
1584
1585 return result;
1586
1587 }
1588
1589 //void TRGCDCJSignal::calTypeBitwidth(TRGCDCJSignal const & lhs, string operation, TRGCDCJSignal const & rhs, int & type, int & bitwidth) const{
1590 // int outputBitsize;
1591 // int outputType;
1592 // if(operation=="+"){
1593 // // Find largest and second bitsize.
1594 // TRGCDCJSignal t_large(lhs);
1595 // TRGCDCJSignal t_small(rhs);
1596 // orderLargestBitsize(t_large, t_small);
1597 // // Calculate.
1598 // if(t_large.m_type == 1) {
1599 // if(t_small.m_type == 1) {outputBitsize = t_large.m_bitsize + 1; outputType = 1;}
1600 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 2; outputType = -1;}
1601 // else {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1602 // } else if(t_large.m_type == -1) {
1603 // outputBitsize = t_large.m_bitsize + 1; outputType = -1;
1604 // } else {
1605 // if(t_small.m_type == 1) {outputBitsize = t_large.m_bitsize; outputType = -1;}
1606 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1607 // else {outputBitsize = t_large.m_bitsize + 1; outputType = 0;}
1608 // }
1609 // } else if(operation=="-"){
1610 // // Find largest and second bitsize.
1611 // TRGCDCJSignal t_large(lhs);
1612 // TRGCDCJSignal t_small(rhs);
1613 // orderLargestBitsize(t_large, t_small);
1614 // // Calculate.
1615 // if(t_large.m_type == 1) {
1616 // if(t_small.m_type == 1) {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1617 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 2; outputType = -1;}
1618 // else {
1619 // if(t_large.m_type == lhs.m_type){
1620 // outputBitsize = t_large.m_bitsize + 1; outputType = 1;
1621 // } else {
1622 // outputBitsize = t_large.m_bitsize + 1; outputType = 0;
1623 // }
1624 // }
1625 // } else if(t_large.m_type == -1) {
1626 // outputBitsize = t_large.m_bitsize + 1; outputType = -1;
1627 // } else {
1628 // if(t_small.m_type == 1) {
1629 // if(t_large.m_type == lhs.m_type){
1630 // outputBitsize = t_large.m_bitsize + 1; outputType = 0;
1631 // } else {
1632 // outputBitsize = t_large.m_bitsize + 1; outputType = 1;
1633 // }
1634 // }
1635 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1636 // else {outputBitsize = t_large.m_bitsize; outputType = -1;}
1637 // }
1638 // } else if(operation=="*"){
1639 // TRGCDCJSignal t_large(lhs);
1640 // TRGCDCJSignal t_small(rhs);
1641 // orderLargestBitsize(t_large, t_small);
1642 // if(t_large.m_type == 1 || t_small.m_type == 1) {
1643 // outputBitsize = t_large.m_bitsize + t_small.m_bitsize;
1644 // if(t_large.m_type == 1) outputType = t_small.m_type;
1645 // else outputType = t_large.m_type;
1646 // } else if(t_large.m_type == 0 && t_small.m_type == 0) {
1647 // outputBitsize = t_large.m_bitsize + t_small.m_bitsize - 2;
1648 // outputType = 1;
1649 // } else {
1650 // outputBitsize = t_large.m_bitsize + t_small.m_bitsize - 1;
1651 // outputType = -1;
1652 // }
1653 // }
1654 // type = outputType;
1655 // bitwidth = outputBitsize;
1656 //}
1657
1658 signed long long TRGCDCJSignal::calInt(double value, TRGCDCJSignal const& mother)
1659 {
1660 return (signed long long) FpgaUtility::roundInt(value / mother.m_toReal);
1661 }
1662
1663 void TRGCDCJSignal::calVhdlTypeBitwidth(TRGCDCJSignal const& first, const std::string& operation, TRGCDCJSignal const& second,
1664 int& type,
1665 int& bitwidth)
1666 {
1667 if (first.m_type != second.m_type) {
1668 cout << "[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => first and second type are different." << endl;
1669 cout << first.m_name << " " << second.m_name << endl;
1670 cout << first.m_type << " " << second.m_type << endl;
1671 return;
1672 }
1673 int firstBitwidth = first.m_bitsize;
1674 int secondBitwidth = second.m_bitsize;
1675
1676 if (operation == "+" || operation == "-") {
1677 // cppcheck-suppress knownConditionTrueFalse
1678 if (first.m_type != 1 || second.m_type != 1) {
1679 type = -1;
1680 bitwidth = max(firstBitwidth, secondBitwidth);
1681 } else {
1682 type = 1;
1683 bitwidth = max(first.m_bitsize, second.m_bitsize);
1684 }
1685 } else if (operation == "*") {
1686 // cppcheck-suppress knownConditionTrueFalse
1687 if (first.m_type != 1 || second.m_type != 1) {
1688 type = -1;
1689 bitwidth = firstBitwidth + secondBitwidth;
1690 } else {
1691 type = 1;
1692 bitwidth = first.m_bitsize + second.m_bitsize;
1693 }
1694 } else if (operation == "=" || operation == "/=" || operation == "<" || operation == "<=" ||
1695 operation == ">" || operation == ">=" || operation == "and" || operation == "or") {
1696 type = 1;
1697 bitwidth = 1;
1698 } else {
1699 cout << "[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => No operation named " << operation << "." << endl;
1700 }
1701 }
1702
1703 void TRGCDCJSignal::initArgumentSignals()
1704 {
1705 string t_name;
1706 if (m_argumentSignals.size() == 0) {
1707 if (m_minInt == m_maxInt && m_type != 2) {
1708 if (m_type == 1) {
1709 //t_name="to_unsigned("+to_string(m_int)+","+to_string(m_bitsize)+")";
1710 t_name = "decimal_string_to_unsigned(\"" + to_string(m_int) + "\"," + to_string(m_bitsize) + ")";
1711 } else {
1712 //t_name="to_signed("+to_string(m_int)+","+to_string(m_bitsize)+")";
1713 t_name = "decimal_string_to_signed(\"" + to_string(m_int) + "\"," + to_string(m_bitsize) + ")";
1714 }
1715 // For non-constant constructor signal.
1716 } else if (m_name == "") {
1717 t_name = "INPUT(" + to_string(m_int) + ")";
1718 // For normal signals.
1719 } else {
1720 t_name = m_name;
1721 }
1722 //m_argumentSignals.push_back(make_pair(t_name,m_finishClock));
1723 vector<int> t_argument = {m_type, m_bitsize, m_finishClock};
1724 m_argumentSignals.push_back(make_pair(t_name, t_argument));
1725 } else {
1726 cout << "[Error] TRGCDCJSignal::initArgumentSignals() => m_argumentSignals.size() is not 0." << endl;
1727 }
1728 }
1729
1730 void TRGCDCJSignal::combineArguments(TRGCDCJSignal const& first, TRGCDCJSignal const& second, TRGCDCJSignal& result)
1731 {
1732 result.m_argumentSignals.clear();
1733 result.m_argumentSignals.insert(result.m_argumentSignals.end(), first.m_argumentSignals.begin(), first.m_argumentSignals.end());
1734 result.m_argumentSignals.insert(result.m_argumentSignals.end(), second.m_argumentSignals.begin(), second.m_argumentSignals.end());
1735 }
1736
1737 void TRGCDCJSignal::initVhdlCode()
1738 {
1739 if (m_vhdlCode != "") {
1740 cout << "[Error] TRGCDCJSignal::initVhdlCode() => m_vhdlCode is not \"\"." << endl;
1741 } else {
1742 m_vhdlCode = " ";
1743 }
1744 }
1745
1746 void TRGCDCJSignal::toSignedVhdlCode()
1747 {
1748 if (m_type == -1) return;
1749 // Find if bitwidth is optimized for signed or unsigned. t_bitwidth is optimized for signed.
1750 int t_bitwidth = calBitwidth();
1751 if (calType() == 1) t_bitwidth++;
1752
1753 if (t_bitwidth <= m_bitsize) {
1754 m_vhdlCode = "signed(" + m_vhdlCode + ")";
1755 } else if (t_bitwidth - 1 == m_bitsize) {
1756 m_vhdlCode = "signed('0'&(" + m_vhdlCode + "))";
1757 m_bitsize += 1;
1758 } else {
1759 cout << "[Error] TRGCDCJSignal::toSignedVhdlCode() => m_bitsize is too small." << endl;
1760 }
1761 m_type = -1;
1762 }
1763
1764 void TRGCDCJSignal::toUnsignedVhdlCode()
1765 {
1766 if (m_type == 1) return;
1767
1768 if (calType() == -1) {
1769 cout << "[Warning] TRGCDCJSignal::toUnsignedVhdlCode() => signal can be minus." << endl;
1770 } else {
1771 m_vhdlCode = "unsigned(" + m_vhdlCode + ")";
1772 m_type = 1;
1773 }
1774
1775 }
1776
1777 void TRGCDCJSignal::vhdlCode(TRGCDCJSignal const& first, const std::string& operation, TRGCDCJSignal const& second,
1778 TRGCDCJSignal const& result, string& resultVhdlCode)
1779 {
1780 // Copy first and second for VHDL version.
1781 TRGCDCJSignal t_first(first);
1782 TRGCDCJSignal t_second(second);
1783 // Set m_vhdlCode if needed. (Will require clock implementation later)
1784 if (t_first.m_vhdlCode == "") {
1785 t_first.initVhdlCode();
1786 }
1787 if (t_second.m_vhdlCode == "") {
1788 t_second.initVhdlCode();
1789 }
1790
1791 // Match types.
1792 // Changed to signed.
1793 if (t_first.m_type * t_second.m_type < 0 || (t_first.m_type != -1 && result.m_type == -1)) {
1794 if (t_first.m_type != -1) {
1795 t_first.toSignedVhdlCode();
1796 }
1797 if (t_second.m_type != -1) {
1798 t_second.toSignedVhdlCode();
1799 }
1800 }
1801 // Change to unsigned.
1802 if (t_first.m_type == 2) t_first.toUnsignedVhdlCode();
1803 if (t_second.m_type == 2) t_second.toUnsignedVhdlCode();
1804
1805 // Calculate result type and bitwidth.
1806 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1807 logicType = result.m_type;
1808 logicBitwidth = result.m_bitsize;
1809 calVhdlTypeBitwidth(t_first, operation, t_second, vhdlType, vhdlBitwidth);
1810
1811 // Make code.
1812 string resultCode;
1813 // For compare operations.
1814 if (operation == "=" || operation == "/=" || operation == "<" || operation == "<=" ||
1815 operation == ">" || operation == ">=" || operation == "and" || operation == "or") {
1816 resultCode = "(" + t_first.m_vhdlCode + operation + t_second.m_vhdlCode + ")";
1817 } else {
1818 // For arithmetic operations.
1819 // If special case.
1820 if (logicType > 0 && vhdlType == -1) {
1821 if (logicBitwidth + 1 > vhdlBitwidth) t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(
1822 logicBitwidth + 1) + ")";
1823 resultCode = t_first.m_vhdlCode + operation + t_second.m_vhdlCode;
1824 resultCode = "resize(unsigned(" + resultCode + ")," + to_string(logicBitwidth) + ")";
1825 } else {
1826 // Normal case.
1827 if (logicBitwidth > vhdlBitwidth) {
1828 t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(logicBitwidth) + ")";
1829 }
1830 resultCode = t_first.m_vhdlCode + operation + t_second.m_vhdlCode;
1831 // Resize output to minimum bitwidth.
1832 if (logicBitwidth < vhdlBitwidth) {
1833 resultCode = "resize(" + resultCode + "," + to_string(logicBitwidth) + ")";
1834 }
1835 }
1836 }
1837 resultVhdlCode = resultCode;
1838 }
1839
1840 void TRGCDCJSignal::vhdlCode(const std::string& operation, TRGCDCJSignal const& first, TRGCDCJSignal const& result,
1841 string& resultVhdlCode)
1842 {
1843 // Copy first for VHDL version.
1844 TRGCDCJSignal t_first(first);
1845 // Set m_vhdlCode if needed. (Will require clock implementation later)
1846 if (t_first.m_vhdlCode == "") {
1847 t_first.initVhdlCode();
1848 }
1849 // Match types
1850 // Change VHDL unsigned to VHDL signed if required.
1851 if (t_first.m_type != -1 && result.m_type == -1) {
1852 t_first.toSignedVhdlCode();
1853 }
1854 // Change to unsigned if slv.
1855 if (t_first.m_type == 2) t_first.toUnsignedVhdlCode();
1856
1857 // Calculate result type and bitwidth.
1858 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1859 logicType = result.m_type;
1860 logicBitwidth = result.m_bitsize;
1861 // Will always be same as first type and bitsize.
1862 vhdlType = t_first.m_type;
1863 vhdlBitwidth = t_first.m_bitsize;
1864
1865 // Make code.
1866 string resultCode;
1867 // If logic(out) is unsigned and vhdl(in) is signed.
1868 if (logicType == 1 && vhdlType == -1) {
1869 if (logicBitwidth + 1 > vhdlBitwidth) t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(
1870 logicBitwidth + 1) + ")";
1871 if (operation == "shift_left"
1872 || operation == "shift_right") resultCode = operation + "(" + t_first.m_vhdlCode + "," + to_string(abs(
1873 first.m_bitsize - result.m_bitsize)) + ")";
1874 else if (operation == "unsigned" || operation == "none") resultCode = t_first.m_vhdlCode;
1875 else resultCode = operation + "(" + t_first.m_vhdlCode + ")";
1876 resultCode = "resize(unsigned(" + resultCode + ")," + to_string(logicBitwidth) + ")";
1877 } else {
1878 // Normal case.
1879 if (logicBitwidth > vhdlBitwidth) {
1880 t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(logicBitwidth) + ")";
1881 }
1882 if (operation == "shift_left"
1883 || operation == "shift_right") resultCode = operation + "(" + t_first.m_vhdlCode + "," + to_string(abs(
1884 first.m_bitsize - result.m_bitsize)) + ")";
1885 else if (operation == "resize" || operation == "signed" || operation == "none") resultCode = t_first.m_vhdlCode;
1886 else resultCode = operation + "(" + t_first.m_vhdlCode + ")";
1887 // Resize output to minimum bitwidth.
1888 if (logicBitwidth < vhdlBitwidth) {
1889 resultCode = "resize(" + resultCode + "," + to_string(logicBitwidth) + ")";
1890 }
1891 }
1892 resultVhdlCode = resultCode;
1893 }
1894
1895 std::string TRGCDCJSignal::replaceWithSignalNames(std::string const& fromVhdlCode,
1896 std::vector<pair<std::string, vector<int> > > const& fromArgumentSignals, int const& finishClock,
1897 std::map<std::string, vector<int> >& buffers)
1898 {
1899 // Replace names with token(" ") in from.m_vhdlCode
1900 string t_vhdlCode = "";
1901 size_t start = 0;
1902 size_t end = fromVhdlCode.find(" ");
1903 int nIndex = (int)fromArgumentSignals.size();
1904 int index = 0;
1905 while (end != string::npos) {
1906 // Check if there are enough signal names.
1907 if (nIndex - 1 < index) {
1908 cout << "[Error] TRGCDCJSignal::replaceWithSignalNames() => Not enough signal names in m_argumentSignals." << endl;
1909 return "";
1910 }
1911 // Replace token(" ") with name.
1912 //cout<<"Name: "<<fromArgumentSignals[index].first<<" Clock: "<<fromArgumentSignals[index].second[2]<<endl;
1913 int t_buffer = (finishClock - 1) - fromArgumentSignals[index].second[2] - 1;
1914 // No buffer needed or instant assignment or constant.
1915 if (t_buffer == -1 || t_buffer == -2
1916 || fromArgumentSignals[index].second[2] == -1) t_vhdlCode += fromVhdlCode.substr(start,
1917 end - start) + fromArgumentSignals[index].first;
1918 else if (t_buffer > -1) {
1919 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first + "_b(" + to_string(t_buffer) + ")";
1920 // Fill map for buffers.
1921 if (buffers.find(fromArgumentSignals[index].first) == buffers.end()) {
1922 buffers[fromArgumentSignals[index].first] = {fromArgumentSignals[index].second[0], fromArgumentSignals[index].second[1], t_buffer};
1923 } else {
1924 if (buffers[fromArgumentSignals[index].first][2] < t_buffer) buffers[fromArgumentSignals[index].first][2] = t_buffer;
1925 }
1926 } else {
1927 cout << "[Error] TRGCDCJSignal::replaceWithSignalNames() => finishClock is too small." << endl;
1928 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first;
1929 }
1930
1931 index++;
1932 start = end + 1;
1933 end = fromVhdlCode.find(" ", start);
1934 }
1935 t_vhdlCode += fromVhdlCode.substr(start, end - start);
1936 // Check if number of spaces and signal names are same.
1937 if (nIndex != index) {
1938 cout << "[Error] TRGCDCJSignal::replaceWithSignalNames() => Number of signal names and number of " " do not match." << endl;
1939 return "";
1940 }
1941 return t_vhdlCode;
1942 }
1943
1944 std::string TRGCDCJSignal::assignVhdlCode(TRGCDCJSignal const& target, TRGCDCJSignal const& from)
1945 {
1946
1947 // Check if target has m_commonData.
1948 if (target.m_commonData == 0) {
1949 cout << "[Error] TRGCDCJSignal::assignVhdlCode() => target.m_commonData is not set." << endl;
1950 return "";
1951 }
1952
1953 TRGCDCJSignal t_from(from);
1954
1955 // Set m_vhdlCode if needed.
1956 if (t_from.m_vhdlCode == "") {
1957 t_from.initVhdlCode();
1958 }
1959
1960 // Fill signal map for definition.
1961 {
1962 if (target.m_name == "") cout << "[Error] TRGCDCJSignal::assignVhdlCode() => target.m_name is \"\" " << endl;
1963 map<string, vector<int> >& t_signals = target.m_commonData->m_signals;
1964 if (t_signals.find(target.m_name) == t_signals.end()) {
1965 //t_signals[target.m_name] = {target.m_type, target.m_bitsize};
1966 // From and target should be same.
1967 // cppcheck-suppress stlFindInsert
1968 t_signals[target.m_name] = {from.m_type, from.m_bitsize};
1969 }
1970 }
1971
1972 // Replace names with spaces in from.m_vhdlCode
1973 string t_vhdlCode = replaceWithSignalNames(t_from.m_vhdlCode, t_from.m_argumentSignals, t_from.m_finishClock,
1974 t_from.m_commonData->m_buffers);
1975
1976 return target.m_name + "<=" + t_vhdlCode + ";";
1977 //// For signal <= combined signal.
1978 //if(t_vhdlCode != "") {
1979 // return target.m_name + "<=" + t_vhdlCode + ";";
1980 //// For signal <= constant. Do we need a separate flag?
1981 //} else if (from.m_minInt == from.m_maxInt) {
1982 // if(from.m_type == 1) {
1983 // return target.m_name + "<=to_unsigned(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
1984 // } else {
1985 // return target.m_name + "<=to_signed(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
1986 // }
1987 //// For input signals. [Not exactly sure why this works.]
1988 //} else if (from.m_name=="") {
1989 // if(from.m_type == 1) {
1990 // return target.m_name + "<=to_unsigned(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
1991 // } else {
1992 // return target.m_name + "<=to_signed(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
1993 // }
1994 //} else {
1995 //// For signal <= single signal.
1996 // return target.m_name + "<=" + from.m_name + ";";
1997 //}
1998 }
1999
2000 std::string TRGCDCJSignal::chooseVhdlCode(TRGCDCJSignal const& target, TRGCDCJSignal const& reference,
2001 std::vector<std::vector<TRGCDCJSignal> > const& data)
2002 {
2003 // Check if target has m_name.
2004 if (target.m_name == "") {
2005 cout << "[Error] TRGCDCJSignal::chooseVhdlCode() => target has no name." << endl;
2006 return "";
2007 }
2008 // Check if reference has m_name or vhdlCode.
2009 if (reference.m_name == "" && reference.m_vhdlCode == "") {
2010 cout << "[Error] TRGCDCJSignal::cooseVhdlCode() => reference has no name or vhdlCode." << endl;
2011 return "";
2012 }
2013
2014 TRGCDCJSignal t_reference(reference);
2015
2016 // Set m_vhdlCode if needed.
2017 if (t_reference.m_vhdlCode == "") t_reference.initVhdlCode();
2018 // Replace names with spaces in reference.m_vhdlCode
2019 string t_referenceVhdlCode = replaceWithSignalNames(t_reference.m_vhdlCode, t_reference.m_argumentSignals, target.m_finishClock,
2020 t_reference.m_commonData->m_buffers);
2021
2022 // Storage for used values.
2023 // map<signed long long, bool> t_used;
2024 // Create string for choice().
2025 stringstream t_vhdlCode;
2026 //t_vhdlCode<<"case to_integer("<<t_referenceVhdlCode<<") is"<<endl;
2027 //for(unsigned iCondition=0; iCondition<data.size()-1; iCondition++){
2028 // TRGCDCJSignal const & from = data[iCondition][1];
2029 // TRGCDCJSignal const & to = data[iCondition][2];
2030 // TRGCDCJSignal const & assign = data[iCondition][0];
2031 // signed long long fromInt = from.m_int;
2032 // signed long long toInt = to.m_int;
2033 // // Don't allow same int numbers for from and to.
2034 // if(t_used.count(fromInt)!=0) fromInt += 1;
2035 // else t_used[fromInt] = 1;
2036 // if(t_used.count(toInt)!=0) {
2037 // // Allow same from and to when in one line.
2038 // if(fromInt!=toInt) toInt -= 1;
2039 // } else t_used[toInt] = 1;
2040 // t_vhdlCode<<" when "<<fromInt<<" to "<<toInt<<"=>"<<assignVhdlCode(target,assign)<<endl;;
2041 //}
2042 //t_vhdlCode<<" when others=>"<<assignVhdlCode(target,(*(data.end()-1))[0])<<endl;
2043 //t_vhdlCode<<"end case;";
2044
2045 // For if
2046 TRGCDCJSignal from = data[0][1];
2047 TRGCDCJSignal to = data[0][2];
2048 TRGCDCJSignal assign = data[0][0];
2049 if (from.m_vhdlCode == "") from.initVhdlCode();
2050 if (to.m_vhdlCode == "") to.initVhdlCode();
2051 string t_fromVhdlCode = replaceWithSignalNames(from.m_vhdlCode, from.m_argumentSignals, target.m_finishClock,
2052 target.m_commonData->m_buffers);
2053 string t_toVhdlCode = replaceWithSignalNames(to.m_vhdlCode, to.m_argumentSignals, target.m_finishClock,
2054 target.m_commonData->m_buffers);
2055 string t_fromPrefixSigned;
2056 string t_fromPostfixSigned;
2057 string t_toPrefixSigned;
2058 string t_toPostfixSigned;
2059 if (reference.m_type == -1 && from.m_type == 1) {
2060 t_fromPrefixSigned = "signed('0'&";
2061 t_fromPostfixSigned = ")";
2062 } else {
2063 t_fromPrefixSigned = "";
2064 t_fromPostfixSigned = "";
2065 }
2066 if (reference.m_type == -1 && to.m_type == 1) {
2067 t_toPrefixSigned = "signed('0'&";
2068 t_toPostfixSigned = ")";
2069 } else {
2070 t_toPrefixSigned = "";
2071 t_toPostfixSigned = "";
2072 }
2073 t_vhdlCode << "if (" << t_referenceVhdlCode << " >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned << ") and (" <<
2074 t_referenceVhdlCode << " <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned << ") then" << endl;
2075 t_vhdlCode << " " << assignVhdlCode(target, assign) << endl;
2076
2077 // For if else
2078 for (unsigned iCondition = 1; iCondition < data.size() - 1; iCondition++) {
2079 TRGCDCJSignal from1 = data[iCondition][1];
2080 TRGCDCJSignal to1 = data[iCondition][2];
2081 TRGCDCJSignal assign1 = data[iCondition][0];
2082 if (from1.m_vhdlCode == "") from1.initVhdlCode();
2083 if (to1.m_vhdlCode == "") to1.initVhdlCode();
2084 if (reference.m_type == -1 && from1.m_type == 1) {
2085 t_fromPrefixSigned = "signed('0'&";
2086 t_fromPostfixSigned = ")";
2087 } else {
2088 t_fromPrefixSigned = "";
2089 t_fromPostfixSigned = "";
2090 }
2091 if (reference.m_type == -1 && to1.m_type == 1) {
2092 t_toPrefixSigned = "signed('0'&";
2093 t_toPostfixSigned = ")";
2094 } else {
2095 t_toPrefixSigned = "";
2096 t_toPostfixSigned = "";
2097 }
2098 t_fromVhdlCode = replaceWithSignalNames(from1.m_vhdlCode, from1.m_argumentSignals, target.m_finishClock,
2099 target.m_commonData->m_buffers);
2100 t_toVhdlCode = replaceWithSignalNames(to1.m_vhdlCode, to1.m_argumentSignals, target.m_finishClock, target.m_commonData->m_buffers);
2101 t_vhdlCode << "elsif (" << t_referenceVhdlCode << " >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned << ") and ("
2102 << t_referenceVhdlCode << " <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned << ") then" << endl;
2103 t_vhdlCode << " " << assignVhdlCode(target, assign1) << endl;
2104 }
2105 t_vhdlCode << "else" << endl;
2106 t_vhdlCode << " " << assignVhdlCode(target, (*(data.end() - 1))[0]) << endl;
2107 t_vhdlCode << "end if;";
2108 return t_vhdlCode.str();
2109 }
2110
2111 std::string TRGCDCJSignal::ifElseVhdlCode(
2112 std::vector<std::pair<TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > > const& data)
2113 {
2114 // Check if comp and lhs have name
2115 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
2116 TRGCDCJSignal const& comp = data[iCase].first;
2117 if (iCase != data.size() - 1) {
2118 if (comp.m_name == "") {
2119 cout << "[Error] TRGCDCJSignal::ifElseVhdlCode() => comp has no name." << endl;
2120 return "";
2121 }
2122 }
2123 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2124 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2125 if (lhs.m_name == "") {
2126 cout << "[Error] TRGCDCJSignal::ifElseVhdlCode() => lhs has no name." << endl;
2127 return "";
2128 }
2129 }
2130 }
2131
2132 // Write vhdl
2133 // Else will be last in data.
2134 stringstream t_vhdlCode;
2135 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
2136 TRGCDCJSignal const& comp = data[iCase].first;
2137 string t_compVhdlCode = replaceWithSignalNames(comp.m_vhdlCode, comp.m_argumentSignals, comp.m_finishClock,
2138 comp.m_commonData->m_buffers);
2139 // Choose which if to use.
2140 if (iCase == 0) t_vhdlCode << "if " << t_compVhdlCode << " then" << endl;
2141 else if (comp.m_bitsize == -1) t_vhdlCode << "else" << endl;
2142 else t_vhdlCode << "elsif " << t_compVhdlCode << " then" << endl;
2143 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2144 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2145 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
2146 t_vhdlCode << " " << assignVhdlCode(lhs, rhs) << endl;
2147 }
2148 }
2149 t_vhdlCode << "end if;";
2150
2151 return t_vhdlCode.str();
2152
2153 }
2154
2155 void TRGCDCJSignal::printVhdl(const std::string& vhdlCode) const
2156 {
2157 if (m_commonData) {
2158 ofstream outFile;
2159 outFile.open(m_commonData->m_vhdlOutputFile, ios::app);
2160 if (outFile.is_open()) {
2161 outFile << vhdlCode << endl;
2162 outFile.close();
2163 }
2164 } else {
2165 cout << "[Error] TRGCDCJSignal::printVhdl() => m_commonData doesn't exist." << endl;
2166 }
2167 }
2168
2169 void TRGCDCJSignal::checkInt(const std::string& name) const
2170 {
2171 if (m_int > calMaxInteger()) {
2172 cout << "[Error] " << name << " => m_int overflow" << endl;
2173 cout << " Start dumping" << endl;
2174 dump();
2175 cout << " End dumping" << endl;
2176 }
2177 if (m_int < calMinInteger()) {
2178 cout << "[Error] " << name << " => m_int underflow" << endl;
2179 cout << " Start dumping" << endl;
2180 dump();
2181 cout << " End dumping" << endl;
2182 }
2183 if (m_maxInt > calMaxInteger()) {
2184 cout << "[Error] " << name << " => m_maxInt overflow" << endl;
2185 cout << " Start dumping" << endl;
2186 dump();
2187 cout << " End dumping" << endl;
2188 }
2189 if (m_minInt < calMinInteger()) {
2190 cout << "[Error] " << name << " => m_minInt underflow" << endl;
2191 cout << " Start dumping" << endl;
2192 dump();
2193 cout << " End dumping" << endl;
2194 }
2195 }
2196
2197 bool TRGCDCJSignal::isSameSignal(TRGCDCJSignal const& lhs, TRGCDCJSignal const& rhs)
2198 {
2199 return lhs.m_int == rhs.m_int && lhs.m_minInt == rhs.m_minInt && lhs.m_maxInt == rhs.m_maxInt
2200 && lhs.m_toReal == rhs.m_toReal && lhs.m_actual == rhs.m_actual && lhs.m_minActual == rhs.m_minActual
2201 && lhs.m_maxActual == rhs.m_maxActual;
2202 }
2203
2204 void TRGCDCJSignal::valuesToMapSignals(std::vector<std::tuple<string, double, int, double, double, int> > const& inValues,
2205 Belle2::TRGCDCJSignalData* inCommonData, std::map<std::string, Belle2::TRGCDCJSignal>& outMap)
2206 {
2207 for (unsigned iSignals = 0; iSignals < inValues.size(); iSignals++) {
2208 string const& t_name = get<0>(inValues[iSignals]);
2209 double const& t_value = get<1>(inValues[iSignals]);
2210 int const& t_bitWidth = get<2>(inValues[iSignals]);
2211 double const& t_min = get<3>(inValues[iSignals]);
2212 double const& t_max = get<4>(inValues[iSignals]);
2213 int const& t_clock = get<5>(inValues[iSignals]);
2214 // Put input to signal.
2215 // Input to finish clock is 0.
2216 // Signals that will be used will have finish clock 1 due to below line.
2217 outMap[t_name] <= Belle2::TRGCDCJSignal(t_bitWidth, t_value, t_min, t_max, t_clock, inCommonData);
2218 }
2219 }
2220
2221 void TRGCDCJSignal::mapSignalsToValues(std::map<std::string, Belle2::TRGCDCJSignal> const& inMap,
2222 std::vector<std::pair<std::string, int> > const& inChoose,
2223 std::vector<std::tuple<string, double, int, double, double, int> >& outValues)
2224 {
2225 for (unsigned iSignals = 0; iSignals < inChoose.size(); iSignals++) {
2226 int const& b_integerEffect = inChoose[iSignals].second;
2227 string const& t_name = inChoose[iSignals].first;
2228 Belle2::TRGCDCJSignal const& t_signal = inMap.find(t_name)->second;
2229 double t_value = 0, t_min = 0, t_max = 0;
2230 int t_bitwidth = t_signal.getBitsize();
2231 if (b_integerEffect == 1) {
2232 t_value = t_signal.getRealInt();
2233 t_min = t_signal.getRealMinInt();
2234 t_max = t_signal.getRealMaxInt();
2235 } else {
2236 t_value = t_signal.getActual();
2237 t_min = t_signal.getMinActual();
2238 t_max = t_signal.getMaxActual();
2239 }
2240 int t_clock = t_signal.getFinishClock();
2241 tuple<string, double, int, double, double, int> outValue(t_name, t_value, t_bitwidth, t_min, t_max, t_clock);
2242 outValues.push_back(outValue);
2243 }
2244 }
2245
2246}