[][src]Struct logicsim::graph::GateGraphBuilder

pub struct GateGraphBuilder { /* fields omitted */ }

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

impl GateGraphBuilder[src]

pub fn new() -> GateGraphBuilder[src]

Returns a new GateGraphBuilder containing only OFF and ON.

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

Appends new_dep to the list of dependencies of gate target.

Panics

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

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

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.

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

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.

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

pub fn init(self) -> InitializedGateGraph[src]

Returns a new InitializedGateGraph created from self after running optimizations.

pub fn init_unoptimized(self) -> InitializedGateGraph[src]

Returns a new InitializedGateGraph created from self without running optimizations.

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

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

See OutputHandle for gate querying methods.

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

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

See OutputHandle for gate querying methods.

pub fn len(&self) -> usize[src]

Returns the number of gates in the graph.

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

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

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

"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

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

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

Trait Implementations

impl Clone for GateGraphBuilder[src]

impl Debug for GateGraphBuilder[src]

impl Default for GateGraphBuilder[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.