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.

455 lines
17 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 class_netlist_object.h
  27. * @brief Definition of the NETLIST_OBJECT class.
  28. */
  29. #ifndef _CLASS_NETLIST_OBJECT_H_
  30. #define _CLASS_NETLIST_OBJECT_H_
  31. #include <sch_sheet_path.h>
  32. #include <lib_pin.h> // LIB_PIN::ReturnPinStringNum( m_PinNum )
  33. class NETLIST_OBJECT_LIST;
  34. class SCH_COMPONENT;
  35. /* Type of Net objects (wires, labels, pins...) */
  36. enum NETLIST_ITEM_T {
  37. NET_ITEM_UNSPECIFIED, // only for not yet initialized instances
  38. NET_SEGMENT, // connection by wire
  39. NET_BUS, // connection by bus
  40. NET_JUNCTION, // connection by junction: can connect to
  41. // or more crossing wires
  42. NET_LABEL, // this is a local label
  43. NET_GLOBLABEL, // this is a global label that connect all
  44. // others global label in whole hierarchy
  45. NET_HIERLABEL, // element to indicate connection to a
  46. // higher-level sheet
  47. NET_SHEETLABEL, // element to indicate connection to a
  48. // lower-level sheet.
  49. NET_BUSLABELMEMBER, /* created when a bus label is found:
  50. * the bus label (like DATA[0..7] is
  51. * converted to n single labels like
  52. * DATA0, DATA1 ...
  53. */
  54. NET_GLOBBUSLABELMEMBER, // see NET_BUSLABELMEMBER, used when a
  55. // global bus label is found
  56. NET_HIERBUSLABELMEMBER, // see NET_BUSLABELMEMBER, used when a
  57. // hierarchical bus label is found
  58. NET_SHEETBUSLABELMEMBER, // see NET_BUSLABELMEMBER, used when a
  59. // pin sheet label using bus notation
  60. // is found
  61. NET_PINLABEL, /* created when a pin is POWER (IN or
  62. * OUT) with invisible attribute is found:
  63. * these pins are equivalent to a global
  64. * label and are automatically connected
  65. */
  66. NET_PIN, // this is an usual pin
  67. NET_NOCONNECT // this is a no connect symbol
  68. };
  69. /* Values for .m_FlagOfConnection member */
  70. enum NET_CONNECTION_T {
  71. UNCONNECTED = 0, /* Pin or Label not connected (error) */
  72. NOCONNECT_SYMBOL_PRESENT, /* Pin not connected but have a NoConnect
  73. * symbol on it (no error) */
  74. PAD_CONNECT /* Normal connection (no error) */
  75. };
  76. class NETLIST_OBJECT
  77. {
  78. public:
  79. NETLIST_ITEM_T m_Type; /* Type of item (see NETLIST_ITEM_T enum) */
  80. EDA_ITEM* m_Comp; /* Pointer to the library item that
  81. * created this net object (the parent)
  82. */
  83. SCH_ITEM* m_Link; /* For SCH_SHEET_PIN:
  84. * Pointer to the hierarchy sheet that
  85. * contains this SCH_SHEET_PIN
  86. * For Pins: pointer to the schematic component
  87. * that contains this pin
  88. */
  89. int m_Flag; /* flag used in calculations */
  90. SCH_SHEET_PATH m_SheetPath; // the sheet path which contains this item
  91. SCH_SHEET_PATH m_SheetPathInclude; // sheet path which contains the hierarchical label
  92. int m_ElectricalType; /* Has meaning only for Pins and
  93. * hierarchical pins: electrical type */
  94. int m_BusNetCode; /* Used for BUS connections */
  95. int m_Member; /* for labels type NET_BUSLABELMEMBER ( bus member
  96. * created from the BUS label ) member number.
  97. */
  98. NET_CONNECTION_T m_ConnectionType; // Used to store the connection type
  99. long m_PinNum; // pin number ( 1 long = 4 bytes -> 4 ascii codes)
  100. wxString m_Label; // Label text (for labels) or Pin name (for pins)
  101. wxPoint m_Start; // Position of object or for segments: starting point
  102. wxPoint m_End; // For segments (wire and buses): ending point
  103. private:
  104. int m_netCode; /* net code for all items except BUS
  105. * labels because a BUS label has
  106. * as many net codes as bus members
  107. */
  108. NETLIST_OBJECT* m_netNameCandidate; /* a pointer to a label connected to the net,
  109. * that can be used to give a name to the net
  110. * or a pin if there is no label in net
  111. * When no label, the pin is used to build
  112. * default net name.
  113. */
  114. public:
  115. #if defined(DEBUG)
  116. void Show( std::ostream& out, int ndx ) const; // override
  117. #endif
  118. NETLIST_OBJECT();
  119. NETLIST_OBJECT( NETLIST_OBJECT& aSource ); // Copy constructor
  120. ~NETLIST_OBJECT();
  121. // Accessors:
  122. void SetNet( int aNetCode ) { m_netCode = aNetCode; }
  123. int GetNet() const { return m_netCode; }
  124. /**
  125. * Set the item connection type:
  126. * UNCONNECTED Pin or Label not connected (error)
  127. * NOCONNECT_SYMBOL_PRESENT Pin not connected but have a NoConnect
  128. * symbol on it (no error)
  129. * PAD_CONNECT Normal connection (no error)
  130. */
  131. void SetConnectionType( NET_CONNECTION_T aFlg = UNCONNECTED )
  132. {
  133. m_ConnectionType = aFlg;
  134. }
  135. NET_CONNECTION_T GetConnectionType()
  136. {
  137. return m_ConnectionType;
  138. }
  139. /**
  140. * Set m_netNameCandidate to a connected item which will
  141. * be used to calcule the net name of the item
  142. * Obviously the candidate can be only a label
  143. * when there is no label on the net a pad which will
  144. * used to build a net name (something like Cmp<REF>_Pad<PAD_NAME>
  145. * @param aCandidate = the connected item candidate
  146. */
  147. void SetNetNameCandidate( NETLIST_OBJECT* aCandidate );
  148. /**
  149. * @return true if an item has already a net name candidate
  150. * and false if not ( m_netNameCandidate == NULL )
  151. */
  152. bool HasNetNameCandidate() { return m_netNameCandidate != NULL; }
  153. /**
  154. * Function GetPinNum
  155. * returns a pin number in wxString form. Pin numbers are not always
  156. * numbers. \"A23\" would be a valid pin number.
  157. */
  158. wxString GetPinNumText()
  159. {
  160. // hide the ugliness in here, but do it inline.
  161. return LIB_PIN::ReturnPinStringNum( m_PinNum );
  162. }
  163. /** For Pins (NET_PINS):
  164. * @return the schematic component which contains this pin
  165. * (Note: this is the schematic component, not the library component
  166. * for others items: return NULL
  167. */
  168. SCH_COMPONENT* GetComponentParent() const
  169. {
  170. if( m_Link && m_Link->Type() == SCH_COMPONENT_T )
  171. return (SCH_COMPONENT*) m_Link;
  172. return NULL;
  173. }
  174. /**
  175. * Function IsLabelConnected
  176. * tests if the net list object is a hierarchical label or sheet label and is
  177. * connected to an associated hierarchical label or sheet label of \a aNetItem.
  178. *
  179. * @param aNetItem A pointer to a NETLIST_OBJECT to test against.
  180. * @return A bool value of true if there is a connection with \a aNetItem or false
  181. * if no connection to \a aNetItem.
  182. */
  183. bool IsLabelConnected( NETLIST_OBJECT* aNetItem );
  184. /**
  185. * Function IsLabelGlobal
  186. * @return true if the object is a global label
  187. * (i.e. an real global label or a pin label coming
  188. * from a power pin invisible
  189. */
  190. bool IsLabelGlobal() const
  191. {
  192. return ( m_Type == NET_PINLABEL ) || ( m_Type == NET_GLOBLABEL );
  193. }
  194. /**
  195. * Function IsLabelType
  196. * @return true if the object is a label of any type
  197. */
  198. bool IsLabelType() const;
  199. /**
  200. * Function GetNetName
  201. * @return the full net name of the item, i.e. the net name
  202. * from the "best" label, prefixed by the sheet path
  203. */
  204. wxString GetNetName() const;
  205. /**
  206. * Function GetShortNetName
  207. * @return the short net name of the item i.e. the net name
  208. * from the "best" label without any prefix.
  209. * 2 different nets can have the same short name
  210. */
  211. wxString GetShortNetName() const;
  212. /**
  213. * Function ConvertBusToNetListItems
  214. * breaks the text of a bus label type net list object into as many members as
  215. * it contains and creates a #NETLIST_OBJECT for each label and adds it to \a
  216. * aNetListItems.
  217. *
  218. * @param aNetListItems A reference to vector of #NETLIST_OBJECT pointers to add
  219. * the bus label NETLIST_OBJECTs.
  220. */
  221. void ConvertBusToNetListItems( NETLIST_OBJECT_LIST& aNetListItems );
  222. };
  223. /**
  224. * NETLIST_OBJECT_LIST is a class to handle the list of connected items
  225. * in a full shematic hierarchy for netlist and erc calculations
  226. */
  227. class NETLIST_OBJECT_LIST: public std::vector <NETLIST_OBJECT*>
  228. {
  229. bool m_isOwner; // = true if the objects in list are owned my me, and therefore
  230. // the memory should be freed by the destructor and the list cleared
  231. int m_lastNetCode; // Used in intermediate calculation: last net code created
  232. int m_lastBusNetCode; // Used in intermediate calculation:
  233. // last net code created for bus members
  234. public:
  235. /**
  236. * Constructor.
  237. * NETLIST_OBJECT_LIST handle a list of connected items.
  238. * the instance can be owner of items or not.
  239. * If it is the owner, the items are freeed by the destructor
  240. * @param aIsOwner true if the instance is the owner of item list
  241. * (default = false)
  242. */
  243. NETLIST_OBJECT_LIST( bool aIsOwner = false ) { m_isOwner = aIsOwner; }
  244. ~NETLIST_OBJECT_LIST();
  245. void SetOwner( bool aIsOwner ) { m_isOwner = aIsOwner; }
  246. /**
  247. * Function BuildNetListInfo
  248. * the master function of tgis class.
  249. * Build the list of connected objects (pins, labels ...) and
  250. * all info to generate netlists or run ERC diags
  251. * @param aSheets = the flattened sheet list
  252. * @return true if OK, false is not item found
  253. */
  254. bool BuildNetListInfo( SCH_SHEET_LIST& aSheets );
  255. /*
  256. * Acces to an item in list
  257. */
  258. NETLIST_OBJECT* GetItem( unsigned aIdx ) const
  259. {
  260. return *( this->begin() + aIdx );
  261. }
  262. /*
  263. * Acces to an item type
  264. */
  265. NETLIST_ITEM_T GetItemType( unsigned aIdx ) const
  266. {
  267. return GetItem( aIdx )->m_Type;
  268. }
  269. /*
  270. * Acces to an item net code
  271. */
  272. int GetItemNet( unsigned aIdx ) const
  273. {
  274. return GetItem( aIdx )->GetNet();
  275. }
  276. NET_CONNECTION_T GetConnectionType( unsigned aIdx )
  277. {
  278. return GetItem( aIdx )->GetConnectionType();
  279. }
  280. /**
  281. * Set the item connection type:
  282. * UNCONNECTED Pin or Label not connected (error)
  283. * NOCONNECT_SYMBOL_PRESENT Pin not connected but have a NoConnect
  284. * symbol on it (no error)
  285. * PAD_CONNECT Normal connection (no error)
  286. */
  287. void SetConnectionType( unsigned aIdx, NET_CONNECTION_T aFlg = UNCONNECTED )
  288. {
  289. GetItem( aIdx )->SetConnectionType( aFlg );
  290. }
  291. /*
  292. * Delete all objects in list and clear list
  293. * (delete NETLIST_OBJECT items)
  294. */
  295. void FreeList();
  296. /*
  297. * Clear list but do not delete NETLIST_OBJECT items
  298. * (they can be deleted only if the instance is owner of the items
  299. */
  300. void Clear() { this->clear(); }
  301. /**
  302. * Reset the connection type of all items to UNCONNECTED type
  303. */
  304. void ResetConnectionsType( )
  305. {
  306. for( unsigned ii = 0; ii < size(); ii++ )
  307. GetItem( ii )->SetConnectionType( UNCONNECTED );
  308. }
  309. /*
  310. * Sorts the list of connected items by net code
  311. */
  312. void SortListbyNetcode();
  313. /*
  314. * Sorts the list of connected items by sheet.
  315. * This sorting is used when searching "physical" connection between items
  316. * because obviously only items inside the same sheet can be connected
  317. */
  318. void SortListbySheet();
  319. #if defined(DEBUG)
  320. void DumpNetTable()
  321. {
  322. for( unsigned idx = 0; idx < size(); ++idx )
  323. {
  324. GetItem( idx )->Show( std::cout, idx );
  325. }
  326. }
  327. #endif
  328. private:
  329. /*
  330. * Propagate aNewNetCode to items having an internal netcode aOldNetCode
  331. * used to interconnect group of items already physically connected,
  332. * when a new connection is found between aOldNetCode and aNewNetCode
  333. */
  334. void propageNetCode( int aOldNetCode, int aNewNetCode, bool aIsBus );
  335. /*
  336. * This function merges the net codes of groups of objects already connected
  337. * to labels (wires, bus, pins ... ) when 2 labels are equivalents
  338. * (i.e. group objects connected by labels)
  339. */
  340. void labelConnect( NETLIST_OBJECT* aLabelRef );
  341. /* Comparison function to sort by increasing Netcode the list of connected items
  342. */
  343. static bool sortItemsbyNetcode( const NETLIST_OBJECT* Objet1, const NETLIST_OBJECT* Objet2 )
  344. {
  345. return Objet1->GetNet() < Objet2->GetNet();
  346. }
  347. /* Comparison routine to sort items by Sheet Number
  348. */
  349. static bool sortItemsBySheet( const NETLIST_OBJECT* Objet1, const NETLIST_OBJECT* Objet2 )
  350. {
  351. return Objet1->m_SheetPath.Cmp( Objet2->m_SheetPath ) < 0;
  352. }
  353. /*
  354. * Propagate net codes from a parent sheet to an include sheet,
  355. * from a pin sheet connection
  356. */
  357. void sheetLabelConnect( NETLIST_OBJECT* aSheetLabel );
  358. void pointToPointConnect( NETLIST_OBJECT* aRef, bool aIsBus, int start );
  359. /*
  360. * Search connections betweena junction and segments
  361. * Propagate the junction net code to objects connected by this junction.
  362. * The junction must have a valid net code
  363. * The list of objects is expected sorted by sheets.
  364. * Search is done from index aIdxStart to the last element of list
  365. */
  366. void segmentToPointConnect( NETLIST_OBJECT* aJonction, bool aIsBus, int aIdxStart );
  367. void connectBusLabels();
  368. /*
  369. * Set the m_FlagOfConnection member of items in list
  370. * depending on the connection type:
  371. * UNCONNECTED, PAD_CONNECT or NOCONNECT_SYMBOL_PRESENT
  372. * The list is expected sorted by order of net code,
  373. * i.e. items having the same net code are grouped
  374. */
  375. void setUnconnectedFlag();
  376. /**
  377. * Function findBestNetNameForEachNet
  378. * fill the .m_NetNameCandidate member of each item of aNetItemBuffer
  379. * with a reference to the "best" NETLIST_OBJECT usable to give a name to the net
  380. * If no suitable object found, .m_NetNameCandidate is filled with 0.
  381. * The "best" NETLIST_OBJECT is a NETLIST_OBJECT that have the type label
  382. * and by priority order:
  383. * the label is global or local
  384. * the label is in the first sheet in a hierarchy (the root sheet has the most priority)
  385. * alphabetic order.
  386. */
  387. void findBestNetNameForEachNet();
  388. };
  389. /**
  390. * Function IsBusLabel
  391. * test if \a aLabel has a bus notation.
  392. *
  393. * @param aLabel A wxString object containing the label to test.
  394. * @return true if text is a bus notation format otherwise false is returned.
  395. */
  396. extern bool IsBusLabel( const wxString& aLabel );
  397. #endif // _CLASS_NETLIST_OBJECT_H_