\title{Basic Digital Logic Gates with myHDL} \author{Steven K Armour} \maketitle
@book{brown_vranesic_2014, place={New York, NY}, edition={3}, title={Fundamentals of digital logic with Verilog design}, publisher={McGraw-Hill}, author={Brown, Stephen and Vranesic, Zvonko G}, year={2014} },
@book{lameres_2017, title={Introduction to logic circuits & logic design with Verilog}, publisher={springer}, author={LaMeres, Brock J}, year={2017} }
In [1]:
import numpy as np
import pandas as pd
from sympy import *
init_printing()
from myhdl import *
from myhdlpeek import *
import random
#python file of convince tools. Should be located with this notebook
from sympy_myhdl_tools import *
In [2]:
x_in, y_out=symbols('x_in, y_out')
NOTDef1=Eq(y_out, ~x_in)
NOTDef2=Eq(y_out, Not(x_in))
NOTDef1, NOTDef2
Out[2]:
In [3]:
NOT_TT=TruthTabelGenrator(NOTDef1)
NOT_TT
Out[3]:
In [4]:
NOTDef1N=lambdify((x_in), NOTDef1.rhs, dummify=False)
NOTDef1N(False)
Out[4]:
In [5]:
pyNOTEq1=lambda x: not x
In [6]:
pyNOTEq1(False)
Out[6]:
In [7]:
#create a HDL Module (python function) and its I\O ports
def NotGate(x_in, y_out):
"""Not Logic Gate myHDL Module (function)
Args:
x_in: bool Input port
y_out: bool Output port
Returns:
NOT Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
#state the logic operation: in this case negation of the input 'x_in'
#mapped to the ouput 'y_out' at each Clock cycle
y_out.next= not x_in
return logic
At the moment there was a backward compatibility break so that myHDL could call in stuff from lambda functions involving strargs, see: https://github.com/myhdl/myhdl/issues/148
Till this gets fixed no instant lampdafiy into the logic to define the thing but when this gets fixed I am trying that out
In [8]:
#create the test signals and intilize value to both be false(0)
x_in, y_out=[Signal(bool(0)) for _ in range(2)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_in, 'x_in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=NotGate(x_in=x_in, y_out=y_out)
inputs=[x_in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='NOT gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [10]:
MakeDFfromPeeker(Peeker.to_wavejson())
Out[10]:
In [11]:
toVerilog(NotGate, x_in, y_out)
toVHDL(NotGate, x_in, y_out)
_=VerilogTextReader('NotGate')
In [12]:
_=VHDLTextReader('NotGate')
The following shows the Xilinx's Vivado 2016.1 RTL generated schematic of our NOT Gate from the synthesised verilog code
The symbols for an AND gate are shown below
In [13]:
x_1in, x_2in, y_out=symbols('x_1in, x_2in, y_out')
AND2Def1=Eq(y_out, x_1in & x_2in)
AND2Def2=Eq(y_out, And(x_1in , x_2in))
AND2Def1, AND2Def2
Out[13]:
In [14]:
AND2_TT=TruthTabelGenrator(AND2Def1)
AND2_TT
Out[14]:
In [15]:
AND2Def2N=lambdify((x_1in, x_2in), AND2Def2.rhs, dummify=False)
AND2Def2N(True, False)
Out[15]:
In [16]:
x_1in, x_2in, x_3in, y_out=symbols('x_1in, x_2in, x_3in, y_out')
AND3Def1=Eq(y_out, x_1in & x_2in & x_3in)
AND3Def2=Eq(y_out, And(x_1in , x_2in, x_3in))
AND3Def1, AND3Def2
Out[16]:
In [17]:
AND3_TT=TruthTabelGenrator(AND3Def2)
AND3_TT
Out[17]:
In [18]:
AND3Def1N=lambdify((x_1in, x_2in, x_3in), AND3Def1.rhs, dummify=False)
#not working no idea
#AND3Def1N(True, True, True)
In [19]:
pyAND2Eq2=lambda x1, x2: x1 & x2
pyAND2Eq2=lambda x1, x2: x1 and x2
pyAND3Eq1=lambda x1, x2, x3: x1 & x2 & x3
pyAND3Eq2=lambda x1, x2, x3: x1 and x2 and x3
In [20]:
pyAND2Eq2(0,0), pyAND2Eq2(0,1), pyAND3Eq1(1,1,1), pyAND3Eq1(0,1,1)
Out[20]:
In [21]:
#create a HDL Module (python function) and its I\O ports
def And2Gate(x_1in, x_2in, y_out):
"""AND Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
y_out: bool Output port
Returns:
AND Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= x_1in and x_2in
return logic
In [22]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, y_out=[Signal(bool(0)) for _ in range(3)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=And2Gate(x_1in=x_1in, x_2in=x_2in, y_out=y_out)
inputs=[x_1in, x_2in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='AND 2 gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [23]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[23]:
In [24]:
#create a myHDL Module (python function) and its I\O ports
def And3Gate(x_1in, x_2in, x_3in, y_out):
"""AND Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
x_3in: bool Input port
y_out: bool Output port
Returns:
AND Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= x_1in and x_2in and x_3in
return logic
In [25]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, x_3in, y_out=[Signal(bool(0)) for _ in range(4)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in'); Peeker(x_3in, 'x_3in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=And3Gate(x_1in=x_1in, x_2in=x_2in, x_3in=x_3in, y_out=y_out)
inputs=[x_1in, x_2in, x_3in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='AND 2 gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [26]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[26]:
In [27]:
toVerilog(And3Gate, x_1in, x_2in, x_3in, y_out)
#toVHDL(And3Gate, x_1in, x_2in, x_3in y_out)
_=VerilogTextReader('And3Gate')
The following shows the Xilinx's Vivado 2016.1 RTL generated schematic of our myHDL three input AND gate's verilog code
In [28]:
x_1in, x_2in, y_out=symbols('x_1in, x_2in, y_out')
OR2Def1=Eq(y_out, x_1in | x_2in)
OR2Def2=Eq(y_out, Or(x_1in , x_2in))
OR2Def1, OR2Def2
Out[28]:
In [29]:
OR2_TT=TruthTabelGenrator(OR2Def2)
OR2_TT
Out[29]:
In [30]:
OR2Def1N=lambdify((x_1in, x_2in), OR2Def1.rhs, dummify=False)
OR2Def1N(True, False)
Out[30]:
In [31]:
x_1in, x_2in, x_3in, y_out=symbols('x_1in, x_2in, x_3in, y_out')
OR3Def1=Eq(y_out, x_1in | x_2in | x_3in)
OR3Def2=Eq(y_out, Or(x_1in , x_2in, x_3in))
OR3Def1, OR3Def2
Out[31]:
In [32]:
OR3_TT=TruthTabelGenrator(OR3Def1)
OR3_TT
Out[32]:
In [33]:
OR3Def2N=lambdify((x_1in, x_2in, x_3in), OR3Def2.rhs, dummify=False)
#yeah WTH
#OR3Def2N(False, False, False)
In [34]:
pyOR2Eq1=lambda x1, x2: x1 | x2
pyOR2Eq2=lambda x1, x2: x1 or x2
pyOR3Eq1=lambda x1, x2, x3: x1 | x2 | x3
pyOR3Eq2=lambda x1, x2, x3: x1 or x2 or x3
In [35]:
pyOR2Eq1(0, 0), pyOR2Eq2(1,1), pyOR3Eq1(1,1,0), pyOR3Eq2(0,1,0)
Out[35]:
In [36]:
#create a HDL Module (python function) and its I\O ports
def Or2Gate(x_1in, x_2in, y_out):
"""AND Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
y_out: bool Output port
Returns:
AND Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= x_1in or x_2in
return logic
In [37]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, y_out=[Signal(bool(0)) for _ in range(3)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=Or2Gate(x_1in=x_1in, x_2in=x_2in, y_out=y_out)
inputs=[x_1in, x_2in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='OR 2 gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [40]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[40]:
In [41]:
#create a HDL Module (python function) and its I\O ports
def Or3Gate(x_1in, x_2in, x_3in, y_out):
"""Or Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
x_3in: bool Input port
y_out: bool Output port
Returns:
AND Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= x_1in or x_2in or x_3in
return logic
In [42]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, x_3in, y_out=[Signal(bool(0)) for _ in range(4)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in'); Peeker(x_3in, 'x_3in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=Or3Gate(x_1in=x_1in, x_2in=x_2in, x_3in=x_3in, y_out=y_out)
inputs=[x_1in, x_2in, x_3in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='OR 2 gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [43]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[43]:
In [44]:
toVerilog(Or3Gate, x_1in, x_2in, x_3in, y_out)
#toVHDL(And3Gate, x_1in, x_2in, x_3in y_out)
_=VerilogTextReader('Or3Gate')
The following shows the Xilinx's Vivado 2016.1 RTL generated schematic of our myHDL three input OR gate's verilog code
In [45]:
x_1in, x_2in, y_out=symbols('x_1in, x_2in, y_out')
NAND2Def0=Eq(y_out, Nand(x_1in, x_2in))
NAND2Def1=Eq(y_out, ~(x_1in & x_2in))
NAND2Def2=Eq(y_out, Not(And(x_1in , x_2in)))
NAND2Def3=simplify(NAND2Def2) #DeMorgan equivlinacy
NAND2Def0, NAND2Def1, NAND2Def2, NAND2Def3
Out[45]:
In [46]:
NAND2_TT=TruthTabelGenrator(NAND2Def0)
NAND2_TT
Out[46]:
In [47]:
NAND2_DME_TT=TruthTabelGenrator(NAND2Def2)
NAND2_DME_TT
Out[47]:
In [48]:
NAND2_TT.equals(NAND2_DME_TT)
Out[48]:
In [49]:
NAND2Def3N=lambdify((x_1in, x_2in), NAND2Def3.rhs, dummify=False)
NAND2Def3N(True, True)
Out[49]:
In [50]:
x_1in, x_2in, x_3in, y_out=symbols('x_1in, x_2in, x_3in, y_out')
NAND3Def0=Eq(y_out, Nand(x_1in, x_2in, x_3in))
NAND3Def1=Eq(y_out, ~(x_1in & x_2in & x_3in))
NAND3Def2=Eq(y_out, Not(And(x_1in , x_2in, x_3in)))
NAND3Def3=simplify(NAND3Def1) #DeMorgan equivlinacy
NAND3Def0, NAND3Def1, NAND3Def2, NAND3Def3
Out[50]:
In [51]:
NAND3_TT=TruthTabelGenrator(NAND3Def0)
NAND3_TT
Out[51]:
In [52]:
NAND3_DME_TT=TruthTabelGenrator(NAND3Def1)
NAND3_TT.equals(NAND3_DME_TT)
Out[52]:
In [53]:
NAND3Def3N=lambdify((x_1in, x_2in, x_3in), NAND3Def3, dummify=False)
#again WTH
#NAND3Def3N(1, 0, 1)
In [54]:
#DoNotTrust: pyNAND2Eq1=lambda x1, x2: ~(x1 & x2)
pyNAND2Eq2=lambda x1, x2: not(x1 and x2)
#DoNotTrust: pyNAND2Eq3=lambda x1, x2: ~x1 | ~x2
pyNAND2Eq4=lambda x1, x2: not x1 or not x2
#DoNotTrust:pyNAND3Eq1=lambda x1, x2, x3: ~(x1 & x2 & x3)
pyNAND3Eq2=lambda x1, x2, x3: not(x1 and x2 and x3)
#DoNotTrust:pyNAND3Eq3=lambda x1, x2, x3: ~x1 | ~x2 | ~x3
pyNAND3Eq4=lambda x1, x2, x3: not x1 or not x2 or not x3
In [55]:
pyNAND2Eq2(0,1), pyNAND2Eq4(1,1), pyNAND3Eq2(0,1,0), pyNAND3Eq4(0,0,0)
Out[55]:
In [56]:
#create a HDL Module (python function) and its I\O ports
def Nand2Gate(x_1in, x_2in, y_out):
"""AND Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
y_out: bool Output port
Returns:
AND Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= not(x_1in and x_2in)
return logic
In [57]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, y_out=[Signal(bool(0)) for _ in range(3)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=Nand2Gate(x_1in=x_1in, x_2in=x_2in, y_out=y_out)
inputs=[x_1in, x_2in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='NAND 2 gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [58]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[58]:
In [59]:
#create a HDL Module (python function) and its I\O ports
def Nand3Gate(x_1in, x_2in, x_3in, y_out):
"""AND Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
x_3in: bool Input port
y_out: bool Output port
Returns:
AND Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= (not x_1in) or (not x_2in) or (not x_3in)
return logic
In [60]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, x_3in, y_out=[Signal(bool(0)) for _ in range(4)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in'); Peeker(x_3in, 'x_3in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=Nand3Gate(x_1in=x_1in, x_2in=x_2in, x_3in=x_3in, y_out=y_out)
inputs=[x_1in, x_2in, x_3in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='NAND 3 gate simulation',
## myHDL Three Input AND Gate HDL Synthesis
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [61]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[61]:
In [62]:
toVerilog(Nand3Gate, x_1in, x_2in, x_3in, y_out)
#toVHDL(And3Gate, x_1in, x_2in, x_3in y_out)
_=VerilogTextReader('Nand3Gate')
The following shows the Xilinx's Vivado 2016.1 RTL generated schematic of our myHDL three input NAND gate's verilog code
In [63]:
x_1in, x_2in, y_out=symbols('x_1in, x_2in, y_out')
NOR2Def0=Eq(y_out, Nor(x_1in, x_2in))
NOR2Def1=Eq(y_out, ~(x_1in | x_2in))
NOR2Def2=Eq(y_out, Not(Or(x_1in , x_2in)))
NOR2Def3=simplify(NOR2Def2) #DeMorgan equivlinacy
NOR2Def0, NOR2Def1, NOR2Def2, NOR2Def3
Out[63]:
In [64]:
NOR2_TT=TruthTabelGenrator(NOR2Def0)
NOR2_TT
Out[64]:
In [65]:
NOR2_DME_TT=TruthTabelGenrator(NOR2Def3)
NOR2_TT.equals(NOR2_DME_TT)
Out[65]:
In [66]:
NOR2Def2N=lambdify((x_1in, x_2in), NOR2Def2.rhs, dummify=False)
NOR2Def2N(0,1)
Out[66]:
In [67]:
x_1in, x_2in, x_3in, y_out=symbols('x_1in, x_2in, x_3in, y_out')
NOR3Def0=Eq(y_out, Nor(x_1in, x_2in, x_3in))
NOR3Def1=Eq(y_out, ~(x_1in | x_2in | x_3in))
NOR3Def2=Eq(y_out, Not(Or(x_1in , x_2in, x_3in)))
NOR3Def3=simplify(NOR3Def1) #DeMorgan equivlinacy
NOR3Def0, NOR3Def1, NOR3Def2, NOR3Def3
Out[67]:
In [68]:
NOR3_TT=TruthTabelGenrator(NOR3Def0)
NOR3_TT
Out[68]:
In [69]:
NOR3_DME_TT=TruthTabelGenrator(NOR3Def1)
NOR3_TT.equals(NOR3_DME_TT)
Out[69]:
In [70]:
NOR3Def2N=lambdify((x_1in, x_2in, x_3in), NOR3Def2.rhs, dummify=False)
#Again WTH
#NOR3Def2N(False, False, False)
In [71]:
#DoNotTrust: pyNOR2Eq1=lambda x1, x2: ~(x1 | x2)
pyNOR2Eq2=lambda x1, x2: not(x1 or x2)
#DoNotTrust: pyNOR2Eq3=lambda x1, x2: ~x1 & ~x2
pyNOR2Eq4=lambda x1, x2: not x1 and not x2
#DoNotTrust: pyNOR3Eq1=lambda x1, x2, x3: ~(x1 | x2 | x3)
pyNOR3Eq2=lambda x1, x2, x3: not(x1 or x2 or x3)
#DoNotTrust: pyNOR3Eq3=lambda x1, x2, x3: ~x1 & ~x2 & ~x3
pyNOR3Eq4=lambda x1, x2, x3: not x1 and not x2 and not x3
In [72]:
pyNOR2Eq2(0,1), pyNOR2Eq4(1,0), pyNOR3Eq2(0,0,0), pyNOR3Eq4(1,0,1)
Out[72]:
In [73]:
#create a HDL Module (python function) and its I\O ports
def Nor2Gate(x_1in, x_2in, y_out):
"""AND Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
y_out: bool Output port
Returns:
AND Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= not(x_1in or x_2in)
return logic
In [74]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, y_out=[Signal(bool(0)) for _ in range(3)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=Nor2Gate(x_1in=x_1in, x_2in=x_2in, y_out=y_out)
inputs=[x_1in, x_2in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='NOR 2 gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [75]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[75]:
In [76]:
toVerilog(Nor2Gate, x_1in, x_2in, y_out)
#toVHDL(Nor2Gate, x_1in, x_2in y_out)
_=VerilogTextReader('Nor2Gate')
The following shows the Xilinx's Vivado 2016.1 RTL generated schematic of our myHDL two input NOR gate's verilog code
The Symbols for a XOR gate are show below
In [77]:
x_1in, x_2in, y_out=symbols('x_1in, x_2in, y_out')
XOR2Def0=Eq(y_out, Xor(x_1in, x_2in))
XOR2Def1=Eq(y_out, x_1in ^ x_2in)
XOR2Def2=Eq(y_out, ~x_1in&x_2in | x_1in& ~x_2in)
XOR2Def0, XOR2Def1, XOR2Def2, simplify_logic(XOR2Def2)
Out[77]:
In [78]:
XOR2_TT=TruthTabelGenrator(XOR2Def0)
XOR2_TT
Out[78]:
In [79]:
XOR2_TT2=TruthTabelGenrator(XOR2Def2)
XOR2_TT.equals(XOR2_TT2)
Out[79]:
In [80]:
XOR2Def1N=lambdify((x_1in, x_2in), XOR2Def1, dummify=False)
#oh come on
#XOR2Def1N(0, 1)
In [81]:
pyXOR2Eq1=lambda x1, x2: x1^x2
pyXOR2Eq1(1, 1)
Out[81]:
In [82]:
#create a HDL Module (python function) and its I\O ports
def Xor2Gate(x_1in, x_2in, y_out):
"""Or Logic Gate myHDL Module (function)
Args:
x_1in: bool Input port
x_2in: bool Input port
y_out: bool Output port
Returns:
XOR Combo logic operation
"""
#define what this module is to do
#myHDL built in decorator to say the function is exuiquated on each
#clock cycle
@always_comb
def logic():
y_out.next= x_1in ^ x_2in
return logic
In [83]:
#create the test signals and intilize value to both be false(0)
x_1in, x_2in, y_out=[Signal(bool(0)) for _ in range(3)]
#Peeker is helper libary for viewing wavefrom outputs and run the sim inside
#jupyter notebooks
#clear any previeese data loaded into peeker
Peeker.clear()
#load and name the signals to watch into peeker
Peeker(x_1in, 'x_1in'); Peeker(x_2in, 'x_2in')
Peeker(y_out, 'y_out')
#make an instatince of the NotGate as the DUT
DUT=Xor2Gate(x_1in=x_1in, x_2in=x_2in, y_out=y_out)
inputs=[x_1in, x_2in]
sim=Simulation(DUT, Combo_TB(inputs), *Peeker.instances()).run()
Peeker.to_wavedrom(start_time=0, stop_time=2*2**len(inputs), tock=True,
title='XOR 2 gate simulation',
caption=f'after clock cycle {2**len(inputs)-1} ->random input')
In [84]:
MakeDFfromPeeker(Peeker.to_wavejson(start_time=0, stop_time=2**len(inputs) -1))
Out[84]:
In [85]:
toVerilog(Xor2Gate, x_1in, x_2in, y_out)
#toVHDL(Xor2Gate, x_1in, x_2in y_out)
_=VerilogTextReader('Xor2Gate')
The following shows the Xilinx's Vivado 2016.1 RTL generated schematic of our myHDL two input NOR gate's verilog code
In [88]:
x1, x2, x3, x4=symbols('x_1, x_2, x_3, x_4')
lits=[x1, x2, x3, x4]
In [89]:
terms=set(list(range(2**len(lits)))); terms
Out[89]:
In [90]:
DCterms=set(list(range(10, 15+1))); DCterms
Out[90]:
In [91]:
usableTerms=terms-DCterms; usableTerms
Out[91]:
In [92]:
SOPfromUT=lambda POSterms: usableTerms-POSterms
In [93]:
aPOSterms={1,4}; aSOPterms=SOPfromUT(aPOSterms)
bPOSterms={5,6}; bSOPterms=SOPfromUT(bPOSterms)
cPOSterms={2}; cSOPterms=SOPfromUT(cPOSterms)
dPOSterms={2,4,7}; dSOPterms=SOPfromUT(dPOSterms)
ePOSterms={1,3,4,5,7,9}; eSOPterms=SOPfromUT(ePOSterms)
fPOSterms={1,2,3,7}; fSOPterms=SOPfromUT(fPOSterms)
gPOSterms={0,1,7}; gSOPterms=SOPfromUT(gPOSterms)
In [94]:
aSOP, aPOS=POS_SOPformCalcater(lits, aSOPterms, aPOSterms, DCterms)
bSOP, bPOS=POS_SOPformCalcater(lits, bSOPterms, bPOSterms, DCterms)
cSOP, cPOS=POS_SOPformCalcater(lits, cSOPterms, cPOSterms, DCterms)
dSOP, dPOS=POS_SOPformCalcater(lits, dSOPterms, dPOSterms, DCterms)
eSOP, ePOS=POS_SOPformCalcater(lits, eSOPterms, ePOSterms, DCterms)
fSOP, fPOS=POS_SOPformCalcater(lits, fSOPterms, fPOSterms, DCterms)
gSOP, gPOS=POS_SOPformCalcater(lits, gSOPterms, gPOSterms, DCterms)
In [95]:
aSOP, aPOS
Out[95]:
In [96]:
bSOP, bPOS
Out[96]:
In [97]:
cSOP, cPOS
Out[97]:
In [98]:
dSOP, dPOS
Out[98]:
In [99]:
eSOP, ePOS
Out[99]:
In [100]:
fSOP, fPOS
Out[100]:
In [101]:
gSOP, gPOS
Out[101]:
In [ ]: