Skip to content

Commit

Permalink
basic codegen working as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanku committed Dec 9, 2024
1 parent bcbb05d commit 1083a0b
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 625 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

290 changes: 227 additions & 63 deletions codegen/src/block/schematic.rs

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,5 @@ splines = { version = "4.3.1", features = ["serde"] }

[dev-dependencies]
toml = "0.8"
# TODO: use one or the other
approx = "0.5"
float_eq = "1.0.1"
spice = { version = "<=0.7.1", registry = "substrate", path = "../libs/spice" }
4 changes: 1 addition & 3 deletions substrate/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::sync::Arc;

use arcstr::ArcStr;
pub use codegen::Block;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::types::Io;

Expand All @@ -16,7 +14,7 @@ use crate::types::Io;
/// # Examples
///
#[doc = examples::get_snippets!("core", "inverter")]
pub trait Block: Serialize + DeserializeOwned + Hash + Eq + Send + Sync + Any {
pub trait Block: Hash + Eq + Send + Sync + Any {
/// The ports of this block.
type Io: Io;

Expand Down
13 changes: 6 additions & 7 deletions substrate/src/schematic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ impl<T: Schematic> Schematic for Arc<T> {

/// Block that implements [`Schematic`] in schema `S` for block `B`.
#[derive_where::derive_where(Debug, Hash, PartialEq, Eq; B)]
#[derive(Serialize, Deserialize)]
pub struct ConvertSchema<B, S>(Arc<B>, #[serde(bound(deserialize = ""))] PhantomData<S>);
pub struct ConvertSchema<B, S>(Arc<B>, PhantomData<S>);

impl<B, S> Clone for ConvertSchema<B, S> {
fn clone(&self) -> Self {
Expand Down Expand Up @@ -1651,7 +1650,7 @@ mod tests {
pub n: InOut<Signal>,
}

#[derive(Serialize, Deserialize, Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[derive(Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[substrate(io = "ResistorIo")]
pub struct Resistor;

Expand Down Expand Up @@ -1701,7 +1700,7 @@ mod tests {
pub data: Output<Array<Signal>>,
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct MultiDecoupledBlock;

impl Block for MultiDecoupledBlock {
Expand Down Expand Up @@ -1746,7 +1745,7 @@ mod tests {
}
}

#[derive(Serialize, Deserialize, Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[derive(Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[substrate(io = "()")]
pub struct SuperBlock;

Expand Down Expand Up @@ -1795,7 +1794,7 @@ mod tests {
}
}

#[derive(Serialize, Deserialize, Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[derive(Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[substrate(io = "()")]
pub struct NestedBlock<T>(T);

Expand All @@ -1822,7 +1821,7 @@ mod tests {
pub dout: Output<Signal>,
}

#[derive(Serialize, Deserialize, Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[derive(Block, Debug, Copy, Clone, Hash, PartialEq, Eq)]
#[substrate(io = "VdividerIo")]
pub struct Vdivider;

Expand Down
106 changes: 23 additions & 83 deletions substrate/src/simulation/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@
pub use codegen::FromSaved;

use crate::schematic::Schematic;
use crate::schematic::{HasNestedView, Schematic};
use crate::simulation::{Analysis, SimulationContext, Simulator};

#[derive(Debug, Clone, Copy)]
pub struct SaveOutput;
#[derive(Debug, Clone, Copy)]
pub struct SaveTime;

impl HasNestedView for SaveOutput {
type NestedView = SaveOutput;

fn nested_view(&self, _parent: &substrate::schematic::InstancePath) -> Self::NestedView {
*self
}
}

impl HasNestedView for SaveTime {
type NestedView = SaveTime;

fn nested_view(&self, _parent: &substrate::schematic::InstancePath) -> Self::NestedView {
*self
}
}

/// Gets the [`Save::SaveKey`] corresponding to type `T`.
pub type SaveKey<T, S, A> = <T as Save<S, A>>::SaveKey;

Expand All @@ -13,11 +34,11 @@ pub type SaveKey<T, S, A> = <T as Save<S, A>>::SaveKey;
/// `T` is any type that can be used as arguments for deciding what should be saved in
/// this simulation output.
pub trait Save<S: Simulator, A: Analysis> {
type Save;
/// The key type used to address the saved output within the analysis.
///
/// This key is assigned in [`Save::save`].
type SaveKey;
type Save;

/// Marks the given output for saving, returning a key that can be used to recover
/// the output once the simulation is complete.
Expand All @@ -33,84 +54,3 @@ pub trait Save<S: Simulator, A: Analysis> {
key: &<Self as Save<S, A>>::SaveKey,
) -> <Self as Save<S, A>>::Save;
}

/// Transient data definitions.
pub mod tran {
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::sync::Arc;

/// A time-series of voltage measurements from a transient simulation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Voltage(pub Arc<Vec<f64>>);

impl Deref for Voltage {
type Target = Vec<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

/// A time-series of current measurements from a transient simulation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Current(pub Arc<Vec<f64>>);

impl Deref for Current {
type Target = Vec<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

/// The time points associated with a transient simulation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Time(pub Arc<Vec<f64>>);

impl Deref for Time {
type Target = Vec<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
}

/// AC data definitions.
pub mod ac {
use num::complex::Complex64;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::sync::Arc;

/// A series of voltage vs frequency measurements from an AC simulation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Voltage(pub Arc<Vec<Complex64>>);

impl Deref for Voltage {
type Target = Vec<Complex64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

/// A series of current vs frequency measurements from an AC simulation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Current(pub Arc<Vec<Complex64>>);

impl Deref for Current {
type Target = Vec<Complex64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

/// The frequency points associated with an AC simulation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Freq(pub Arc<Vec<f64>>);

impl Deref for Freq {
type Target = Vec<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
}
2 changes: 0 additions & 2 deletions substrate/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::sync::Arc;

use data::Save;
use impl_trait_for_tuples::impl_for_tuples;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::block::Block;
use crate::context::{Context, Installation};
Expand Down
2 changes: 2 additions & 0 deletions tools/spectre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ spice = { version = "0.7.1", registry = "substrate", path = "../../libs/spice" }
regex = "1.10.2"
num = { version = "0.4.1", features = ["serde"] }

[dev-dependencies]
approx = "0.5"
Loading

0 comments on commit 1083a0b

Please sign in to comment.