1use std::{
4 borrow::Borrow,
5 fmt::Debug,
6 ops::{Deref, Index},
7};
8
9pub use ::codegen::{BundleKind, Io};
10use arcstr::ArcStr;
11use serde::{Deserialize, Serialize};
12
13use crate::{
14 block::Block,
15 schematic::{CellId, InstanceId, InstancePath},
16};
17
18pub use scir::Direction;
19
20#[doc(hidden)]
21pub mod codegen;
22mod impls;
23pub mod layout;
24pub mod schematic;
25#[cfg(test)]
26mod tests;
27
28pub type IoKind<T> = <<T as Block>::Io as HasBundleKind>::BundleKind;
30
31pub trait FlatLen {
35 fn len(&self) -> usize;
37 fn is_empty(&self) -> bool {
39 self.len() == 0
40 }
41}
42
43impl<T: FlatLen> FlatLen for &T {
44 fn len(&self) -> usize {
45 (*self).len()
46 }
47}
48
49pub trait Flatten<T>: FlatLen {
51 fn flatten<E>(&self, output: &mut E)
53 where
54 E: Extend<T>;
55
56 fn flatten_vec(&self) -> Vec<T> {
58 let len = self.len();
59 let mut vec = Vec::with_capacity(len);
60 self.flatten(&mut vec);
61 assert_eq!(
62 vec.len(),
63 len,
64 "Flatten::flatten_vec produced a Vec with an incorrect length: expected {} from FlatLen::len, got {}",
65 len,
66 vec.len()
67 );
68 vec
69 }
70}
71
72pub trait Unflatten<D, T>: FlatLen + Sized {
74 fn unflatten<I>(data: &D, source: &mut I) -> Option<Self>
81 where
82 I: Iterator<Item = T>;
83}
84
85impl<S, T: Flatten<S>> Flatten<S> for &T {
86 fn flatten<E>(&self, output: &mut E)
87 where
88 E: Extend<S>,
89 {
90 (*self).flatten(output)
91 }
92}
93
94pub trait HasNameTree {
96 fn names(&self) -> Option<Vec<NameTree>>;
101
102 fn flat_names(&self, root: Option<NameFragment>) -> Vec<NameBuf> {
104 self.names()
105 .map(|t| NameTree::with_optional_fragment(root, t).flatten())
106 .unwrap_or_default()
107 }
108}
109
110pub trait BundleKind:
112 FlatLen + HasNameTree + HasBundleKind<BundleKind = Self> + Debug + Clone + Eq + Send + Sync
113{
114}
115impl<T: FlatLen + HasNameTree + HasBundleKind<BundleKind = T> + Debug + Clone + Eq + Send + Sync>
116 BundleKind for T
117{
118}
119
120pub trait Directed: Flatten<Direction> {}
122impl<T: Flatten<Direction>> Directed for T {}
123
124pub trait Io: Directed + HasBundleKind + Clone {}
126impl<T: Directed + HasBundleKind + Clone> Io for T {}
127
128pub trait HasBundleKind: Send + Sync {
130 type BundleKind: BundleKind;
132
133 fn kind(&self) -> Self::BundleKind;
135}
136
137impl<T: HasBundleKind> HasBundleKind for &T {
138 type BundleKind = T::BundleKind;
139
140 fn kind(&self) -> Self::BundleKind {
141 (*self).kind()
142 }
143}
144
145#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
151pub enum NameFragment {
152 Str(ArcStr),
154 Idx(usize),
156}
157
158#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
160pub struct NameBuf {
161 fragments: Vec<NameFragment>,
162}
163
164#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
166pub struct NameTree {
167 fragment: Option<NameFragment>,
168 children: Vec<NameTree>,
169}
170
171#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
175pub struct Input<T>(pub T);
176
177#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
181pub struct Output<T>(pub T);
182
183#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
187pub struct InOut<T>(pub T);
188
189#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
193pub struct Flipped<T>(pub T);
194
195#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
197pub struct Signal;
198
199impl Signal {
200 #[inline]
202 pub fn new() -> Self {
203 Self
204 }
205}
206
207#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
209pub struct Array<T> {
210 len: usize,
211 kind: T,
212}
213
214impl<T> Array<T> {
215 #[inline]
217 pub fn new(len: usize, kind: T) -> Self {
218 Self { len, kind }
219 }
220
221 pub fn is_empty(&self) -> bool {
223 self.len == 0
224 }
225
226 pub fn len(&self) -> usize {
228 self.len
229 }
230
231 pub fn kind(&self) -> &T {
233 &self.kind
234 }
235}
236
237#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
239pub struct ArrayBundle<T: HasBundleKind> {
240 elems: Vec<T>,
241 kind: T::BundleKind,
242}
243
244#[derive(Debug, Default, Clone, Io)]
250pub struct MosIo {
251 pub d: InOut<Signal>,
253 pub g: Input<Signal>,
255 pub s: InOut<Signal>,
257 pub b: InOut<Signal>,
259}
260
261#[derive(Debug, Default, Clone, Io)]
263pub struct TestbenchIo {
264 pub vss: InOut<Signal>,
266}
267
268#[derive(Debug, Default, Clone, Io)]
270pub struct TwoTerminalIo {
271 pub p: InOut<Signal>,
273 pub n: InOut<Signal>,
275}
276
277#[derive(Debug, Default, Clone, Io)]
279pub struct PowerIo {
280 pub vdd: InOut<Signal>,
282 pub vss: InOut<Signal>,
284}
285
286#[derive(Debug, Default, Copy, Clone, Io)]
289pub struct DiffPair {
290 pub p: InOut<Signal>,
292 pub n: InOut<Signal>,
294}
295
296