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