logicsim::graph

Struct GateGraphBuilder

Source
pub struct GateGraphBuilder { /* private fields */ }
Expand description

Data structure that represents a graph of logic gates, it can be initialized to simulate the circuit.

Conceptually the logic gates are represented as nodes in a graph with dependency edges to other nodes.

Inputs are represented by constants(ON, OFF) and levers.

Outputs are represented by OutputHandles which allow you to query the state of gates and are created by GateGraphBuilder::output.

Once the graph is initialized, it transforms into an InitializedGateGraph which cannot be modified. The initialization process optimizes the gate graph so that expressive abstractions that potentially generate lots of constants or useless gates can be used without fear. All constants and dead gates will be optimized away and the remaining graph simplified very aggressively.

Zero overhead abstractions!

§Examples

Simple gates.

let mut g = GateGraphBuilder::new();

// Providing each gate with a string name allows for some very neat debugging.
// If you don't want them affecting performance, you can disable feature "debug_gates",
// all of the strings will be optimized away.
let or = g.or2(ON, OFF, "or");
let or_output = g.output1(or, "or_output");

let and = g.and2(ON, OFF, "and");
let and_output = g.output1(and, "and_output");

let ig = &g.init();

// `b0()` accesses the 0th bit of the output.
// Outputs can have as many bits as you want
// and be accessed with methods like `u8()`, `char()` or `i128()`.
assert_eq!(or_output.b0(ig), true);
assert_eq!(and_output.b0(ig), false);

Levers!

let l1 = g.lever("l1");
let l2 = g.lever("l2");

let or = g.or2(l1.bit(), l2.bit(), "or");
let or_output = g.output1(or, "or_output");

let and = g.and2(l1.bit(), l2.bit(), "and");
let and_output = g.output1(and, "and_output");

let ig = &mut g.init();

assert_eq!(or_output.b0(ig), false);
assert_eq!(and_output.b0(ig), false);

// `_stable` means that the graph will run until gate states have stopped changing.
// This might not be what you want if you have a circuit that never stabilizes,
// like 3 not gates connected in a circle!
// See [InitializedGateGraph::run_until_stable].
ig.flip_lever_stable(l1);
assert_eq!(or_output.b0(ig), true);
assert_eq!(and_output.b0(ig), false);

ig.flip_lever_stable(l2);
assert_eq!(or_output.b0(ig), true);
assert_eq!(and_output.b0(ig), true);

SR Latch!

let r = g.lever("l1");
let s = g.lever("l2");

let q = g.nor2(r.bit(), OFF, "q");
let nq = g.nor2(s.bit(), q, "nq");

let q_output = g.output1(q, "q");
let nq_output = g.output1(nq, "nq");

// `d1()` replaces the dependency at index 1 with nq.
// We used OFF as a placeholder above.
g.d1(q, nq);

let ig = &mut g.init();
// With latches, the initial state should be treated as undefined,
// so remember to always reset your latches at the beginning of the simulation.
ig.pulse_lever_stable(r);
assert_eq!(q_output.b0(ig), false);
assert_eq!(nq_output.b0(ig), true);

ig.pulse_lever_stable(s);
assert_eq!(q_output.b0(ig), true);
assert_eq!(nq_output.b0(ig), false);

ig.pulse_lever_stable(r);
assert_eq!(q_output.b0(ig), false);
assert_eq!(nq_output.b0(ig), true);

Implementations§

Source§

impl GateGraphBuilder

Source

pub fn new() -> GateGraphBuilder

Returns a new GateGraphBuilder containing only OFF and ON.

Source

pub fn dpush(&mut self, target: GateIndex, new_dep: GateIndex)

Appends new_dep to the list of dependencies of gate target.

§Panics

Will panic if target can’t have a variable number of dependencies.

Source

pub fn dx(&mut self, target: GateIndex, new_dep: GateIndex, x: usize)

Sets the dependency at index x in target dependencies to new_dep.

§Panics

Will panic if target has less than x + 1 dependencies, you probably want GateGraphBuilder::dpush instead.

Will panic if target is Not and x > 0.

Will panic if target can’t have dependencies.

Source

pub fn d0(&mut self, target: GateIndex, new_dep: GateIndex)

Sets the dependency at index 0 in target dependencies to new_dep.

§Panics

Will panic if target has less than 1 dependency, you probably want GateGraphBuilder::dpush instead.

Will panic if target can’t have dependencies.

Source

pub fn d1(&mut self, target: GateIndex, new_dep: GateIndex)

Sets the dependency at index 1 in target dependencies to new_dep.

§Panics

Will panic if target has less than 1 dependency, you probably want GateGraphBuilder::dpush instead.

Will panic if target can’t have more than 1 dependency.

Source

pub fn lever<S: Into<String>>(&mut self, name: S) -> LeverHandle

Returns the LeverHandle of a new lever gate.

Providing a good name allows for a great debugging experience. You can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn not1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex

Returns the GateIndex of a new not gate with 1 dependency.

Providing a good name allows for a great debugging experience. You can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn or<S: Into<String>>(&mut self, name: S) -> GateIndex

Returns the GateIndex of a new or gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance

Source

pub fn or1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex

Returns the GateIndex of a new gate with 1 dependency.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn or2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with 2 dependencies.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn orx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn nor<S: Into<String>>(&mut self, name: S) -> GateIndex

Returns the GateIndex of a new nor gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance

Source

pub fn nor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex

Returns the GateIndex of a new gate with 1 dependency.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn nor2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with 2 dependencies.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn norx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn and<S: Into<String>>(&mut self, name: S) -> GateIndex

Returns the GateIndex of a new and gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance

Source

pub fn and1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex

Returns the GateIndex of a new gate with 1 dependency.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn and2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with 2 dependencies.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn andx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn nand<S: Into<String>>(&mut self, name: S) -> GateIndex

Returns the GateIndex of a new nand gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance

Source

pub fn nand1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex

Returns the GateIndex of a new gate with 1 dependency.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn nand2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with 2 dependencies.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn nandx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn xor<S: Into<String>>(&mut self, name: S) -> GateIndex

Returns the GateIndex of a new xor gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance

Source

pub fn xor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex

Returns the GateIndex of a new gate with 1 dependency.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn xor2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with 2 dependencies.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn xorx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn xnor<S: Into<String>>(&mut self, name: S) -> GateIndex

Returns the GateIndex of a new xnor gate with no dependencies. Dependencies can be added with GateGraphBuilder::dpush

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance

Source

pub fn xnor1<S: Into<String>>(&mut self, dep: GateIndex, name: S) -> GateIndex

Returns the GateIndex of a new gate with 1 dependency.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn xnor2<S: Into<String>>( &mut self, dep1: GateIndex, dep2: GateIndex, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with 2 dependencies.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn xnorx<S: Into<String>, I: Iterator<Item = GateIndex> + Clone>( &mut self, iter: I, name: S, ) -> GateIndex

Returns the GateIndex of a new gate with x dependencies. the dependencies are taken in order from iter.

Providing a good name allows for a great debugging experience, you can disable the “debug_gates” feature to slightly increase performance.

Source

pub fn init(self) -> InitializedGateGraph

Returns a new InitializedGateGraph created from self after running optimizations.

Source

pub fn init_unoptimized(self) -> InitializedGateGraph

Returns a new InitializedGateGraph created from self without running optimizations.

Source

pub fn output<S: Into<String>>( &mut self, bits: &[GateIndex], name: S, ) -> OutputHandle

Returns a new OutputHandle with name name for the gates in bits.

See OutputHandle for gate querying methods.

Source

pub fn output1<S: Into<String>>( &mut self, bit: GateIndex, name: S, ) -> OutputHandle

Returns a new OutputHandle with name name for a single gate bit.

See OutputHandle for gate querying methods.

Source

pub fn len(&self) -> usize

Returns the number of gates in the graph.

Source

pub fn dump_dot(&self, filename: &'static str)

Dumps the graph in dot format to path filename, to be visualized by many supported tools, I recommend gephi.

Source

pub fn probe<S: Into<String>>(&mut self, bits: &[GateIndex], name: S)

“Probes” the gates in bits, meaning that whenever the state of any of them changes, the new state of the group will be printed to stdout along with name.

§Example
let mut g = GateGraphBuilder::new();

let l1 = g.lever("l1");
let l2 = g.lever("l2");


let or = g.xor2(l1.bit(), l2.bit(), "or");
let xor = g.xor2(l1.bit(), l2.bit(), "xor");
g.probe(&[or,xor],"or_xor");
let xor_output = g.output1(xor, "xor_output");


let ig = &mut g.init();
assert_eq!(xor_output.b0(ig), false);

ig.set_lever_stable(l1);
assert_eq!(xor_output.b0(ig), true);

ig.set_lever_stable(l2);
assert_eq!(xor_output.b0(ig), false);

ig.reset_lever_stable(l1);
assert_eq!(xor_output.b0(ig), true);

ig.reset_lever_stable(l2);
assert_eq!(xor_output.b0(ig), false);

In the terminal you’ll see:

or_xor: 3
or_xor: 1
or_xor: 3
or_xor: 0
Source

pub fn probe1<S: Into<String>>(&mut self, bit: GateIndex, name: S)

“Probes” the gate bit, meaning that whenever its state changes, the new state will be printed to stdout along with name.

Trait Implementations§

Source§

impl Clone for GateGraphBuilder

Source§

fn clone(&self) -> GateGraphBuilder

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GateGraphBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for GateGraphBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.