gateconvert/lib.rs
1// lib.rs - main code
2
3#![cfg_attr(docsrs, feature(doc_cfg))]
4//! The library allows to easily convert Gate circuit from/to one of few foreign formats.
5//! This library is used by `gateconvert_exec` program that allow conversion by command line
6//! interface.
7//!
8//! A conversion to foreign logic format writes result data into output (by `Write` trait).
9//! A conversion from foreign logic format returns Gate circuit object and sometimes
10//! additional mapping. Any functions that make conversion returns Result to allow handle
11//! various errors.
12
13/// Utility to mark negation
14#[derive(Clone, Copy, PartialEq, Eq, Debug)]
15pub enum VNegs {
16 /// If no negations.
17 NoNegs,
18 /// Second (starting from 0) argument will be negated.
19 // second input in gate
20 NegInput1,
21 /// Output will be negated.
22 NegOutput,
23}
24
25pub mod aiger;
26pub mod blif;
27mod blif_pla;
28pub mod btor2;
29pub mod cnf;
30mod vbinopcircuit;
31mod vcircuit;
32pub mod verilog;
33pub mod vhdl;
34mod xor_table;
35
36pub use gategen;
37pub use gateutil;
38pub use gateutil::gatesim;
39
40use std::fmt::{self, Debug, Display};
41
42/// Generate output string from mapping. The `T` must be convertible to string.
43///
44/// This function simplify generation of map file. Mapping in form:
45/// index - original variable index, value - index of circuit inputs.
46pub fn map_to_string<T: ToString>(map: &[Option<T>]) -> String {
47 let mut out = String::new();
48 for t in map {
49 if let Some(t) = t {
50 out += &t.to_string();
51 } else {
52 out += "-";
53 }
54 out.push('\n');
55 }
56 out
57}
58
59/// Entry of assignment for mapping.
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61pub enum AssignEntry {
62 /// No mapping.
63 NoMap,
64 /// Boolean value set for variable.
65 Value(bool),
66 /// Circuit wire index and its negation.
67 Var(usize, bool),
68}
69
70impl Display for AssignEntry {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self {
73 AssignEntry::NoMap => write!(f, "-"),
74 AssignEntry::Value(v) => write!(f, "{}", v),
75 AssignEntry::Var(v, n) => write!(f, "{}{}", if *n { "!" } else { "" }, v),
76 }
77 }
78}
79
80/// Generate output string from mapping.
81///
82/// This function simplify generation of map file. Mapping in form:
83/// key - original variable index, value - asisgnment to circuit.
84pub fn assign_map_to_string(map: &[(usize, AssignEntry)]) -> String {
85 let mut out = String::new();
86 for (i, t) in map {
87 out += &i.to_string();
88 out.push(' ');
89 out += &t.to_string();
90 out.push('\n');
91 }
92 out
93}
94
95/// Generate output string from mapping.
96///
97/// This function simplify generation of map file. Mapping in form:
98/// key - original variable name, value - asisgnment to circuit.
99pub fn string_assign_map_to_string(map: &[(String, AssignEntry)]) -> String {
100 let mut out = String::new();
101 for (k, t) in map {
102 out += &k;
103 out.push(' ');
104 out += &t.to_string();
105 out.push('\n');
106 }
107 out
108}