Belle II Software development
JSignal.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9//-----------------------------------------------------------------------------
10// Description : A class for Signals in CDC Trigger for 3D tracker.
11//-----------------------------------------------------------------------------
12
13#define TRG_SHORT_NAMES
14#define TRGCDC_SHORT_NAMES
15
16#ifndef __EXTERNAL__
17#include "trg/cdc/JSignal.h"
18#include "trg/cdc/FpgaUtility.h"
19#include "trg/cdc/JSignalData.h"
20#else
21#include "JSignal.h"
22#include "FpgaUtility.h"
23#include "JSignalData.h"
24#endif
25#include <iostream>
26#include <sstream>
27#include <fstream>
28#include <cstdlib>
29#include <cmath>
30//#include <algorithm>
31
32using std::cout;
33using std::endl;
34using std::pair;
35using std::make_pair;
36using std::tuple;
37using std::vector;
38using std::map;
39using std::string;
40using std::stringstream;
41using std::to_string;
42using std::get;
43using std::max;
44using std::min;
45using std::ofstream;
46using std::ios;
47
48namespace Belle2 {
53
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 calculated 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 calculated bitwidth don't match"<<endl;
224 //}
226 //if(t_rhs.m_type != t_type){
227 // cout<<"Type and calculated 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 calculated 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
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
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
769 if (data.size() == 0) {
770 cout << "[Error] TRGCDCJSignal::choose() => There is not enough data." << endl;
771 return;
772 }
773
774 // Find targetMin and targetMax between assignments.
775 for (unsigned iCondition = 0; iCondition < data.size(); iCondition++) {
776 const TRGCDCJSignal* assign = &(data[iCondition][0]);
777 if (iCondition == 0) {
778 t_minInt = assign->getMinInt();
779 t_maxInt = assign->getMaxInt();
780 t_minActual = assign->getMinActual();
781 t_maxActual = assign->getMaxActual();
782 t_toReal = assign->getToReal();
783 } else {
784 if (t_minInt > assign->getMinInt()) t_minInt = assign->getMinInt();
785 if (t_maxInt < assign->getMaxInt()) t_maxInt = assign->getMaxInt();
786 if (t_minActual > assign->getMinActual()) t_minActual = assign->getMinActual();
787 if (t_maxActual < assign->getMaxActual()) t_maxActual = assign->getMaxActual();
788 if (t_toReal != assign->getToReal()) cout << "[Error] TRGCDCJSignal::choose() => toReal are not same between assignments in data."
789 << endl;
790 }
791 }
792
793 TRGCDCJSignal t_targetMax(t_maxInt, t_toReal, t_maxInt, t_maxInt, t_maxActual, t_maxActual, t_maxActual, -1, target.m_commonData);
794 TRGCDCJSignal t_targetMin(t_minInt, t_toReal, t_minInt, t_minInt, t_minActual, t_minActual, t_minActual, -1, target.m_commonData);
795
796 choose(target, t_targetMin, t_targetMax, reference, data, targetClock);
797 }
798
799 void TRGCDCJSignal::choose(TRGCDCJSignal& target, TRGCDCJSignal const& reference,
800 const std::vector<std::vector<TRGCDCJSignal> >& data)
801 {
802 choose(target, reference, data, -3);
803 }
804
805
806 void TRGCDCJSignal::ifElse(std::vector<std::pair<TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > >& data,
807 int targetClock)
808 {
809 // Handle input errors.
810 if (data.size() == 0) {
811 cout << "[Error] TRGCDCJSignal::ifElse() => There is not enough data." << endl;
812 return;
813 }
814
815 // Init arguments of data.
816 // These errors prevent else case being in between data.
817 // Loop over all cases.
818 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
819 TRGCDCJSignal const& comp = data[iCase].first;
820 if (iCase != (data.size() - 1)) {
821 if (comp.m_argumentSignals.size() == 0) cout << "[Error] TRGCDCJSignal::ifElse() => comp signal has no m_vhdl." << endl;
822 if (comp.m_minInt != 0
823 || comp.m_maxInt != 1) cout << "[Error] TRGCDCJSignal::ifElse() => comp signal m_minInt or m_maxInt is not correct." << endl;
824 }
825 // Loop over all assignments.
826 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
827 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
828 //if(rhs.m_type==2) cout<<"[Error] TRGCDCJSignal::ifElse() => Cannot handle slv in assignments currently. Should be fixed later."<<endl;
829 if (rhs.m_argumentSignals.size() == 0) rhs.initArgumentSignals();
830 }
831 }
832
833 // Calculate final clock.
834 // Loop over all cases.
835 int t_maxClock = data[0].first.calFinishClock();
836 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
837 int t_clock = data[iCase].first.calFinishClock();
838 if (t_maxClock < t_clock) t_maxClock = t_clock;
839 // Loop over all assignments.
840 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
841 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
842 t_clock = rhs.calFinishClock();
843 if (t_maxClock < t_clock) t_maxClock = t_clock;
844 }
845 }
846 int t_finishClock = 0;
847 if (targetClock == -3) {
848 if (t_maxClock < 0) cout << "[Error] TRGCDCJSignal::ifElse() => t_maxClock is negative. Did not consider this case." << endl;
849 t_finishClock = t_maxClock + 1;
850 } else {
851 if (targetClock < t_maxClock) cout << "[Error] TRGCDCJSignal::ifElse() => targetClock is too small." << endl;
852 else t_finishClock = targetClock;
853 }
854
855 // Change rhs, comp clock to finish clock.
856 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
857 TRGCDCJSignal& comp = data[iCase].first;
858 comp.m_finishClock = t_finishClock;
859 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
860 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
861 rhs.m_finishClock = t_finishClock;
862 }
863 }
864
865 // Find min and max ranges between all rhs.
866 // <minInt, maxInt, minActual, maxActual>
867 map<TRGCDCJSignal*, pair<vector<long long int>, vector<double> > > t_signalsRange;
868 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
869 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
870 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
871 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
872 if (t_signalsRange.find(&lhs) == t_signalsRange.end()) {
873 vector<long long int> t_intRange = {rhs.m_minInt, rhs.m_maxInt};
874 vector<double> t_actualRange = {rhs.m_minActual, rhs.m_maxActual};
875 t_signalsRange[&lhs] = make_pair(t_intRange, t_actualRange);
876 } else {
877 long long int& t_minInt = t_signalsRange[&lhs].first[0];
878 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
879 double& t_minActual = t_signalsRange[&lhs].second[0];
880 double& t_maxActual = t_signalsRange[&lhs].second[1];
881 if (t_minInt > rhs.m_minInt) t_minInt = rhs.m_minInt;
882 if (t_maxInt < rhs.m_maxInt) t_maxInt = rhs.m_maxInt;
883 if (t_minActual > rhs.m_minActual) t_minActual = rhs.m_minActual;
884 if (t_maxActual < rhs.m_maxActual) t_maxActual = rhs.m_maxActual;
885 }
886 }
887 }
888 // Change min, max ranges and type, bitwidth for all rhs.
889 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
890 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
891 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
892 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
893 TRGCDCJSignal orgRhs(rhs);
894 long long int& t_minInt = t_signalsRange[&lhs].first[0];
895 long long int& t_maxInt = t_signalsRange[&lhs].first[1];
896 double& t_minActual = t_signalsRange[&lhs].second[0];
897 double& t_maxActual = t_signalsRange[&lhs].second[1];
898 rhs.m_minInt = t_minInt;
899 rhs.m_maxInt = t_maxInt;
900 rhs.m_minActual = t_minActual;
901 rhs.m_maxActual = t_maxActual;
902 rhs.m_type = rhs.calType();
903 rhs.m_bitsize = rhs.calBitwidth();
904 if (rhs.getPrintVhdl() == 1) vhdlCode("none", orgRhs, rhs, rhs.m_vhdlCode);
905 }
906 }
907
908 // For integer case.
909 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
910 TRGCDCJSignal const& comp = data[iCase].first;
911 // If comp is 1 or else.
912 if (comp.m_int == 1 || comp.m_bitsize == -1) {
913 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
914 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
915 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
916 string t_string = "";
917 lhs.assignTo(rhs, t_finishClock, t_string);
918 // Check ranges.
919 lhs.checkInt("TRGCDCJSignal::ifElse()");
920 }
921 break;
922 }
923 }
924
925 // For actual case.
926 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
927 TRGCDCJSignal const& comp = data[iCase].first;
928 // If comp is 1 or else.
929 if (comp.m_actual == 1 || comp.m_bitsize == -1) {
930 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
931 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
932 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
933 lhs.m_actual = rhs.m_actual;
934 }
935 break;
936 }
937 }
938
939 // Print vhdl code.
940 if (data[0].first.getPrintVhdl() == 1) {
941 // Set m_commonData for all lhs signals.
942 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
943 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
944 TRGCDCJSignal& lhs = *(data[iCase].second[iAssignment].first);
945 /* cppcheck-suppress variableScope */
946 TRGCDCJSignal& rhs = data[iCase].second[iAssignment].second;
947 if (lhs.m_commonData == 0) lhs.m_commonData = rhs.m_commonData;
948 }
949 }
950 string t_finalCode = ifElseVhdlCode(data);
951 data[0].first.m_commonData->m_vhdlInProcess += t_finalCode + "\n";
952 }
953
954
955 }
956
957 void TRGCDCJSignal::ifElse(std::vector<std::pair<TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > >& data)
958 {
959 ifElse(data, -3);
960 }
961
962
963 TRGCDCJSignal const TRGCDCJSignal::limit(signed long long minInt, signed long long maxInt, double minActual,
964 double maxActual) const
965 {
966 TRGCDCJSignal t_result(*this);
967
968 //if(t_result.m_type==2) {
969 // cout<<"[Error] TRGCDCJSignal::limit() => should not be used with slv type."<<endl;
970 //}
971
972
973 if (t_result.m_argumentSignals.size() == 0) t_result.initArgumentSignals();
974
975 t_result.m_minInt = minInt;
976 t_result.m_maxInt = maxInt;
977 t_result.m_minActual = minActual;
978 t_result.m_maxActual = maxActual;
979 t_result.m_type = t_result.calType();
980 t_result.m_bitsize = t_result.calBitwidth();
982 //t_result.checkInt("TRGCDCJSignal::limit()");
983
984
985 if (getPrintVhdl() == 1) vhdlCode("none", *this, t_result, t_result.m_vhdlCode);
986 //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
987 return t_result;
988 }
989
991 {
992 return limit(min.m_int, max.m_int, min.m_actual, max.m_actual);
993 }
994
995 TRGCDCJSignal const TRGCDCJSignal::resize(int bitwidth) const
996 {
997 if (this->m_bitsize == bitwidth) return (*this);
998
999 TRGCDCJSignal t_result(*this);
1000 if (t_result.m_argumentSignals.size() == 0) t_result.initArgumentSignals();
1001
1002 t_result.m_bitsize = bitwidth;
1003 // Check if there is overflow or underflow.
1004 t_result.checkInt("TRGCDCJSignal::resize()");
1005
1006
1007 if (getPrintVhdl() == 1) vhdlCode("resize", *this, t_result, t_result.m_vhdlCode);
1008 //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
1009 return t_result;
1010 }
1011
1012 //TRGCDCJSignal const TRGCDCJSignal::toSigned() const {
1013 // if(m_type==-1) return (*this);
1014
1015 // TRGCDCJSignal t_result(*this);
1016 // if(t_result.m_argumentSignals.size() ==0) t_result.initArgumentSignals();
1017
1018 // t_result.m_type = -1;
1019 // t_result.m_bitsize = m_bitsize+1;
1020 // // Check if there is overflow or underflow.
1021 // t_result.checkInt("TRGCDCJSignal::toSigned()");
1022 //
1023
1024 // if(getPrintVhdl()==1) vhdlCode("signed", *this, t_result, t_result.m_vhdlCode);
1025 // //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
1026 // return t_result;
1027 //}
1028
1029 //TRGCDCJSignal const TRGCDCJSignal::toUnsigned() const {
1030 // if(m_type==1) return (*this);
1031 // TRGCDCJSignal t_result(*this);
1032 // if(t_result.m_argumentSignals.size() ==0) t_result.initArgumentSignals();
1033
1034 // t_result.m_type = 1;
1035 // t_result.m_bitsize = m_bitsize-1;
1036 // // Check if there is overflow or underflow.
1037 // t_result.checkInt("TRGCDCJSignal::toUnsigned()");
1038 //
1039
1040 // if(getPrintVhdl()==1) vhdlCode("unsigned", *this, t_result, t_result.m_vhdlCode);
1041 // //t_result.m_vhdlCode = "resize("+t_result.m_vhdlCode+","+to_string(t_result.m_bitsize)+")";
1042 // return t_result;
1043 //}
1044
1045
1046 TRGCDCJSignal const TRGCDCJSignal::slvToSigned(TRGCDCJSignal const& in, double const& toReal, signed long long const& minInt,
1047 signed long long const& maxInt, double const& actual, double const& minActual, double const& maxActual, int const& finishClock)
1048 {
1049
1050 vector<bool> const& slv = in.m_slv;
1051
1052 // Check input
1053 if (in.m_type != 2) cout << "[Error] TRGCDCJSignal::slvToSigned() => type is not slv." << endl;
1054 if (slv.size() > 63) cout << "[Error] TRGCDCJSignal::slvToSigned() => bitwidth is too large." << endl;
1055
1056 TRGCDCJSignal result(in);
1057
1058 // Calculate value. [Index 0 is lsb]
1059 result.m_int = 0;
1060 for (unsigned iBit = 0; iBit < slv.size() - 1; iBit++) {
1061 result.m_int += slv[iBit] * pow(2, iBit);
1062 }
1063 result.m_int -= slv[slv.size() - 1] * pow(2, slv.size() - 1);
1064 // Set values.
1065 result.m_minInt = minInt;
1066 result.m_maxInt = maxInt;
1067 result.m_actual = actual;
1068 result.m_minActual = minActual;
1069 result.m_maxActual = maxActual;
1070 result.m_toReal = toReal;
1071 result.m_type = 0;
1072 result.m_type = result.calType();
1073 result.m_bitsize = result.calBitwidth();
1074 result.m_finishClock = finishClock;
1075 // Check if there is overflow or underflow.
1076 result.checkInt("slvToSigned");
1077 result.m_commonData = in.m_commonData;
1078
1079 return result;
1080 }
1081
1086
1087 TRGCDCJSignal const TRGCDCJSignal::slvToUnsigned(TRGCDCJSignal const& in, double const& toReal, signed long long const& minInt,
1088 signed long long const& maxInt, double const& actual, double const& minActual, double const& maxActual, int const& finishClock)
1089 {
1090
1091 vector<bool> const& slv = in.m_slv;
1092
1093 // Check input
1094 if (in.m_type != 2) cout << "[Error] TRGCDCJSignal::slvToUnsigned() => type is not slv." << endl;
1095 if (slv.size() > 63) cout << "[Error] TRGCDCJSignal::slvToUnsigned() => bitwidth is too large." << endl;
1096
1097 TRGCDCJSignal result(in);
1098
1099 // Calculate value. [Index 0 is lsb]
1100 result.m_int = 0;
1101 for (unsigned iBit = 0; iBit < slv.size(); iBit++) {
1102 result.m_int += slv[iBit] * pow(2, iBit);
1103 }
1104 // Set values.
1105 result.m_minInt = minInt;
1106 result.m_maxInt = maxInt;
1107 result.m_actual = actual;
1108 result.m_minActual = minActual;
1109 result.m_maxActual = maxActual;
1110 result.m_toReal = toReal;
1111 result.m_type = 0;
1112 result.m_type = result.calType();
1113 result.m_bitsize = result.calBitwidth();
1114 result.m_finishClock = finishClock;
1115 // Check if there is overflow or underflow.
1116 result.checkInt("slvToSigned");
1117 result.m_commonData = in.m_commonData;
1118
1119 return result;
1120 }
1121
1126
1127 TRGCDCJSignal const TRGCDCJSignal::toSlv(TRGCDCJSignal const& in, int /*test*/)
1128 {
1129
1130 TRGCDCJSignal result(in);
1131
1132 // Temporary signal.
1133 //test += 0; //TODO why?
1134 signed long long t_int = in.m_int;
1135 if (in.m_type == -1) {
1136 // Change signed m_int to vector<bool>
1137 t_int = ~t_int;
1138 t_int += 1;
1139 result.m_slv.clear();
1140 result.m_slv.resize(in.m_bitsize);
1141 for (int iBit = 0; iBit < in.m_bitsize; iBit++) {
1142 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1143 }
1144 } else if (in.m_type == 1) {
1145 // Change unsigned m_int to vector<bool>
1146 result.m_slv.clear();
1147 result.m_slv.resize(in.m_bitsize);
1148 for (int iBit = 0; iBit < in.m_bitsize; iBit++) {
1149 result.m_slv[iBit] = (bool)(t_int >> iBit) & 1;
1150 }
1151 } else {
1152 cout << "[Error] TRGCDCJSignal::toSlv() => m_type is not 1 or -1." << endl;
1153 }
1154
1155 return result;
1156 }
1157
1159 {
1160 if (in.m_type == 2) return (in);
1161 // Don't think I need below but will keep it for now.
1162 if (in.m_type == -1) {
1163 cout << "[Error] TRGCDCJSignal::toSlv() => Slv is not same as unsigned anymore." << endl;
1164 }
1165 TRGCDCJSignal t_result(in);
1166 t_result.m_type = 2;
1167
1168 if (in.getPrintVhdl() == 1) vhdlCode("std_logic_vector", in, t_result, t_result.m_vhdlCode);
1169
1170 return t_result;
1171 }
1172
1173
1174
1175 TRGCDCJSignal TRGCDCJSignal::comp(TRGCDCJSignal const& lhs, const std::string& operate, TRGCDCJSignal const& rhs)
1176 {
1177 // Make copies for method.
1178 TRGCDCJSignal t_lhs(lhs);
1179 TRGCDCJSignal t_rhs(rhs);
1180 TRGCDCJSignal t_result(t_lhs);
1181 // Make arguments.
1182 if (t_lhs.m_argumentSignals.size() == 0) t_lhs.initArgumentSignals();
1183 if (t_rhs.m_argumentSignals.size() == 0) t_rhs.initArgumentSignals();
1184 combineArguments(t_lhs, t_rhs, t_result);
1185 // Int Operation.
1186 bool t_bool = false;
1187 if (operate == "=") {
1188 t_bool = (t_lhs.m_int == t_rhs.m_int ? 1 : 0);
1189 } else if (operate == "/=") {
1190 t_bool = (t_lhs.m_int != t_rhs.m_int ? 1 : 0);
1191 } else if (operate == "<") {
1192 t_bool = (t_lhs.m_int < t_rhs.m_int ? 1 : 0);
1193 } else if (operate == "<=") {
1194 t_bool = (t_lhs.m_int <= t_rhs.m_int ? 1 : 0);
1195 } else if (operate == ">") {
1196 t_bool = (t_lhs.m_int > t_rhs.m_int ? 1 : 0);
1197 } else if (operate == ">=") {
1198 t_bool = (t_lhs.m_int >= t_rhs.m_int ? 1 : 0);
1199 } else if (operate == "and") {
1200 t_bool = (t_lhs.m_int && t_rhs.m_int ? 1 : 0);
1201 } else if (operate == "or") {
1202 t_bool = (t_lhs.m_int || t_rhs.m_int ? 1 : 0);
1203 } else {
1204 cout << "[Error] TRGCDCJSignal::comp() => No operator named " << operate << "." << endl;
1205 }
1206 // Actual Operation.
1207 if (operate == "=") {
1208 t_result.m_actual = (t_lhs.m_actual == t_rhs.m_actual ? 1 : 0);
1209 } else if (operate == "/=") {
1210 t_result.m_actual = (t_lhs.m_actual != t_rhs.m_actual ? 1 : 0);
1211 } else if (operate == "<") {
1212 t_result.m_actual = (t_lhs.m_actual < t_rhs.m_actual ? 1 : 0);
1213 } else if (operate == "<=") {
1214 t_result.m_actual = (t_lhs.m_actual <= t_rhs.m_actual ? 1 : 0);
1215 } else if (operate == ">") {
1216 t_result.m_actual = (t_lhs.m_actual > t_rhs.m_actual ? 1 : 0);
1217 } else if (operate == ">=") {
1218 t_result.m_actual = (t_lhs.m_actual >= t_rhs.m_actual ? 1 : 0);
1219 } else if (operate == "and") {
1220 t_result.m_actual = (t_lhs.m_actual && t_rhs.m_actual ? 1 : 0);
1221 } else if (operate == "or") {
1222 t_result.m_actual = (t_lhs.m_actual || t_rhs.m_actual ? 1 : 0);
1223 } else {
1224 cout << "[Error] TRGCDCJSignal::comp() => No operator named " << operate << "." << endl;
1225 }
1226 // Change values.
1227 t_result.m_name = t_lhs.m_name + operate + t_rhs.m_name;
1228 t_result.m_type = 1;
1229 t_result.m_bitsize = 1;
1230 t_result.m_minInt = 0;
1231 t_result.m_maxInt = 1;
1232 t_result.m_minActual = 0;
1233 t_result.m_maxActual = 1;
1234 t_result.m_toReal = 1;
1235 if (t_bool == 1) {
1236 t_result.m_int = 1;
1237 } else {
1238 t_result.m_int = 0;
1239 }
1240
1241 if (t_result.getPrintVhdl() == 1) vhdlCode(t_lhs, operate, t_rhs, t_result, t_result.m_vhdlCode);
1242
1243 return t_result;
1244
1245 }
1246
1248 {
1249 std::swap(first.m_name, second.m_name);
1250 std::swap(first.m_type, second.m_type);
1251 std::swap(first.m_bitsize, second.m_bitsize);
1252 std::swap(first.m_int, second.m_int);
1253 std::swap(first.m_minInt, second.m_minInt);
1254 std::swap(first.m_maxInt, second.m_maxInt);
1255 std::swap(first.m_actual, second.m_actual);
1256 std::swap(first.m_minActual, second.m_minActual);
1257 std::swap(first.m_maxActual, second.m_maxActual);
1258 std::swap(first.m_toReal, second.m_toReal);
1259 std::swap(first.m_debug, second.m_debug);
1260 std::swap(first.m_vhdlCode, second.m_vhdlCode);
1261 std::swap(first.m_argumentSignals, second.m_argumentSignals);
1262 std::swap(first.m_finishClock, second.m_finishClock);
1263 std::swap(first.m_commonData, second.m_commonData);
1264 }
1265
1267 {
1268 if (first.m_toReal != second.m_toReal) {
1269 // Find number of bits to shift to match unit.
1270 double t_nBits = log(first.m_toReal / second.m_toReal) / log(2);
1271 // Round to integer
1272 if (t_nBits > 0) {
1273 t_nBits = int(t_nBits + 0.5);
1274 } else {
1275 t_nBits = int(t_nBits - 0.5);
1276 }
1277 // Shift bits to match unit
1278 if (t_nBits > 0) {
1279 first = first.shift(-t_nBits, 0);
1280 //cout<<"mu::shift_left(resize(" + first.m_name + "," + to_string(int(first.m_bitsize+t_nBits)) + ")," + to_string(int(t_nBits)) + ")"<<endl;
1281 //cout<<first.m_vhdlCode<<endl;
1282 } else if (t_nBits < 0) {
1283 second = second.shift(t_nBits, 0);
1284 //cout<<"mu::shift_left(resize(" + second.m_name + "," + to_string(int(second.m_bitsize-t_nBits)) + ")," + to_string(int(-t_nBits)) + ")"<<endl;
1285 //cout<<second.m_vhdlCode<<endl;
1286 }
1287
1288 if (fabs(first.m_toReal - second.m_toReal) > 0.00001 * first.m_toReal) {
1289 cout << "[Warning] TRGCDCJSignal::matchUnit() => in1 and in2 units are still different." << endl;
1290 cout << first.m_toReal << " " << second.m_toReal << endl;
1291 }
1292
1293 if (m_debug == 1) {
1294 cout << "TRGCDCJSignal::matchUnit()" << endl;
1295 if (t_nBits > 0) cout << "lhs was shifted to left by " << t_nBits << endl;
1296 else if (t_nBits < 0) cout << "rhs was shifted to left by " << -t_nBits << endl;
1297 cout << "TRGCDCJSignal::matchUnit()" << endl;
1298 }
1299 } // End of if units are different.
1300 first.checkInt("TRGCDCJSignal::matchUnit()::first");
1301 second.checkInt("TRGCDCJSignal::matchUnit()::second");
1302 }
1303
1304 void TRGCDCJSignal::setName(const string& name)
1305 {
1306 m_name = name;
1307 }
1308
1309 void TRGCDCJSignal::setInt(signed long long value)
1310 {
1311 m_int = value;
1312 }
1313
1314 void TRGCDCJSignal::setActual(double value)
1315 {
1316 m_actual = value;
1317 }
1318
1319 void TRGCDCJSignal::setToReal(double value)
1320 {
1321 m_toReal = value;
1322 }
1323
1325 {
1326 m_type = type;
1327 }
1328
1330 {
1331 m_bitsize = bitsize;
1332
1333 // Check if there is overflow or underflow.
1334 checkInt("TRGCDCJSignal::setBitsize()");
1335 }
1336
1337 void TRGCDCJSignal::setFinishClock(int finishClock)
1338 {
1339 m_finishClock = finishClock;
1340 }
1341
1343 {
1344 m_debug = debug;
1345 }
1346
1348 {
1349 if (m_commonData) m_commonData->m_printVhdl = printVhdl;
1350 else {
1351 cout << "[Error] TRGCDCJSignal::setPrintVhdl() => m_commonData does not exist." << endl;
1352 }
1353 }
1354
1355 void TRGCDCJSignal::setVhdlOutputFile(const string& filename)
1356 {
1357 if (m_commonData) m_commonData->m_vhdlOutputFile = filename;
1358 else {
1359 cout << "[Error] TRGCDCJSignal::setVhdlOutputFile() => m_commonData does not exist." << endl;
1360 }
1361 }
1362
1364 {
1365 m_commonData = commonData;
1366 }
1367
1369 {
1370 return m_name;
1371 }
1372
1374 {
1375 return m_type;
1376 }
1377
1379 {
1380 return m_bitsize;
1381 }
1382
1383 signed long long TRGCDCJSignal::getInt() const
1384 {
1385 return m_int;
1386 }
1387
1388 signed long long TRGCDCJSignal::getMinInt() const
1389 {
1390 return m_minInt;
1391 }
1392
1393 signed long long TRGCDCJSignal::getMaxInt() const
1394 {
1395 return m_maxInt;
1396 }
1397
1399 {
1400 return m_actual;
1401 }
1402
1404 {
1405 return m_minActual;
1406 }
1407
1409 {
1410 return m_maxActual;
1411 }
1412
1414 {
1415 return m_int * m_toReal;
1416 }
1417
1419 {
1420 return m_minInt * m_toReal;
1421 }
1422
1424 {
1425 return m_maxInt * m_toReal;
1426 }
1427
1429 {
1430 return m_toReal;
1431 }
1432
1434 {
1435 return m_finishClock;
1436 }
1437
1438 std::vector<std::pair<std::string, vector<int> > > const TRGCDCJSignal::getArgumentSignals() const
1439 {
1440 return m_argumentSignals;
1441 }
1442
1444 {
1445 if (m_commonData) return m_commonData;
1446 else {
1447 cout << "[Error] TRGCDCJSignal::getCommonData() => m_commonData does not exist." << endl;
1448 return 0;
1449 }
1450 }
1451
1452
1454 {
1455 //if(m_type==2) cout<<"[Error] TRGCDCJSignal::calType() => m_type should not be slv."<<endl;
1456 if (m_minInt < 0) return -1;
1457 else return 1;
1458 }
1459
1460 int TRGCDCJSignal::calType(double minValue) const
1461 {
1462 //if(m_type==2) cout<<"[Error] TRGCDCJSignal::calType() => m_type should not be slv."<<endl;
1463 if (minValue < 0) return -1;
1464 else return 1;
1465 }
1466
1468 {
1469 //int t_bitwidth = ceil(log(max(m_maxInt, llabs(m_minInt))+1)/log(2));
1470 int t_bitwidth = ceil(log1p(max(m_maxInt, llabs(m_minInt))) / log(2));
1471 if (calType() != 1) t_bitwidth++;
1472 return t_bitwidth;
1473 }
1474
1475 int TRGCDCJSignal::calBitwidth(int type, signed long long minInt, signed long long maxInt)
1476 {
1477 //int t_bitwidth = ceil(log(max(maxInt, llabs(minInt))+1)/log(2));
1478 int t_bitwidth = ceil(log1p(max(maxInt, llabs(minInt))) / log(2));
1479 if (type != 1) t_bitwidth++;
1480 return t_bitwidth;
1481 }
1482
1484 {
1485 // Combined signal?
1486 if (m_argumentSignals.size() != 0) {
1487 int maxClock = -1;
1488 // Find max clock between signals.
1489 for (unsigned iSignals = 0; iSignals < m_argumentSignals.size(); iSignals++) {
1490 if (maxClock < m_argumentSignals[iSignals].second[2]) maxClock = m_argumentSignals[iSignals].second[2];
1491 }
1492 return maxClock;
1493 } else {
1494 return m_finishClock;
1495 }
1496 }
1497
1499 {
1500 return m_vhdlCode;
1501 }
1502
1504 {
1505 if (m_commonData) return m_commonData->m_printVhdl;
1506 else {
1507 cout << "[Error] TRGCDCJSignal::getPrintVhdl() => m_commonData does not exist." << endl;
1508 return 0;
1509 }
1510 }
1511
1513 {
1514 if (m_commonData) return m_commonData->m_vhdlOutputFile;
1515 else {
1516 cout << "[Error] TRGCDCJSignal::getVhdlOutputFile() => m_commonData does not exist." << endl;
1517 return "";
1518 }
1519 }
1520
1522 {
1523 bool result = 0;
1524 if (large.m_bitsize < small.m_bitsize) {
1525 swap(large, small);
1526 result = 1;
1527 } else if (large.m_bitsize == small.m_bitsize) {
1528 if (large.m_type == 1) {;}
1529 else if (large.m_type == -1 && small.m_type == 1) {
1530 swap(large, small);
1531 result = 1;
1532 } else if (large.m_type == 0 && small.m_type != 0) {
1533 swap(large, small);
1534 result = 1;
1535 }
1536 }
1537
1538 return result;
1539 }
1540
1541 signed long long TRGCDCJSignal::calMaxInteger() const
1542 {
1543 signed long long t_maxValue;
1544 if (calType() == 1) {
1545 t_maxValue = pow(2, m_bitsize) - 1;
1546 } else if (calType() == -1) {
1547 t_maxValue = pow(2, m_bitsize - 1) - 1;
1548 } else {
1549 t_maxValue = 0;
1550 }
1551 return t_maxValue;
1552 }
1553
1554 signed long long TRGCDCJSignal::calMinInteger() const
1555 {
1556 //if(m_type==2) cout<<"[Error] TRGCDCJSignal::calMinInteger() => m_type should not be slv"<<endl;
1557 signed long long t_minValue;
1558 if (m_type == 1) {
1559 t_minValue = 0;
1560 } else if (m_type == -1) {
1561 t_minValue = -pow(2, m_bitsize - 1) + 1;
1562 } else {
1563 t_minValue = -pow(2, m_bitsize - 1) + 1;
1564 }
1565 return t_minValue;
1566 }
1567
1569 {
1570
1571 TRGCDCJSignal result(first);
1572
1573 //if(result.m_type==2) cout<<"[Error] TRGCDCJSignal::absolute() => m_type should not be slv"<<endl;
1574
1575 if (result.m_argumentSignals.size() == 0) result.initArgumentSignals();
1576
1577 if (result.m_type != 1) {
1578 result.m_type = 1;
1579 result.m_int = llabs(first.m_int);
1580 if (first.m_minInt < 0 && first.m_maxInt > 0) result.m_minInt = 0;
1581 else result.m_minInt = min(llabs(first.m_maxInt), llabs(first.m_minInt));
1582 result.m_maxInt = max(llabs(first.m_maxInt), llabs(first.m_minInt));
1583 result.m_actual = fabs(first.m_actual);
1584 if (first.m_minActual < 0 && first.m_maxActual > 0) result.m_minActual = 0;
1585 else result.m_minActual = min(fabs(first.m_maxActual), fabs(first.m_minActual));
1586 result.m_maxActual = max(fabs(first.m_maxActual), fabs(first.m_minActual));
1587 result.m_bitsize = first.m_bitsize - 1;
1588 }
1589
1590 result.checkInt("TRGCDCJSignal::absolute()");
1591
1592 // For vhdl code.
1593 if (first.getPrintVhdl() == 1) vhdlCode("abs", first, result, result.m_vhdlCode);
1594
1595 return result;
1596
1597 }
1598
1599 //void TRGCDCJSignal::calTypeBitwidth(TRGCDCJSignal const & lhs, string operation, TRGCDCJSignal const & rhs, int & type, int & bitwidth) const{
1600 // int outputBitsize;
1601 // int outputType;
1602 // if(operation=="+"){
1603 // // Find largest and second bitsize.
1604 // TRGCDCJSignal t_large(lhs);
1605 // TRGCDCJSignal t_small(rhs);
1606 // orderLargestBitsize(t_large, t_small);
1607 // // Calculate.
1608 // if(t_large.m_type == 1) {
1609 // if(t_small.m_type == 1) {outputBitsize = t_large.m_bitsize + 1; outputType = 1;}
1610 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 2; outputType = -1;}
1611 // else {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1612 // } else if(t_large.m_type == -1) {
1613 // outputBitsize = t_large.m_bitsize + 1; outputType = -1;
1614 // } else {
1615 // if(t_small.m_type == 1) {outputBitsize = t_large.m_bitsize; outputType = -1;}
1616 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1617 // else {outputBitsize = t_large.m_bitsize + 1; outputType = 0;}
1618 // }
1619 // } else if(operation=="-"){
1620 // // Find largest and second bitsize.
1621 // TRGCDCJSignal t_large(lhs);
1622 // TRGCDCJSignal t_small(rhs);
1623 // orderLargestBitsize(t_large, t_small);
1624 // // Calculate.
1625 // if(t_large.m_type == 1) {
1626 // if(t_small.m_type == 1) {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1627 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 2; outputType = -1;}
1628 // else {
1629 // if(t_large.m_type == lhs.m_type){
1630 // outputBitsize = t_large.m_bitsize + 1; outputType = 1;
1631 // } else {
1632 // outputBitsize = t_large.m_bitsize + 1; outputType = 0;
1633 // }
1634 // }
1635 // } else if(t_large.m_type == -1) {
1636 // outputBitsize = t_large.m_bitsize + 1; outputType = -1;
1637 // } else {
1638 // if(t_small.m_type == 1) {
1639 // if(t_large.m_type == lhs.m_type){
1640 // outputBitsize = t_large.m_bitsize + 1; outputType = 0;
1641 // } else {
1642 // outputBitsize = t_large.m_bitsize + 1; outputType = 1;
1643 // }
1644 // }
1645 // else if(t_small.m_type == -1) {outputBitsize = t_large.m_bitsize + 1; outputType = -1;}
1646 // else {outputBitsize = t_large.m_bitsize; outputType = -1;}
1647 // }
1648 // } else if(operation=="*"){
1649 // TRGCDCJSignal t_large(lhs);
1650 // TRGCDCJSignal t_small(rhs);
1651 // orderLargestBitsize(t_large, t_small);
1652 // if(t_large.m_type == 1 || t_small.m_type == 1) {
1653 // outputBitsize = t_large.m_bitsize + t_small.m_bitsize;
1654 // if(t_large.m_type == 1) outputType = t_small.m_type;
1655 // else outputType = t_large.m_type;
1656 // } else if(t_large.m_type == 0 && t_small.m_type == 0) {
1657 // outputBitsize = t_large.m_bitsize + t_small.m_bitsize - 2;
1658 // outputType = 1;
1659 // } else {
1660 // outputBitsize = t_large.m_bitsize + t_small.m_bitsize - 1;
1661 // outputType = -1;
1662 // }
1663 // }
1664 // type = outputType;
1665 // bitwidth = outputBitsize;
1666 //}
1667
1668 signed long long TRGCDCJSignal::calInt(double value, TRGCDCJSignal const& mother)
1669 {
1670 return (signed long long) FpgaUtility::roundInt(value / mother.m_toReal);
1671 }
1672
1673 void TRGCDCJSignal::calVhdlTypeBitwidth(TRGCDCJSignal const& first, const std::string& operation, TRGCDCJSignal const& second,
1674 int& type,
1675 int& bitwidth)
1676 {
1677 if (first.m_type != second.m_type) {
1678 cout << "[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => first and second type are different." << endl;
1679 cout << first.m_name << " " << second.m_name << endl;
1680 cout << first.m_type << " " << second.m_type << endl;
1681 return;
1682 }
1683 int firstBitwidth = first.m_bitsize;
1684 int secondBitwidth = second.m_bitsize;
1685
1686 if (operation == "+" || operation == "-") {
1687 // cppcheck-suppress knownConditionTrueFalse
1688 if (first.m_type != 1 || second.m_type != 1) {
1689 type = -1;
1690 bitwidth = max(firstBitwidth, secondBitwidth);
1691 } else {
1692 type = 1;
1693 bitwidth = max(first.m_bitsize, second.m_bitsize);
1694 }
1695 } else if (operation == "*") {
1696 // cppcheck-suppress knownConditionTrueFalse
1697 if (first.m_type != 1 || second.m_type != 1) {
1698 type = -1;
1699 bitwidth = firstBitwidth + secondBitwidth;
1700 } else {
1701 type = 1;
1702 bitwidth = first.m_bitsize + second.m_bitsize;
1703 }
1704 } else if (operation == "=" || operation == "/=" || operation == "<" || operation == "<=" ||
1705 operation == ">" || operation == ">=" || operation == "and" || operation == "or") {
1706 type = 1;
1707 bitwidth = 1;
1708 } else {
1709 cout << "[Error] TRGCDCJSignal::calVhdlTypeBitwidth() => No operation named " << operation << "." << endl;
1710 }
1711 }
1712
1714 {
1715 string t_name;
1716 if (m_argumentSignals.size() == 0) {
1717 if (m_minInt == m_maxInt && m_type != 2) {
1718 if (m_type == 1) {
1719 //t_name="to_unsigned("+to_string(m_int)+","+to_string(m_bitsize)+")";
1720 t_name = "decimal_string_to_unsigned(\"" + to_string(m_int) + "\"," + to_string(m_bitsize) + ")";
1721 } else {
1722 //t_name="to_signed("+to_string(m_int)+","+to_string(m_bitsize)+")";
1723 t_name = "decimal_string_to_signed(\"" + to_string(m_int) + "\"," + to_string(m_bitsize) + ")";
1724 }
1725 // For non-constant constructor signal.
1726 } else if (m_name == "") {
1727 t_name = "INPUT(" + to_string(m_int) + ")";
1728 // For normal signals.
1729 } else {
1730 t_name = m_name;
1731 }
1732 //m_argumentSignals.push_back(make_pair(t_name,m_finishClock));
1733 vector<int> t_argument = {m_type, m_bitsize, m_finishClock};
1734 m_argumentSignals.push_back(make_pair(t_name, t_argument));
1735 } else {
1736 cout << "[Error] TRGCDCJSignal::initArgumentSignals() => m_argumentSignals.size() is not 0." << endl;
1737 }
1738 }
1739
1741 {
1742 result.m_argumentSignals.clear();
1743 result.m_argumentSignals.insert(result.m_argumentSignals.end(), first.m_argumentSignals.begin(), first.m_argumentSignals.end());
1744 result.m_argumentSignals.insert(result.m_argumentSignals.end(), second.m_argumentSignals.begin(), second.m_argumentSignals.end());
1745 }
1746
1748 {
1749 if (m_vhdlCode != "") {
1750 cout << "[Error] TRGCDCJSignal::initVhdlCode() => m_vhdlCode is not \"\"." << endl;
1751 } else {
1752 m_vhdlCode = " ";
1753 }
1754 }
1755
1757 {
1758 if (m_type == -1) return;
1759 // Find if bitwidth is optimized for signed or unsigned. t_bitwidth is optimized for signed.
1760 int t_bitwidth = calBitwidth();
1761 if (calType() == 1) t_bitwidth++;
1762
1763 if (t_bitwidth <= m_bitsize) {
1764 m_vhdlCode = "signed(" + m_vhdlCode + ")";
1765 } else if (t_bitwidth - 1 == m_bitsize) {
1766 m_vhdlCode = "signed('0'&(" + m_vhdlCode + "))";
1767 m_bitsize += 1;
1768 } else {
1769 cout << "[Error] TRGCDCJSignal::toSignedVhdlCode() => m_bitsize is too small." << endl;
1770 }
1771 m_type = -1;
1772 }
1773
1775 {
1776 if (m_type == 1) return;
1777
1778 if (calType() == -1) {
1779 cout << "[Warning] TRGCDCJSignal::toUnsignedVhdlCode() => signal can be minus." << endl;
1780 } else {
1781 m_vhdlCode = "unsigned(" + m_vhdlCode + ")";
1782 m_type = 1;
1783 }
1784
1785 }
1786
1787 void TRGCDCJSignal::vhdlCode(TRGCDCJSignal const& first, const std::string& operation, TRGCDCJSignal const& second,
1788 TRGCDCJSignal const& result, string& resultVhdlCode)
1789 {
1790 // Copy first and second for VHDL version.
1791 TRGCDCJSignal t_first(first);
1792 TRGCDCJSignal t_second(second);
1793 // Set m_vhdlCode if needed. (Will require clock implementation later)
1794 if (t_first.m_vhdlCode == "") {
1795 t_first.initVhdlCode();
1796 }
1797 if (t_second.m_vhdlCode == "") {
1798 t_second.initVhdlCode();
1799 }
1800
1801 // Match types.
1802 // Changed to signed.
1803 if (t_first.m_type * t_second.m_type < 0 || (t_first.m_type != -1 && result.m_type == -1)) {
1804 if (t_first.m_type != -1) {
1805 t_first.toSignedVhdlCode();
1806 }
1807 if (t_second.m_type != -1) {
1808 t_second.toSignedVhdlCode();
1809 }
1810 }
1811 // Change to unsigned.
1812 if (t_first.m_type == 2) t_first.toUnsignedVhdlCode();
1813 if (t_second.m_type == 2) t_second.toUnsignedVhdlCode();
1814
1815 // Calculate result type and bitwidth.
1816 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1817 logicType = result.m_type;
1818 logicBitwidth = result.m_bitsize;
1819 calVhdlTypeBitwidth(t_first, operation, t_second, vhdlType, vhdlBitwidth);
1820
1821 // Make code.
1822 string resultCode;
1823 // For compare operations.
1824 if (operation == "=" || operation == "/=" || operation == "<" || operation == "<=" ||
1825 operation == ">" || operation == ">=" || operation == "and" || operation == "or") {
1826 resultCode = "(" + t_first.m_vhdlCode + operation + t_second.m_vhdlCode + ")";
1827 } else {
1828 // For arithmetic operations.
1829 // If special case.
1830 if (logicType > 0 && vhdlType == -1) {
1831 if (logicBitwidth + 1 > vhdlBitwidth) t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(
1832 logicBitwidth + 1) + ")";
1833 resultCode = t_first.m_vhdlCode + operation + t_second.m_vhdlCode;
1834 resultCode = "resize(unsigned(" + resultCode + ")," + to_string(logicBitwidth) + ")";
1835 } else {
1836 // Normal case.
1837 if (logicBitwidth > vhdlBitwidth) {
1838 t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(logicBitwidth) + ")";
1839 }
1840 resultCode = t_first.m_vhdlCode + operation + t_second.m_vhdlCode;
1841 // Resize output to minimum bitwidth.
1842 if (logicBitwidth < vhdlBitwidth) {
1843 resultCode = "resize(" + resultCode + "," + to_string(logicBitwidth) + ")";
1844 }
1845 }
1846 }
1847 resultVhdlCode = resultCode;
1848 }
1849
1850 void TRGCDCJSignal::vhdlCode(const std::string& operation, TRGCDCJSignal const& first, TRGCDCJSignal const& result,
1851 string& resultVhdlCode)
1852 {
1853 // Copy first for VHDL version.
1854 TRGCDCJSignal t_first(first);
1855 // Set m_vhdlCode if needed. (Will require clock implementation later)
1856 if (t_first.m_vhdlCode == "") {
1857 t_first.initVhdlCode();
1858 }
1859 // Match types
1860 // Change VHDL unsigned to VHDL signed if required.
1861 if (t_first.m_type != -1 && result.m_type == -1) {
1862 t_first.toSignedVhdlCode();
1863 }
1864 // Change to unsigned if slv.
1865 if (t_first.m_type == 2) t_first.toUnsignedVhdlCode();
1866
1867 // Calculate result type and bitwidth.
1868 int logicType, logicBitwidth, vhdlType, vhdlBitwidth;
1869 logicType = result.m_type;
1870 logicBitwidth = result.m_bitsize;
1871 // Will always be same as first type and bitsize.
1872 vhdlType = t_first.m_type;
1873 vhdlBitwidth = t_first.m_bitsize;
1874
1875 // Make code.
1876 string resultCode;
1877 // If logic(out) is unsigned and vhdl(in) is signed.
1878 if (logicType == 1 && vhdlType == -1) {
1879 if (logicBitwidth + 1 > vhdlBitwidth) t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(
1880 logicBitwidth + 1) + ")";
1881 if (operation == "shift_left"
1882 || operation == "shift_right") resultCode = operation + "(" + t_first.m_vhdlCode + "," + to_string(abs(
1883 first.m_bitsize - result.m_bitsize)) + ")";
1884 else if (operation == "unsigned" || operation == "none") resultCode = t_first.m_vhdlCode;
1885 else resultCode = operation + "(" + t_first.m_vhdlCode + ")";
1886 resultCode = "resize(unsigned(" + resultCode + ")," + to_string(logicBitwidth) + ")";
1887 } else {
1888 // Normal case.
1889 if (logicBitwidth > vhdlBitwidth) {
1890 t_first.m_vhdlCode = "resize(" + t_first.m_vhdlCode + "," + to_string(logicBitwidth) + ")";
1891 }
1892 if (operation == "shift_left"
1893 || operation == "shift_right") resultCode = operation + "(" + t_first.m_vhdlCode + "," + to_string(abs(
1894 first.m_bitsize - result.m_bitsize)) + ")";
1895 else if (operation == "resize" || operation == "signed" || operation == "none") resultCode = t_first.m_vhdlCode;
1896 else resultCode = operation + "(" + t_first.m_vhdlCode + ")";
1897 // Resize output to minimum bitwidth.
1898 if (logicBitwidth < vhdlBitwidth) {
1899 resultCode = "resize(" + resultCode + "," + to_string(logicBitwidth) + ")";
1900 }
1901 }
1902 resultVhdlCode = resultCode;
1903 }
1904
1905 std::string TRGCDCJSignal::replaceWithSignalNames(std::string const& fromVhdlCode,
1906 std::vector<pair<std::string, vector<int> > > const& fromArgumentSignals, int const& finishClock,
1907 std::map<std::string, vector<int> >& buffers)
1908 {
1909 // Replace names with token(" ") in from.m_vhdlCode
1910 string t_vhdlCode = "";
1911 size_t start = 0;
1912 size_t end = fromVhdlCode.find(" ");
1913 int nIndex = (int)fromArgumentSignals.size();
1914 int index = 0;
1915 while (end != string::npos) {
1916 // Check if there are enough signal names.
1917 if (nIndex - 1 < index) {
1918 cout << "[Error] TRGCDCJSignal::replaceWithSignalNames() => Not enough signal names in m_argumentSignals." << endl;
1919 return "";
1920 }
1921 // Replace token(" ") with name.
1922 //cout<<"Name: "<<fromArgumentSignals[index].first<<" Clock: "<<fromArgumentSignals[index].second[2]<<endl;
1923 int t_buffer = (finishClock - 1) - fromArgumentSignals[index].second[2] - 1;
1924 // No buffer needed or instant assignment or constant.
1925 if (t_buffer == -1 || t_buffer == -2
1926 || fromArgumentSignals[index].second[2] == -1) t_vhdlCode += fromVhdlCode.substr(start,
1927 end - start) + fromArgumentSignals[index].first;
1928 else if (t_buffer > -1) {
1929 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first + "_b(" + to_string(t_buffer) + ")";
1930 // Fill map for buffers.
1931 if (buffers.find(fromArgumentSignals[index].first) == buffers.end()) {
1932 buffers[fromArgumentSignals[index].first] = {fromArgumentSignals[index].second[0], fromArgumentSignals[index].second[1], t_buffer};
1933 } else {
1934 if (buffers[fromArgumentSignals[index].first][2] < t_buffer) buffers[fromArgumentSignals[index].first][2] = t_buffer;
1935 }
1936 } else {
1937 cout << "[Error] TRGCDCJSignal::replaceWithSignalNames() => finishClock is too small." << endl;
1938 t_vhdlCode += fromVhdlCode.substr(start, end - start) + fromArgumentSignals[index].first;
1939 }
1940
1941 index++;
1942 start = end + 1;
1943 end = fromVhdlCode.find(" ", start);
1944 }
1945 t_vhdlCode += fromVhdlCode.substr(start, end - start);
1946 // Check if number of spaces and signal names are same.
1947 if (nIndex != index) {
1948 cout << "[Error] TRGCDCJSignal::replaceWithSignalNames() => Number of signal names and number of " " do not match." << endl;
1949 return "";
1950 }
1951 return t_vhdlCode;
1952 }
1953
1954 std::string TRGCDCJSignal::assignVhdlCode(TRGCDCJSignal const& target, TRGCDCJSignal const& from)
1955 {
1956
1957 // Check if target has m_commonData.
1958 if (target.m_commonData == 0) {
1959 cout << "[Error] TRGCDCJSignal::assignVhdlCode() => target.m_commonData is not set." << endl;
1960 return "";
1961 }
1962
1963 TRGCDCJSignal t_from(from);
1964
1965 // Set m_vhdlCode if needed.
1966 if (t_from.m_vhdlCode == "") {
1967 t_from.initVhdlCode();
1968 }
1969
1970 // Fill signal map for definition.
1971 {
1972 if (target.m_name == "") cout << "[Error] TRGCDCJSignal::assignVhdlCode() => target.m_name is \"\" " << endl;
1973 map<string, vector<int> >& t_signals = target.m_commonData->m_signals;
1974 if (t_signals.find(target.m_name) == t_signals.end()) {
1975 //t_signals[target.m_name] = {target.m_type, target.m_bitsize};
1976 // From and target should be same.
1977 // cppcheck-suppress stlFindInsert
1978 t_signals[target.m_name] = {from.m_type, from.m_bitsize};
1979 }
1980 }
1981
1982 // Replace names with spaces in from.m_vhdlCode
1983 string t_vhdlCode = replaceWithSignalNames(t_from.m_vhdlCode, t_from.m_argumentSignals, t_from.m_finishClock,
1984 t_from.m_commonData->m_buffers);
1985
1986 return target.m_name + "<=" + t_vhdlCode + ";";
1988 //if(t_vhdlCode != "") {
1989 // return target.m_name + "<=" + t_vhdlCode + ";";
1991 //} else if (from.m_minInt == from.m_maxInt) {
1992 // if(from.m_type == 1) {
1993 // return target.m_name + "<=to_unsigned(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
1994 // } else {
1995 // return target.m_name + "<=to_signed(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
1996 // }
1998 //} else if (from.m_name=="") {
1999 // if(from.m_type == 1) {
2000 // return target.m_name + "<=to_unsigned(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
2001 // } else {
2002 // return target.m_name + "<=to_signed(" + to_string(from.m_int) + "," + to_string(from.m_bitsize) + ")";
2003 // }
2004 //} else {
2006 // return target.m_name + "<=" + from.m_name + ";";
2007 //}
2008 }
2009
2010 std::string TRGCDCJSignal::chooseVhdlCode(TRGCDCJSignal const& target, TRGCDCJSignal const& reference,
2011 std::vector<std::vector<TRGCDCJSignal> > const& data)
2012 {
2013 // Check if target has m_name.
2014 if (target.m_name == "") {
2015 cout << "[Error] TRGCDCJSignal::chooseVhdlCode() => target has no name." << endl;
2016 return "";
2017 }
2018 // Check if reference has m_name or vhdlCode.
2019 if (reference.m_name == "" && reference.m_vhdlCode == "") {
2020 cout << "[Error] TRGCDCJSignal::cooseVhdlCode() => reference has no name or vhdlCode." << endl;
2021 return "";
2022 }
2023
2024 TRGCDCJSignal t_reference(reference);
2025
2026 // Set m_vhdlCode if needed.
2027 if (t_reference.m_vhdlCode == "") t_reference.initVhdlCode();
2028 // Replace names with spaces in reference.m_vhdlCode
2029 string t_referenceVhdlCode = replaceWithSignalNames(t_reference.m_vhdlCode, t_reference.m_argumentSignals, target.m_finishClock,
2030 t_reference.m_commonData->m_buffers);
2031
2032 // Storage for used values.
2033 // map<signed long long, bool> t_used;
2034 // Create string for choice().
2035 stringstream t_vhdlCode;
2036 //t_vhdlCode<<"case to_integer("<<t_referenceVhdlCode<<") is"<<endl;
2037 //for(unsigned iCondition=0; iCondition<data.size()-1; iCondition++){
2038 // TRGCDCJSignal const & from = data[iCondition][1];
2039 // TRGCDCJSignal const & to = data[iCondition][2];
2040 // TRGCDCJSignal const & assign = data[iCondition][0];
2041 // signed long long fromInt = from.m_int;
2042 // signed long long toInt = to.m_int;
2043 // // Don't allow same int numbers for from and to.
2044 // if(t_used.count(fromInt)!=0) fromInt += 1;
2045 // else t_used[fromInt] = 1;
2046 // if(t_used.count(toInt)!=0) {
2047 // // Allow same from and to when in one line.
2048 // if(fromInt!=toInt) toInt -= 1;
2049 // } else t_used[toInt] = 1;
2050 // t_vhdlCode<<" when "<<fromInt<<" to "<<toInt<<"=>"<<assignVhdlCode(target,assign)<<endl;;
2051 //}
2052 //t_vhdlCode<<" when others=>"<<assignVhdlCode(target,(*(data.end()-1))[0])<<endl;
2053 //t_vhdlCode<<"end case;";
2054
2055 // For if
2056 TRGCDCJSignal from = data[0][1];
2057 TRGCDCJSignal to = data[0][2];
2058 TRGCDCJSignal assign = data[0][0];
2059 if (from.m_vhdlCode == "") from.initVhdlCode();
2060 if (to.m_vhdlCode == "") to.initVhdlCode();
2061 string t_fromVhdlCode = replaceWithSignalNames(from.m_vhdlCode, from.m_argumentSignals, target.m_finishClock,
2062 target.m_commonData->m_buffers);
2063 string t_toVhdlCode = replaceWithSignalNames(to.m_vhdlCode, to.m_argumentSignals, target.m_finishClock,
2064 target.m_commonData->m_buffers);
2065 string t_fromPrefixSigned;
2066 string t_fromPostfixSigned;
2067 string t_toPrefixSigned;
2068 string t_toPostfixSigned;
2069 if (reference.m_type == -1 && from.m_type == 1) {
2070 t_fromPrefixSigned = "signed('0'&";
2071 t_fromPostfixSigned = ")";
2072 } else {
2073 t_fromPrefixSigned = "";
2074 t_fromPostfixSigned = "";
2075 }
2076 if (reference.m_type == -1 && to.m_type == 1) {
2077 t_toPrefixSigned = "signed('0'&";
2078 t_toPostfixSigned = ")";
2079 } else {
2080 t_toPrefixSigned = "";
2081 t_toPostfixSigned = "";
2082 }
2083 t_vhdlCode << "if (" << t_referenceVhdlCode << " >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned << ") and (" <<
2084 t_referenceVhdlCode << " <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned << ") then" << endl;
2085 t_vhdlCode << " " << assignVhdlCode(target, assign) << endl;
2086
2087 // For if else
2088 for (unsigned iCondition = 1; iCondition < data.size() - 1; iCondition++) {
2089 TRGCDCJSignal from1 = data[iCondition][1];
2090 TRGCDCJSignal to1 = data[iCondition][2];
2091 TRGCDCJSignal assign1 = data[iCondition][0];
2092 if (from1.m_vhdlCode == "") from1.initVhdlCode();
2093 if (to1.m_vhdlCode == "") to1.initVhdlCode();
2094 if (reference.m_type == -1 && from1.m_type == 1) {
2095 t_fromPrefixSigned = "signed('0'&";
2096 t_fromPostfixSigned = ")";
2097 } else {
2098 t_fromPrefixSigned = "";
2099 t_fromPostfixSigned = "";
2100 }
2101 if (reference.m_type == -1 && to1.m_type == 1) {
2102 t_toPrefixSigned = "signed('0'&";
2103 t_toPostfixSigned = ")";
2104 } else {
2105 t_toPrefixSigned = "";
2106 t_toPostfixSigned = "";
2107 }
2108 t_fromVhdlCode = replaceWithSignalNames(from1.m_vhdlCode, from1.m_argumentSignals, target.m_finishClock,
2109 target.m_commonData->m_buffers);
2110 t_toVhdlCode = replaceWithSignalNames(to1.m_vhdlCode, to1.m_argumentSignals, target.m_finishClock, target.m_commonData->m_buffers);
2111 t_vhdlCode << "elsif (" << t_referenceVhdlCode << " >= " << t_fromPrefixSigned << t_fromVhdlCode << t_fromPostfixSigned << ") and ("
2112 << t_referenceVhdlCode << " <= " << t_toPrefixSigned << t_toVhdlCode << t_toPostfixSigned << ") then" << endl;
2113 t_vhdlCode << " " << assignVhdlCode(target, assign1) << endl;
2114 }
2115 t_vhdlCode << "else" << endl;
2116 t_vhdlCode << " " << assignVhdlCode(target, (*(data.end() - 1))[0]) << endl;
2117 t_vhdlCode << "end if;";
2118 return t_vhdlCode.str();
2119 }
2120
2122 std::vector<std::pair<TRGCDCJSignal, std::vector<std::pair<TRGCDCJSignal*, TRGCDCJSignal> > > > const& data)
2123 {
2124 // Check if comp and lhs have name
2125 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
2126 TRGCDCJSignal const& comp = data[iCase].first;
2127 if (iCase != data.size() - 1) {
2128 if (comp.m_name == "") {
2129 cout << "[Error] TRGCDCJSignal::ifElseVhdlCode() => comp has no name." << endl;
2130 return "";
2131 }
2132 }
2133 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2134 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2135 if (lhs.m_name == "") {
2136 cout << "[Error] TRGCDCJSignal::ifElseVhdlCode() => lhs has no name." << endl;
2137 return "";
2138 }
2139 }
2140 }
2141
2142 // Write vhdl
2143 // Else will be last in data.
2144 stringstream t_vhdlCode;
2145 for (unsigned iCase = 0; iCase < data.size(); iCase++) {
2146 TRGCDCJSignal const& comp = data[iCase].first;
2147 string t_compVhdlCode = replaceWithSignalNames(comp.m_vhdlCode, comp.m_argumentSignals, comp.m_finishClock,
2148 comp.m_commonData->m_buffers);
2149 // Choose which if to use.
2150 if (iCase == 0) t_vhdlCode << "if " << t_compVhdlCode << " then" << endl;
2151 else if (comp.m_bitsize == -1) t_vhdlCode << "else" << endl;
2152 else t_vhdlCode << "elsif " << t_compVhdlCode << " then" << endl;
2153 for (unsigned iAssignment = 0; iAssignment < data[iCase].second.size(); iAssignment++) {
2154 TRGCDCJSignal const& lhs = *(data[iCase].second[iAssignment].first);
2155 TRGCDCJSignal const& rhs = data[iCase].second[iAssignment].second;
2156 t_vhdlCode << " " << assignVhdlCode(lhs, rhs) << endl;
2157 }
2158 }
2159 t_vhdlCode << "end if;";
2160
2161 return t_vhdlCode.str();
2162
2163 }
2164
2165 void TRGCDCJSignal::printVhdl(const std::string& vhdlCode) const
2166 {
2167 if (m_commonData) {
2168 ofstream outFile;
2169 outFile.open(m_commonData->m_vhdlOutputFile, ios::app);
2170 if (outFile.is_open()) {
2171 outFile << vhdlCode << endl;
2172 outFile.close();
2173 }
2174 } else {
2175 cout << "[Error] TRGCDCJSignal::printVhdl() => m_commonData doesn't exist." << endl;
2176 }
2177 }
2178
2179 void TRGCDCJSignal::checkInt(const std::string& name) const
2180 {
2181 if (m_int > calMaxInteger()) {
2182 cout << "[Error] " << name << " => m_int overflow" << endl;
2183 cout << " Start dumping" << endl;
2184 dump();
2185 cout << " End dumping" << endl;
2186 }
2187 if (m_int < calMinInteger()) {
2188 cout << "[Error] " << name << " => m_int underflow" << endl;
2189 cout << " Start dumping" << endl;
2190 dump();
2191 cout << " End dumping" << endl;
2192 }
2193 if (m_maxInt > calMaxInteger()) {
2194 cout << "[Error] " << name << " => m_maxInt overflow" << endl;
2195 cout << " Start dumping" << endl;
2196 dump();
2197 cout << " End dumping" << endl;
2198 }
2199 if (m_minInt < calMinInteger()) {
2200 cout << "[Error] " << name << " => m_minInt underflow" << endl;
2201 cout << " Start dumping" << endl;
2202 dump();
2203 cout << " End dumping" << endl;
2204 }
2205 }
2206
2208 {
2209 return lhs.m_int == rhs.m_int && lhs.m_minInt == rhs.m_minInt && lhs.m_maxInt == rhs.m_maxInt
2210 && lhs.m_toReal == rhs.m_toReal && lhs.m_actual == rhs.m_actual && lhs.m_minActual == rhs.m_minActual
2211 && lhs.m_maxActual == rhs.m_maxActual;
2212 }
2213
2214 void TRGCDCJSignal::valuesToMapSignals(std::vector<std::tuple<string, double, int, double, double, int> > const& inValues,
2215 Belle2::TRGCDCJSignalData* inCommonData, std::map<std::string, Belle2::TRGCDCJSignal>& outMap)
2216 {
2217 for (unsigned iSignals = 0; iSignals < inValues.size(); iSignals++) {
2218 string const& t_name = get<0>(inValues[iSignals]);
2219 double const& t_value = get<1>(inValues[iSignals]);
2220 int const& t_bitWidth = get<2>(inValues[iSignals]);
2221 double const& t_min = get<3>(inValues[iSignals]);
2222 double const& t_max = get<4>(inValues[iSignals]);
2223 int const& t_clock = get<5>(inValues[iSignals]);
2224 // Put input to signal.
2225 // Input to finish clock is 0.
2226 // Signals that will be used will have finish clock 1 due to below line.
2227 outMap[t_name] <= Belle2::TRGCDCJSignal(t_bitWidth, t_value, t_min, t_max, t_clock, inCommonData);
2228 }
2229 }
2230
2231 void TRGCDCJSignal::mapSignalsToValues(std::map<std::string, Belle2::TRGCDCJSignal> const& inMap,
2232 std::vector<std::pair<std::string, int> > const& inChoose,
2233 std::vector<std::tuple<string, double, int, double, double, int> >& outValues)
2234 {
2235 for (unsigned iSignals = 0; iSignals < inChoose.size(); iSignals++) {
2236 int const& b_integerEffect = inChoose[iSignals].second;
2237 string const& t_name = inChoose[iSignals].first;
2238 Belle2::TRGCDCJSignal const& t_signal = inMap.find(t_name)->second;
2239 double t_value = 0, t_min = 0, t_max = 0;
2240 int t_bitwidth = t_signal.getBitsize();
2241 if (b_integerEffect == 1) {
2242 t_value = t_signal.getRealInt();
2243 t_min = t_signal.getRealMinInt();
2244 t_max = t_signal.getRealMaxInt();
2245 } else {
2246 t_value = t_signal.getActual();
2247 t_min = t_signal.getMinActual();
2248 t_max = t_signal.getMaxActual();
2249 }
2250 int t_clock = t_signal.getFinishClock();
2251 tuple<string, double, int, double, double, int> outValue(t_name, t_value, t_bitwidth, t_min, t_max, t_clock);
2252 outValues.push_back(outValue);
2253 }
2254 }
2255
2257}
A class to hold common data for JSignals.
Definition JSignalData.h:33
std::map< std::string, std::vector< int > > m_buffers
vector<int> is {type, bitwidth, buffer} Holds all the required 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
Members.
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.
signed long long calMaxInteger() const
Calculate max value for a binary number.
Definition JSignal.cc:1541
int calType() const
Calculated what type the signal is.
Definition JSignal.cc:1453
double getActual() const
Get float value of signal.
Definition JSignal.cc:1398
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:2165
std::string getName() const
Get name of signal.
Definition JSignal.cc:1368
std::string getVhdlOutputFile() const
Gets the vhdl output file name.
Definition JSignal.cc:1512
void setCommonData(TRGCDCJSignalData *)
Sets JSignalData to signal.
Definition JSignal.cc:1363
void setBitsize(int bitsize)
Sets bitwidth of signal.
Definition JSignal.cc:1329
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:1787
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:2214
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 outputting 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:1521
static bool isSameSignal(TRGCDCJSignal const &lhs, TRGCDCJSignal const &rhs)
Checks if signal is same signal.
Definition JSignal.cc:2207
void setActual(double value)
Sets float value of signal.
Definition JSignal.cc:1314
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:1905
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:1443
double getMaxActual() const
Get maximum float value of signal.
Definition JSignal.cc:1408
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:1127
void initArgumentSignals()
Initializes the argument signals for the signal.
Definition JSignal.cc:1713
void setVhdlOutputFile(const std::string &filename)
Sets output VHDL filename.
Definition JSignal.cc:1355
std::string getVhdlCode() const
Gets the vhdl code for the signal.
Definition JSignal.cc:1498
void setInt(signed long long value)
Sets integer value of signal.
Definition JSignal.cc:1309
int calBitwidth() const
Calculates the bit width.
Definition JSignal.cc:1467
bool getPrintVhdl() const
Gets the print vhdl flag from common JSignalData class.
Definition JSignal.cc:1503
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:806
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:1438
static std::string assignVhdlCode(TRGCDCJSignal const &target, TRGCDCJSignal const &from)
Writes the assign vhdl code.
Definition JSignal.cc:1954
TRGCDCJSignal const resize(int bitwidth) const
Resizes signal.
Definition JSignal.cc:995
double getRealMinInt() const
Get converted float value of minimum integer.
Definition JSignal.cc:1418
static void combineArguments(TRGCDCJSignal const &first, TRGCDCJSignal const &second, TRGCDCJSignal &result)
Combines arguments of signals.
Definition JSignal.cc:1740
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:2010
static TRGCDCJSignal const absolute(TRGCDCJSignal const &first)
Absolute TRGCDCJSignal. Removes 1 bit if signed or minus unsigned.
Definition JSignal.cc:1568
int getFinishClock() const
Gets clock tick where signal is make.
Definition JSignal.cc:1433
static signed long long calInt(double value, TRGCDCJSignal const &mother)
Calculates integer value with unit of a TRGCDCJSignal.
Definition JSignal.cc:1668
void setFinishClock(int finishClock)
Sets finish clock of signal.
Definition JSignal.cc:1337
signed long long getInt() const
Get integer value of signal.
Definition JSignal.cc:1383
double getToReal() const
Get toReal value of signal.
Definition JSignal.cc:1428
void matchUnit(TRGCDCJSignal &first, TRGCDCJSignal &second) const
Utilities.
Definition JSignal.cc:1266
void setDebug(bool debug)
Sets debug flag for signal.
Definition JSignal.cc:1342
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:963
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:1673
void setName(const std::string &name)
Setters, Getters Sets name of signal.
Definition JSignal.cc:1304
double getRealMaxInt() const
Get converted float value of maximum integer.
Definition JSignal.cc:1423
void checkInt(const std::string &name) const
Checks underflow or overflow for TRGCDCJSignal.
Definition JSignal.cc:2179
void swap(TRGCDCJSignal &first, TRGCDCJSignal &second) const
Swaps the TRGCDCJSignals.
Definition JSignal.cc:1247
TRGCDCJSignal const operator*(TRGCDCJSignal const &rhs) const
Arithmetic multiply operator.
Definition JSignal.cc:432
double getBitsize() const
Get bitwidth of signal.
Definition JSignal.cc:1378
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:1554
void initVhdlCode()
Initializes the vhdl code.
Definition JSignal.cc:1747
void setPrintVhdl(bool printVhdl)
Sets print flag for signal.
Definition JSignal.cc:1347
int getType() const
Get type of signal.
Definition JSignal.cc:1373
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:2121
void toUnsignedVhdlCode()
Changes signal type to unsigned type in VHDL.
Definition JSignal.cc:1774
double getMinActual() const
Get minimum float value of signal.
Definition JSignal.cc:1403
signed long long getMaxInt() const
Get maximum integer value of signal.
Definition JSignal.cc:1393
int calFinishClock() const
Calculates the clock tick where the signal is valid.
Definition JSignal.cc:1483
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:1087
void dump() const
Methods.
Definition JSignal.cc:539
double getRealInt() const
Get converted float value of integer.
Definition JSignal.cc:1413
TRGCDCJSignal()
Constructor.
Definition JSignal.cc:55
signed long long getMinInt() const
Get minimum integer value of signal.
Definition JSignal.cc:1388
void setToReal(double value)
Sets toReal factor of signal.
Definition JSignal.cc:1319
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:2231
void setType(int type)
Sets type of signal.
Definition JSignal.cc:1324
void toSignedVhdlCode()
Changes signal type to signed type in VHDL.
Definition JSignal.cc:1756
static TRGCDCJSignal comp(TRGCDCJSignal const &lhs, const std::string &operate, TRGCDCJSignal const &rhs)
Compare two signals.
Definition JSignal.cc:1175
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:1046
Abstract base class for different kinds of events.