Python module including a variety of logic gate classes, and a truth table class to automatically generate truth tables of logic circuits.
Click here for the latest (pre-)release.
This module includes a variety of classes to define logic gates and other related components. A logic circuit can be built by instantiating gate objects linked to each other. Most logic gates require one or more output gates, meaning that the circuit must generally be built beginning with the outputs and working backwards.
Most gates are instantiated in the same way, and require defining a list of output gates (one for each output). Gates which accept multiple inputs maintain these inputs in a list. The keyword out_ports
argument accepts a list of port indices corresponding to the output gates. The keyword name
argument accepts a name for the gate to use in truth tables.
For example, if we already have two XOR
gate objects called Xor1
and Xor2
, then instantiating a SPLIT
gate with SplitGate([Xor1, Xor2], out_ports=[1, 0])
would connect the SPLIT
gate to port 1
of Xor1
and port 0
of Xor2
. Note that even gates with a single output must be given their output gates in list form.
Most gates also include a process()
method which causes it to evaluate its boolean inputs and calculate the appropriate boolean outputs, and a _send_out()
method which sends its outputs to all output gates and prompts them to also evaluate their process()
methods. This may lead to infinite loops if there are cycles in the logic circuit.
The IN
and SWITCH
gates both include a toggle()
method which can be used to set their internal state. For IN
, this means setting it to True
or False
, and for SWITCH
, this means either transferring its output to its first ("left") or its second ("right") output gate.
The OUT
gate has no output, and is mostly meant for use in defining truth table entries.
The TruthTable
class can store sets of variable inputs, constant inputs, and outputs, and cycle through every possible setting of the variable inputs to generate a complete truth table of the resulting outputs via the generate_table()
method.
The circuit_load()
function can be used to load a circuit defined in an external text file in the circuits/
folder. The format for a circuit file is described in the included _template
file. As the input file is read, gate objects are created and linked to each other, and then used to instantiate a TruthTable
object. The function returns a dictionary of the created gate objects as well as the created TruthTable
object.
-
Inputs (0 Inputs, 1 Output):
InGate
-- Simply outputs a signal. Includes atoggle()
method which sets its signal asTrue
orFalse
. Can be used as a truth table input.TrueGate
-- LikeInGate
, but outputs a constantTrue
signal.FalseGate
-- LikeInGate
, but outputs a constantFalse
signal.
-
Outputs (1 Input, 0 Outputs):
OutGate
-- Simply stores its input signal. Meant for use in defining the truth table outputs.
-
Unary Gates (1 Input, 1 Output):
DiodeGate
-- Outputs a copy of its own input signal.NotGate
-- Outputs negation of input signal.
-
Binary Gates (2 Inputs, 1 Output):
AndGate
-- OutputsTrue
if both inputs areTrue
, andFalse
otherwise.NandGate
-- OutputsTrue
if both inputs areFalse
, andFalse
otherwise.OrGate
-- OutputsTrue
if at least one input isTrue
, andFalse
otherwise.NorGate
-- OutputsTrue
if at least one input isFalse
, andFalse
otherwise.XorGate
-- OutputsTrue
if exactly one input isTrue
, andFalse
otherwise.XnorGate
-- OutputsTrue
if both inputs are the same, andFalse
otherwise.
-
Splitters (1 Input, 2 Outputs):
SplitGate
-- Outputs a copy of its own input signal to both outputs.SwitchGate
-- Outputs a copy of its own input signal to one output gate, outputtingFalse
to the other gate. Includes atoggle()
method which selects which gate is selected.False
indicates gate0
("left") whileTrue
indicates gate1
("right"). Can be used as a truth table input.
A logic circuit can be defined as a tab-separated table in a text file. Each row corresponds to a gate in the circuit. The main module's circuit_load()
method will attempt to create a TruthTable
object with the specified gates, in the order of their listing in the circuit file. Since gates always require their output gates to already be defined, the gates must be listed in reverse topological order, beginning with the outputs and working backwards to the inputs. That is, nothing should be listed as an OutGate
unless it has already been defined in a previous row as an ID.
The column order is:
Name
-- Name string for the gate. Must be unique for each gate, and must not contain any whitespace characters.Type
-- String name of gate type. Case insensitive. Must be chosen from the following list:OUT
IN
TRUE
FALSE
AND
OR
XOR
NAND
NOR
XNOR
NOT
DIODE
SPLIT
SWITCH
OutGates
-- Tab-separated list of output gate names. The length of the list must be appropriate for the given type of gate.OutPorts
-- Tab-separated list of output port IDs. The length of the list must be appropriate for the given type of gate.
Comment lines may be included if preceded by a "#
" symbol.