Skip to content
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

Added more functions for introspecting pads of a component #182

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/design/introspection.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defpackage jsl/design/introspection:
import jitx
import jitx/commands

import maybe-utils

import jsl/errors
import jsl/landpatterns/introspection

Expand Down Expand Up @@ -269,6 +271,21 @@ public defn get-pads-from-port (pt:JITXObject, cxt:JITXObject = self) -> Maybe<T
val p2p-map = port-to-pads(value(given))
get-pads-from-port(p2p-map, pt)

doc: \<DOC>
Get pads for each port and combine them into a single list
<DOC>
public defn get-pads-from-ports (pts:Seqable<JITXObject>, cxt:JITXObject = self) -> Seq<JITXObject>:
for pt in pts seq-cat:
val pds? = get-pads-from-port(pt, cxt)
value-or(pds?, [])

doc: \<DOC>
Convert a Sequence of JITXObjects to LandPatternPad objects.
<DOC>
public defn to-lp-pad-seq (objs:Seq<JITXObject>) -> Seq<LandPatternPad> :
for obj in objs seq:
obj as LandPatternPad
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only possible change is to match and throw an exception instead of cast -- slightly scary to expose a blind cast via public API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - perhaps I'm being too cavalier with the as operator. Will that fatal if it doesn't match correct ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure it will fatal, and I think weird stuff can happen in optimized mode

Honestly this function seems a little specific for jsl; it feels more like user code. We could actually provide a more general version of this function in jsl instead.
There are two options:

downcast-objs<T> (objs:Seq<JITXObject>) -> Seq<T>, which matches all objects in the sequence against the explicit type parameter

downcast-objs<?T> (objs:Seq<JITXObject&?T> -> Seq<T> which infers the subtype of JITXObject -- but might not always infer the strongest type T if you give it a heterogeneous collection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya - so I think this function should be in JITX not JSL if anywhere.
In this particular case, I'm confident that this JITXObject is a LandPatternPad. I could put this code in the design but it is going to be duplicated everywhere I use this pattern.
The other option is to change the bounds function to accept a JITXObject but that really just moves the problem. When bounds tries to inspect a LandPatternPad and it doesn't find one - then an error will be raised there. I think the jsl/landpatterns/leads/bounds function was probably written when I was trying for typing - but perhaps I need to re-evaluate that instead of adding this function.


doc: \<DOC>
Retrieve the parent instance (component or module) for a port.

Expand Down Expand Up @@ -508,15 +525,15 @@ pcb-module top-level:
; This will return two nets: one for each statement above
val n1 = get-connected-nets(W.GND)

; This will return an empty tuple
; This will return an empty tuple
val n2 = get-connected-nets(W.C.internal-port)
```

`n1` will be a tuple of two `net` objects, since
there are two `net` statements defined in the `pcb-module` context.

The `n2` invocation will fail to find `INT` as the net
for `W.C.internal-port` and return an empty tuple
for `W.C.internal-port` and return an empty tuple

@param pt Port of a component or module in this module context.
@param cxt Optional context to search. The default value is `self`.
Expand Down