netlist_db/
instance.rs

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