You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

505 lines
19 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file netlist_object.h
  27. * @brief Definition of the NETLIST_OBJECT class.
  28. */
  29. #ifndef NETLIST_OBJECT_H
  30. #define NETLIST_OBJECT_H
  31. #include <sch_sheet_path.h>
  32. #include <lib_pin.h>
  33. #include <sch_item_struct.h>
  34. class NETLIST_OBJECT_LIST;
  35. class SCH_COMPONENT;
  36. /* Type of Net objects (wires, labels, pins...) */
  37. enum NETLIST_ITEM_T
  38. {
  39. NET_ITEM_UNSPECIFIED, // only for not yet initialized instances
  40. NET_SEGMENT, // connection by wire
  41. NET_BUS, // connection by bus
  42. NET_JUNCTION, // connection by junction: can connect to
  43. // or more crossing wires
  44. NET_LABEL, // this is a local label
  45. NET_GLOBLABEL, // this is a global label that connect all
  46. // others global label in whole hierarchy
  47. NET_HIERLABEL, // element to indicate connection to a
  48. // higher-level sheet
  49. NET_SHEETLABEL, // element to indicate connection to a
  50. // lower-level sheet.
  51. NET_BUSLABELMEMBER, /* created when a bus label is found:
  52. * the bus label (like DATA[0..7] is
  53. * converted to n single labels like
  54. * DATA0, DATA1 ...
  55. * These objects are living only in the current
  56. * NETLIST_OBJECT_LIST, not in shematic.
  57. */
  58. NET_GLOBBUSLABELMEMBER, // see NET_BUSLABELMEMBER, used when a
  59. // global bus label is found
  60. NET_HIERBUSLABELMEMBER, // see NET_BUSLABELMEMBER, used when a
  61. // hierarchical bus label is found
  62. NET_SHEETBUSLABELMEMBER, // see NET_BUSLABELMEMBER, used when a
  63. // pin sheet label using bus notation
  64. // is found
  65. NET_PINLABEL, /* created when a pin is POWER (IN or
  66. * OUT) with invisible attribute is found:
  67. * these pins are equivalent to a global
  68. * label and are automatically connected
  69. */
  70. NET_PIN, // this is an usual pin
  71. NET_NOCONNECT // this is a no connect symbol
  72. };
  73. /* Values for .m_FlagOfConnection member */
  74. enum NET_CONNECTION_T
  75. {
  76. UNCONNECTED = 0, /* Pin or Label not connected (error) */
  77. NOCONNECT_SYMBOL_PRESENT, /* Pin not connected but have a NoConnect
  78. * symbol on it (no error) */
  79. PAD_CONNECT /* Normal connection (no error) */
  80. };
  81. class NETLIST_OBJECT
  82. {
  83. public:
  84. NETLIST_ITEM_T m_Type; /* Type of item (see NETLIST_ITEM_T enum) */
  85. EDA_ITEM* m_Comp; /* Pointer to the library item that
  86. * created this net object (the parent)
  87. */
  88. SCH_ITEM* m_Link; /* For SCH_SHEET_PIN:
  89. * Pointer to the hierarchy sheet that
  90. * contains this SCH_SHEET_PIN
  91. * For Pins: pointer to the schematic component
  92. * that contains this pin
  93. */
  94. int m_Flag; /* flag used in calculations */
  95. SCH_SHEET_PATH m_SheetPath; // the sheet path which contains this item
  96. SCH_SHEET_PATH m_SheetPathInclude; // sheet path which contains the hierarchical label
  97. ELECTRICAL_PINTYPE m_ElectricalPinType; // Has meaning only for Pins: electrical type of the pin
  98. int m_BusNetCode; /* Used for BUS connections */
  99. int m_Member; /* for labels type NET_BUSLABELMEMBER ( bus member
  100. * created from the BUS label ) member number.
  101. */
  102. NET_CONNECTION_T m_ConnectionType; // Used to store the connection type
  103. wxString m_PinNum; // pin number
  104. wxString m_Label; // Label text (for labels) or Pin name (for pins)
  105. wxPoint m_Start; // Position of object or for segments: starting point
  106. wxPoint m_End; // For segments (wire and buses): ending point
  107. private:
  108. int m_netCode; /* net code for all items except BUS
  109. * labels because a BUS label has
  110. * as many net codes as bus members
  111. */
  112. NETLIST_OBJECT* m_netNameCandidate; /* a pointer to a label connected to the net,
  113. * that can be used to give a name to the net
  114. * or a pin if there is no label in net
  115. * When no label, the pin is used to build
  116. * default net name.
  117. */
  118. public:
  119. #if defined(DEBUG)
  120. void Show( std::ostream& out, int ndx ) const;
  121. #endif
  122. NETLIST_OBJECT();
  123. NETLIST_OBJECT( NETLIST_OBJECT& aSource ); // Copy constructor
  124. ~NETLIST_OBJECT();
  125. // Accessors:
  126. void SetNet( int aNetCode ) { m_netCode = aNetCode; }
  127. int GetNet() const { return m_netCode; }
  128. /**
  129. * Set the item connection type:
  130. * UNCONNECTED Pin or Label not connected (error)
  131. * NOCONNECT_SYMBOL_PRESENT Pin not connected but have a NoConnect
  132. * symbol on it (no error)
  133. * PAD_CONNECT Normal connection (no error)
  134. */
  135. void SetConnectionType( NET_CONNECTION_T aFlg = UNCONNECTED )
  136. {
  137. m_ConnectionType = aFlg;
  138. }
  139. NET_CONNECTION_T GetConnectionType() const
  140. {
  141. return m_ConnectionType;
  142. }
  143. /**
  144. * Set m_netNameCandidate to a connected item which will
  145. * be used to calcule the net name of the item
  146. * Obviously the candidate can be only a label
  147. * when there is no label on the net a pad which will
  148. * used to build a net name (something like Cmp<REF>_Pad<PAD_NAME>
  149. * @param aCandidate = the connected item candidate
  150. */
  151. void SetNetNameCandidate( NETLIST_OBJECT* aCandidate );
  152. /**
  153. * @return true if an item has already a net name candidate
  154. * and false if not ( m_netNameCandidate == NULL )
  155. */
  156. bool HasNetNameCandidate() { return m_netNameCandidate != NULL; }
  157. /**
  158. * Function GetPinNum
  159. * returns a pin number in wxString form. Pin numbers are not always
  160. * numbers. \"A23\" would be a valid pin number.
  161. */
  162. const wxString& GetPinNumText() const
  163. {
  164. return m_PinNum;
  165. }
  166. /** For Pins (NET_PINS):
  167. * @return the schematic component which contains this pin
  168. * (Note: this is the schematic component, not the library component
  169. * for others items: return NULL
  170. */
  171. SCH_COMPONENT* GetComponentParent() const
  172. {
  173. if( m_Link && m_Link->Type() == SCH_COMPONENT_T )
  174. return (SCH_COMPONENT*) m_Link;
  175. return NULL;
  176. }
  177. /**
  178. * Function IsLabelConnected
  179. * tests if the net list object is a hierarchical label or sheet label and is
  180. * connected to an associated hierarchical label or sheet label of \a aNetItem.
  181. *
  182. * @param aNetItem A pointer to a NETLIST_OBJECT to test against.
  183. * @return A bool value of true if there is a connection with \a aNetItem or false
  184. * if no connection to \a aNetItem.
  185. */
  186. bool IsLabelConnected( NETLIST_OBJECT* aNetItem );
  187. /**
  188. * Function IsLabelGlobal
  189. * @return true if the object is a global label
  190. * (i.e. an real global label or a pin label coming
  191. * from a power pin invisible
  192. */
  193. bool IsLabelGlobal() const;
  194. /**
  195. * Function IsLabelBusMemberType
  196. * @return true if the object is a bus label member build from a
  197. * schematic bus label (like label[xx..yy], xx and yy are the first and last
  198. * bus member id)
  199. * bus label members have specific properties:
  200. * they do not live in schematic
  201. * they have specific properties in connections:
  202. * 2 bus label members can be connected connected only if they have the same member value.
  203. */
  204. bool IsLabelBusMemberType() const;
  205. /**
  206. * Function IsLabelType
  207. * @return true if the object is a label of any type
  208. */
  209. bool IsLabelType() const;
  210. /**
  211. * Function GetNetName
  212. * @param adoptTimestamp if annotation is not done (i.e. GetRef returns something with an ? at the end)
  213. * @return the full net name of the item, i.e. the net name
  214. * from the "best" label, prefixed by the sheet path
  215. */
  216. wxString GetNetName( bool adoptTimestamp = false ) const;
  217. /**
  218. * Function GetShortNetName
  219. * @param adoptTimestamp if annotation is not done (i.e. GetRef returns something with an ? at the end)
  220. * @return the short net name of the item i.e. the net name
  221. * from the "best" label without any prefix.
  222. * 2 different nets can have the same short name
  223. */
  224. wxString GetShortNetName( bool adoptTimestamp = false ) const;
  225. /**
  226. * Function ConvertBusToNetListItems
  227. * breaks the text of a bus label type net list object into as many members as
  228. * it contains and creates a #NETLIST_OBJECT for each label and adds it to \a
  229. * aNetListItems.
  230. *
  231. * @param aNetListItems A reference to vector of #NETLIST_OBJECT pointers to add
  232. * the bus label NETLIST_OBJECTs.
  233. */
  234. void ConvertBusToNetListItems( NETLIST_OBJECT_LIST& aNetListItems );
  235. };
  236. /**
  237. * Type NETLIST_OBJECTS
  238. * is a container referring to (not owning) NETLIST_OBJECTs, which are connected items
  239. * in a full schematic hierarchy. It is useful when referring to NETLIST_OBJECTs
  240. * actually owned by some other container.
  241. */
  242. typedef std::vector<NETLIST_OBJECT*> NETLIST_OBJECTS;
  243. /**
  244. * Class NETLIST_OBJECT_LIST
  245. * is a container holding and _owning_ NETLIST_OBJECTs, which are connected items
  246. * in a full schematic hierarchy. It is helpful for netlist and ERC calculations.
  247. */
  248. class NETLIST_OBJECT_LIST : public NETLIST_OBJECTS
  249. {
  250. int m_lastNetCode; // Used in intermediate calculation: last net code created
  251. int m_lastBusNetCode; // Used in intermediate calculation:
  252. // last net code created for bus members
  253. public:
  254. /**
  255. * Constructor.
  256. * NETLIST_OBJECT_LIST handle a list of connected items.
  257. * these NETLIST_OBJECT items are freeed by the destructor
  258. */
  259. NETLIST_OBJECT_LIST()
  260. {
  261. // Do not leave some members uninitialized:
  262. m_lastNetCode = 0;
  263. m_lastBusNetCode = 0;
  264. }
  265. ~NETLIST_OBJECT_LIST();
  266. /**
  267. * Function BuildNetListInfo
  268. * the master function of tgis class.
  269. * Build the list of connected objects (pins, labels ...) and
  270. * all info to generate netlists or run ERC diags
  271. * @param aSheets = the flattened sheet list
  272. * @return true if OK, false is not item found
  273. */
  274. bool BuildNetListInfo( SCH_SHEET_LIST& aSheets );
  275. /**
  276. * Acces to an item in list
  277. */
  278. NETLIST_OBJECT* GetItem( unsigned aIdx ) const
  279. {
  280. return *( this->begin() + aIdx );
  281. }
  282. /**
  283. * Acces to an item type
  284. */
  285. NETLIST_ITEM_T GetItemType( unsigned aIdx ) const
  286. {
  287. return GetItem( aIdx )->m_Type;
  288. }
  289. /**
  290. * Acces to an item net code
  291. */
  292. int GetItemNet( unsigned aIdx ) const
  293. {
  294. return GetItem( aIdx )->GetNet();
  295. }
  296. NET_CONNECTION_T GetConnectionType( unsigned aIdx )
  297. {
  298. return GetItem( aIdx )->GetConnectionType();
  299. }
  300. /**
  301. * Set the item connection type:
  302. * UNCONNECTED Pin or Label not connected (error)
  303. * NOCONNECT_SYMBOL_PRESENT Pin not connected but have a NoConnect
  304. * symbol on it (no error)
  305. * PAD_CONNECT Normal connection (no error)
  306. */
  307. void SetConnectionType( unsigned aIdx, NET_CONNECTION_T aFlg = UNCONNECTED )
  308. {
  309. GetItem( aIdx )->SetConnectionType( aFlg );
  310. }
  311. /** Delete all objects in list and clear list */
  312. void Clear();
  313. /**
  314. * Reset the connection type of all items to UNCONNECTED type
  315. */
  316. void ResetConnectionsType()
  317. {
  318. for( unsigned ii = 0; ii < size(); ii++ )
  319. GetItem( ii )->SetConnectionType( UNCONNECTED );
  320. }
  321. /*
  322. * Sorts the list of connected items by net code
  323. */
  324. void SortListbyNetcode();
  325. /*
  326. * Sorts the list of connected items by sheet.
  327. * This sorting is used when searching "physical" connection between items
  328. * because obviously only items inside the same sheet can be connected
  329. */
  330. void SortListbySheet();
  331. /**
  332. * Counts number of pins connected on the same net.
  333. * Used to count all pins connected to a no connect symbol
  334. * @return the pin count of the net starting at aNetStart
  335. * @param aNetStart = index in list of net objects of the first item
  336. */
  337. int CountPinsInNet( unsigned aNetStart );
  338. /**
  339. * Function TestforNonOrphanLabel
  340. * Sheet labels are expected to be connected to a hierarchical label.
  341. * Hierarchical labels are expected to be connected to a sheet label.
  342. * Global labels are expected to be not orphan (connected to at least one other global label.
  343. * this function tests the connection to an other suitable label
  344. */
  345. void TestforNonOrphanLabel( unsigned aNetItemRef, unsigned aStartNet );
  346. /**
  347. * Function TestforSimilarLabels
  348. * detects labels which are different when using case sensitive comparisons
  349. * but are equal when using case insensitive comparisons
  350. * It can be due to a mistake from designer, so this kind of labels
  351. * is reported by TestforSimilarLabels
  352. */
  353. void TestforSimilarLabels();
  354. #if defined(DEBUG)
  355. void DumpNetTable()
  356. {
  357. for( unsigned idx = 0; idx < size(); ++idx )
  358. {
  359. GetItem( idx )->Show( std::cout, idx );
  360. }
  361. }
  362. #endif
  363. private:
  364. /*
  365. * Propagate aNewNetCode to items having an internal netcode aOldNetCode
  366. * used to interconnect group of items already physically connected,
  367. * when a new connection is found between aOldNetCode and aNewNetCode
  368. */
  369. void propagateNetCode( int aOldNetCode, int aNewNetCode, bool aIsBus );
  370. /*
  371. * This function merges the net codes of groups of objects already connected
  372. * to labels (wires, bus, pins ... ) when 2 labels are equivalents
  373. * (i.e. group objects connected by labels)
  374. */
  375. void labelConnect( NETLIST_OBJECT* aLabelRef );
  376. /* Comparison function to sort by increasing Netcode the list of connected items
  377. */
  378. static bool sortItemsbyNetcode( const NETLIST_OBJECT* Objet1, const NETLIST_OBJECT* Objet2 )
  379. {
  380. return Objet1->GetNet() < Objet2->GetNet();
  381. }
  382. /* Comparison routine to sort items by Sheet path
  383. */
  384. static bool sortItemsBySheet( const NETLIST_OBJECT* Objet1, const NETLIST_OBJECT* Objet2 )
  385. {
  386. return Objet1->m_SheetPath.Cmp( Objet2->m_SheetPath ) < 0;
  387. }
  388. /**
  389. * Propagate net codes from a parent sheet to an include sheet,
  390. * from a pin sheet connection
  391. */
  392. void sheetLabelConnect( NETLIST_OBJECT* aSheetLabel );
  393. void pointToPointConnect( NETLIST_OBJECT* aRef, bool aIsBus, int start );
  394. /**
  395. * Search connections between a junction and segments
  396. * Propagate the junction net code to objects connected by this junction.
  397. * The junction must have a valid net code
  398. * The list of objects is expected sorted by sheets.
  399. * Search is done from index aIdxStart to the last element of list
  400. */
  401. void segmentToPointConnect( NETLIST_OBJECT* aJonction, bool aIsBus, int aIdxStart );
  402. /**
  403. * Function connectBusLabels
  404. * Propagate the net code (and create it, if not yet existing) between
  405. * all bus label member objects connected by they name.
  406. * Search is done in the entire list
  407. */
  408. void connectBusLabels();
  409. /**
  410. * Set the m_FlagOfConnection member of items in list
  411. * depending on the connection type:
  412. * UNCONNECTED, PAD_CONNECT or NOCONNECT_SYMBOL_PRESENT
  413. * The list is expected sorted by order of net code,
  414. * i.e. items having the same net code are grouped
  415. */
  416. void setUnconnectedFlag();
  417. /**
  418. * Function findBestNetNameForEachNet
  419. * fill the .m_NetNameCandidate member of each item of aNetItemBuffer
  420. * with a reference to the "best" NETLIST_OBJECT usable to give a name to the net
  421. * If no suitable object found, .m_NetNameCandidate is filled with 0.
  422. * The "best" NETLIST_OBJECT is a NETLIST_OBJECT that have the type label
  423. * and by priority order:
  424. * the label is global or local
  425. * the label is in the first sheet in a hierarchy (the root sheet has the most priority)
  426. * alphabetic order.
  427. */
  428. void findBestNetNameForEachNet();
  429. };
  430. /**
  431. * Function IsBusLabel
  432. * test if \a aLabel has a bus notation.
  433. *
  434. * @param aLabel A wxString object containing the label to test.
  435. * @return true if text is a bus notation format otherwise false is returned.
  436. */
  437. extern bool IsBusLabel( const wxString& aLabel );
  438. #endif // NETLIST_OBJECT_H