safety-net 0.14.0

A reference-counted netlist library for EDA tools
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use safety_net::Gate;
use safety_net::GateNetlist;
use safety_net::Netlist;
use safety_net::assert_verilog_eq;
use safety_net::format_id;
use std::rc::Rc;

fn and_gate() -> Gate {
    Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into())
}

fn or_gate() -> Gate {
    Gate::new_logical("OR".into(), vec!["A".into(), "B".into()], "Y".into())
}

fn two_out_gate() -> Gate {
    Gate::new_logical_multi(
        "DUP".into(),
        vec!["I".into()],
        vec!["O0".into(), "O1".into()],
    )
}

fn get_simple_example() -> Rc<GateNetlist> {
    let netlist = Netlist::new("example".into());

    let a = netlist.insert_input("a".into());
    let b = netlist.insert_input("b".into());

    let instance = netlist
        .insert_gate(and_gate(), "inst_0".into(), &[a, b])
        .unwrap();

    instance.expose_with_name("y".into());

    netlist
}

#[test]
fn test_clean() {
    let netlist = get_simple_example();
    assert!(netlist.verify().is_ok());
    assert!(netlist.clean().unwrap().is_empty());
    let inputs: Vec<_> = netlist.inputs().collect();
    assert_eq!(inputs.len(), 2);
    {
        let _new_cell = netlist
            .insert_gate(and_gate(), "inst_1".into(), &inputs)
            .unwrap();
        assert!(netlist.verify().is_ok());
        assert_eq!(netlist.objects().count(), 4);
        assert!(netlist.clean().is_err());
    }
    assert!(!netlist.clean().unwrap().is_empty());
    assert_eq!(netlist.objects().count(), 3);
    assert!(netlist.clean().unwrap().is_empty());
}

#[test]
fn test_multiple_output_aliases() {
    let netlist = GateNetlist::new("passthru_example".into());

    // Add the input
    let b = netlist.insert_input("b".into());

    // Instantiate an AND gate
    let instance = netlist
        .insert_gate(
            Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into()),
            "inst_0".into(),
            &[b.clone(), b.clone()],
        )
        .unwrap();

    // Create multiple output aliases for the same net
    instance.clone().expose_with_name("y".into());
    instance.clone().expose_with_name("z".into());

    // Verify that we have two outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(
        outputs.len(),
        2,
        "Expected 2 output aliases but got {}",
        outputs.len()
    );

    // Verify that the outputs have the correct names
    let output_names: Vec<String> = outputs
        .iter()
        .map(|net| net.get_identifier().emit_name())
        .collect();
    assert!(
        output_names.contains(&"y".to_string()),
        "Expected output 'y' but got {:?}",
        output_names
    );
    assert!(
        output_names.contains(&"z".to_string()),
        "Expected output 'z' but got {:?}",
        output_names
    );
}

#[test]
fn test_remove_output() {
    let netlist = GateNetlist::new("remove_output_test".into());

    // Add the input
    let b = netlist.insert_input("b".into());

    // Instantiate an AND gate
    let instance = netlist
        .insert_gate(
            Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into()),
            "inst_0".into(),
            &[b.clone(), b.clone()],
        )
        .unwrap();

    // Create multiple output aliases
    instance.clone().expose_with_name("y".into());
    instance.clone().expose_with_name("z".into());
    instance.clone().expose_with_name("w".into());

    // Verify we have three outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 3);

    // Remove one output alias using NetRef method
    let removed = instance.remove_output(&"z".into());
    assert!(removed, "Should have successfully removed 'z'");

    // Verify we now have two outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(
        outputs.len(),
        2,
        "Expected 2 outputs after removal but got {}",
        outputs.len()
    );

    // Verify the correct outputs remain
    let output_names: Vec<String> = outputs
        .iter()
        .map(|net| net.get_identifier().emit_name())
        .collect();
    assert!(output_names.contains(&"y".to_string()));
    assert!(!output_names.contains(&"z".to_string()));
    assert!(output_names.contains(&"w".to_string()));

    // Try to remove a non-existent output
    let removed = instance.remove_output(&"nonexistent".into());
    assert!(
        !removed,
        "Should return false when removing non-existent output"
    );

    // Verify output count unchanged
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 2);
}

#[test]
fn test_remove_all_outputs() {
    let netlist = GateNetlist::new("remove_all_outputs_test".into());

    // Add the input
    let b = netlist.insert_input("b".into());

    // Instantiate an AND gate
    let instance = netlist
        .insert_gate(
            Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into()),
            "inst_0".into(),
            &[b.clone(), b.clone()],
        )
        .unwrap();

    // Create multiple output aliases
    instance.clone().expose_with_name("y".into());
    instance.clone().expose_with_name("z".into());
    instance.clone().expose_with_name("w".into());

    // Verify we have three outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 3);

    // Remove all outputs using NetRef method
    let count = instance.remove_all_outputs();
    assert_eq!(
        count, 3,
        "Should have removed 3 outputs but removed {}",
        count
    );

    // Verify we have no outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(
        outputs.len(),
        0,
        "Expected 0 outputs but got {}",
        outputs.len()
    );

    // Verify calling remove_all_outputs again returns 0
    let count = instance.remove_all_outputs();
    assert_eq!(count, 0, "Should return 0 when removing from empty outputs");
}

#[test]
fn test_driven_net_remove_output() {
    let netlist = GateNetlist::new("driven_net_remove_test".into());

    // Add inputs
    let a = netlist.insert_input("a".into());
    let b = netlist.insert_input("b".into());

    // Instantiate an AND gate
    let instance = netlist
        .insert_gate(
            Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into()),
            "inst_0".into(),
            &[a, b],
        )
        .unwrap();

    // Get the driven net output
    let driven_net = instance.get_output(0);

    // Create multiple output aliases via DrivenNet
    driven_net.clone().expose_with_name("y".into());
    driven_net.clone().expose_with_name("z".into());
    driven_net.clone().expose_with_name("w".into());

    // Verify we have three outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 3);

    // Remove one output using DrivenNet method
    let removed = driven_net.remove_output(&"y".into());
    assert!(removed, "Should have successfully removed 'y'");

    // Verify we now have two outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 2);

    // Remove all remaining outputs using DrivenNet method
    let count = driven_net.remove_all_outputs();
    assert_eq!(count, 2, "Should have removed 2 remaining outputs");

    // Verify we have no outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 0);
}

#[test]
fn test_netlist_remove_output_by_operand() {
    let netlist = GateNetlist::new("netlist_remove_test".into());

    // Add inputs
    let a = netlist.insert_input("a".into());
    let b = netlist.insert_input("b".into());

    // Instantiate an AND gate
    let instance = netlist
        .insert_gate(
            Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into()),
            "inst_0".into(),
            &[a, b],
        )
        .unwrap();

    // Create multiple output aliases
    instance.clone().expose_with_name("y".into());
    instance.clone().expose_with_name("z".into());

    // Verify we have two outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 2);

    // Remove one output directly from netlist
    let removed = netlist.remove_output(&instance.clone().into(), &"y".into());
    assert!(removed, "Should have successfully removed 'y'");

    // Verify we have one output left
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 1);

    // Remove all outputs using netlist method
    let count = netlist.remove_outputs(&instance.into());
    assert_eq!(count, 1, "Should have removed 1 remaining output");

    // Verify we have no outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 0);
}

#[test]
fn test_netlist_clear_outputs() {
    let netlist = GateNetlist::new("clear_outputs_test".into());

    // Add inputs
    let a = netlist.insert_input("a".into());
    let b = netlist.insert_input("b".into());

    // Instantiate gates
    let instance1 = netlist
        .insert_gate(
            Gate::new_logical("AND".into(), vec!["A".into(), "B".into()], "Y".into()),
            "inst_0".into(),
            &[a.clone(), b.clone()],
        )
        .unwrap();

    let instance2 = netlist
        .insert_gate(
            Gate::new_logical("OR".into(), vec!["A".into(), "B".into()], "Y".into()),
            "inst_1".into(),
            &[a.clone(), b.clone()],
        )
        .unwrap();

    // Create multiple output aliases on both gates
    instance1.clone().expose_with_name("y1".into());
    instance1.clone().expose_with_name("y2".into());
    instance2.clone().expose_with_name("z1".into());
    instance2.clone().expose_with_name("z2".into());

    // Verify we have four outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 4);

    // Clear all outputs
    netlist.clear_outputs();

    // Verify we have no outputs
    let outputs = netlist.get_output_ports();
    assert_eq!(outputs.len(), 0, "Expected 0 outputs after clear");
}

#[test]
fn test_replace_self() {
    let netlist = get_simple_example();
    let gate = netlist.last().unwrap();
    let output = gate.get_output(0);
    let result = netlist.replace_net_uses(output.clone(), &output);
    assert!(result.is_ok());
    let result = result.unwrap();
    assert!(result.get_instance_type().is_some());
    let gtype = result.get_instance_type().unwrap();
    assert_eq!(gtype.get_gate_name(), &"AND".into());
}

#[test]
fn test_replace() {
    let netlist = get_simple_example();
    let input = netlist.inputs().next().unwrap();
    let inverter = Gate::new_logical("INV".into(), vec!["I".into()], "O".into());
    let inverted = netlist
        .insert_gate(inverter, "inst_0".into(), std::slice::from_ref(&input))
        .unwrap();
    assert!(netlist.replace_net_uses(input, &inverted.into()).is_ok());
    assert_verilog_eq!(
        netlist.to_string(),
        "module example (
           a,
           b,
           y
         );
           input wire a;
           input wire b;
           output wire y;
           wire inst_0_O;
           wire inst_0_Y;
           AND inst_0 (
             .A(inst_0_O),
             .B(b),
             .Y(inst_0_Y)
           );
           INV inst_0 (
             .I(inst_0_O),
             .O(inst_0_O)
           );
           assign y = inst_0_Y;
         endmodule\n"
    );
}

#[test]
fn test_replace2() {
    let netlist = get_simple_example();
    let input = netlist.inputs().next().unwrap();
    let inverter = Gate::new_logical("INV".into(), vec!["I".into()], "O".into());
    let inverted = netlist.insert_gate_disconnected(inverter, "inst_0".into());
    assert!(
        netlist
            .replace_net_uses(input.clone(), &inverted.clone().into())
            .is_err()
    );
    inverted.find_input(&"I".into()).unwrap().connect(input);
    assert_verilog_eq!(
        netlist.to_string(),
        "module example (
           a,
           b,
           y
         );
           input wire a;
           input wire b;
           output wire y;
           wire inst_0_O;
           wire inst_0_Y;
           AND inst_0 (
             .A(a),
             .B(b),
             .Y(inst_0_Y)
           );
           INV inst_0 (
             .I(a),
             .O(inst_0_O)
           );
           assign y = inst_0_Y;
         endmodule\n"
    );
}

#[test]
fn test_replace_single_single() {
    let netlist = Netlist::new("example".into());
    let a = netlist.insert_input("a".into());
    let b = netlist.insert_input("b".into());
    let and_inst = netlist
        .insert_gate(and_gate(), "and_0".into(), &[a.clone(), b.clone()])
        .unwrap();
    let and_out = and_inst.get_output(0);
    let or_inst = netlist
        .insert_gate(or_gate(), "or_0".into(), &[a.clone(), and_out.clone()])
        .unwrap();
    drop(and_inst);
    assert!(
        netlist
            .replace_net_uses(and_out, &or_inst.clone().into())
            .is_ok()
    );
    or_inst.get_output(0).expose_with_name("y".into());
    assert!(netlist.verify().is_ok());
    assert_verilog_eq!(
        netlist.to_string(),
        "module example (
            a,
            b,
            y
          );
            input wire a;
            input wire b;
            output wire y;
            wire and_0_Y;
            wire or_0_Y;
            AND and_0 (
              .A(a),
              .B(b),
              .Y(and_0_Y)
            );
            OR or_0 (
              .A(a),
              .B(or_0_Y),
              .Y(or_0_Y)
            );
            assign y = or_0_Y;
          endmodule"
    );
}
#[test]
fn test_replace_single_multiple() {
    let netlist = Netlist::new("example".into());
    let a = netlist.insert_input("a".into());
    let b = netlist.insert_input("b".into());

    let and_inst = netlist
        .insert_gate(and_gate(), "and_0".into(), &[a.clone(), b.clone()])
        .unwrap();

    let dup = netlist
        .insert_gate(two_out_gate(), "dup0".into(), &[a])
        .unwrap();

    dup.get_output(1).expose_with_name("y".into());

    let and_out = and_inst.get_output(0);
    let dup_out1 = dup.get_output(1);
    drop(dup);
    netlist.replace_net_uses(dup_out1, &and_out).unwrap();
    assert!(netlist.verify().is_ok());
    assert_verilog_eq!(
        netlist.to_string(),
        "module example (
          a,
          b,
          y
        );
          input wire a;
          input wire b;
          output wire y;
          wire and_0_Y;
          wire dup0_O0;
          wire dup0_O1;
          AND and_0 (
            .A(a),
            .B(b),
            .Y(and_0_Y)
          );
          DUP dup0 (
            .I(a),
            .O0(dup0_O0),
            .O1(dup0_O1)
          );
          assign y = and_0_Y;
        endmodule"
    );
}

#[test]
fn test_replace_multiple_single() {
    let netlist = Netlist::new("example".into());
    let a = netlist.insert_input("a".into());
    let b = netlist.insert_input("b".into());

    let and_inst = netlist
        .insert_gate(and_gate(), "and_0".into(), &[a.clone(), b.clone()])
        .unwrap();

    let dup = netlist
        .insert_gate(two_out_gate(), "dup0".into(), &[a])
        .unwrap();

    and_inst.get_output(0).expose_with_name("y".into());

    let and_out = and_inst.get_output(0);
    let dup_out0 = dup.get_output(0);
    drop(and_inst);
    netlist.replace_net_uses(and_out, &dup_out0).unwrap();
    assert!(netlist.verify().is_ok());
    assert_verilog_eq!(
        netlist.to_string(),
        "module example (
          a,
          b,
          y
          );
          input wire a;
          input wire b;
          output wire y;
          wire and_0_Y;
          wire dup0_O0;
          wire dup0_O1;
          AND and_0 (
            .A(a),
            .B(b),
            .Y(and_0_Y)
          );
          DUP dup0 (
            .I(a),
            .O0(dup0_O0),
            .O1(dup0_O1)
          );
          assign y = dup0_O0;
        endmodule"
    );
}

#[test]
fn test_replace_multiple_multiple() {
    let netlist = Netlist::new("example".into());
    let a = netlist.insert_input("a".into());

    let dup2 = netlist
        .insert_gate(two_out_gate(), "dup2".into(), &[a])
        .unwrap();

    dup2.get_output(1).expose_with_name("y".into());
    let dup2_out0 = dup2.get_output(0);
    let dup2_out1 = dup2.get_output(1);

    drop(dup2);
    netlist.replace_net_uses(dup2_out1, &dup2_out0).unwrap();
    assert!(netlist.verify().is_ok());
    assert_verilog_eq!(
        netlist.to_string(),
        "module example (
            a,
            y
            );
            input wire a;
            output wire y;
            wire dup2_O0;
            wire dup2_O1;
            DUP dup2 (
              .I(a),
              .O0(dup2_O0),
              .O1(dup2_O1)
            );
            assign y = dup2_O0;
          endmodule"
    );
}

#[test]
fn test_rename() {
    let netlist = get_simple_example();
    assert!(netlist.rename_nets(|_, _| format_id!("__yo__")).is_err());
    assert!(netlist.rename_nets(|_, i| format_id!("__{i}__")).is_ok());
    let gate = netlist.last().unwrap();
    assert_eq!(gate.get_instance_name().unwrap(), "__1__".into());
}

#[test]
fn test_bad_inst_swap() {
    let netlist = get_simple_example();
    let gate = netlist.last().unwrap();
    let mut inst = gate.get_instance_type_mut().unwrap();
    *inst = two_out_gate();

    drop(inst);
    let verify = netlist.verify();
    assert!(verify.is_err());
    let rsn = verify.err().unwrap();
    assert!(matches!(rsn, safety_net::Error::ArgumentMismatch(1, 2)));
}