HAL  v4.5.0-311-gd7fdf7b20
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
simulation_input.cpp
Go to the documentation of this file.
3 #include "hal_core/netlist/net.h"
6 #include <algorithm>
7 #include <cctype>
8 #include <stdio.h>
9 #include <tuple>
10 #include <unordered_map>
11 #include <unordered_set>
12 
13 namespace hal {
14 
15  std::vector<const Net *> SimulationInput::NetGroup::get_nets() const
16  {
17  std::vector<const Net *> retval;
18  if (gate)
19  {
20  for (GatePin* gp : gate_pin_group->get_pins())
21  {
22  Net* n = nullptr;
23  if (gp->get_direction() == PinDirection::input || gp->get_direction() == PinDirection::inout)
24  n = gate->get_fan_in_net(gp);
25  else
26  n = gate->get_fan_out_net(gp);
27  if (n) retval.push_back(n);
28  }
29  }
30  else
31  {
32  for (ModulePin* mp : module_pin_group->get_pins())
33  retval.push_back(mp->get_net());
34  }
35  return retval;
36  }
37 
39  {
40  if (gate)
41  return gate_pin_group->get_name();
42  return module_pin_group->get_name();
43  }
44 
45  bool SimulationInput::contains_gate(const Gate* g) const
46  {
47  return (mSimulationSet.find(g) != mSimulationSet.end());
48  }
49 
50  void SimulationInput::add_gates(const std::vector<Gate*>& gates)
51  {
52  mSimulationSet.insert(gates.begin(), gates.end());
53 
54  compute_input_nets();
55  compute_output_nets();
56  compute_partial_nets();
57  }
58 
59  const std::unordered_set<const Gate*>& SimulationInput::get_gates() const
60  {
61  return mSimulationSet;
62  }
63 
64  bool SimulationInput::is_clock(const Net* n) const
65  {
66  for (const Clock& c: m_clocks)
67  if (c.clock_net == n) return true;
68  return false;
69  }
70 
72  {
73  m_clocks.push_back(clk);
74  }
75 
77  {
78  mSimulationSet.clear();
79  m_clocks.clear();
80  m_input_nets.clear();
81  m_output_nets.clear();
82  m_partial_nets.clear();
83  m_netgroups.clear();
84  }
85 
87  {
88  return has_gates() && (!m_clocks.empty() || mNoClockUsed) && !m_input_nets.empty();
89  }
90 
92  {
93  return !mSimulationSet.empty();
94  }
95 
96  const std::unordered_set<const Net *>& SimulationInput::get_input_nets() const
97  {
98  return m_input_nets;
99  }
100 
101  const std::vector<const Net*>& SimulationInput::get_output_nets() const
102  {
103  return m_output_nets;
104  }
105 
106  const std::vector<const Net*>& SimulationInput::get_partial_netlist_nets() const
107  {
108  return m_partial_nets;
109  }
110 
111  bool SimulationInput::is_input_net(const Net* n) const
112  {
113  return (m_input_nets.find(n) != m_input_nets.end());
114  }
115 
117  {
118  mNoClockUsed = true;
119  }
120 
122  {
123  return mNoClockUsed;
124  }
125 
126  void SimulationInput::dump(std::string filename) const
127  {
128  FILE* of = stderr;
129  if (!filename.empty())
130  if (!(of = fopen(filename.c_str(),"w")))
131  {
132  log_warning("SimulationInput", "cannot open simulation input dump file '{}'", filename);
133  return;
134  }
135 
136  fprintf(of, "Gates:______________________________________\n");
137  for (const Gate* g: mSimulationSet)
138  {
139  fprintf(of, " %4d <%s>\n", g->get_id(), g->get_name().c_str());
140  }
141  fprintf(of, "Clocks:_____________________________________\n");
142  for (const Clock& clk: m_clocks)
143  {
144  fprintf(of, " %4d <%s> \t period: %u \n", clk.clock_net->get_id(), clk.clock_net->get_name().c_str(), (unsigned int) clk.period());
145  }
146  fprintf(of, "Input nets:_________________________________\n");
147  for (const Net* n: m_input_nets)
148  {
149  fprintf(of, " %4d <%s>\n", n->get_id(), n->get_name().c_str());
150  }
151  fprintf(of, "Output nets:________________________________\n");
152  for (const Net* n: m_output_nets)
153  {
154  fprintf(of, " %4d <%s>\n", n->get_id(), n->get_name().c_str());
155  }
156  fprintf(of, "Partial netlist nets:________________________________\n");
157  for (const Net* n: m_partial_nets)
158  {
159  fprintf(of, " %4d <%s>\n", n->get_id(), n->get_name().c_str());
160  }
161  if (filename.empty())
162  fflush(of);
163  else
164  fclose(of);
165  }
166 
167  void SimulationInput::compute_input_nets()
168  {
169  m_input_nets.clear();
170  for (auto gate : mSimulationSet)
171  {
172  for (auto net : gate->get_fan_in_nets())
173  {
174  // "input net" is either a global input...
175  if (net->is_global_input_net())
176  {
177  m_input_nets.insert(net);
178  }
179  else // ... or has a source outside of the simulation set
180  {
181  int number_sources = 0;
182  for (auto src : net->get_sources())
183  {
184  ++number_sources;
185  if (!contains_gate(src->get_gate()))
186  {
187  m_input_nets.insert(net);
188  break;
189  }
190  }
191  if (!number_sources)
192  m_input_nets.insert(net);
193  }
194  }
195  }
196  }
197 
198  void SimulationInput::compute_output_nets()
199  {
200  m_output_nets.clear();
201  for (auto gate : mSimulationSet)
202  {
203  for (auto net : gate->get_fan_out_nets())
204  {
205  // "output net" is either a global output...
206  if (net->is_global_output_net())
207  {
208  m_output_nets.push_back(net);
209  }
210  else // ... or has a destination outside of the simulation set
211  {
212  int number_destinations = 0;
213  for (auto dst : net->get_destinations())
214  {
215  ++number_destinations;
216  if (!contains_gate(dst->get_gate()))
217  {
218  m_output_nets.push_back(net);
219  break;
220  }
221  }
222  if (!number_destinations)
223  m_output_nets.push_back(net);
224  }
225  }
226  }
227  }
228 
229  void SimulationInput::compute_partial_nets()
230  {
231  m_partial_nets.clear();
232  std::unordered_set<const Net*> found;
233  for (const Gate* g : mSimulationSet)
234  {
235  for (const Net* n : g->get_fan_in_nets())
236  {
237  if (found.find(n) == found.end())
238  {
239  found.insert(n);
240  m_partial_nets.push_back(n);
241  }
242  }
243  for (const Net* n : g->get_fan_out_nets())
244  {
245  if (found.find(n) == found.end())
246  {
247  found.insert(n);
248  m_partial_nets.push_back(n);
249  }
250  }
251  }
252  }
253 
254  namespace
255  {
256  // A pin group whose nets are all part of the simulation, competing for its nets with other pin groups.
257  struct NetGroupCandidate
258  {
259  SimulationInput::NetGroup group;
260  std::vector<const Net*> nets;
261  int rank; // 0: module source group, 1: other module group, 2: gate source group, 3: other gate group
262  std::string sort_name; // lower-case pin group name
263  std::string name;
264  u32 owner_id;
265  u32 pin_group_id;
266  };
267 
268  SimulationInput::DirectionType group_direction(const std::vector<const Net*>& nets, const std::unordered_map<const Net*, SimulationInput::DirectionType>& simulated_nets)
269  {
271  for (const Net* n : nets)
272  {
273  SimulationInput::DirectionType dir = simulated_nets.at(n);
274  if (retval == SimulationInput::Undefined)
275  retval = dir;
276  else if (retval != SimulationInput::Mixed && retval != dir)
277  retval = SimulationInput::Mixed;
278  }
279  return retval;
280  }
281 
282  std::string to_lower(std::string s)
283  {
284  std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
285  return s;
286  }
287  } // namespace
288 
290  {
291  m_netgroups.clear();
292 
293  // all nets that are part of the simulation
294  std::unordered_map<const Net*, DirectionType> simulated_nets;
295 
296  for (const Net* n : m_partial_nets) simulated_nets[n] = Undefined;
297  for (const Net* n : m_input_nets) simulated_nets[n] = InputOnly;
298  for (const Net* n : m_output_nets) simulated_nets[n] = OutputOnly;
299 
300  // all modules that contain at least one simulated gate
301  std::unordered_set<const Module*> simulated_modules;
302  for (const Gate* g : mSimulationSet)
303  {
304  const Module *m = g->get_module();
305  while (m)
306  {
307  simulated_modules.insert(m);
308  m = m->get_parent_module();
309  }
310  }
311 
312  std::vector<NetGroupCandidate> candidates;
313 
314  for (const Module* m : simulated_modules)
315  {
316  for (PinGroup<ModulePin>* pg : m->get_pin_groups())
317  {
318  if (pg->size() < 2 || pg->size() > 31) continue;
319  bool pin_group_simulated = true;
320  bool is_source = true;
321  std::vector<const Net*> nets;
322 
323  for (ModulePin* mp : pg->get_pins())
324  {
325  if (mp->get_direction() != PinDirection::output)
326  is_source = false;
327  const Net* n = mp->get_net();
328  if (!n) continue;
329  if (simulated_nets.find(n) == simulated_nets.end())
330  {
331  // pin : net exists and is not part of the simulation, ignore pin group
332  pin_group_simulated = false;
333  break;
334  }
335  nets.push_back(n);
336  }
337  if (!pin_group_simulated || nets.empty()) continue;
338 
339  NetGroupCandidate cand;
340  cand.group.module_pin_group = pg;
341  cand.group.direction = group_direction(nets, simulated_nets);
342  cand.group.ascending = pg->is_ascending();
343  cand.nets = nets;
344  cand.rank = is_source ? 0 : 1;
345  cand.name = pg->get_name();
346  cand.sort_name = to_lower(cand.name);
347  cand.owner_id = m->get_id();
348  cand.pin_group_id = pg->get_id();
349  candidates.push_back(cand);
350  }
351  }
352 
353  for (const Gate* g : mSimulationSet)
354  {
355  for (PinGroup<GatePin>* pg : g->get_type()->get_pin_groups())
356  {
357  if (pg->size() < 2 || pg->size() > 31) continue;
358  bool pin_group_simulated = true;
359  bool is_source = true;
360  std::vector<const Net*> nets;
361 
362  for (GatePin* gp : pg->get_pins())
363  {
364  if (gp->get_direction() != PinDirection::output)
365  is_source = false;
366  Net* n = nullptr;
367  switch (gp->get_direction())
368  {
369  case PinDirection::inout:
370  n = g->get_fan_in_net(gp);
371  break;
373  n = g->get_fan_out_net(gp);
374  break;
375  default:
376  break;
377  }
378  if (!n || simulated_nets.find(n) == simulated_nets.end())
379  {
380  pin_group_simulated = false;
381  break;
382  }
383  nets.push_back(n);
384  }
385  if (!pin_group_simulated) continue;
386 
387  NetGroupCandidate cand;
388  cand.group.gate = g;
389  cand.group.gate_pin_group = pg;
390  cand.group.direction = group_direction(nets, simulated_nets);
391  cand.group.ascending = pg->is_ascending();
392  cand.nets = nets;
393  cand.rank = is_source ? 2 : 3;
394  cand.name = pg->get_name();
395  cand.sort_name = to_lower(cand.name);
396  cand.owner_id = g->get_id();
397  cand.pin_group_id = pg->get_id();
398  candidates.push_back(cand);
399  }
400  }
401 
402  // pin groups sharing nets: module groups before gate groups, then source groups (all pins are outputs), then the alphabetically first name
403  std::sort(candidates.begin(), candidates.end(), [](const NetGroupCandidate& a, const NetGroupCandidate& b) {
404  return std::tie(a.rank, a.sort_name, a.name, a.owner_id, a.pin_group_id) < std::tie(b.rank, b.sort_name, b.name, b.owner_id, b.pin_group_id);
405  });
406 
407  std::unordered_set<const Net*> grouped_nets;
408  for (const NetGroupCandidate& cand : candidates)
409  {
410  if (std::any_of(cand.nets.begin(), cand.nets.end(), [&grouped_nets](const Net* n) { return grouped_nets.find(n) != grouped_nets.end(); }))
411  continue;
412  grouped_nets.insert(cand.nets.begin(), cand.nets.end());
413  m_netgroups.push_back(cand.group);
414  }
415  }
416 }
Definition: gate.h:59
Net * get_fan_in_net(const std::string &pin_name) const
Definition: gate.cpp:852
Net * get_fan_out_net(const std::string &pin_name) const
Definition: gate.cpp:996
Module * get_parent_module() const
Definition: module.cpp:126
Definition: net.h:58
PinType get_type() const
Definition: pin_group.h:173
const std::vector< const Net * > & get_partial_netlist_nets() const
const std::vector< const Net * > & get_output_nets() const
void dump(std::string filename=std::string()) const
bool is_input_net(const Net *n) const
bool is_clock(const Net *n) const
const std::unordered_set< const Net * > & get_input_nets() const
void add_gates(const std::vector< Gate * > &gates)
bool contains_gate(const Gate *g) const
const std::unordered_set< const Gate * > & get_gates() const
void add_clock(const Clock &clk)
uint32_t u32
Definition: defines.h:41
#define log_warning(channel,...)
Definition: log.h:76
T to_lower(const T &s)
Definition: utils.h:523
Definition: defines.h:45
Net * net
std::string name
PinGroup< GatePin > * gate_pin_group
PinGroup< ModulePin > * module_pin_group
std::vector< const Net * > get_nets() const