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.

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