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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use libertyparse::{ Liberty, Cell, Pin, PinDirection, SequentialDef };
use arcstr::Substr;
use indexmap::{ IndexMap, IndexSet };
use super::{ LogicVal, LogicLib, LogicCell, LogicOutputPin };
use super::{ combinational, sequential };
use sequential::SequentialInterface;
impl LogicLib {
pub fn from(liberty: &Liberty) -> LogicLib {
let cell_refs: IndexMap<&Substr, &Cell> = liberty.libs.iter()
.map(|(_, lib)| lib.cells.iter().map(|(n, p)| (n, p)))
.flatten().collect();
let mut logic_cells = cell_refs.into_iter().map(|(name, cell)| {
let (logic_cell, seq) = LogicCell::build_pin_sizes(cell);
(name.clone(), (cell, logic_cell, seq))
}).collect::<Vec<_>>();
let mut truthtable_size = 0;
for (_, (_, lc, _)) in &mut logic_cells {
for (_, pin) in &mut lc.output_pins {
if let Ok(pin) = pin {
pin.table_start = truthtable_size;
truthtable_size += pin.table_size;
}
}
}
let mut truthtable = vec![LogicVal::U; truthtable_size];
for (_, (c, lc, seq)) in &logic_cells {
lc.build_pin_table(c, seq.as_ref(), &mut truthtable[..]);
}
LogicLib {
logic_cells: logic_cells.into_iter()
.map(|(name, (_, lc, _))| (name, lc)).collect(),
truthtable
}
}
}
impl LogicCell {
fn build_pin_sizes(
cell: &Cell
) -> (LogicCell, Option<SequentialInterface>) {
let inputs = cell.pins.iter().filter_map(|(n, p)| {
match p.direction {
PinDirection::I => Some(n),
_ => None
}
}).collect();
let seq = match &cell.sequential_def {
None => None,
Some(seq) => Some(SequentialInterface::from(seq))
};
(LogicCell {
output_pins: cell.pins.iter().filter_map(|(n, p)| {
match p.direction {
PinDirection::I => None,
PinDirection::O => Some((
n.clone(),
LogicOutputPin::build_size(
&inputs, seq.as_ref(), p)
)),
_ => Some((
n.clone(),
Err("unknown pin direction in liberty def")
))
}
}).collect()
},
seq)
}
fn build_pin_table(
&self,
cell: &Cell,
seq: Option<&SequentialInterface>,
tab: &mut [LogicVal]
) {
for (n, pin) in &cell.pins {
if let Some(Ok(lp)) = self.output_pins.get(n) {
lp.build_table(
pin,
cell.sequential_def.as_ref(),
seq,
&mut tab[
lp.table_start..
lp.table_start + lp.table_size]);
}
}
}
}
impl LogicOutputPin {
fn build_size(
all_cell_inputs: &IndexSet<&Substr>,
seq_int: Option<&SequentialInterface>,
pin: &Pin
) -> Result<LogicOutputPin, &'static str> {
let expr = match &pin.function {
Some(expr) => expr,
None => return Err("No function field in liberty")
};
let mut related_inputs = IndexMap::new();
let mut has_internal = false;
for name in combinational::all_idents(expr) {
if all_cell_inputs.contains(name) {
related_inputs.insert(name.clone(), false);
continue;
}
if let Some(seq_int) = seq_int {
if seq_int.internals.contains(name) {
has_internal = true;
continue;
}
}
clilog::error!(
LOGICLIB_FUNC_BADREF,
"Parsing logiclib: bad reference {}",
name);
return Err("Bad function reference")
}
let num_internals = if has_internal {
let seq_int = seq_int.unwrap();
for (name, rf_sens) in &seq_int.inputs {
related_inputs.insert((*name).clone(), *rf_sens);
}
seq_int.internals.len()
}
else { 0 };
related_inputs.sort_keys();
let table_size = related_inputs.iter()
.map(|(_, rf_s)| match rf_s { true => 7, false => 5 })
.product::<usize>()
* 4usize.pow(num_internals as u32) * (num_internals + 1);
Ok(LogicOutputPin {
related_inputs,
num_internals: num_internals.try_into().unwrap(),
table_size,
table_start: usize::MAX
})
}
fn build_table(
&self,
pin: &Pin,
seq: Option<&SequentialDef>,
seq_int: Option<&SequentialInterface>,
tab: &mut [LogicVal]
) {
use LogicVal::*;
fn map_rf_nu_base(rf: bool) -> usize {
match rf { true => 6, false => 4 }
}
fn map_rf_u_base(rf: bool) -> usize {
match rf { true => 7, false => 5 }
}
fn map_nu_val(v: usize) -> LogicVal {
match v {
0 => L, 1 => H, 2 => X, 3 => Z,
4 => R, 5 => F,
_ => unreachable!()
}
}
fn map_u_val(v: usize) -> LogicVal {
(v as u8).try_into().unwrap()
}
fn map_val_u(lv: LogicVal) -> usize {
lv as u8 as usize
}
assert_eq!(tab.len(), self.table_size);
let internals_mul = 4usize.pow(self.num_internals as u32);
let set_size = self.related_inputs.iter()
.map(|(_, rf_s)| map_rf_nu_base(*rf_s))
.product::<usize>() * internals_mul;
let n_inputs = self.related_inputs.len();
let n_internals = self.num_internals as usize;
let mut b = vec![U; n_inputs + n_internals];
for s in 0..set_size {
let mut t = s;
for (i, (_, rf)) in self.related_inputs.iter().enumerate() {
let base = map_rf_nu_base(*rf);
b[i] = map_nu_val(t % base);
t /= base;
}
for i in 0..n_internals {
b[n_inputs + i] = map_nu_val(t % 4);
t /= 4;
}
assert_eq!(t, 0);
for i in (0..n_internals).rev() {
t = t * 4 + map_val_u(b[n_inputs + i]);
}
for (i, (_, rf)) in self.related_inputs.iter().enumerate().rev() {
t = t * map_rf_u_base(*rf) + map_val_u(b[i]);
}
let t = t; match (seq, seq_int) {
(None, None) => {
tab[t] = combinational::eval(
pin.function.as_ref().unwrap(),
&|name| {
b[self.related_inputs
.get_index_of(name).unwrap()]
},
false
);
}
(Some(seq), Some(seq_int)) => {
let tab_start = t * (n_internals + 1);
sequential::eval(
seq,
seq_int,
&|name| {
b[match self.related_inputs
.get_index_of(name) {
Some(i) => i,
None => n_inputs + seq_int.internals
.get_index_of(name).unwrap()
}]
},
&mut tab[tab_start + 1..tab_start + n_internals + 1]
);
tab[tab_start] = combinational::eval(
pin.function.as_ref().unwrap(),
&|name| {
match self.related_inputs
.get_index_of(name)
{
Some(i) => b[i],
None => tab[
tab_start + 1 + seq_int.internals
.get_index_of(name)
.unwrap()]
}
},
false
);
}
_ => panic!()
}
}
for s_u in 1..(1 << n_inputs) {
let first_u = (0..n_inputs)
.filter(|i| (s_u >> i & 1) != 0)
.next().unwrap();
let set_size = self.related_inputs.iter()
.enumerate()
.map(|(i, (_, rf_s))| {
if (s_u >> i & 1) != 0 { 1 }
else { map_rf_nu_base(*rf_s) }
})
.product::<usize>() * internals_mul;
let first_u_base = map_rf_u_base(
*self.related_inputs
.get_index(first_u).unwrap().1
);
let first_u_skip = self.related_inputs.iter()
.enumerate()
.take_while(|(i, _)| *i < first_u)
.map(|(_, (_, rf_s))| map_rf_u_base(*rf_s))
.product::<usize>();
for s in 0..set_size {
let mut t = s;
let mut s_bit = t % internals_mul;
t /= internals_mul;
for (i, (_, rf_s)) in self.related_inputs
.iter().enumerate().rev()
{
let s_bit_base = map_rf_u_base(*rf_s);
let s_base = map_rf_nu_base(*rf_s);
s_bit *= s_bit_base;
if i == first_u {
}
else if (s_u >> i & 1) != 0 {
s_bit += map_val_u(U);
}
else {
s_bit += map_val_u(map_nu_val(t % s_base));
t /= s_base;
}
}
assert_eq!(t, 0);
let mut all_eq = true;
let gslice = |s_bit: usize| -> std::ops::Range<usize> {
s_bit * (n_internals + 1)..
(s_bit + 1) * (n_internals + 1)
};
for first_to in 1..first_u_base {
if map_u_val(first_to) == U { continue }
let s_bit_first_to = s_bit +
first_to * first_u_skip;
if tab[gslice(s_bit)] != tab[gslice(s_bit_first_to)] {
all_eq = false;
break;
}
}
let s_bit_first_u = s_bit + map_val_u(U) * first_u_skip;
if all_eq {
tab.copy_within(gslice(s_bit), gslice(s_bit_first_u).start);
}
else {
if s_bit_first_u == 3717 {
panic!("{s_u} {s} {s_bit} {first_u_skip}")
}
tab[gslice(s_bit_first_u)].fill(U);
}
}
}
}
}