1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Standard cell definitions and utilities.

use crate::Sky130Pdk;
use arcstr::ArcStr;
use paste::paste;
use serde::{Deserialize, Serialize};
use spice::Spice;
use std::path::PathBuf;
use substrate::block::Block;
use substrate::io::schematic::HardwareType;
use substrate::io::{InOut, Input, Io, Output, Signal};
use substrate::schematic::{CellBuilder, ExportsNestedData, Schematic};

impl Sky130Pdk {
    pub(crate) fn stdcell_path(&self, lib: &str, name: &str) -> PathBuf {
        self.open_root_dir
            .as_ref()
            .expect("Requires Sky130 open PDK root directory to be specified")
            .join(format!("libraries/{lib}/latest/cells/{name}"))
    }
}

/// The power IO for Sky130 standard cells.
#[derive(Default, Debug, Clone, Copy, Io)]
pub struct PowerIo {
    /// The ground rail.
    pub vgnd: InOut<Signal>,
    /// The power rail.
    pub vpwr: InOut<Signal>,
    /// The nwell body contact.
    pub vnb: InOut<Signal>,
    /// The pwell body contact.
    pub vpb: InOut<Signal>,
}

impl PowerIoSchematic {
    /// Creates a `PowerIo` with `vnb` and `vpb` tied to `vdd` and `vss`, respectively.
    pub fn with_bodies_tied_to_rails(pwr: substrate::io::PowerIoSchematic) -> Self {
        Self {
            vgnd: pwr.vss,
            vnb: pwr.vss,
            vpb: pwr.vdd,
            vpwr: pwr.vdd,
        }
    }
}

macro_rules! define_stdcell {
    ($typ:ident, $name:ident, $doc:literal, [$($ports_upper:ident),*], [$($ports_lower:ident),*], [$($directions:ident),*], [$($port_docs:literal),*], [$($strengths:expr),*]) => {
paste! {
    #[derive(Debug, Default, Clone, Copy, Io)]
    #[doc = concat!("The IO of a `", stringify!($name), "` standard cell.")]
    pub struct [<$typ Io>] {
        /// The power interface.
        pub pwr: PowerIo,
        $(
            #[doc = $port_docs]
            pub $ports_lower: $directions<Signal>,
        )*
    }

    #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
    #[doc = $doc]
    pub enum $typ {
        $(
            #[doc = concat!("A strength ", stringify!($strengths), " variant of this standard cell.")]
            [<S $strengths>],
        )*
    }

    impl $typ {
        #[doc = concat!("Returns the strength of this `", stringify!($name), "` standard cell.")]
        pub fn strength(&self) -> i64 {
            match self {
                $(Self::[<S $strengths>] => $strengths,)*
            }
        }
    }

    impl Block for $typ {
        type Io = [<$typ Io>];

        fn id() -> ArcStr {
            arcstr::literal!(stringify!($name))
        }

        fn name(&self) -> ArcStr {
            arcstr::format!("{}_{}", stringify!($name), self.strength())
        }

        fn io(&self) -> Self::Io {
            Default::default()
        }
    }

    impl ExportsNestedData for $typ {
        type NestedData = ();
    }

    impl Schematic<Sky130Pdk> for $typ {
        fn schematic(
            &self,
            io: &<<Self as Block>::Io as HardwareType>::Bundle,
            cell: &mut CellBuilder<Sky130Pdk>,
        ) -> substrate::error::Result<Self::NestedData> {
            let pdk = cell
                .ctx()
                .get_installation::<Sky130Pdk>()
                .expect("Requires Sky130 PDK installation");

            let lib = "sky130_fd_sc_hd";
            let name = stringify!($name);
            let cell_name = format!("{lib}__{name}_{}", self.strength());
            let mut scir = Spice::scir_cell_from_file(
                pdk.stdcell_path(lib, name)
                    .join(format!("{}.spice", cell_name)),
                &cell_name,
            )
            .convert_schema::<Sky130Pdk>()?;

            scir.connect("VGND", io.pwr.vgnd);
            scir.connect("VNB", io.pwr.vnb);
            scir.connect("VPB", io.pwr.vpb);
            scir.connect("VPWR", io.pwr.vpwr);
            $(scir.connect(stringify!($ports_upper), io.$ports_lower);)*

            cell.set_scir(scir);
            Ok(())
        }
    }
}
    };
}

define_stdcell!(
    And2,
    and2,
    "A 2-input AND gate.",
    [A, B, X],
    [a, b, x],
    [Input, Input, Output],
    ["Input A.", "Input B.", "The gate output."],
    [0, 1, 2, 4]
);
define_stdcell!(
    And3,
    and3,
    "A 3-input AND gate.",
    [A, B, C, X],
    [a, b, c, x],
    [Input, Input, Input, Output],
    ["Input A.", "Input B.", "Input C.", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Buf,
    buf,
    "A buffer.",
    [A, X],
    [a, x],
    [Input, Output],
    ["The buffer input.", "The buffer output."],
    [1, 2, 4, 6, 8, 12, 16]
);
define_stdcell!(
    Bufbuf,
    bufbuf,
    "A cascaded pair of buffers.",
    [A, X],
    [a, x],
    [Input, Output],
    ["The buffer input.", "The buffer output."],
    [8, 16]
);
define_stdcell!(
    Inv,
    inv,
    "An inverter.",
    [A, Y],
    [a, y],
    [Input, Output],
    ["The inverter input.", "The inverter output."],
    [1, 2, 4, 6, 8]
);
// TODO: Manually implement for tap since no need to nest power IO.
define_stdcell!(Tap, tap, "A tap to VDD and GND.", [], [], [], [], [1, 2]);
define_stdcell!(
    Mux2,
    mux2,
    "A 2-input multiplexer.",
    [A0, A1, S, X],
    [a0, a1, s, x],
    [Input, Input, Input, Output],
    [
        "Input 0.",
        "Input 1.",
        "The select bit.",
        "The multiplexer output."
    ],
    [1, 2, 4, 8]
);
define_stdcell!(
    Mux4,
    mux4,
    "A 4-input multiplexer.",
    [A0, A1, A2, A3, S0, S1, X],
    [a0, a1, a2, a3, s0, s1, x],
    [Input, Input, Input, Input, Input, Input, Output],
    [
        "Input 0.",
        "Input 1.",
        "Input 2.",
        "Input 3.",
        "Select bit 0.",
        "Select bit 1.",
        "The multiplexer output."
    ],
    [1, 2, 4]
);
define_stdcell!(
    Nand2,
    nand2,
    "A 2-input NAND gate.",
    [A, B, Y],
    [a, b, y],
    [Input, Input, Output],
    ["Input A.", "Input B.", "The gate output."],
    [1, 2, 4, 8]
);
define_stdcell!(
    Nand3,
    nand3,
    "A 3-input NAND gate.",
    [A, B, C, Y],
    [a, b, c, y],
    [Input, Input, Input, Output],
    ["Input A.", "Input B.", "Input C", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Nor2,
    nor2,
    "A 2-input NOR gate.",
    [A, B, Y],
    [a, b, y],
    [Input, Input, Output],
    ["Input A.", "Input B.", "The gate output."],
    [1, 2, 4, 8]
);
define_stdcell!(
    Nor3,
    nor3,
    "A 3-input NOR gate.",
    [A, B, C, Y],
    [a, b, c, y],
    [Input, Input, Input, Output],
    ["Input A.", "Input B.", "Input C.", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Or2,
    or2,
    "A 2-input OR gate.",
    [A, B, X],
    [a, b, x],
    [Input, Input, Output],
    ["Input A.", "Input B.", "The gate output."],
    [0, 1, 2, 4]
);
define_stdcell!(
    Or3,
    or3,
    "A 3-input OR gate.",
    [A, B, C, X],
    [a, b, c, x],
    [Input, Input, Input, Output],
    ["Input A.", "Input B.", "Input C.", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Xnor2,
    xnor2,
    "A 2-input XNOR gate.",
    [A, B, Y],
    [a, b, y],
    [Input, Input, Output],
    ["Input A.", "Input B.", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Xnor3,
    xnor3,
    "A 3-input XNOR gate.",
    [A, B, C, Y],
    [a, b, c, y],
    [Input, Input, Input, Output],
    ["Input A.", "Input B.", "Input C.", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Xor2,
    xor2,
    "A 2-input XOR gate.",
    [A, B, X],
    [a, b, x],
    [Input, Input, Output],
    ["Input A.", "Input B.", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Xor3,
    xor3,
    "A 3-input XOR gate.",
    [A, B, C, X],
    [a, b, c, x],
    [Input, Input, Input, Output],
    ["Input A.", "Input B.", "Input C.", "The gate output."],
    [1, 2, 4]
);
define_stdcell!(
    Diode,
    diode,
    "An antenna diode.",
    [DIODE],
    [diode],
    [InOut],
    ["The diode node."],
    [2]
);
define_stdcell!(
    Dfxtp,
    dfxtp,
    "A positive edge triggered delay flop.",
    [CLK, D, Q],
    [clk, d, q],
    [Input, Input, Output],
    ["The clock signal.", "The data input.", "The data output."],
    [1, 2, 4]
);
define_stdcell!(
    Dfrtp,
    dfrtp,
    "A positive edge triggered delay flop with inverted reset.",
    [CLK, D, RESET_B, Q],
    [clk, d, reset_b, q],
    [Input, Input, Input, Output],
    [
        "The clock signal.",
        "The data input.",
        "The inverted reset signal.",
        "The data output."
    ],
    [1, 2, 4]
);