HAL  v4.5.0-133-g64838ea8d
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
selection_relay.cpp
Go to the documentation of this file.
2 
3 #include "gui/gui_globals.h"
6 #include "hal_core/netlist/net.h"
9 
10 namespace hal
11 {
12  // SET VIA SETTINGS OR TOOLBUTTON
13  bool SelectionRelay::sNavigationSkipsEnabled = false;
14 
16  mAction(nullptr), mDisableExecution(false),
17  mFocusType(ItemType::None), mSubfocus(Subfocus::None)
18  {
19  clear();
20  }
21 
23  {
24  initializeAction();
25  mAction->mModules.clear();
26  mAction->mGates.clear();
27  mAction->mNets.clear();
28  mAction->mObject = UserActionObject();
29  mAction->mSubfocus = Subfocus::None;
30  mAction->mSubfocusIndex = 0;
31  mModulesSuppressedByFilter.clear();
32  mGatesSuppressedByFilter.clear();
33  mNetsSuppressedByFilter.clear();
34  }
35 
36  void SelectionRelay::initializeAction()
37  {
38  if (!mAction)
39  {
40  mAction = new ActionSetSelectionFocus;
41  mAction->mModules = mSelectedModules;
42  mAction->mGates = mSelectedGates;
43  mAction->mNets = mSelectedNets;
44  mAction->mObject = UserActionObject(
45  mFocusId,
47  mAction->mSubfocus = mSubfocus;
48  mAction->mSubfocusIndex = mSubfocusIndex;
49  }
50  }
51 
52  void SelectionRelay::executeAction()
53  {
54  if (!mAction || mDisableExecution) return;
55 
56  mDisableExecution = true;
57  if (mAction->hasModifications())
58  mAction->exec();
59  else
60  delete mAction;
61  mAction = nullptr;
62  mDisableExecution = false;
63  }
64 
66  {
67  initializeAction();
68  mAction->mGates.insert(id);
69  }
70 
72  {
73  initializeAction();
74  mAction->mNets.insert(id);
75  }
76 
78  {
79  initializeAction();
80  mAction->mModules.insert(id);
81  }
82 
84  {
86  for (u32 id : mSelectedModules)
88  for (u32 id : mSelectedGates)
90  for (u32 id : mSelectedNets)
92  return retval;
93  }
94 
96  {
97  initializeAction();
98  mAction->mGates = ids;
99  }
100 
102  {
103  initializeAction();
104  mAction->mNets = ids;
105  }
106 
108  {
109  initializeAction();
110  mAction->mModules = ids;
111  }
112 
113  void SelectionRelay::actionSetSelected(const QSet<u32>& mods, const QSet<u32>& gats, const QSet<u32>& nets)
114  {
115  mSelectedModules = mods;
116  mSelectedGates = gats;
117  mSelectedNets = nets;
118  Q_EMIT selectionChanged(nullptr);
119  }
120 
122  {
123  initializeAction();
124  mAction->mGates.remove(id);
125  }
126 
128  {
129  initializeAction();
130  mAction->mNets.remove(id);
131  }
132 
134  {
135  initializeAction();
136  mAction->mModules.remove(id);
137  }
138 
139  void SelectionRelay::setFocus(ItemType ftype, u32 fid, Subfocus sfoc, u32 sfinx)
140  {
141  initializeAction();
143  mAction->mSubfocus = sfoc;
144  mAction->mSubfocusIndex = sfinx;
145  }
146 
148  {
149  mFocusType = ftype;
150  mFocusId = fid;
151  mSubfocus = sfoc;
152  mSubfocusIndex = sfinx;
153  }
154 
156  {
157  clear();
158  executeAction();
159  }
160 
162  {
163  mSenderRegister.append(QPair<void*, QString>(sender, name));
164  }
165 
166  void SelectionRelay::removeSender(void* sender)
167  {
168  for (QPair<void*, QString> pair : mSenderRegister)
169  {
170  if (pair.first == sender)
171  mSenderRegister.removeOne(pair);
172  }
173  }
174 
176  {
177 #ifdef HAL_STUDY
178  evaluateSelectionChanged(sender);
179 #else
180  Q_UNUSED(sender);
181 #endif
182  executeAction();
183  }
184 
186  {
188  executeAction();
189  }
190 
191  // TODO deduplicate navigateUp and navigateDown
193  {
194  u32 size = 0;
195 
196  switch (mFocusType)
197  {
198  case ItemType::None: {
199  return;
200  }
201  case ItemType::Gate: {
202  Gate* g = gNetlist->get_gate_by_id(mFocusId);
203 
204  if (!g)
205  return;
206 
207  if (mSubfocus == Subfocus::Left)
208  {
209  size = g->get_type()->get_input_pins().size();
210 
211  if (!size) // CHECK NECESSARY ???
212  return; // INVALID STATE, FIX OR IGNORE ???
213 
214  break;
215  }
216 
217  if (mSubfocus == Subfocus::Right)
218  {
219  size = g->get_type()->get_output_pins().size();
220 
221  if (!size) // CHECK NECESSARY ???
222  return; // INVALID STATE, FIX OR IGNORE ???
223 
224  break;
225  }
226 
227  return;
228  }
229  case ItemType::Net: {
230  Net* n = gNetlist->get_net_by_id(mFocusId);
231 
232  if (!n)
233  return;
234 
235  if (mSubfocus == Subfocus::Right)
236  {
237  size = n->get_destinations().size();
238 
239  if (!size) // CHECK NECESSARY ???
240  return; // INVALID STATE, FIX OR IGNORE ???
241 
242  break;
243  }
244 
245  return;
246  }
247  case ItemType::Module: {
248  Module* m = gNetlist->get_module_by_id(mFocusId);
249 
250  if (!m)
251  return;
252 
253  if (mSubfocus == Subfocus::Left)
254  {
255  size = m->get_input_nets().size();
256 
257  if (!size) // CHECK NECESSARY ???
258  return; // INVALID STATE, FIX OR IGNORE ???
259 
260  break;
261  }
262 
263  if (mSubfocus == Subfocus::Right)
264  {
265  size = m->get_output_nets().size();
266 
267  if (!size) // CHECK NECESSARY ???
268  return; // INVALID STATE, FIX OR IGNORE ???
269 
270  break;
271  }
272 
273  return;
274  }
275  }
276 
277  initializeAction();
278  if (mSubfocusIndex == 0)
279  mAction->mSubfocusIndex = size - 1;
280  else
281  --mAction->mSubfocusIndex;
282  relaySubfocusChanged(nullptr);
283  }
284 
286  {
287  u32 size = 0;
288 
289  switch (mFocusType)
290  {
291  case ItemType::None: {
292  return;
293  }
294  case ItemType::Gate: {
295  Gate* g = gNetlist->get_gate_by_id(mFocusId);
296 
297  if (!g)
298  return;
299 
300  if (mSubfocus == Subfocus::Left)
301  {
302  size = g->get_type()->get_input_pins().size();
303 
304  if (!size) // CHECK NECESSARY ???
305  return; // INVALID STATE, FIX OR IGNORE ???
306 
307  break;
308  }
309 
310  if (mSubfocus == Subfocus::Right)
311  {
312  size = g->get_type()->get_output_pins().size();
313 
314  if (!size) // CHECK NECESSARY ???
315  return; // INVALID STATE, FIX OR IGNORE ???
316 
317  break;
318  }
319 
320  return;
321  }
322  case ItemType::Net: {
323  Net* n = gNetlist->get_net_by_id(mFocusId);
324 
325  if (!n)
326  return;
327 
328  if (mSubfocus == Subfocus::Right)
329  {
330  size = n->get_destinations().size();
331 
332  if (!size) // CHECK NECESSARY ???
333  return; // INVALID STATE, FIX OR IGNORE ???
334 
335  break;
336  }
337 
338  return;
339  }
340  case ItemType::Module: {
341  Module* m = gNetlist->get_module_by_id(mFocusId);
342 
343  if (!m)
344  return;
345 
346  if (mSubfocus == Subfocus::Left)
347  {
348  size = m->get_input_nets().size();
349 
350  if (!size) // CHECK NECESSARY ???
351  return; // INVALID STATE, FIX OR IGNORE ???
352 
353  break;
354  }
355 
356  if (mSubfocus == Subfocus::Right)
357  {
358  size = m->get_output_nets().size();
359 
360  if (!size) // CHECK NECESSARY ???
361  return; // INVALID STATE, FIX OR IGNORE ???
362 
363  break;
364  }
365 
366  return;
367  }
368  }
369 
370  initializeAction();
371  if (mSubfocusIndex == size - 1)
372  mAction->mSubfocusIndex = 0;
373  else
374  ++mAction->mSubfocusIndex;
375  relaySubfocusChanged(nullptr);
376  }
377 
379  {
380  return mSelectedModules.contains(id) && !mModulesSuppressedByFilter.contains(id);
381  }
382 
384  {
385  return mSelectedGates.contains(id) && !mGatesSuppressedByFilter.contains(id);
386  }
387 
389  {
390  return mSelectedNets.contains(id) && !mNetsSuppressedByFilter.contains(id);
391  }
392 
394  {
395  auto it = mSelectedModules.find(id);
396  if (it != mSelectedModules.end())
397  {
398  initializeAction();
399  mAction->mModules.remove(id);
400  executeAction();
401  }
402  }
403 
405  {
406  auto it = mSelectedGates.find(id);
407  if (it != mSelectedGates.end())
408  {
409  initializeAction();
410  mAction->mGates.remove(id);
411  executeAction();
412  }
413  }
414 
416  {
417  auto it = mSelectedNets.find(id);
418  if (it != mSelectedNets.end())
419  {
420  initializeAction();
421  mAction->mNets.remove(id);
422  executeAction();
423  }
424  }
425 
426  void SelectionRelay::followModuleInputPin(Module* m, u32 input_pin_index)
427  {
428  Q_UNUSED(m)
429  Q_UNUSED(input_pin_index)
430  // TODO implement
431  }
432 
433  void SelectionRelay::followModuleOutputPin(Module* m, u32 output_pin_index)
434  {
435  Q_UNUSED(m)
436  Q_UNUSED(output_pin_index)
437  // TODO implement
438  }
439 
440  void SelectionRelay::subfocusNone()
441  {
442  initializeAction();
443  mAction->mSubfocus = Subfocus::None;
444  mAction->mSubfocusIndex = 0;
445  relaySubfocusChanged(nullptr);
446  }
447 
448  void SelectionRelay::subfocusLeft()
449  {
450  initializeAction();
451  mAction->mSubfocus = Subfocus::Left;
452  mAction->mSubfocusIndex = 0;
453  relaySubfocusChanged(nullptr);
454  }
455 
456  void SelectionRelay::subfocusRight()
457  {
458  mSubfocus = Subfocus::Right;
459  mSubfocusIndex = 0;
460 
461  Q_EMIT subfocusChanged(nullptr);
462  }
463 
465  {
466  QList<Node> retval;
467  for (u32 mid : selectedModulesList())
468  retval.append(Node(mid,Node::Module));
469  for (u32 gid : selectedGatesList())
470  retval.append(Node(gid,Node::Gate));
471  return retval;
472  }
473 
474 #ifdef HAL_STUDY
475  void SelectionRelay::evaluateSelectionChanged(void *sender)
476  {
477  QString method = "unknown";
478  for(const auto pair : mSenderRegister)
479  {
480  if(pair.first == sender)
481  {
482  method = pair.second;
483  break;
484  }
485  }
486 
487  auto createSubstring = [](std::string first_part, QSet<u32> ids){
488 
489  std::string final_string = first_part;
490  for(const auto &i : ids)
491  final_string += std::to_string(i) + ", ";
492 
493  if(!ids.isEmpty())
494  final_string.resize(final_string.size()-2);
495 
496  return final_string + "}";
497  };
498 
499  std::string gateIdsSubstring = createSubstring("Gate-Ids: {", mSelectedGates);
500  std::string netIdsSubstring = createSubstring("Net-Ids: {", mSelectedNets);
501  std::string moduleIdsSubstring = createSubstring("Module-Ids: {", mSelectedModules);
502  log_info("UserStudy", "Selection changed, Method: {}, New Sel.: {}, {}, {}", method.toStdString(), gateIdsSubstring, netIdsSubstring, moduleIdsSubstring);
503 
504 
505  }
506 #endif
507 } // namespace hal
u32 size
Set the selection and focus.
void setObject(const UserActionObject &obj) override
Definition: gate.h:58
GateType * get_type() const
Definition: gate.cpp:125
std::vector< GatePin * > get_output_pins() const
Definition: gate_type.cpp:285
std::vector< GatePin * > get_input_pins() const
Definition: gate_type.cpp:269
const std::unordered_set< Net * > & get_input_nets() const
Definition: module.cpp:549
const std::unordered_set< Net * > & get_output_nets() const
Definition: module.cpp:554
Definition: net.h:58
std::vector< Endpoint * > get_destinations(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
Definition: net.cpp:450
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:195
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:615
Net * get_net_by_id(u32 net_id) const
Definition: netlist.cpp:355
The Node class object represents a module or a gate.
Definition: gui_def.h:61
@ Module
Definition: gui_def.h:63
@ Gate
Definition: gui_def.h:63
QList< UserActionObject > toUserActionObject() const
void setFocus(ItemType ftype, u32 fid, Subfocus sfoc=Subfocus::None, u32 sfinx=0)
void selectionChanged(void *sender)
bool isNetSelected(u32 id) const
void relaySelectionChanged(void *sender)
void removeSender(void *sender)
void relaySubfocusChanged(void *sender)
QList< Node > selectedNodesList() const
QList< u32 > selectedModulesList() const
void setFocusDirect(ItemType ftype, u32 fid, Subfocus sfoc=Subfocus::None, u32 sfinx=0)
void handleNetRemoved(const u32 id)
void actionSetSelected(const QSet< u32 > &mods, const QSet< u32 > &gats, const QSet< u32 > &nets)
QList< u32 > selectedGatesList() const
void handleGateRemoved(const u32 id)
void handleModuleRemoved(const u32 id)
void setSelectedGates(const QSet< u32 > &ids)
SelectionRelay(QObject *parent=nullptr)
bool isModuleSelected(u32 id) const
void setSelectedNets(const QSet< u32 > &ids)
void subfocusChanged(void *sender)
void registerSender(void *sender, QString name)
bool isGateSelected(u32 id) const
void setSelectedModules(const QSet< u32 > &ids)
UserActionObject mObject
Definition: user_action.h:183
The UserActionObject class represents a single object used in UserAction.
static ObjectType fromSelectionType(SelectionRelay::ItemType itp)
uint32_t u32
Definition: defines.h:41
#define log_info(channel,...)
Definition: log.h:70
Definition: defines.h:45
Netlist * gNetlist
Definition: gui_globals.h:69
std::string name
void append(const T &value)
Q_EMITQ_EMIT
QObject * sender() const const
void clear()
bool contains(const T &value) const const
QSet::iterator end()
QSet::iterator find(const T &value)
QSet::iterator insert(const T &value)
bool remove(const T &value)
void resize(int size)
std::string toStdString() const const
void append(const T &value)