netlist_db/
instance.rs

1use std::{borrow::Cow, collections::HashSet};
2
3use crate::{
4    ast::{KeyValueBuilder, ValueBuilder},
5    self_builder,
6    span::Span,
7};
8use netlist_macros::Builder;
9
10/// ``` spice
11/// XX1 net48 D VDD VNW PHVT11LL_CKT W=0.22u L=40.00n
12/// ```
13#[derive(Debug, Clone, Builder)]
14pub struct InstanceBuilder {
15    pub name: Span,
16    /// subckt/model name is the last arg
17    /// (fisrt, rest)
18    pub ctx: InstanceCtxBuilder,
19}
20
21#[derive(Debug, Clone, Builder)]
22pub enum InstanceCtxBuilder {
23    Resistor(ResistorBuilder),
24    Capacitor(CapacitorBuilder),
25    Inductor(InductorBuilder),
26    Voltage(VoltageBuilder),
27    Current(CurrentBuilder),
28    MOSFET(MOSFETBuilder),
29    BJT(BJTBuilder),
30    Diode(DiodeBuilder),
31    Subckt(SubcktBuilder),
32    Unknown {
33        r#type: u8,
34        nodes: Vec<Span>,
35        params: Vec<KeyValueBuilder>,
36    },
37}
38impl<'s> InstanceCtx<'s> {
39    pub fn append_nodes<'a>(&'a self, nodes: &mut HashSet<&'a Cow<'s, str>>) {
40        match self {
41            InstanceCtx::Resistor(r) => nodes.extend([&r.n1, &r.n2]),
42            InstanceCtx::Capacitor(c) => nodes.extend([&c.n1, &c.n2]),
43            InstanceCtx::Inductor(i) => nodes.extend([&i.n1, &i.n2]),
44            InstanceCtx::Voltage(v) => nodes.extend([&v.n1, &v.n2]),
45            InstanceCtx::Current(c) => nodes.extend([&c.n1, &c.n2]),
46            InstanceCtx::MOSFET(m) => {
47                nodes.extend([&m.ng, &m.nd, &m.ns]);
48                nodes.extend(m.nb.as_ref())
49            }
50            InstanceCtx::BJT(b) => {
51                nodes.extend([&b.nb, &b.nc, &b.ne]);
52                nodes.extend(b.ns.as_ref())
53            }
54            InstanceCtx::Diode(d) => nodes.extend([&d.nminus, &d.nplus]),
55            InstanceCtx::Subckt(x) => nodes.extend(x.nodes.iter()),
56            InstanceCtx::Unknown {
57                r#type: _,
58                nodes: _nodes,
59                params: _,
60            } => nodes.extend(_nodes.iter()),
61        }
62    }
63}
64#[derive(Debug, Clone, Builder)]
65pub struct ResistorBuilder {
66    pub n1: Span,
67    pub n2: Span,
68    pub value: ValueBuilder,
69}
70#[derive(Debug, Clone, Builder)]
71pub struct CapacitorBuilder {
72    pub n1: Span,
73    pub n2: Span,
74    pub value: ValueBuilder,
75}
76#[derive(Debug, Clone, Builder)]
77pub struct InductorBuilder {
78    pub n1: Span,
79    pub n2: Span,
80    pub value: ValueBuilder,
81}
82#[derive(Debug, Clone, Builder)]
83pub struct SubcktBuilder {
84    pub nodes: Vec<Span>,
85    pub cktname: Span,
86    pub params: Vec<KeyValueBuilder>,
87}
88/// https://eda-cpu1.eias.junzhuo.site/~junzhuo/hspice/index.htm#page/hspice_11/bipolar_junction_transistor_bjt_element.htm
89#[derive(Debug, Clone, Builder)]
90pub struct BJTBuilder {
91    pub nc: Span,
92    pub nb: Span,
93    pub ne: Span,
94    pub ns: Option<Span>,
95    pub mname: Span,
96    pub params: Vec<KeyValueBuilder>,
97}
98#[derive(Debug, Clone, Builder)]
99pub struct MOSFETBuilder {
100    pub nd: Span,
101    pub ng: Span,
102    pub ns: Span,
103    pub nb: Option<Span>,
104    pub mname: Span,
105    pub params: Vec<KeyValueBuilder>,
106}
107/// https://eda-cpu1.eias.junzhuo.site/~junzhuo/hspice/index.htm#page/hspice_11/diode_element.htm
108#[derive(Debug, Clone, Builder)]
109pub struct DiodeBuilder {
110    pub nplus: Span,
111    pub nminus: Span,
112    pub mname: Span,
113    pub params: Vec<KeyValueBuilder>,
114}
115#[derive(Debug, Clone, Builder)]
116pub struct VoltageBuilder {
117    pub n1: Span,
118    pub n2: Span,
119    pub source: VoltageSourceBuilder,
120}
121#[derive(Debug, Clone, Builder)]
122pub enum VoltageSourceBuilder {
123    Params(Vec<KeyValueBuilder>),
124    Value(ValueBuilder),
125    PWL(PWLBuilder),
126}
127#[derive(Debug, Clone, Builder)]
128pub struct CurrentBuilder {
129    pub n1: Span,
130    pub n2: Span,
131    pub source: CurrentSourceBuilder,
132}
133#[derive(Debug, Clone, Builder)]
134pub enum CurrentSourceBuilder {
135    Params(Vec<KeyValueBuilder>),
136    Value(ValueBuilder),
137    PWL(PWLBuilder),
138}
139#[derive(Debug, Clone, Builder)]
140pub struct TimeValuePointBuilder {
141    pub time: ValueBuilder,
142    pub value: ValueBuilder,
143}
144
145/// https://eda-cpu1.eias.junzhuo.site/~junzhuo/hspice/index.htm#page/hspice_11/pwl_source.htm
146#[derive(Debug, Clone, Default, Builder)]
147pub struct PWLBuilder {
148    pub points: Vec<TimeValuePointBuilder>,
149    /// Keyword and time value to specify a repeating function.
150    /// With no argument, the source repeats from the beginning of the function.
151    /// repeat is the time, in units of seconds, which specifies
152    /// the startpoint of the waveform to repeat. This time needs
153    /// to be less than the greatest time point, tn.
154    pub repeat: Option<ValueBuilder>,
155    /// Specifies the stop time for the repeat.
156    pub rstop: Option<ValueBuilder>,
157    /// Specifies the value of the current/voltage source at the time of rstop.
158    /// stopvalue can be either a real number or Z for high impedance state.
159    pub stopvalue: Option<ValueBuilder>,
160    /// stopeslope is the switching time from the last PWL value to the stopvalue.
161    /// Default value is 30ps, if unspecified.
162    pub stopslope: Option<ValueBuilder>,
163    /// `TD=delay`
164    ///
165    /// Time, in units of seconds, which specifies the length of time to delay (propagation delay) the piecewise linear function.
166    pub delay: Option<ValueBuilder>,
167    pub edgetype: EdgeType,
168}
169
170#[derive(Debug, Clone, Copy, Default)]
171pub enum EdgeType {
172    #[default]
173    Linear,
174    HalfSine,
175}
176
177self_builder!(EdgeType);