Belle II Software  release-06-02-00
SignalBundle.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 #define TRG_SHORT_NAMES
10 
11 #include <string>
12 #include <algorithm>
13 #include <limits>
14 #include <iostream>
15 #include <fstream>
16 #include "trg/trg/Utilities.h"
17 #include "trg/trg/Debug.h"
18 #include "trg/trg/Clock.h"
19 #include "trg/trg/Signal.h"
20 #include "trg/trg/SignalVector.h"
21 #include "trg/trg/SignalBundle.h"
22 #include "trg/trg/State.h"
23 
24 using namespace std;
25 
26 namespace Belle2 {
32  TRGSignalBundle::TRGSignalBundle(const TRGClock& c)
33  : _name("unknown"),
34  _clock(& c)
35  {
36  }
37 
38  TRGSignalBundle::TRGSignalBundle(const string& name, const TRGClock& c)
39  : _name(name),
40  _clock(& c)
41  {
42  }
43 
45  const TRGClock& c,
46  const TRGSignalBundle& input,
47  const unsigned outputBitSize,
48  TRGState(* packer)(const TRGState&))
49  : _name(name),
50  _clock(& c)
51  {
52 
53  //...Get state information...
54  const vector<int> states = input.stateChanges();
55  const unsigned nStates = states.size();
56 
57  //...Loop over all states...
58  vector<TRGState*> outputStates;
59  for (unsigned i = 0; i < nStates; i++) {
60  TRGState s = input.state(states[i]);
61 
62  // if (TRGDebug::level()) {
63  // cout << TRGDebug::tab() << "Clock=" << states[i] << endl;
64  // }
65 
66  outputStates.push_back(new TRGState((* packer)(s)));
67  }
68 
69  //...Bit size of output...
70  // unsigned outputSize = 0;
71  // if (outputStates.size())
72  // outputSize = outputStates.back()->size();
73 
74  //...Creat a SignalVector...
75  TRGSignalVector* sb = new TRGSignalVector(_name, c, outputBitSize);
76 
77  //...Make a SignalVector...
78  // const TRGState &os0 = * outputStates[0];
79  // sb->set(os0, 0); delete &os0;
80  // const unsigned n = outputStates.size(); // same as nStates
81 
82  for (unsigned i = 0; i < nStates; i++) {
83  const TRGState* s = outputStates[i];
84  sb->set((*s), states[i]);
85  delete s;
86  }
87 
88  push_back(sb);
89  }
90 
91  TRGSignalBundle::TRGSignalBundle(const string& name,
92  const TRGClock& c,
93  const TRGSignalBundle& input,
94  const unsigned outputBitSize,
95  const unsigned registerBitSize,
96  TRGState(* packer)(const TRGState&,
97  TRGState&,
98  bool&))
99  : _name(name),
100  _clock(& c)
101  {
102 
103  const string sn = "TRGSignalBundle constructor(4)";
105 
106  //...Get state information...
107  const vector<int> states = input.stateChanges();
108  const unsigned nStates = states.size();
109 
110  if (TRGDebug::level()) {
111  cout << TRGDebug::tab() << "#states=" << nStates << endl;
112  for (unsigned i = 0; i < nStates; i++)
113  cout << TRGDebug::tab() << i << " : " << states[i] << endl;
114  }
115 
116  //...Input is stably inactive...
117  if (nStates == 0) {
118  TRGSignalVector* sb = new TRGSignalVector(_name, c, outputBitSize);
119  push_back(sb);
121  return;
122  }
123 
124  //...Loop over all states...
125  vector<TRGState*> outputStates;
126  vector<int> newStates;
127  int lastClock = states[0] - 1;
128  TRGState* r = new TRGState(registerBitSize);
129  for (unsigned i = 0; i < nStates; i++) {
130 
131  if (TRGDebug::level())
132  cout << TRGDebug::tab() << "Last clock=" << lastClock << endl;
133 
134  //...Check clock position...
135  if (states[i] <= lastClock)
136  continue;
137 
138  if (TRGDebug::level()) {
139  cout << TRGDebug::tab() << "state=" << i << endl;
140  r->dump("", TRGDebug::tab() + "reg=");
141  }
142 
143  bool active = true;
144  int nLoops = 0;
145  // cppcheck-suppress knownConditionTrueFalse
146  while (active) {
147  int clk = states[i] + nLoops;
148  lastClock = clk;
149 
150  TRGState s = input.state(clk);
151  outputStates.push_back(new TRGState((* packer)(s, * r, active)));
152  newStates.push_back(clk);
153  ++nLoops;
154 
155  if (TRGDebug::level()) {
156  cout << TRGDebug::tab() << "state=" << i << ",clock=" << clk <<
157  ",LogicActive=" << active << endl;
158  r->dump("", TRGDebug::tab() + "reg=");
159  }
160  }
161  }
162  delete r;
163 
164  //...Size of output...
165  // unsigned outputSize = 0;
166  // if (outputStates.size())
167  // outputSize = outputStates.back()->size();
168 
169  //...Creat a SignalVector...
170  TRGSignalVector* sb = new TRGSignalVector(_name, c, outputBitSize);
171 
172  //...Make a SignalVector...
173  // const TRGState &os0 = * outputStates[0];
174  // sb->set(os0, 0); delete &os0;
175  // const unsigned n = outputStates.size(); // same as nStates
176 
177  const unsigned n = outputStates.size();
178  for (unsigned i = 0; i < n; i++) {
179  const TRGState* s = outputStates[i];
180  sb->set((* s), newStates[i]);
181  delete s;
182  }
183 
184  push_back(sb);
185 
187  }
188 
190  {
191  }
192 
193  void
194  TRGSignalBundle::dump(const string& msg,
195  const string& pre) const
196  {
197  cout << pre << _name << ":" << size() << " signal vector(s)" << endl;
198  for (unsigned i = 0; i < size(); i++)
199  (* this)[i]->dump(msg, " " + pre);
200  }
201 
202  bool
204  {
205  for (unsigned i = 0; i < size(); i++)
206  if ((* this)[i]->active())
207  return true;
208  return false;
209  }
210 
211  std::vector<int>
213  {
214 
215  //...List for clock positions...
216  std::vector<int> list;
217 
218  //...Pick up all clock points with state changes...
219  const unsigned n = size();
220  for (unsigned i = 0; i < n; i++) {
221  vector<int> a = (* this)[i]->stateChanges();
222  for (unsigned j = 0; j < a.size(); j++)
223  list.push_back(a[j]);
224  }
225 
226  //...Append the first clock point if there are state changes...
227  if (list.size())
228  list.push_back(_clock->min());
229 
230  //...Sorting...
231  std::sort(list.begin(), list.end());
232 
233  //...Remove duplicated clock...
234  std::vector<int> list2;
235  int last = numeric_limits<int>::min();
236  for (unsigned i = 0; i < list.size(); i++) {
237  const int j = list[i];
238  if (j != last) {
239  list2.push_back(j);
240  last = j;
241  }
242  }
243  return list2;
244  }
245 
246  TRGState
247  TRGSignalBundle::state(int clockPosition) const
248  {
249  TRGState s;
250  const unsigned n = size();
251  for (unsigned i = 0; i < n; i++) {
252  TRGState t = (* this)[i]->state(clockPosition);
253  s += t;
254  }
255  return s;
256  }
257 
258  const TRGClock&
260  {
261  _clock = & c;
262 
263  for (unsigned i = 0; i < size(); i++) {
264  TRGSignalVector& t = * (* this)[i];
265  t.clock(c);
266  }
267 
268  return * _clock;
269  }
270 
271  TRGSignal
273  {
274 
275  //...Get state information...
276  const vector<int> states = stateChanges();
277  const unsigned nStates = states.size();
278 
279  //...Output...
280  TRGSignal ored;
281 
282  //...Loop over all states...
283  // vector<TRGState *> outputStates;
284  for (unsigned i = 0; i < nStates; i++) {
285  // if (TRGDebug::level())
286  // cout << TRGDebug::tab() << "Clock=" << states[i] << endl;
287 
288  // if (active(states[i])) {
289  // TRGSignal p(* _clock,
290 
291  // }
292 
293 
294  cout << "TRGSginalBundle::ored !!! not completed yet" << endl;
295  }
296 
297  return TRGSignal();
298  }
299 
300  void
301  TRGSignalBundle::dumpCOE(const string& fnIn, int start , int stop) const
302  {
303 
304  string fn = fnIn;
305 
306  if (fnIn.size() == 0)
307  fn = name();
308 
309  //...Open a file to output...
310  ofstream file(fn + ".coe", ios::out);
311  if (! file.is_open()) {
312  cout << "!!! " << name() << " can not open file : " << fn << endl;
313  return;
314  }
315 
316  // //...Determine start clock to output...
317  // int clk0 = _clock->max();
318  // vector<int> sc = stateChanges();
319  // for (unsigned i = 0; i < sc.size(); i++) {
320  // if (state(sc[i]).active()) {
321  // clk0 = sc[i];
322  // break;
323  // }
324  // }
325 
326  // //...Determine end clock to output...
327  // int clk1 = _clock->min();
328  // for (unsigned i = 0; i < sc.size(); i++) {
329  // if (sc[i] < clk0)
330  // continue;
331 
332  // if (state(sc[i]).active()) {
333  // clk1 = sc[i];
334  // }
335  // }
336 
337  // //...No active case...
338  // if (clk0 > clk1) {
339  // clk0 = 0;
340  // clk1 = 1;
341  // }
342 
343  // if (TRGDebug::level()) {
344  // cout << TRGDebug::tab() << "SignalBundle::dumpCOE : " << name()
345  // << endl;
346  // cout << TRGDebug::tab() << " clock region to output" << endl;
347  // cout << TRGDebug::tab() << " start=" << clk0 << endl;
348  // cout << TRGDebug::tab() << " end =" << clk1 << endl;
349  // }
350 
351  // //...32 clocks as default : 2016/04...
352  // clk0 = 0;
353  // clk1 = 31;
354 
355  int clk0 = start;
356  int clk1 = stop;
357 
358  TRGState tmp = state(0);
359  const TRGSignalVector& cc = _clock->clockCounter();
360 
361  file << "; " << name() << endl;
362  file << "; generated at " << TRGUtil::dateString() << endl;
363  file << "; start clock = " << clk0 << endl;
364  file << "; end clock = " << clk1 << endl;
365  file << "; bit size = " << tmp.size() << " + clock counter(" << cc.size()
366  << ")" << endl;
367  file << ";" << endl;
368  file << "memory_initialization_radix=2;" << endl;
369  file << "memory_initialization_vector=" << endl;
370 
371  for (int i = clk0; i <= clk1; i++) {
372  TRGState sd = state(i);
373  TRGState sc = cc.state(i);
374  file << sc;
375  file << sd;
376  if (i == clk1)
377  file << ";" << endl;
378  else
379  file << "," << endl;
380  }
381 
382  file.close();
383 
384  }
385 
387 } // namespace Belle2
A class to represent a digitized signal. Unit is nano second.
Definition: Clock.h:38
A class to represent a bundle of SignalVectors.
Definition: SignalBundle.h:26
const TRGClock * _clock
Clock.
Definition: SignalBundle.h:106
std::string _name
Name.
Definition: SignalBundle.h:103
A class to represent a bundle of digitized signals.
Definition: SignalVector.h:26
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:23
A class to represent a state of multi bits.
Definition: State.h:24
static std::string tab(void)
returns tab spaces.
Definition: Debug.cc:47
const TRGSignalVector & set(const TRGState &, int clockPosition)
sets state at given clock.
virtual ~TRGSignalBundle()
Destructor.
TRGState state(int clockPosition) const
returns state at given clock position.
const TRGClock & clock(void) const
returns clock.
Definition: SignalBundle.h:127
const std::string & name(void) const
returns name.
Definition: SignalBundle.h:113
std::vector< int > stateChanges(void) const
returns a list of clock position of state change.
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
Definition: Debug.cc:24
int min(void) const
returns min. clock point.
Definition: Clock.h:194
TRGSignalBundle(const TRGClock &)
Default constructor.
Definition: SignalBundle.cc:32
TRGSignal ored(void) const
returns signal of all ORed.
static int level(void)
returns the debug level.
Definition: Debug.cc:67
bool active(void) const
returns true if there is a signal.
const TRGSignalVector & clockCounter(void) const
returns the clock counter.
Definition: Clock.cc:166
static void leaveStage(const std::string &stageName)
Declare that you leave a stage.
Definition: Debug.cc:34
void dumpCOE(const std::string &fileName="", int start=0, int stop=0) const
makes coe output.
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Abstract base class for different kinds of events.