use safety_net::{Identifier, Instantiable, Logic, Net, Netlist, Parameter, filter_nodes};
#[derive(Debug, Clone)]
enum Gate {
And(Identifier, Vec<Net>, Net),
}
impl Instantiable for Gate {
fn get_name(&self) -> &Identifier {
match self {
Gate::And(id, _, _) => id,
}
}
fn get_input_ports(&self) -> &[Net] {
match self {
Gate::And(_, inputs, _) => inputs,
}
}
fn get_output_ports(&self) -> &[Net] {
match self {
Gate::And(_, _, output) => std::slice::from_ref(output),
}
}
fn get_parameter(&self, _id: &Identifier) -> Option<Parameter> {
None
}
fn set_parameter(&mut self, _id: &Identifier, _val: Parameter) -> Option<Parameter> {
panic!("Gate does not support parameters");
}
fn clear_parameter(&mut self, _id: &Identifier) -> Option<Parameter> {
None
}
fn parameters(&self) -> Vec<(Identifier, Parameter)> {
Vec::new()
}
fn from_constant(_val: Logic) -> Option<Self> {
None
}
fn get_constant(&self) -> Option<Logic> {
None
}
fn is_seq(&self) -> bool {
false
}
}
fn and_gate() -> Gate {
Gate::And(
"AND".into(),
vec![Net::new_logic("A".into()), Net::new_logic("B".into())],
Net::new_logic("Y".into()),
)
}
fn main() {
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());
println!("{netlist}");
for node in filter_nodes!(netlist, Gate::And(_, _, _)) {
println!("Found AND gate: {node}");
}
}