1use std::fmt::{self, Debug};
2use std::{collections::HashMap, iter::Iterator};
3use serde::{Deserialize, Serialize};
4
5
6pub type NetIndex = usize;
7pub type GateIndex = usize;
8pub type PinIndex = usize;
9pub type BlockIndex = usize;
10pub type ScopeIndex = usize;
11pub type NodeIndex = usize;
12
13#[derive(Default)]
14pub struct NetList<W, N, G, B, P, S> {
15 pub name: String, pub nets: Vec<Net<W>>,
17 pub gates: Vec<Gate<G>>,
18 pub blocks: Vec<Block<B>>,
19 pub pins: Vec<Pin<P>>,
20 pub nodes: Vec<Node<N>>, pub scopes: Vec<Scope<S>>, pub net_map: HashMap<String, NetIndex>,
24 pub gate_map: HashMap<String, GateIndex>,
25 pub pin_map: HashMap<String, PinIndex>,
26}
27
28impl<
29 W: Default + Debug,
30 N: Default + Debug,
31 G: Default + Debug,
32 B: Default + Debug,
33 P: Default + Debug,
34 S: Default + Debug,
35 > Debug for NetList<W, N, G, B, P, S>
36{
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 write!(f, "nets:")?;
39 f.debug_list().entries(self.nets.iter()).finish()?;
40 f.write_str("\n")?;
41 write!(f, "gates:")?;
42 f.debug_list().entries(self.gates.iter()).finish()?;
43 f.write_str("\n")?;
44 write!(f, "blocks:")?;
45 f.debug_list().entries(self.blocks.iter()).finish()?;
46 f.write_str("\n")?;
47 write!(f, "pins:")?;
48 f.debug_list().entries(self.pins.iter()).finish()?;
49 f.write_str("\n")?;
50 write!(f, "scopes:")?;
51 f.debug_list().entries(self.scopes.iter()).finish()?;
52 f.write_str("\n")?;
53 write!(f, "net_map:")?;
54 f.debug_map().entries(self.net_map.iter()).finish()?;
55 f.write_str("\n")?;
56 write!(f, "gate_map:")?;
57 f.debug_map().entries(self.gate_map.iter()).finish()?;
58 f.write_str("\n")?;
59 write!(f, "pin_map:")?;
60 f.debug_list().entries(self.pin_map.iter()).finish()?;
61 f.write_str("\n")
62 }
63}
64
65impl<
66 W: Default + Debug,
67 N: Default + Debug,
68 G: Default + Debug,
69 B: Default + Debug,
70 P: Default + Debug,
71 S: Default + Debug,
72 > NetList<W, N, G, B, P, S>
73{
74 pub fn new() -> Self {
75 NetList::default()
76 }
77}
78
79#[derive(Default)]
80pub struct Net<W> {
81 pub name: String,
82 pub bitwidth: u32,
83 pub connection: Vec<NodeIndex>,
84 pub data: W,
85}
86
87impl<W: Default + Debug> Debug for Net<W> {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 write!(f, "\n\t")?;
90 f.debug_struct("Net")
91 .field("name", &self.name)
92 .field("bitwidth", &self.bitwidth)
93 .field("data", &self.data)
94 .finish()
95 }
96}
97
98#[derive(Default,PartialEq, Debug,Serialize, Deserialize)]
99pub enum PinDirection {
100 #[default]
101 Input,
102 Output,
103}
104
105impl From<u8> for PinDirection {
107 fn from(item:u8) -> Self {
108 if item == 0x00 {
109 Self::Input
110 } else {
111 Self::Output
112 }
113 }
114}
115
116#[derive(Clone, Debug)]
117pub enum NodeOwner {
118 GateInput(GateIndex),
119 GateOutput(GateIndex),
120 PinInput(PinIndex),
121 PinOutput(PinIndex),
122}
123
124#[derive(Debug)]
125pub struct Node<N> {
126 pub name: String, pub owner: NodeOwner,
128 pub connection: NetIndex,
129 pub data: N,
130 pub next_node: Option<NodeIndex>,
131}
132
133#[derive(Debug, Serialize, Deserialize)]
134#[serde(rename_all = "kebab-case")]
135pub struct PortList<P:Default> {
136 pub items: Vec<Pin<P>>,
137}
138
139#[derive(Default,Serialize, Deserialize)]
140pub struct Pin<P> {
141 pub name: String,
142 pub direction: PinDirection,
143 pub bitwidth: u32,
144 pub first_node: NodeIndex,
145 pub data: P,
146}
147
148impl<P: Default + Debug> Debug for Pin<P> {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 write!(f, "\n\t")?;
151 f.debug_struct("Pin")
152 .field("name", &self.name)
153 .field("direction", &self.direction)
154 .field("bitwidth", &self.bitwidth)
155 .field("data", &self.data)
156 .finish()
157 }
158}
159
160pub struct NodeGraph<'a, W, N, G, B, P, S> {
161 pub netlist: &'a NetList<W, N, G, B, P, S>,
162 pub current_node_idx: NodeIndex,
163}
164
165impl<'a, W, N, G, B, P, S> Iterator for NodeGraph<'a, W, N, G, B, P, S> {
166 type Item = NodeIndex;
167 fn next(&mut self) -> Option<NodeIndex> {
168 match self.netlist.nodes[self.current_node_idx].next_node {
169 None => None,
170 Some(idx) => {
171 self.current_node_idx = idx;
172 Some(idx)
173 }
174 }
175 }
176}
177
178#[derive(Default)]
179pub struct Block<B> {
180 pub gates: Vec<GateIndex>,
181 pub data: B,
182}
183
184impl<W: Default + Debug> Debug for Block<W> {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 write!(f, "\n\t")?;
187 f.debug_struct("Block").field("data", &self.data).finish()
188 }
189}
190
191#[derive(Default)]
192pub struct Gate<G> {
193 pub name: String,
194 pub model: String,
195 pub first_node: NodeIndex,
196 pub data: G,
197}
198
199impl<W: Default + Debug> Debug for Gate<W> {
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 write!(f, "\n\t")?;
202 f.debug_struct("Gate")
203 .field("name", &self.name)
204 .field("model", &self.model)
205 .field("data", &self.data)
206 .finish()
207 }
208}
209
210#[derive(Default)]
211pub struct Scope<S> {
212 pub name: String,
213 pub derive: String, pub subscope: Vec<ScopeIndex>,
215 pub scoped_block: Vec<BlockIndex>,
216 pub scoped_net: Vec<NetIndex>,
217 pub data: S,
218}
219
220impl<S: Default + Debug> Debug for Scope<S> {
221 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222 write!(f, "\n\t")?;
223 f.debug_struct("Scope")
224 .field("name", &self.name)
225 .field("derive", &self.derive)
226 .field("scoped_block", &self.scoped_block)
227 .field("data", &self.data)
228 .finish()
229 }
230}
231