substrate/schematic/
pex.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
//! Utilities for tracking nested data through parasitic extraction.

use std::sync::Arc;

use scir::{Library, NamedSliceOne, NetlistLibConversion, SliceOnePath};

use crate::{
    simulation::{
        data::{Save, SaveKey, Saved},
        Analysis, Simulator,
    },
    types::schematic::{NestedNode, RawNestedNode},
};

use super::{
    conv::{ConvertedNodePath, RawLib},
    schema::Schema,
    Cell, ContextView, HasContextView, HasNestedView, InstancePath, NestedView, Schematic,
};

/// Captures information for mapping nodes/elements between schematic and extracted netlists.
pub struct PexContext<S: Schema> {
    /// The source spice file for this DSPF extracted view.
    lib: Arc<RawLib<S>>,
    conv: Arc<NetlistLibConversion>,
    path: InstancePath,
}

impl<S: Schema> Clone for PexContext<S> {
    fn clone(&self) -> Self {
        Self {
            lib: self.lib.clone(),
            conv: self.conv.clone(),
            path: self.path.clone(),
        }
    }
}

/// A schema that can convert element paths to strings.
pub trait StringPathSchema: Schema {
    /// Convert a node path to a raw string.
    fn node_path(lib: &Library<Self>, conv: &NetlistLibConversion, path: &SliceOnePath) -> String;
}

impl<S: StringPathSchema> HasContextView<PexContext<S>> for NestedNode {
    type ContextView = RawNestedNode;

    fn context_view(&self, parent: &PexContext<S>) -> ContextView<Self, PexContext<S>> {
        let n = self;
        let path = parent.lib.convert_node_path(&n.path()).unwrap();
        let path = match path {
            ConvertedNodePath::Cell(path) => path,
            ConvertedNodePath::Primitive {
                instances, port, ..
            } => SliceOnePath::new(instances.clone(), NamedSliceOne::new(port.clone())),
        };
        let path = parent.lib.scir.simplify_path(path);
        RawNestedNode::new(
            parent.path.clone(),
            S::node_path(&parent.lib.scir, &parent.conv, &path),
        )
    }
}

/// Nested data exposed by an extracted view of a circuit.
pub struct PexData<T: Schematic> {
    cell: Cell<Arc<T>>,
    lib: Arc<RawLib<T::Schema>>,
    conv: Arc<NetlistLibConversion>,
}

impl<T: Schematic> Clone for PexData<T> {
    fn clone(&self) -> Self {
        Self {
            cell: self.cell.clone(),
            lib: self.lib.clone(),
            conv: self.conv.clone(),
        }
    }
}

impl<T: Schematic> PexData<T> {
    /// Creates a new [`PexData`].
    pub fn new(
        cell: Cell<Arc<T>>,
        lib: Arc<RawLib<T::Schema>>,
        conv: Arc<NetlistLibConversion>,
    ) -> Self {
        Self { cell, lib, conv }
    }
}

/// The nested view of [`PexData`].
pub struct NestedPexData<T: Schematic> {
    cell: Cell<Arc<T>>,
    ctx: PexContext<T::Schema>,
}

impl<T: Schematic> Clone for NestedPexData<T> {
    fn clone(&self) -> Self {
        Self {
            cell: self.cell.clone(),
            ctx: self.ctx.clone(),
        }
    }
}

impl<T: Schematic> NestedPexData<T>
where
    T::NestedData: HasContextView<PexContext<T::Schema>>,
{
    /// Access the underlying data.
    pub fn data(&self) -> ContextView<T::NestedData, PexContext<T::Schema>> {
        self.cell.context_data(&self.ctx)
    }
}

impl<T: Schematic> HasNestedView for PexData<T> {
    type NestedView = NestedPexData<T>;
    fn nested_view(&self, parent: &InstancePath) -> NestedView<Self> {
        NestedPexData {
            cell: self.cell.clone(),
            ctx: PexContext {
                lib: self.lib.clone(),
                conv: self.conv.clone(),
                path: parent.clone(),
            },
        }
    }
}

impl<T: Schematic> HasNestedView for NestedPexData<T> {
    type NestedView = NestedPexData<T>;
    fn nested_view(&self, parent: &InstancePath) -> NestedView<Self> {
        NestedPexData {
            cell: self.cell.clone(),
            ctx: PexContext {
                lib: self.ctx.lib.clone(),
                conv: self.ctx.conv.clone(),
                path: self.ctx.path.prepend(parent),
            },
        }
    }
}

impl<S: Simulator, A: Analysis, T: Schematic> Save<S, A> for NestedPexData<T>
where
    T::NestedData: HasContextView<PexContext<T::Schema>>,
    ContextView<T::NestedData, PexContext<T::Schema>>: Save<S, A>,
{
    type SaveKey = SaveKey<ContextView<T::NestedData, PexContext<T::Schema>>, S, A>;
    type Saved = Saved<ContextView<T::NestedData, PexContext<T::Schema>>, S, A>;

    fn save(
        &self,
        ctx: &substrate::simulation::SimulationContext<S>,
        opts: &mut <S as Simulator>::Options,
    ) -> <Self as Save<S, A>>::SaveKey {
        self.data().save(ctx, opts)
    }

    fn from_saved(
        output: &<A as Analysis>::Output,
        key: &<Self as Save<S, A>>::SaveKey,
    ) -> <Self as Save<S, A>>::Saved {
        <ContextView<T::NestedData, PexContext<T::Schema>> as Save<S, A>>::from_saved(output, key)
    }
}