substrate/types/mod.rs
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
//! Traits and types for defining interfaces and signals in Substrate.
use std::{
borrow::Borrow,
fmt::Debug,
ops::{Deref, Index},
};
pub use ::codegen::{BundleKind, Io};
use arcstr::ArcStr;
use serde::{Deserialize, Serialize};
use crate::{
block::Block,
schematic::{CellId, InstanceId, InstancePath},
};
pub use scir::Direction;
#[doc(hidden)]
pub mod codegen;
mod impls;
pub mod layout;
pub mod schematic;
#[cfg(test)]
mod tests;
/// The [`BundleKind`] of a block's IO.
pub type IoKind<T> = <<T as Block>::Io as HasBundleKind>::BundleKind;
// BEGIN TRAITS
/// The length of the flattened list.
pub trait FlatLen {
/// The length of the flattened list.
fn len(&self) -> usize;
/// Whether or not the flattened representation is empty.
fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<T: FlatLen> FlatLen for &T {
fn len(&self) -> usize {
(*self).len()
}
}
/// Flatten a structure into a list.
pub trait Flatten<T>: FlatLen {
/// Flatten a structure into a list.
fn flatten<E>(&self, output: &mut E)
where
E: Extend<T>;
/// Flatten into a [`Vec`].
fn flatten_vec(&self) -> Vec<T> {
let len = self.len();
let mut vec = Vec::with_capacity(len);
self.flatten(&mut vec);
assert_eq!(vec.len(), len, "Flatten::flatten_vec produced a Vec with an incorrect length: expected {} from FlatLen::len, got {}", len, vec.len());
vec
}
}
/// Unflatten a structure from an iterator.
pub trait Unflatten<D, T>: FlatLen + Sized {
/// Unflatten a structure from an iterator.
///
/// A correct implementation must only return [`None`]
/// if the iterator has insufficient elements.
/// Returning None for any other reason is a logic error.
/// Unsafe code should not rely on implementations of this method being correct.
fn unflatten<I>(data: &D, source: &mut I) -> Option<Self>
where
I: Iterator<Item = T>;
}
impl<S, T: Flatten<S>> Flatten<S> for &T {
fn flatten<E>(&self, output: &mut E)
where
E: Extend<S>,
{
(*self).flatten(output)
}
}
/// An object with named flattened components.
pub trait HasNameTree {
/// Return a tree specifying how nodes contained within this type should be named.
///
/// Important: empty types (i.e. those with a flattened length of 0) must return [`None`].
/// All non-empty types must return [`Some`].
fn names(&self) -> Option<Vec<NameTree>>;
/// Returns a flattened list of node names.
fn flat_names(&self, root: Option<NameFragment>) -> Vec<NameBuf> {
self.names()
.map(|t| NameTree::with_optional_fragment(root, t).flatten())
.unwrap_or_default()
}
}
/// A bundle kind.
pub trait BundleKind:
FlatLen + HasNameTree + HasBundleKind<BundleKind = Self> + Debug + Clone + Eq + Send + Sync
{
}
impl<
T: FlatLen + HasNameTree + HasBundleKind<BundleKind = T> + Debug + Clone + Eq + Send + Sync,
> BundleKind for T
{
}
/// Indicates that an IO specifies signal directions for all of its fields.
pub trait Directed: Flatten<Direction> {}
impl<T: Flatten<Direction>> Directed for T {}
/// A trait implemented by block input/output interfaces.
pub trait Io: Directed + HasBundleKind + Clone {}
impl<T: Directed + HasBundleKind + Clone> Io for T {}
/// A construct with an associated [`BundleKind`].
pub trait HasBundleKind: Send + Sync {
/// The Rust type of the [`BundleKind`] associated with this bundle.
type BundleKind: BundleKind;
/// Returns the [`BundleKind`] of this bundle.
fn kind(&self) -> Self::BundleKind;
}
impl<T: HasBundleKind> HasBundleKind for &T {
type BundleKind = T::BundleKind;
fn kind(&self) -> Self::BundleKind {
(*self).kind()
}
}
// END TRAITS
// BEGIN TYPES
/// A portion of a node name.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub enum NameFragment {
/// An element identified by a string name, such as a struct field.
Str(ArcStr),
/// A numbered element of an array/bus.
Idx(usize),
}
/// An owned node name, consisting of an ordered list of [`NameFragment`]s.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
pub struct NameBuf {
fragments: Vec<NameFragment>,
}
/// A tree for hierarchical node naming.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub struct NameTree {
fragment: Option<NameFragment>,
children: Vec<NameTree>,
}
/// An input port of kind `T`.
///
/// Recursively overrides the direction of all components of `T` to be [`Input`](Direction::Input)
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
pub struct Input<T>(pub T);
/// An output port of kind `T`.
///
/// Recursively overrides the direction of all components of `T` to be [`Output`](Direction::Output)
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
pub struct Output<T>(pub T);
/// An inout port of kind `T`.
///
/// Recursively overrides the direction of all components of `T` to be [`InOut`](Direction::InOut)
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
pub struct InOut<T>(pub T);
/// Flip the direction of all ports in `T`
///
/// See [`Direction::flip`]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Serialize, Deserialize)]
pub struct Flipped<T>(pub T);
/// A type representing a single hardware wire in a [`BundleKind`].
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Signal;
impl Signal {
/// Creates a new [`Signal`].
#[inline]
pub fn new() -> Self {
Self
}
}
/// An array containing some number of elements of kind `T`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub struct Array<T> {
len: usize,
kind: T,
}
impl<T> Array<T> {
/// Create a new array of the given length and bundle kind.
#[inline]
pub fn new(len: usize, kind: T) -> Self {
Self { len, kind }
}
/// Returns if the array has length 0.
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Returns the number of elements in the array.
pub fn len(&self) -> usize {
self.len
}
/// Returns the kind of the elements in the array.
pub fn kind(&self) -> &T {
&self.kind
}
}
/// An instantiated array containing a fixed number of elements of `T`.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub struct ArrayBundle<T: HasBundleKind> {
elems: Vec<T>,
kind: T::BundleKind,
}
// END TYPES
// BEGIN COMMON IO TYPES
/// The interface to a standard 4-terminal MOSFET.
#[derive(Debug, Default, Clone, Io)]
pub struct MosIo {
/// The drain.
pub d: InOut<Signal>,
/// The gate.
pub g: Input<Signal>,
/// The source.
pub s: InOut<Signal>,
/// The body.
pub b: InOut<Signal>,
}
/// The interface to which simulation testbenches should conform.
#[derive(Debug, Default, Clone, Io)]
pub struct TestbenchIo {
/// The global ground net.
pub vss: InOut<Signal>,
}
/// The interface for 2-terminal blocks.
#[derive(Debug, Default, Clone, Io)]
pub struct TwoTerminalIo {
/// The positive terminal.
pub p: InOut<Signal>,
/// The negative terminal.
pub n: InOut<Signal>,
}
/// The interface for VDD and VSS rails.
#[derive(Debug, Default, Clone, Io)]
pub struct PowerIo {
/// The VDD rail.
pub vdd: InOut<Signal>,
/// The VSS rail.
pub vss: InOut<Signal>,
}
/// A pair of differential signals.
// TODO: Create proc macro for defining un-directioned (non-IO) bundle types directly.
#[derive(Debug, Default, Copy, Clone, Io)]
pub struct DiffPair {
/// The positive signal.
pub p: InOut<Signal>,
/// The negative signal.
pub n: InOut<Signal>,
}
// END COMMON IO TYPES