HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
graph_layouter.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 
33 #include "gui/gui_def.h"
35 #include "hal_core/defines.h"
36 #include "hal_core/netlist/gate.h"
37 #include "hal_core/netlist/net.h"
38 
39 #include <QMap>
40 #include <QMultiHash>
41 #include <QObject>
42 #include <QPoint>
43 #include <QRect>
44 #include <QSet>
45 #include <QVector>
46 #include <QThread>
47 
48 namespace hal
49 {
50  class GraphContext;
51  class GraphicsGate;
52  class GraphicsItem;
53  class GraphicsNet;
54  class GraphicsNode;
55  class GraphicsScene;
56  class SeparatedGraphicsNet;
57  class StandardGraphicsNet;
58 
59  class NetLayoutJunctionHash;
60  class NetLayoutJunctionEntries;
61  class CommentSpeechBubble;
62  class CommentEntry;
66  class JunctionThread;
70  class DrawNetThread;
71 
83  class GraphLayouter : public QObject
84  {
85  Q_OBJECT
86 
87  friend class DrawNetThread;
88 
92  class SceneCoordinate
93  {
94  int minLane;
95  int maxLane;
96  float mOffset;
97  float mPadding;
98 
99  public:
100  SceneCoordinate() : minLane(0), maxLane(0), mOffset(0), mPadding(0)
101  {
102  ;
103  }
104  void testMinMax(int ilane);
105  void setOffset(float off)
106  {
107  mOffset = off;
108  }
109  void setPadding(float pad)
110  {
111  mPadding = pad;
112  }
113  void setOffsetYje(const SceneCoordinate& previous, float minimumJunction);
114  void setOffsetYej(const SceneCoordinate& previous, float maximumBlock, float minimumJunction);
115  void setOffsetX(const SceneCoordinate& previous, float maximumBlock, float sepOut, float sepInp);
116  float lanePosition(int ilane) const;
117  int preLanes() const
118  {
119  return -minLane;
120  }
121  float junctionEntry() const
122  {
123  return lanePosition(minLane);
124  }
125  float junctionExit() const
126  {
127  return lanePosition(maxLane - 1);
128  }
129  float xBoxOffset() const;
130  };
131 
135  class SceneCoordinateArray
136  {
137  float* mArray;
138  int mFirstIndex;
139  public:
140  SceneCoordinateArray(const QMap<int,SceneCoordinate>& inputMap);
141  ~SceneCoordinateArray();
142  float lanePosition(int igrid, int ilane) const;
143  };
144 
148  class EndpointCoordinate
149  {
150  float mYoffset;
151  float mXoutput;
152  float mXinput;
153  float mPinDistance;
154  float mTopPin;
155  int mNumberPins;
156  QMultiHash<u32, int> mInputHash;
157  QMultiHash<u32, int> mOutputHash;
158 
159  public:
160  EndpointCoordinate();
161  void setInputPosition(QPointF p0pos);
162  void setOutputPosition(QPointF p0pos);
163  float lanePosition(int ilane, bool absolute) const;
164  float xInput() const
165  {
166  return mXinput;
167  }
168  float xOutput() const
169  {
170  return mXoutput;
171  }
172  void setInputPins(const QList<u32>& pinList, float p0dist, float pdist);
173  void setOutputPins(const QList<u32>& pinList, float p0dist, float pdist);
174  int numberPins() const;
175  QList<int> inputPinIndex(u32 id) const;
176  QList<int> outputPinIndex(u32 id) const;
177  };
178 
182  class EndpointList : public QList<NetLayoutPoint>
183  {
184  public:
185  enum EndpointType
186  {
187  NoEndpoint = 0,
188  SingleSource = 1,
189  SingleDestination = 2,
190  SourceAndDestination = 3,
191  ConstantLevel = 4,
192  HasGlobalEndpoint = 5
193  };
194  EndpointList()
195  : mNetType(NoEndpoint), mInputArrow(false), mOutputArrow(false)
196  {;}
197  void addSource(const NetLayoutPoint& pnt);
198  void addDestination(const NetLayoutPoint& pnt);
199  void setNetType(EndpointType tp) { mNetType = tp; }
200  EndpointType netType() const { return mNetType; }
201  bool isInput(int index) const { return mPointIsInput.at(index); }
202  void setInputArrow() { mInputArrow = true; }
203  bool hasInputArrow() const { return mInputArrow; }
204  void setOutputArrow() { mOutputArrow = true; }
205  bool hasOutputArrow() const { return mOutputArrow; }
206 
207  private:
208  EndpointType mNetType;
209  QList<bool> mPointIsInput;
210  bool mInputArrow;
211  bool mOutputArrow;
212  };
213 
217  class SeparatedNetWidth
218  {
219  public:
220  float mInputSpace;
221  float mOutputSpace;
222  SeparatedNetWidth() : mInputSpace(0), mOutputSpace(0)
223  {
224  ;
225  }
226  void requireInputSpace(float spc);
227  void requireOutputSpace(float spc);
228  };
229 
233  struct Road
234  {
235  Road(const int x_coordinate, const int y_coordinate) : x(x_coordinate), y(y_coordinate), mLanes(0)
236  {
237  }
238 
239  int x;
240  int y;
241 
242  unsigned int mLanes = 0;
243  };
244 
248  struct UsedPaths
249  {
250  QSet<Road*> mHRoads;
251  QSet<Road*> mVRoads;
252  };
253 
254  public:
261  explicit GraphLayouter(GraphContext *context, QObject* parent = nullptr);
262 
266  ~GraphLayouter();
267 
273  virtual QString name() const = 0;
274 
280  virtual QString mDescription() const = 0;
281 
282  virtual void add(const QSet<u32> modules, const QSet<u32> gates, const QSet<u32> nets, PlacementHint placement = PlacementHint()) = 0;
283 
284  virtual void remove(const QSet<u32> modules, const QSet<u32> gates, const QSet<u32> nets) = 0;
285 
289  void layout();
290 
296  GraphicsScene* scene() const;
297 
300 
306 
307  NetLayoutPoint positonForNode(const Node& nd) const;
308  Node nodeAtPosition(const QPoint& p) const;
309 
310  QPoint gridPointByItem(GraphicsNode* item) const;
311 
312  void dumpNodePositions(const QPoint& search) const;
313 
314  void updatePlacement(const GridPlacement& plc);
315 
316  void setNodePosition(const Node& n, const QPoint& p);
317  void swapNodePositions(const Node& n1, const Node& n2);
318  void removeNodeFromMaps(const Node& n);
319 
320  int minXIndex() const;
321  int minYIndex() const;
322 
323  bool done() const;
324 
325  bool dumpJunctionEnabled();
326  void setDumpJunctionEnabled(bool enabled);
327 
328  QVector<qreal> xValues() const;
329  QVector<qreal> yValues() const;
330 
331  qreal maxNodeWidth() const;
332  qreal maxNodeHeight() const;
333 
334  qreal defaultGridWidth() const;
335  qreal defaultGridHeight() const;
336 
337  qreal gridXposition(int ix) const;
338  qreal gridYposition(int iy) const;
339 
340  const NodeBoxes& boxes() const
341  {
342  return mBoxes;
343  }
344 
345  void prepareRollback();
346  bool canRollback() const;
347  bool rollback();
348 
349  protected:
355 
356  private Q_SLOTS:
357  void handleDrawNetThreadFinished();
358  void handleJunctionThreadFinished();
359 
360  private:
361  void clearLayoutData();
362  void clearComments();
363  void createBoxes();
364  void getWireHash();
365  void findMaxBoxDimensions();
366  void findMaxChannelLanes();
367  void calculateJunctionMinDistance();
368  void calculateGateOffsets();
369  void placeGates();
370  void drawNets();
371  void drawComments();
372  void drawNetsIsolated(u32 id, Net* n, const EndpointList& epl);
373  void updateSceneRect();
374  static bool verifyModulePort(Net* n, const Node& modNode, bool isModInput);
375  void handleCommentAboutToDeleted(CommentEntry* entry);
376  void handleCommentAdded(CommentEntry* entry);
377 
378  bool boxExists(const int x, const int y) const;
379 
380  bool hRoadJumpPossible(const int x, const int y1, const int y2) const;
381  bool hRoadJumpPossible(const Road* const r1, const Road* const r2) const;
382 
383  bool vRoadJumpPossible(const int x1, const int x2, const int y) const;
384  bool vRoadJumpPossible(const Road* const r1, const Road* const r2) const;
385 
386  qreal hRoadHeight(const unsigned int mLanes) const;
387  qreal vRoadWidth(const unsigned int mLanes) const;
388 
389  static bool isConstNet(const Net* n);
390 
391  NodeBoxes mBoxes;
392 
393  QMap<int, qreal> mMaxNodeWidthForX;
394  QMap<int, qreal> mMaxNodeHeightForY;
395 
396  QMap<int, qreal> mNodeOffsetForX;
397  QMap<int, qreal> mNodeOffsetForY;
398 
399  QMap<int, qreal> mMaxLeftIoPaddingForChannelX;
400  QMap<int, qreal> mMaxRightIoPaddingForChannelX;
401 
402  QSet<u32> mViewInput;
403  QSet<u32> mViewOutput;
404 
405  int mMinXIndex;
406  int mMinYIndex;
407 
408  int mMaxXIndex;
409  int mMaxYIndex;
410 
411  QVector<qreal> mXValues;
412  QVector<qreal> mYValues;
413 
414  qreal mMaxNodeWidth;
415  qreal mMaxNodeHeight;
416 
417  bool mDone;
418  int mRollbackStatus;
419 
420  QRect mNodeBoundingBox;
421  NetLayoutConnectionMetric mConnectionMetric;
426  QHash<NetLayoutPoint, float> mSpaceSeparatedOutputs;
427  NetLayoutJunctionHash mJunctionHash;
430  QMap<int, float> mJunctionMinDistanceY;
431  QHash<u32, EndpointList> mWireEndpoint;
432  QHash<u32, int> mGlobalInputHash;
433  QHash<u32, int> mGlobalOutputHash;
434 
435  bool mDumpJunctions;
436  QList<CommentSpeechBubble*> mCommentBubbles;
437  QSet<u32> mNetsToDraw;
438  QSet<u32>::const_iterator mNetIterator;
439  QList<DrawNetThread*> mDrawNetThreads;
440  QList<JunctionThread*> mJunctionThreads;
442 
443  SceneCoordinateArray* mCoordArrayX;
444  SceneCoordinateArray* mCoordArrayY;
445  };
446 
450  class DrawNetThread : public QThread
451  {
452  Q_OBJECT
453  u32 mId;
454  GraphLayouter* mLayouter;
455  void drawJunction();
456  void drawEndpoint();
457  public:
460  DrawNetThread(u32 id, GraphLayouter* parent) : QThread(parent), mId(id), mLayouter(parent) {;}
461  u32 id() const { return mId; }
462  void run() override;
463  };
464 
468  class JunctionThread : public QThread
469  {
470  Q_OBJECT
471  public:
476  : mNetLayoutPoint(nlp), mEntries(entr), mJunction(nullptr) {;}
477  void run() override;
478  };
479 } // namespace hal
The CommentEntry class encapsulated information related to a comment.
Definition: comment_entry.h:43
void run() override
DrawNetThread(u32 id, GraphLayouter *parent)
StandardGraphicsNet::Lines mLines
QList< QPointF > mKnots
Logical container for modules, gates, and nets.
Definition: graph_context.h:55
Base class for all specific layouters.
QVector< qreal > yValues() const
qreal gridXposition(int ix) const
virtual void remove(const QSet< u32 > modules, const QSet< u32 > gates, const QSet< u32 > nets)=0
void setNodePosition(const Node &n, const QPoint &p)
void removeNodeFromMaps(const Node &n)
virtual QString mDescription() const =0
QVector< qreal > xValues() const
QMap< Node, QPoint > mNodeToPositionRollback
qreal defaultGridHeight() const
void setDumpJunctionEnabled(bool enabled)
void swapNodePositions(const Node &n1, const Node &n2)
QMap< QPoint, Node > mPositionToNodeMap
virtual void add(const QSet< u32 > modules, const QSet< u32 > gates, const QSet< u32 > nets, PlacementHint placement=PlacementHint())=0
const QMap< QPoint, Node > positionToNodeMap() const
NetLayoutPoint positonForNode(const Node &nd) const
GraphLayouter(GraphContext *context, QObject *parent=nullptr)
GraphicsScene * mScene
void dumpNodePositions(const QPoint &search) const
const QMap< Node, QPoint > nodeToPositionMap() const
qreal maxNodeWidth() const
qreal gridYposition(int iy) const
qreal maxNodeHeight() const
Node nodeAtPosition(const QPoint &p) const
GridPlacement * gridPlacementFactory() const
GraphicsScene * scene() const
bool canRollback() const
qreal defaultGridWidth() const
QPoint gridPointByItem(GraphicsNode *item) const
virtual QString name() const =0
GraphContext * mParentContext
QMap< Node, QPoint > mNodeToPositionMap
const NodeBoxes & boxes() const
void updatePlacement(const GridPlacement &plc)
Abstract base class for nodes (e.g. gates, modules)
Definition: graphics_node.h:42
Container for a GraphGraphicsView containing gates, nets, and modules.
NetLayoutPoint mNetLayoutPoint
NetLayoutJunction * mJunction
JunctionThread(const NetLayoutPoint &nlp, const NetLayoutJunctionEntries &entr)
NetLayoutJunctionEntries mEntries
Definition: net.h:58
The NodeBoxes class owns all NodeBox'es from hal view.
Definition: node_box.h:150
The Node class object represents a module or a gate.
Definition: gui_def.h:61
The PlacementHint class object provides hints for the layouter how new box objects are placed on a vi...
Definition: gui_def.h:214
uint32_t u32
Definition: defines.h:41
Definition: defines.h:45
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QObject * parent() const const