HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
module_model.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
29 #include "gui/gui_utils/sort.h"
32 
33 
34 #include <QAbstractItemModel>
35 #include <QModelIndex>
36 #include <QVariant>
37 #include <set>
38 #include <array>
39 #include <vector>
40 #include <QTextStream>
41 
42 namespace hal
43 {
44 
53  class ModuleModel : public BaseTreeModel
54  {
55  friend class ModuleItem;
56  Q_OBJECT
57 
61  class TempGateAssignment
62  {
63  int mAccumulate;
64  public:
65  QHash<u32, Module*> mGateRemove;
66  QHash<u32, Module*> mGateAssign;
67 
68  TempGateAssignment() : mAccumulate(0) {;}
69  void removeGateFromModule(u32 gateId, Module* m)
70  {
71  if (!mGateRemove.contains(gateId))
72  mGateRemove[gateId] = m;
73  }
74  void assignGateToModule(u32 gateId, Module* m)
75  {
76  mGateAssign[gateId] = m;
77  }
78  bool isAccumulate() const { return mAccumulate > 0; }
79  void beginAccumulate() { mAccumulate++; }
80  void endAccumulate() { mAccumulate--; }
81  };
82 
83  public:
90  explicit ModuleModel(QObject* parent = nullptr);
91 
95  ~ModuleModel();
96 
97  // === Pure Virtual ===
98 
106  QVariant data(const QModelIndex& index, int role) const override;
107 
108  // === Virtual ===
115  Qt::ItemFlags flags(const QModelIndex& index) const override;
116 
117  // === Others ===
118 
125  ModuleItem* getItem(const QModelIndex& index) const;
126 
135 
144 
148  void clear() override;
149 
150  QMimeData* mimeData(const QModelIndexList &indexes) const override;
151 
163  void populateTree(const QVector<u32>& modIds = {}, const QVector<u32>& gatIds = {}, const QVector<u32>& netIds = {});
164 
172  void populateFromGatelist(const std::vector<Gate*>& gates);
173 
182  void setShowNetsOutsideModules(bool enable);
183 
187  bool showNetsOutsideModules() const;
188 
195  void addModule(const u32 id, const u32 parentId);
196 
203  void addGate(const u32 id, const u32 parentId);
204 
211  void addNet(const u32 id, const u32 parentId);
212 
222  void addRecursively(const Module* module, BaseTreeItem* parentItem = nullptr);
223 
229  void removeModule(const u32 id);
230 
236  void removeGate(const u32 id);
237 
243  void removeNet(const u32 id);
244 
249  void moduleAssignGate(const u32 moduleId, const u32 gateId);
250 
255  void moduleAssignNets(const QList<u32>& gateIds = QList<u32>());
256 
267  void updateNetParent(const Net* net, const QHash<const Net*,ModuleItem*>* parentAssignment = nullptr);
268 
275  void updateModuleParent(const Module* module);
276 
282  void updateModuleName(const u32 id);
283 
289  void updateModuleType(const u32 id);
290 
296  void updateGateName(const u32 id);
297 
303  void updateNetName(const u32 id);
304 
312  bool isModifying();
313 
314  private Q_SLOTS:
315  void handleModuleNameChanged(Module* mod);
316 
317  void handleModuleTypeChanged(Module* mod);
318 
319  void handleModuleRemoved(Module* mod);
320 
321  void handleModuleCreated(Module* mod);
322 
330  void handleModuleParentChanged(const Module* mod);
331 
332  void handleModuleSubmoduleAdded(Module* mod, u32 submodId);
333 
334  void handleModuleSubmoduleRemoved(Module* mod, u32 submodId);
335 
336  void handleModuleGateAssigned(Module* mod, u32 gateId);
337 
338  void handleModuleGateRemoved(Module* mod, u32 gateId);
339 
340  void handleModuleGatesAssignBegin(Module* mod, u32 numberGates);
341 
342  void handleModuleGatesAssignEnd(Module* mod, u32 numberGates);
343 
344  void handleGateCreated(Gate* gat);
345 
346  void handleGateNameChanged(Gate* gat);
347 
348  void handleGateRemoved(Gate* gat);
349 
350  void handleNetCreated(Net* net);
351 
352  void handleNetNameChanged(Net* net);
353 
354  void handleNetRemoved(Net* net);
355 
356  void handleNetUpdated(Net* net, u32 data);
357 
365  void handleNetGlobalStateChanged(Netlist* netlist, u32 netId);
366 
367  protected:
375  ModuleItem* createChildItem(u32 id, ModuleItem::TreeItemType itemType, BaseTreeItem* parentItem = nullptr);
376 
383  void removeChildItem(ModuleItem* itemToRemove, BaseTreeItem* parentItem);
384 
388  void setIsModifying(bool pIsModifying);
389 
390  private:
400  Module* findNetParent(const Net* net) const;
401 
408  void findNetParentRecursion(BaseTreeItem* parent, QHash<const Net*,ModuleItem*>& parentAssignment, std::unordered_set<Net*>& assignedNets) const;
409 
416  void updateNetOutsideModules(u32 netId, bool outside);
417 
418  QMultiMap<u32, ModuleItem*> mModuleMap;
421  QMultiMap<u32, ModuleItem*>* mModuleItemMaps[3] = {&mModuleMap, &mGateMap, &mNetMap};;
422 
423  bool mIsModifying;
424 
430  bool mResetInProgress;
431 
433  bool mShowNetsOutsideModules;
434 
435  TempGateAssignment mTempGateAssignment;
436  };
437 } // namespace hal
The BaseTreeModel implements generic standard functions of a tree model.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
An item in the ModuleModel.
Definition: module_item.h:48
A model for displaying multiple netlist elements.
Definition: module_model.h:54
void updateModuleParent(const Module *module)
Qt::ItemFlags flags(const QModelIndex &index) const override
void updateNetName(const u32 id)
void setShowNetsOutsideModules(bool enable)
void updateModuleType(const u32 id)
void addRecursively(const Module *module, BaseTreeItem *parentItem=nullptr)
void addNet(const u32 id, const u32 parentId)
void removeGate(const u32 id)
void moduleAssignNets(const QList< u32 > &gateIds=QList< u32 >())
bool showNetsOutsideModules() const
void removeModule(const u32 id)
QList< ModuleItem * > getItems(const u32 id, ModuleItem::TreeItemType type=ModuleItem::TreeItemType::Module) const
void addGate(const u32 id, const u32 parentId)
ModuleItem * getItem(const QModelIndex &index) const
void populateFromGatelist(const std::vector< Gate * > &gates)
ModuleItem * createChildItem(u32 id, ModuleItem::TreeItemType itemType, BaseTreeItem *parentItem=nullptr)
void updateGateName(const u32 id)
void addModule(const u32 id, const u32 parentId)
QVariant data(const QModelIndex &index, int role) const override
void updateNetParent(const Net *net, const QHash< const Net *, ModuleItem * > *parentAssignment=nullptr)
void setIsModifying(bool pIsModifying)
ModuleModel(QObject *parent=nullptr)
void removeNet(const u32 id)
QMimeData * mimeData(const QModelIndexList &indexes) const override
void updateModuleName(const u32 id)
void clear() override
void removeChildItem(ModuleItem *itemToRemove, BaseTreeItem *parentItem)
void populateTree(const QVector< u32 > &modIds={}, const QVector< u32 > &gatIds={}, const QVector< u32 > &netIds={})
friend class ModuleItem
Definition: module_model.h:55
void moduleAssignGate(const u32 moduleId, const u32 gateId)
uint32_t u32
Definition: defines.h:41
const Module * module(const Gate *g, const NodeBoxes &boxes)
Definition: defines.h:45
PinType type
Net * net
bool contains(const Key &key) const const
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QObject * parent() const const
typedef ItemFlags