Py4Hw User Guide> 2.1 Bitwise Logic Operations¶

Next: 2.2 Logical and Relational Operations¶

py4hw supports the classic bitwise logic operations that you would expect to have in any programming language: and, or, not, xor.

In [1]:
import py4hw
import matplotlib.pyplot as plt
In [2]:
sys = py4hw.HWSystem()
a = py4hw.Wire(sys, 'a', 8)
b = py4hw.Wire(sys, 'b', 8)
c = py4hw.Wire(sys, 'c', 8)
d = py4hw.Wire(sys, 'd', 8)
e = py4hw.Wire(sys, 'e', 8)
f = py4hw.Wire(sys, 'f', 8)
g = py4hw.Wire(sys, 'g', 8)
h = py4hw.Wire(sys, 'h', 8)

py4hw.Constant(sys, 'a', 13, a)
py4hw.Constant(sys, 'b', 25, b)
py4hw.Constant(sys, 'd', 67, d)
py4hw.Constant(sys, 'f', 32, f)

#py4hw.Sequence(sys, '')
py4hw.And2(sys, 'and', a, b, c)
py4hw.Or2(sys, 'or', c, d, e)
py4hw.Xor2(sys, 'xor', e, f, g)
py4hw.Not(sys, 'not', g, h)

py4hw.Scope(sys, 'h', h)

# py4hw.debug.checkIntegrity(sys)
Out[2]:
<py4hw.logic.simulation.Scope at 0x1b28f43c510>
In [3]:
sch = py4hw.Schematic(sys)
sch.draw()
In [4]:
sim = sys.getSimulator()
sim.clk(1)
Scope [h]:
h=148
----------

we also support logic gates with more than 2 inputs

In [5]:
sys = py4hw.HWSystem()

a = py4hw.Wire(sys, 'a', 8)
b = py4hw.Wire(sys, 'b', 8)
c = py4hw.Wire(sys, 'c', 8)
d = py4hw.Wire(sys, 'd', 8)
e = py4hw.Wire(sys, 'e', 8)
f = py4hw.Wire(sys, 'f', 8)
g = py4hw.Wire(sys, 'g', 8)
h = py4hw.Wire(sys, 'h', 8)

py4hw.Constant(sys, 'a', 13, a)
py4hw.Constant(sys, 'b', 25, b)
py4hw.Constant(sys, 'c', 67, c)
py4hw.Constant(sys, 'e', 32, e)

#py4hw.Sequence(sys, '')
py4hw.And(sys, 'and', [a, b, c], d)
py4hw.Or(sys, 'or', [c, d, e], f)
py4hw.Xor(sys, 'xor', [a, d, f], g)

py4hw.Scope(sys, 'g', g)

sch = py4hw.Schematic(sys)
sch.draw()

Summary¶

  • 1, and 2 input binary logic gates are available (Not, And2, Or2, Xor2)
  • You need to pass the input (a, b) and output (r) wires to the constructor of the circuit (after the parent & name default parameters)
  • n input binary logic gates are also available (And, Or, Xor)
  • n input binary logic gates use a python list of wires and an output wire in the constructor
In [ ]: