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
use super::{
base::{tstring, ws},
subparser::{
comment, instantiate_stmt, port_direction_declare_stmt, port_map_stmt, wire_declare_stmt,
},
ParseRes,
};
use crate::{
error::NetListError,
model::{Gate, Net, NetList, Node, NodeOwner, Pin, PinDirection},
NResult,
};
use nom::{
branch::permutation,
bytes::complete::tag,
error::convert_error,
multi::{many0, many1},
sequence::{delimited, preceded, tuple},
Finish,
};
pub fn verilog_parser<W: Default, N: Default, G: Default, B: Default, P: Default, S: Default>(
s: &str,
) -> NResult<NetList<W, N, G, B, P, S>> {
match preceded(many0(comment), module_parser)(s).finish() {
Ok(d) => Ok(d.1),
Err(e) => Err(NetListError::ParseErr(convert_error(s, e))),
}
}
pub fn module_parser<W: Default, N: Default, G: Default, B: Default, P: Default, S: Default>(
s: &str,
) -> ParseRes<&str, NetList<W, N, G, B, P, S>> {
delimited(
ws(tag("module")),
tuple((
tstring,
port_map_stmt,
permutation((
many1(port_direction_declare_stmt),
many1(instantiate_stmt),
many0(wire_declare_stmt), // ignore it when updating netlist
many0(comment), // ignore
)),
)),
ws(tag("endmodule")),
)(s)
.map(|(s, d)| {
let mut netlist = NetList::default();
netlist.name = d.0.to_string();
// create new pin by port direction declare state
for p in (d.2).0 {
// node,pin,net index
let mut node_id = netlist.nodes.len();
let mut pin_id = netlist.pins.len();
let mut net_id = netlist.nets.len();
match p.0 {
// Input pin means that
// (1) new node with net as load will be created
// (2) new input pin wih created node will be created
// (3) new net with created node will be created
PinDirection::Input => {
for pname in &p.2 {
let mut new_pin = Pin {
name: pname.to_string(),
bitwidth: 1,
first_node: node_id,
..Default::default() // default as Input direction
};
if let Some((msb, lsb)) = p.1 {
new_pin.bitwidth = msb - lsb + 1;
for bit in lsb..=msb {
let net_name = &format!("{}[{}]", pname, bit);
netlist.nets.push(Net {
name: net_name.to_string(),
connection: vec![node_id],
..Default::default()
});
netlist.net_map.insert(net_name.to_string(), net_id);
if bit != msb {
netlist.nodes.push(Node {
name: net_name.to_string(),
owner: NodeOwner::PinInput(pin_id),
data: N::default(),
connection: net_id,
next_node: Some(node_id + 1),
});
} else {
netlist.nodes.push(Node {
name: net_name.to_string(),
owner: NodeOwner::PinInput(pin_id),
data: N::default(),
connection: net_id,
next_node: None,
});
}
node_id += 1;
pin_id += 1;
net_id += 1;
}
} else {
netlist.nets.push(Net {
name: pname.to_string(), // pname is also net name, according to verilog standard
connection: vec![node_id],
..Default::default()
});
netlist.net_map.insert(pname.to_string(), net_id);
netlist.nodes.push(Node {
name: pname.to_string(),
owner: NodeOwner::PinInput(pin_id),
connection: net_id,
data: N::default(),
next_node: None,
});
}
netlist.pins.push(new_pin);
netlist.pin_map.insert(pname.to_string(), pin_id);
}
}
// Output pin means that
// (1) new node with pin as load will be created
// (2) new output pin with created node will be created
// (3) new net that same name with pin name will be created, with created node
PinDirection::Output => {
for pname in &p.2 {
let mut new_pin = Pin {
name: pname.to_string(),
bitwidth: 1,
direction: PinDirection::Output,
first_node: node_id,
..Default::default()
};
if let Some((msb, lsb)) = p.1 {
new_pin.bitwidth = msb - lsb + 1;
for bit in lsb..=msb {
let net_name = &format!("{}[{}]", pname, bit);
netlist.nets.push(Net {
name: net_name.to_string(),
connection: vec![node_id],
..Default::default()
});
netlist.net_map.insert(net_name.to_string(), net_id);
if bit != msb {
netlist.nodes.push(Node {
name: net_name.to_string(),
owner: NodeOwner::PinOutput(pin_id),
data: N::default(),
connection: net_id,
next_node: Some(node_id + 1),
});
} else {
netlist.nodes.push(Node {
name: net_name.to_string(),
owner: NodeOwner::PinOutput(pin_id),
data: N::default(),
connection: net_id,
next_node: None,
});
}
node_id += 1;
pin_id += 1;
net_id += 1;
}
} else {
// when pin bitwidth == 1, pin_name = net_name = node_name
netlist.nets.push(Net {
name: pname.to_string(),
connection: vec![node_id],
..Default::default()
});
netlist.net_map.insert(pname.to_string(), net_id);
netlist.nodes.push(Node {
name: pname.to_string(),
owner: NodeOwner::PinOutput(pin_id),
data: N::default(),
connection: net_id,
next_node: None,
});
}
netlist.pins.push(new_pin);
netlist.pin_map.insert(pname.to_string(), pin_id);
}
}
}
}
// create new gate and update its binded net
for n in (d.2).1 {
let gate_id = netlist.gates.len();
let mut node_id = netlist.nodes.len();
netlist.gate_map.insert(n.1.to_string(), gate_id);
let new_gate: Gate<G> = Gate {
name: n.1.to_string(),
model: n.0.to_string(),
first_node: node_id,
..Default::default()
};
netlist.gates.push(new_gate);
// update its binded net
// first check if net is already exists in the net_map,
// if not , create new net
// first node index in gate
// second node index = first node index + 1, so on
let new_node_num = n.2.len();
for (i, d) in n.2.iter().enumerate() {
let gate_port = d.0;
let bind_net = d.1;
match netlist.net_map.get(bind_net) {
// if bind_net already in net_map, it means that
// (1) no need to create new net in this step
// (2) update nodes in net
Some(net_id) => {
let net = &mut netlist.nets[*net_id];
net.connection.push(node_id);
// create new node
if i != new_node_num - 1 {
netlist.nodes.push(Node {
name: gate_port.to_string(),
owner: NodeOwner::GateInput(gate_id), // as we dont know node pin direction
data: N::default(),
connection: *net_id,
next_node: Some(node_id + 1),
})
} else {
netlist.nodes.push(Node {
name: gate_port.to_string(),
owner: NodeOwner::GateInput(gate_id), // as we dont know node pin direction
data: N::default(),
connection: *net_id,
next_node: None,
})
}
}
// if bind_net no in the net map, it means that
// (1) need to create new net in this step
None => {
let net_id = netlist.nets.len();
netlist.net_map.insert(bind_net.to_string(), net_id);
netlist.nets.push(Net {
name: bind_net.to_string(),
connection: vec![node_id],
..Default::default()
});
// create new node
if i != new_node_num - 1 {
netlist.nodes.push(Node {
name: gate_port.to_string(),
owner: NodeOwner::GateInput(gate_id), // as we dont know node pin direction
data: N::default(),
connection: net_id,
next_node: Some(node_id + 1),
})
} else {
netlist.nodes.push(Node {
name: gate_port.to_string(),
owner: NodeOwner::GateInput(gate_id), // as we dont know node pin direction
data: N::default(),
connection: net_id,
next_node: None,
})
}
} // create new net
}
node_id += 1;
}
}
(s, netlist)
})
}