-
Notifications
You must be signed in to change notification settings - Fork 24
Warn when chip power pins lack decoupling capacitors #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0hmX
wants to merge
3
commits into
main
Choose a base branch
from
emdash/tired-brooms-attend-71wd2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { | ||
| AnyCircuitElement, | ||
| SourcePinMissingTraceWarning, | ||
| } from "circuit-json" | ||
| import { DecouplingCapacitorChecker } from "./check-chip-power-pins-have-decoupling-capacitors/decoupling-capacitor-checker" | ||
|
|
||
| export const checkChipPowerPinsHaveDecouplingCapacitors = ( | ||
| circuitJson: AnyCircuitElement[], | ||
| ): SourcePinMissingTraceWarning[] => | ||
| new DecouplingCapacitorChecker(circuitJson).getWarnings() | ||
28 changes: 28 additions & 0 deletions
28
...capacitors/decoupling-capacitor-checker-capacitor-connects-power-source-port-to-ground.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { SourcePort, SourceSimpleCapacitor } from "circuit-json" | ||
| import type { DecouplingCapacitorChecker } from "./decoupling-capacitor-checker" | ||
|
|
||
| export const capacitorConnectsPowerSourcePortToGround = ( | ||
| checker: DecouplingCapacitorChecker, | ||
| capacitorSourceComponent: SourceSimpleCapacitor, | ||
| chipPowerSourcePort: SourcePort, | ||
| ): boolean => { | ||
| const capacitorSourcePorts = checker.getSourcePorts( | ||
| capacitorSourceComponent.source_component_id, | ||
| ) | ||
| if (capacitorSourcePorts.length !== 2) return false | ||
|
|
||
| const [firstCapacitorSourcePort, secondCapacitorSourcePort] = | ||
| capacitorSourcePorts | ||
| return ( | ||
| (checker.sourceConnectivityMap.areIdsConnected( | ||
| chipPowerSourcePort.source_port_id, | ||
| firstCapacitorSourcePort.source_port_id, | ||
| ) && | ||
| checker.sourcePortIsConnectedToGround(secondCapacitorSourcePort)) || | ||
| (checker.sourceConnectivityMap.areIdsConnected( | ||
| chipPowerSourcePort.source_port_id, | ||
| secondCapacitorSourcePort.source_port_id, | ||
| ) && | ||
| checker.sourcePortIsConnectedToGround(firstCapacitorSourcePort)) | ||
| ) | ||
| } |
48 changes: 48 additions & 0 deletions
48
...k-chip-power-pins-have-decoupling-capacitors/decoupling-capacitor-checker-get-warnings.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { SourcePinMissingTraceWarning } from "circuit-json" | ||
| import type { DecouplingCapacitorChecker } from "./decoupling-capacitor-checker" | ||
| import { getSourcePortDisplayLabel } from "./get-source-port-display-label" | ||
|
|
||
| export const getDecouplingCapacitorWarnings = ( | ||
| checker: DecouplingCapacitorChecker, | ||
| ): SourcePinMissingTraceWarning[] => { | ||
| const warnings: SourcePinMissingTraceWarning[] = [] | ||
|
|
||
| for (const chipSourceComponent of checker.sourceComponents) { | ||
| if (chipSourceComponent.ftype !== "simple_chip") continue | ||
|
|
||
| for (const chipSourcePort of checker.getSourcePorts( | ||
| chipSourceComponent.source_component_id, | ||
| )) { | ||
| if (!checker.sourcePortShouldHaveDecouplingCapacitor(chipSourcePort)) { | ||
| continue | ||
| } | ||
| if (!checker.sourcePortHasConnection(chipSourcePort)) continue | ||
|
|
||
| const hasDecouplingCapacitor = checker.capacitorSourceComponents.some( | ||
| (capacitorSourceComponent) => | ||
| checker.capacitorConnectsPowerSourcePortToGround( | ||
| capacitorSourceComponent, | ||
| chipSourcePort, | ||
| ), | ||
| ) | ||
| if (hasDecouplingCapacitor) continue | ||
|
|
||
| const recommendedCapacitance = | ||
| chipSourcePort.recommended_decoupling_capacitor_capacitance | ||
| const capacitanceDescription = | ||
| recommendedCapacitance === undefined ? "" : ` ${recommendedCapacitance}` | ||
|
|
||
| warnings.push({ | ||
| type: "source_pin_missing_trace_warning", | ||
| source_pin_missing_trace_warning_id: `source_pin_missing_trace_warning_decoupling_${chipSourcePort.source_port_id}`, | ||
| warning_type: "source_pin_missing_trace_warning", | ||
| message: `Power pin ${getSourcePortDisplayLabel(chipSourcePort)} on ${chipSourceComponent.name} should have a${capacitanceDescription} decoupling capacitor connected to ground`, | ||
| source_component_id: chipSourceComponent.source_component_id, | ||
| source_port_id: chipSourcePort.source_port_id, | ||
| subcircuit_id: chipSourcePort.subcircuit_id, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return warnings | ||
| } |
15 changes: 15 additions & 0 deletions
15
...ins-have-decoupling-capacitors/decoupling-capacitor-checker-source-port-has-connection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import type { SourcePort } from "circuit-json" | ||
| import type { DecouplingCapacitorChecker } from "./decoupling-capacitor-checker" | ||
|
|
||
| export const sourcePortHasConnection = ( | ||
| checker: DecouplingCapacitorChecker, | ||
| sourcePort: SourcePort, | ||
| ): boolean => { | ||
| const connectedNetId = checker.sourceConnectivityMap.getNetConnectedToId( | ||
| sourcePort.source_port_id, | ||
| ) | ||
| if (!connectedNetId) return false | ||
| return checker.sourceConnectivityMap | ||
| .getIdsConnectedToNet(connectedNetId) | ||
| .some((connectedId) => connectedId !== sourcePort.source_port_id) | ||
| } |
19 changes: 19 additions & 0 deletions
19
...-decoupling-capacitors/decoupling-capacitor-checker-source-port-is-connected-to-ground.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { SourcePort } from "circuit-json" | ||
| import type { DecouplingCapacitorChecker } from "./decoupling-capacitor-checker" | ||
|
|
||
| export const sourcePortIsConnectedToGround = ( | ||
| checker: DecouplingCapacitorChecker, | ||
| sourcePort: SourcePort, | ||
| ): boolean => | ||
| checker.groundSourcePorts.some((groundSourcePort) => | ||
| checker.sourceConnectivityMap.areIdsConnected( | ||
| sourcePort.source_port_id, | ||
| groundSourcePort.source_port_id, | ||
| ), | ||
| ) || | ||
| checker.groundSourceNets.some((groundSourceNet) => | ||
| checker.sourceConnectivityMap.areIdsConnected( | ||
| sourcePort.source_port_id, | ||
| groundSourceNet.source_net_id, | ||
| ), | ||
| ) |
12 changes: 12 additions & 0 deletions
12
...g-capacitors/decoupling-capacitor-checker-source-port-should-have-decoupling-capacitor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import type { SourcePort } from "circuit-json" | ||
|
|
||
| export const sourcePortShouldHaveDecouplingCapacitor = ( | ||
| sourcePort: SourcePort, | ||
| ): boolean => { | ||
| if (sourcePort.should_have_decoupling_capacitor !== undefined) { | ||
| return sourcePort.should_have_decoupling_capacitor | ||
| } | ||
| return ( | ||
| sourcePort.requires_power === true && sourcePort.provides_power !== true | ||
| ) | ||
| } |
96 changes: 96 additions & 0 deletions
96
lib/check-chip-power-pins-have-decoupling-capacitors/decoupling-capacitor-checker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { cju } from "@tscircuit/circuit-json-util" | ||
| import type { | ||
| AnyCircuitElement, | ||
| SourceComponentBase, | ||
| SourceNet, | ||
| SourcePinMissingTraceWarning, | ||
| SourcePort, | ||
| SourceSimpleCapacitor, | ||
| } from "circuit-json" | ||
| import { | ||
| type ConnectivityMap, | ||
| getSourcePortConnectivityMapFromCircuitJson, | ||
| } from "circuit-json-to-connectivity-map" | ||
| import { capacitorConnectsPowerSourcePortToGround } from "./decoupling-capacitor-checker-capacitor-connects-power-source-port-to-ground" | ||
| import { getDecouplingCapacitorWarnings } from "./decoupling-capacitor-checker-get-warnings" | ||
| import { sourcePortHasConnection } from "./decoupling-capacitor-checker-source-port-has-connection" | ||
| import { sourcePortIsConnectedToGround } from "./decoupling-capacitor-checker-source-port-is-connected-to-ground" | ||
| import { sourcePortShouldHaveDecouplingCapacitor } from "./decoupling-capacitor-checker-source-port-should-have-decoupling-capacitor" | ||
| import type { SourceComponentId } from "./types" | ||
|
|
||
| export class DecouplingCapacitorChecker { | ||
| readonly sourceComponents: SourceComponentBase[] | ||
| readonly sourcePorts: SourcePort[] | ||
| readonly groundSourcePorts: SourcePort[] | ||
| readonly groundSourceNets: SourceNet[] | ||
| readonly capacitorSourceComponents: SourceSimpleCapacitor[] | ||
| readonly sourcePortsBySourceComponentId = new Map< | ||
| SourceComponentId, | ||
| SourcePort[] | ||
| >() | ||
| readonly sourceConnectivityMap: ConnectivityMap | ||
|
|
||
| constructor(circuitJson: AnyCircuitElement[]) { | ||
| const db = cju(circuitJson) | ||
| this.sourceComponents = db.source_component.list() as SourceComponentBase[] | ||
| this.sourcePorts = db.source_port.list() | ||
| this.groundSourcePorts = this.sourcePorts.filter( | ||
| (sourcePort) => | ||
| sourcePort.requires_ground === true || | ||
| sourcePort.provides_ground === true, | ||
| ) | ||
| this.groundSourceNets = db.source_net | ||
| .list() | ||
| .filter((sourceNet) => sourceNet.is_ground) | ||
| this.capacitorSourceComponents = this.sourceComponents.filter( | ||
| (sourceComponent): sourceComponent is SourceSimpleCapacitor => | ||
| sourceComponent.ftype === "simple_capacitor", | ||
| ) | ||
| this.sourceConnectivityMap = | ||
| getSourcePortConnectivityMapFromCircuitJson(circuitJson) | ||
|
|
||
| for (const sourcePort of this.sourcePorts) { | ||
| if (!sourcePort.source_component_id) continue | ||
| const sourceComponentPorts = | ||
| this.sourcePortsBySourceComponentId.get( | ||
| sourcePort.source_component_id, | ||
| ) ?? [] | ||
| sourceComponentPorts.push(sourcePort) | ||
| this.sourcePortsBySourceComponentId.set( | ||
| sourcePort.source_component_id, | ||
| sourceComponentPorts, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| getWarnings(): SourcePinMissingTraceWarning[] { | ||
| return getDecouplingCapacitorWarnings(this) | ||
| } | ||
|
|
||
| getSourcePorts(sourceComponentId: SourceComponentId): SourcePort[] { | ||
| return this.sourcePortsBySourceComponentId.get(sourceComponentId) ?? [] | ||
| } | ||
|
|
||
| sourcePortShouldHaveDecouplingCapacitor(sourcePort: SourcePort): boolean { | ||
| return sourcePortShouldHaveDecouplingCapacitor(sourcePort) | ||
| } | ||
|
|
||
| sourcePortHasConnection(sourcePort: SourcePort): boolean { | ||
| return sourcePortHasConnection(this, sourcePort) | ||
| } | ||
|
|
||
| sourcePortIsConnectedToGround(sourcePort: SourcePort): boolean { | ||
| return sourcePortIsConnectedToGround(this, sourcePort) | ||
| } | ||
|
|
||
| capacitorConnectsPowerSourcePortToGround( | ||
| capacitorSourceComponent: SourceSimpleCapacitor, | ||
| chipPowerSourcePort: SourcePort, | ||
| ): boolean { | ||
| return capacitorConnectsPowerSourcePortToGround( | ||
| this, | ||
| capacitorSourceComponent, | ||
| chipPowerSourcePort, | ||
| ) | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
lib/check-chip-power-pins-have-decoupling-capacitors/get-source-port-display-label.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { SourcePort } from "circuit-json" | ||
|
|
||
| const sourcePortLabelIsGenericPinName = (sourcePortLabel: string): boolean => { | ||
| const normalizedSourcePortLabel = sourcePortLabel.trim().toLowerCase() | ||
| const possiblePinNumber = normalizedSourcePortLabel.startsWith("pin") | ||
| ? normalizedSourcePortLabel.slice(3) | ||
| : normalizedSourcePortLabel | ||
| return ( | ||
| possiblePinNumber.length > 0 && | ||
| [...possiblePinNumber].every( | ||
| (character) => character >= "0" && character <= "9", | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| export const getSourcePortDisplayLabel = (sourcePort: SourcePort): string => | ||
| sourcePort.port_hints?.find( | ||
| (sourcePortHint) => !sourcePortLabelIsGenericPinName(sourcePortHint), | ||
| ) ?? sourcePort.name |
4 changes: 4 additions & 0 deletions
4
lib/check-chip-power-pins-have-decoupling-capacitors/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import type { SourceComponentBase } from "circuit-json" | ||
|
|
||
| // circuit-json does not currently export a standalone source-component ID type. | ||
| export type SourceComponentId = SourceComponentBase["source_component_id"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.