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.

526 lines
18 KiB

7 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 CERN
  5. * @author Jon Evans <jon@craftyjon.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef _CONNECTION_GRAPH_H
  21. #define _CONNECTION_GRAPH_H
  22. #include <mutex>
  23. #include <vector>
  24. #include <erc_settings.h>
  25. #include <sch_connection.h>
  26. #include <sch_item.h>
  27. #ifdef DEBUG
  28. // Uncomment this line to enable connectivity debugging features
  29. // #define CONNECTIVITY_DEBUG
  30. #endif
  31. class CONNECTION_GRAPH;
  32. class SCHEMATIC;
  33. class SCH_EDIT_FRAME;
  34. class SCH_HIERLABEL;
  35. class SCH_PIN;
  36. class SCH_SHEET_PIN;
  37. /**
  38. * A subgraph is a set of items that are electrically connected on a single sheet.
  39. *
  40. * For example, a label connected to a wire and so on.
  41. * A net is composed of one or more subgraphs.
  42. *
  43. * A set of items that appears to be physically connected may actually be more
  44. * than one subgraph, because some items don't connect electrically.
  45. *
  46. * For example, multiple bus wires can come together at a junction but have
  47. * different labels on each branch. Each label+wire branch is its own subgraph.
  48. *
  49. */
  50. class CONNECTION_SUBGRAPH
  51. {
  52. public:
  53. enum class PRIORITY
  54. {
  55. INVALID = -1,
  56. NONE = 0,
  57. PIN,
  58. SHEET_PIN,
  59. HIER_LABEL,
  60. LOCAL_LABEL,
  61. POWER_PIN,
  62. GLOBAL
  63. };
  64. explicit CONNECTION_SUBGRAPH( CONNECTION_GRAPH* aGraph ) :
  65. m_graph( aGraph ),
  66. m_dirty( false ),
  67. m_absorbed( false ),
  68. m_absorbed_by( nullptr ),
  69. m_code( -1 ),
  70. m_multiple_drivers( false ),
  71. m_strong_driver( false ),
  72. m_local_driver( false ),
  73. m_no_connect( nullptr ),
  74. m_bus_entry( nullptr ),
  75. m_driver( nullptr ),
  76. m_driver_connection( nullptr ),
  77. m_hier_parent( nullptr )
  78. {}
  79. ~CONNECTION_SUBGRAPH() = default;
  80. /**
  81. * Determines which potential driver should drive the subgraph.
  82. *
  83. * If multiple possible drivers exist, picks one according to the priority.
  84. * If multiple "winners" exist, returns false and sets m_driver to nullptr.
  85. *
  86. * @param aCreateMarkers controls whether ERC markers should be added for conflicts
  87. * @return true if m_driver was set, or false if a conflict occurred
  88. */
  89. bool ResolveDrivers( bool aCreateMarkers = false );
  90. /**
  91. * Returns the fully-qualified net name for this subgraph (if one exists)
  92. */
  93. wxString GetNetName() const;
  94. /// Returns all the bus labels attached to this subgraph (if any)
  95. std::vector<SCH_ITEM*> GetBusLabels() const;
  96. /// Returns the candidate net name for a driver
  97. const wxString& GetNameForDriver( SCH_ITEM* aItem );
  98. /// Combines another subgraph on the same sheet into this one.
  99. void Absorb( CONNECTION_SUBGRAPH* aOther );
  100. /// Adds a new item to the subgraph
  101. void AddItem( SCH_ITEM* aItem );
  102. /// Updates all items to match the driver connection
  103. void UpdateItemConnections();
  104. /**
  105. * Returns the priority (higher is more important) of a candidate driver
  106. *
  107. * 0: Invalid driver
  108. * 1: Component pin
  109. * 2: Hierarchical sheet pin
  110. * 3: Hierarchical label
  111. * 4: Local label
  112. * 5: Power pin
  113. * 6: Global label
  114. *
  115. * @param aDriver is the item to inspect
  116. * @return a PRIORITY
  117. */
  118. static PRIORITY GetDriverPriority( SCH_ITEM* aDriver );
  119. PRIORITY GetDriverPriority()
  120. {
  121. if( m_driver )
  122. return GetDriverPriority( m_driver );
  123. else
  124. return PRIORITY::NONE;
  125. }
  126. CONNECTION_GRAPH* m_graph;
  127. bool m_dirty;
  128. /// True if this subgraph has been absorbed into another. No pointers here are safe if so!
  129. bool m_absorbed;
  130. /// If this subgraph is absorbed, points to the absorbing (and valid) subgraph
  131. CONNECTION_SUBGRAPH* m_absorbed_by;
  132. long m_code;
  133. /**
  134. * True if this subgraph contains more than one driver that should be
  135. * shorted together in the netlist. For example, two labels or
  136. * two power ports.
  137. */
  138. bool m_multiple_drivers;
  139. /// True if the driver is "strong": a label or power object
  140. bool m_strong_driver;
  141. /// True if the driver is a local (i.e. non-global) type
  142. bool m_local_driver;
  143. /// No-connect item in graph, if any
  144. SCH_ITEM* m_no_connect;
  145. /// Bus entry in graph, if any
  146. SCH_ITEM* m_bus_entry;
  147. std::vector<SCH_ITEM*> m_items;
  148. std::vector<SCH_ITEM*> m_drivers;
  149. SCH_ITEM* m_driver;
  150. SCH_SHEET_PATH m_sheet;
  151. /// Cache for driver connection
  152. SCH_CONNECTION* m_driver_connection;
  153. /**
  154. * If a subgraph is a bus, this map contains links between the bus members and any
  155. * local sheet neighbors with the same connection name.
  156. *
  157. * For example, if this subgraph is a bus D[7..0], and on the same sheet there is
  158. * a net with label D7, this map will contain an entry for the D7 bus member, and
  159. * the vector will contain a pointer to the D7 net subgraph.
  160. */
  161. std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
  162. std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_neighbors;
  163. /**
  164. * If this is a net, this vector contains links to any same-sheet buses that contain it.
  165. * The string key is the name of the connection that forms the link (which isn't necessarily
  166. * the same as the name of the connection driving this subgraph)
  167. */
  168. std::unordered_map< std::shared_ptr<SCH_CONNECTION>,
  169. std::unordered_set<CONNECTION_SUBGRAPH*> > m_bus_parents;
  170. // Cache for lookup of any hierarchical (sheet) pins on this subgraph (for referring down)
  171. std::vector<SCH_SHEET_PIN*> m_hier_pins;
  172. // Cache for lookup of any hierarchical ports on this subgraph (for referring up)
  173. std::vector<SCH_HIERLABEL*> m_hier_ports;
  174. // If not null, this indicates the subgraph on a higher level sheet that is linked to this one
  175. CONNECTION_SUBGRAPH* m_hier_parent;
  176. /// A cache of escaped netnames from schematic items
  177. std::unordered_map<SCH_ITEM*, wxString> m_driver_name_cache;
  178. };
  179. /// Associates a net code with the final name of a net
  180. typedef std::pair<wxString, int> NET_NAME_CODE;
  181. /// Associates a NET_CODE_NAME with all the subgraphs in that net
  182. typedef std::map<NET_NAME_CODE, std::vector<CONNECTION_SUBGRAPH*>> NET_MAP;
  183. /**
  184. * Calculates the connectivity of a schematic and generates netlists
  185. */
  186. class CONNECTION_GRAPH
  187. {
  188. public:
  189. CONNECTION_GRAPH( SCHEMATIC* aSchematic = nullptr ) :
  190. m_last_net_code( 1 ),
  191. m_last_bus_code( 1 ),
  192. m_last_subgraph_code( 1 ),
  193. m_schematic( aSchematic )
  194. {}
  195. ~CONNECTION_GRAPH()
  196. {
  197. Reset();
  198. }
  199. void Reset();
  200. void SetSchematic( SCHEMATIC* aSchematic )
  201. {
  202. m_schematic = aSchematic;
  203. }
  204. /**
  205. * Updates the connection graph for the given list of sheets.
  206. *
  207. * @param aSheetList is the list of possibly modified sheets
  208. * @param aUnconditional is true if an unconditional full recalculation should be done
  209. * @param aChangedItemHandler an optional handler to receive any changed items
  210. */
  211. void Recalculate( const SCH_SHEET_LIST& aSheetList, bool aUnconditional = false,
  212. std::function<void( SCH_ITEM* )>* aChangedItemHandler = nullptr );
  213. /**
  214. * Returns a bus alias pointer for the given name if it exists (from cache)
  215. *
  216. * CONNECTION_GRAPH caches these, they are owned by the SCH_SCREEN that
  217. * the alias was defined on. The cache is only used to update the graph.
  218. */
  219. std::shared_ptr<BUS_ALIAS> GetBusAlias( const wxString& aName );
  220. /**
  221. * Determines which subgraphs have more than one conflicting bus label.
  222. *
  223. * @see DIALOG_MIGRATE_BUSES
  224. * @return a list of subgraphs that need migration
  225. */
  226. std::vector<const CONNECTION_SUBGRAPH*> GetBusesNeedingMigration();
  227. /**
  228. * Runs electrical rule checks on the connectivity graph.
  229. *
  230. * Precondition: graph is up-to-date
  231. *
  232. * @return the number of errors found
  233. */
  234. int RunERC();
  235. const NET_MAP& GetNetMap() const { return m_net_code_to_subgraphs_map; }
  236. /**
  237. * Returns the subgraph for a given net name on a given sheet
  238. * @param aNetName is the local net name to look for
  239. * @param aPath is a sheet path to look on
  240. * @return the subgraph matching the query, or nullptr if none is found
  241. */
  242. CONNECTION_SUBGRAPH* FindSubgraphByName( const wxString& aNetName,
  243. const SCH_SHEET_PATH& aPath );
  244. /**
  245. * Retrieves a subgraph for the given net name, if one exists.
  246. * Searches every sheet
  247. * @param aNetName is the full net name to search for
  248. * @return the subgraph matching the query, or nullptr if none is found
  249. */
  250. CONNECTION_SUBGRAPH* FindFirstSubgraphByName( const wxString& aNetName );
  251. CONNECTION_SUBGRAPH* GetSubgraphForItem( SCH_ITEM* aItem );
  252. // TODO(JE) Remove this when pressure valve is removed
  253. static bool m_allowRealTime;
  254. private:
  255. // All the sheets in the schematic (as long as we don't have partial updates)
  256. SCH_SHEET_LIST m_sheetList;
  257. // All connectable items in the schematic
  258. std::vector<SCH_ITEM*> m_items;
  259. // The owner of all CONNECTION_SUBGRAPH objects
  260. std::vector<CONNECTION_SUBGRAPH*> m_subgraphs;
  261. // Cache of a subset of m_subgraphs
  262. std::vector<CONNECTION_SUBGRAPH*> m_driver_subgraphs;
  263. // Cache to lookup subgraphs in m_driver_subgraphs by sheet path
  264. std::unordered_map<SCH_SHEET_PATH, std::vector<CONNECTION_SUBGRAPH*>> m_sheet_to_subgraphs_map;
  265. std::vector<std::pair<SCH_SHEET_PATH, SCH_PIN*>> m_invisible_power_pins;
  266. std::unordered_map< wxString, std::shared_ptr<BUS_ALIAS> > m_bus_alias_cache;
  267. std::map<wxString, int> m_net_name_to_code_map;
  268. std::map<wxString, int> m_bus_name_to_code_map;
  269. std::map<wxString, std::vector<const CONNECTION_SUBGRAPH*>> m_global_label_cache;
  270. std::map< std::pair<SCH_SHEET_PATH, wxString>,
  271. std::vector<const CONNECTION_SUBGRAPH*> > m_local_label_cache;
  272. std::unordered_map<wxString, std::vector<CONNECTION_SUBGRAPH*>> m_net_name_to_subgraphs_map;
  273. std::map<SCH_ITEM*, CONNECTION_SUBGRAPH*> m_item_to_subgraph_map;
  274. NET_MAP m_net_code_to_subgraphs_map;
  275. int m_last_net_code;
  276. int m_last_bus_code;
  277. int m_last_subgraph_code;
  278. SCHEMATIC* m_schematic; ///< The schematic this graph represents
  279. /**
  280. * Updates the graphical connectivity between items (i.e. where they touch)
  281. * The items passed in must be on the same sheet.
  282. *
  283. * In the first phase, all items in aItemList have their connections
  284. * initialized for the given sheet (since they may have connections on more
  285. * than one sheet, and each needs to be calculated individually). The
  286. * graphical connection points for the item are added to a map that stores
  287. * (x, y) -> [list of items].
  288. *
  289. * Any item that is stored in the list of items that have a connection point
  290. * at a given (x, y) location will eventually be electrically connected.
  291. * This means that we can't store SCH_COMPONENTs in this map -- we must store
  292. * a structure that links a specific pin on a component back to that
  293. * component: a SCH_PIN_CONNECTION. This wrapper class is a convenience for
  294. * linking a pin and component to a specific (x, y) point.
  295. *
  296. * In the second phase, we iterate over each value in the map, which is a
  297. * vector of items that have overlapping connection points. After some
  298. * checks to ensure that the items should actually connect, the items are
  299. * linked together using ConnectedItems().
  300. *
  301. * As a side effect, items are loaded into m_items for BuildConnectionGraph()
  302. *
  303. * @param aSheet is the path to the sheet of all items in the list
  304. * @param aItemList is a list of items to consider
  305. */
  306. void updateItemConnectivity( const SCH_SHEET_PATH& aSheet,
  307. const std::vector<SCH_ITEM*>& aItemList );
  308. /**
  309. * Generates the connection graph (after all item connectivity has been updated)
  310. *
  311. * In the first phase, the algorithm iterates over all items, and then over
  312. * all items that are connected (graphically) to each item, placing them into
  313. * CONNECTION_SUBGRAPHs. Items that can potentially drive connectivity (i.e.
  314. * labels, pins, etc.) are added to the m_drivers vector of the subgraph.
  315. *
  316. * In the second phase, each subgraph is resolved. To resolve a subgraph,
  317. * the driver is first selected by CONNECTION_SUBGRAPH::ResolveDrivers(),
  318. * and then the connection for the chosen driver is propagated to all the
  319. * other items in the subgraph.
  320. */
  321. void buildConnectionGraph();
  322. /**
  323. * Helper to assign a new net code to a connection
  324. *
  325. * @return the assigned code
  326. */
  327. int assignNewNetCode( SCH_CONNECTION& aConnection );
  328. /**
  329. * Ensures all members of the bus connection have a valid net code assigned
  330. * @param aConnection is a bus connection
  331. */
  332. void assignNetCodesToBus( SCH_CONNECTION* aConnection );
  333. /**
  334. * Updates all neighbors of a subgraph with this one's connectivity info
  335. *
  336. * If this subgraph contains hierarchical links, this method will descent the
  337. * hierarchy and propagate the connectivity across all linked sheets.
  338. */
  339. void propagateToNeighbors( CONNECTION_SUBGRAPH* aSubgraph );
  340. /**
  341. * Search for a matching bus member inside a bus connection
  342. *
  343. * For bus groups, this returns a bus member that matches aSearch by name.
  344. * For bus vectors, this returns a bus member that matches by vector index.
  345. *
  346. * @param aBusConnection is the bus connection to search
  347. * @param aSearch is the net connection to search for
  348. * @returns a member of aBusConnection that matches aSearch
  349. */
  350. static SCH_CONNECTION* matchBusMember( SCH_CONNECTION* aBusConnection,
  351. SCH_CONNECTION* aSearch );
  352. /**
  353. * Builds a new default connection for the given item based on its properties.
  354. * Handles strong drivers (power pins and labels) only
  355. *
  356. * @param aItem is an item that can generate a connection name
  357. * @param aSubgraph is used to determine the sheet to use and retrieve the cached name
  358. * @return a connection generated from the item, or nullptr if item is not valid
  359. */
  360. std::shared_ptr<SCH_CONNECTION> getDefaultConnection( SCH_ITEM* aItem,
  361. CONNECTION_SUBGRAPH* aSubgraph );
  362. void recacheSubgraphName( CONNECTION_SUBGRAPH* aSubgraph, const wxString& aOldName );
  363. /**
  364. * Checks one subgraph for conflicting connections between net and bus labels
  365. *
  366. * For example, a net wire connected to a bus port/pin, or vice versa
  367. *
  368. * @param aSubgraph is the subgraph to examine
  369. * @return true for no errors, false for errors
  370. */
  371. bool ercCheckBusToNetConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
  372. /**
  373. * Checks one subgraph for conflicting connections between two bus items
  374. *
  375. * For example, a labeled bus wire connected to a hierarchical sheet pin
  376. * where the labeled bus doesn't contain any of the same bus members as the
  377. * sheet pin
  378. *
  379. * @param aSubgraph is the subgraph to examine
  380. * @return true for no errors, false for errors
  381. */
  382. bool ercCheckBusToBusConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
  383. /**
  384. * Checks one subgraph for conflicting bus entry to bus connections
  385. *
  386. * For example, a wire with label "A0" is connected to a bus labeled "D[8..0]"
  387. *
  388. * Will also check for mistakes related to bus group names, for example:
  389. * A bus group named "USB{DP DM}" should have bus entry connections like
  390. * "USB.DP" but someone might accidentally just enter "DP"
  391. *
  392. * @param aSubgraph is the subgraph to examine
  393. * @return true for no errors, false for errors
  394. */
  395. bool ercCheckBusToBusEntryConflicts( const CONNECTION_SUBGRAPH* aSubgraph );
  396. /**
  397. * Checks one subgraph for proper presence or absence of no-connect symbols
  398. *
  399. * A pin with a no-connect symbol should not have any connections
  400. * A pin without a no-connect symbol should have at least one connection
  401. *
  402. * @param aSubgraph is the subgraph to examine
  403. * @return true for no errors, false for errors
  404. */
  405. bool ercCheckNoConnects( const CONNECTION_SUBGRAPH* aSubgraph );
  406. /**
  407. * Checks one subgraph for floating wires
  408. *
  409. * Will throw an error for any subgraph that consists of just wires with no driver
  410. *
  411. * @param aSubgraph is the subgraph to examine
  412. * @return true for no errors, false for errors
  413. */
  414. bool ercCheckFloatingWires( const CONNECTION_SUBGRAPH* aSubgraph );
  415. /**
  416. * Checks one subgraph for proper connection of labels
  417. *
  418. * Labels should be connected to something
  419. *
  420. * @param aSubgraph is the subgraph to examine
  421. * @param aCheckGlobalLabels is true if global labels should be checked for loneliness
  422. * @return true for no errors, false for errors
  423. */
  424. bool ercCheckLabels( const CONNECTION_SUBGRAPH* aSubgraph );
  425. /**
  426. * Checks that a hierarchical sheet has at least one matching label inside the sheet for each
  427. * port on the parent sheet object
  428. *
  429. * @param aSubgraph is the subgraph to examine
  430. * @return the number of errors found
  431. */
  432. int ercCheckHierSheets();
  433. };
  434. #endif