PyCircHDL - Python Logic Circuit
Hardware Description Language Package

Installing the PyCircHDL package

Loading the PyCircHDL package

Introduction

Example 1: The Circuit FOO

Circuit libraries

Gates as Cell Instances


The Circuit FRED

The Circuit HAM

Cell Query and Manipulation

4x1 Multiplexer Design

# File for MUX2
# input:  x3, x2, x1, x0, s2, s1
# output: y

GATE("x0", type="inp")
GATE("x1", type="inp")
GATE("x2", type="inp")
GATE("x3", type="inp")
GATE("s1", type="inp")
GATE("s2", type="inp")
GATE("y", type="out")
GATE("g1", type="not")
GATE("g2", type="not")
GATE("g3", type="and3")
GATE("g4", type="and3")
GATE("g5", type="and3")
GATE("g6", type="and3")
GATE("g7", type="or4")

WIRE("s1", "g1/x")
WIRE("s1", "g5/x1")
WIRE("s1", "g6/x1")
WIRE("s2", "g2/x")
WIRE("s2", "g4/x3")
WIRE("s2", "g6/x3")
WIRE("x0", "g3/x2")
WIRE("x1", "g4/x2")
WIRE("x2", "g5/x2")
WIRE("x3", "g6/x2")
WIRE("g1/y", "g3/x1")
WIRE("g1/y", "g4/x1")
WIRE("g2/y", "g3/x3")
WIRE("g2/y", "g5/x3")
WIRE("g3/y", "g7/x1")
WIRE("g4/y", "g7/x2")
WIRE("g5/y", "g7/x3")
WIRE("g6/y", "g7/x4")
WIRE("g7/y", "y")
# File: mux2.py (compressed version!)
# MUX2  (4x1 Multiplexer)
# input:  x3, x2, x1, x0, s2, s1
# output: y

GATE("x<0:3>", type="inp")             # 4 bits
GATE("s1;s2", type="inp")              # 2 selectors
GATE("y", type="out")
GATE("g1; g2", type="not")
GATE("g<3:6>", type="and3")            # 4 gates of basic type "and3"
GATE("g7", type="or4")

WIRE("s1", "g1/x; g5/x1; g6/x1")
WIRE("s2", "g2/x; g4/x3; g6/x3")       # 3 wires!
WIRE("x<0:3>", "g<3:6>/x2")
WIRE("g1/y", "g3/x1; g4/x1")
WIRE("g2/y", "g3/x3; g5/x3")
WIRE("g<3:6>/y", "g7/x<1:4>")          # 4 wires
WIRE("g7/y", "y")

Truth Assignments in PyCirc

Here are some code examples for manipulating an Assign object

How to initialize a PyCirc object?

Getting the circuit output

Assignment Iterator

8x1 Multiplexer Design

3-bits Adder Design

ADDER9 - 9-bits Adder Design

Half Adder

Boolean Operators

def And(a, output="y"):
    o = Assign(output)
    for x in a:
        if a[x] == 0:
            for y in o: o[y] = 0
            return o
    for y in o: o[y] = 1
    return o

def Or(a, output="y"):
    o = Assign(output)
    for x in a:
        if a[x] == 1:
            for y in o: o[y] = 1
            return o
    for y in o: o[y] = 0
    return o

def Nor(a, output="y"):
    o = Assign(output)
    o1 = OR(a, Assign("y"))
    b = Assign("x", o1["y"])
    return NOT(b, o)

def Xor(a, output="y"):
    o = Assign(output)
    if sum(a[x] for x in a) == 1:
        for y in o: o[y] = 1
        return o
    else:
        for y in o: o[y] = 0
        return o
def Zero(a=None, output="y"):
    o = Assign(output)
    for y in o: o[y] = 0
    return o

def One(a=None, output="y"):
    o = Assign(output)
    for y in o: o[y] = 1
    return o

Creating Cells with the Cell Class

Advanced Topics -- To be Continued ...