A python package for creating logic circuits.
Project description
Circuits-py
A python package for creating logic circuits.
from circuits_py import State
class DLatch():
def __init__(self, Q, QI):
self.Q = Q
self.QI = QI
def d_latch(self, Clock, D):
self.Q = State(D).NOT().AND(Clock).NOR(self.QI).state
self.QI = State(D).AND(Clock).NOR(self.Q).state
l = DLatch(1, 0)
from circuits_py import State, logic, divide, combine
def addCircuit(a, b, carry):
a = State(a) # A State object represents current up to the next logic gate.
divide(a.XOR(b), # when an circuit path divides
lambda e: e.XOR(carry),
lambda e: a.AND(carry).OR(a.AND(b).state)
)
addCircuit(1, 1, 0)
Design
using plain functions to create circuits make it harder to understand and write:
out = AND(0, AND(0, XOR(input, 0))) # gates are nested
# <----<----<----<----<-----
# the circuit flows in the opposite direction of the code
instead:
State(1).XOR(0).AND(0).AND(0)
# ----->---->---->---->
# the circuit flows in the direction of the code
State
A State object represents path of electrical current up to the next logical gate, a state can be initialized either 0 or 1.
a = State(0)
b = State(1)
supported logic functions: AND, OR, XOR, NOT, NOR, NAND, XNOR and BUFFER
when a logic gate is invoked a new State() object is returned with the output of that gate
State(1).XOR(0).AND(1) # returns State(1)
the output state can be accessed by get_output()
print(State(1).XOR(0).AND(1).get_output())
Custom Logic
use the combine() function to use custom logic,
from circuits_py import combine, logic_gate
@logic_gate
def custom_logic(A, B):
if ((A) and (not B)):
return True
else:
return False
# combine() takes two or more paths and combines it to a logic function
combine(
State(1).AND(1),
State(0).AND(0),
Logic=custom_logic # pass the logic function
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
circuits_py-0.0.1.tar.gz
(4.7 kB
view hashes)
Built Distribution
Close
Hashes for circuits_py-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 773525cef02a89c4f497dc3cf67d36f8005c0a238a23f41e0bc63b067b2b3771 |
|
MD5 | 28283a50735e76f210ed6a64d69dd92f |
|
BLAKE2b-256 | 4231cfbb1c05eeb261880381b996037e758de70786eee46b02fb47aea0aaa001 |