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