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
//! A flattened gate-level circuit netlist database.

use std::collections::HashMap;
use std::sync::Arc;
use std::collections::HashSet;
use sverilogparse::*;
use arcstr::{ArcStr, Substr};

/// types of directions: input or output.
/// note: inout is not supported yet.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Direction {
    /// input
    I,
    /// output
    O,
    /// unknown (unassigned)
    Unknown
}

mod csr;
pub use csr::VecCSR;

mod hier_name;
pub use hier_name::HierName;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum LogicPinType {
    TopPort,
    Net,
    LeafCellPin,
    Others
}

impl LogicPinType {
    #[inline]
    pub fn is_pin(self) -> bool {
        use LogicPinType::*;
        if let TopPort | LeafCellPin = self { true } else { false }
    }

    #[inline]
    pub fn is_net(self) -> bool {
        use LogicPinType::*;
        if let TopPort | Net = self { true } else { false }
    }
}

/// The netlist storage.
/// 
/// The public members are all READ-ONLY outside. Please modify
/// them through the ECO commands that will be available
/// in the future.
#[readonly::make]
#[derive(Debug)]
pub struct NetlistDB {
    /// top-level design name.
    pub name: Substr,
    /// number of cells/nodes/instances in the netlist.
    ///
    /// This is always greater than 1, as the 0th cell is always
    /// the top-level macro.
    pub num_cells: usize,
    /// number of logical pins.
    /// 
    /// A logical pin is not necessarily a pin. It might
    /// be the I/O port of non-leaf modules, or the result
    /// of an assign operation.
    pub num_logic_pins: usize,
    /// number of pins.
    pub num_pins: usize,
    /// number of nets/wires.
    pub num_nets: usize,

    /// Cell name to index.
    ///
    /// The top-level macro is always the 0th cell, which has a
    /// special name of empty string.
    /// Also, the hierarchical non-leaf cells do NOT reside in here,
    /// yet -- they are to-be-added in the future.
    /// This map only contains leaf cells.
    pub cellname2id: HashMap<HierName, usize>,
    /// Logical pin name tuple (cell hier name, macro pin type, vec idx) to logical pin index.
    ///
    /// Logic pin names are always unique without ambiguity.
    /// The case of logic pins include:
    /// 1. net wires  (yes, nets are also ``logic pins''.)
    /// 2. I/O ports of top module and submodules
    /// 3. pins of leaf cells.
    logicpinname2id: HashMap<(HierName, Substr, Option<isize>), usize>,
    /// Pin name tuple (cell hier name, macro pin type, vec idx) to index.
    /// 
    /// Pin names are always unique without ambiguity.
    /// For top-level named port connections, only the port names are
    /// created as valid pin names. The I/O definition can be referred
    /// in logicpinname2id (private member).
    pub pinname2id: HashMap<(HierName, Substr, Option<isize>), usize>,
    /// Net name tuple (net hier name, vec idx) to index.
    ///
    /// Multiple nets can be mapped to one single
    /// index, due to connected nets across hierarchy boundaries.
    pub netname2id: HashMap<(HierName, Substr, Option<isize>), usize>,

    /// Cell index to macro name.
    pub celltypes: Vec<Substr>,
    /// Cell index to name (hierarchical).
    ///
    /// This information actually contains the tree structure that
    /// might be useful later when we implement verilog writer.
    pub cellnames: Vec<HierName>,
    /// Logic pin classes.
    logicpintypes: Vec<LogicPinType>,
    /// Logic pin index to name.
    logicpinnames: Vec<(HierName, Substr, Option<isize>)>,
    /// Pin index to corresponding logic pin index.
    pinid2logicpinid: Vec<usize>,
    /// Pin index to cell hier, macro pin name, and pin index.
    pub pinnames: Vec<(HierName, Substr, Option<isize>)>,

    /// Pin to parent cell.
    pub pin2cell: Vec<usize>,
    /// Pin to parent net.
    pub pin2net: Vec<usize>,
    /// Cell CSR.
    pub cell2pin: VecCSR,
    /// Net CSR.
    ///
    /// **Caveat**: After assigning directions, it is guaranteed that
    /// the net root would be the first in net CSR.
    /// Before such assignment, the order is not determined.
    pub net2pin: VecCSR,

    /// Pin direction.
    pub pindirect: Vec<Direction>,
}

mod utils;
use utils::*;

mod disjoint_set;
use disjoint_set::*;

mod builder;
pub use builder::{DirectionProvider, NoDirection};