\title{myHDL to PYNQ Fabric Only Exsample} \author{Steven K Armour} \maketitle
In [1]:
from myhdl import *
from myhdlpeek import Peeker
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sympy import *
init_printing()
import random
#https://github.com/jrjohansson/version_information
%load_ext version_information
%version_information myhdl, myhdlpeek, numpy, pandas, matplotlib, sympy, random
Out[1]:
In [2]:
#helper functions to read in the .v and .vhd generated files into python
def VerilogTextReader(loc, printresult=True):
with open(f'{loc}.v', 'r') as vText:
VerilogText=vText.read()
if printresult:
print(f'***Verilog modual from {loc}.v***\n\n', VerilogText)
return VerilogText
def VHDLTextReader(loc, printresult=True):
with open(f'{loc}.vhd', 'r') as vText:
VerilogText=vText.read()
if printresult:
print(f'***VHDL modual from {loc}.vhd***\n\n', VerilogText)
return VerilogText
In [3]:
@block
def S0L0(sw, clk, led):
"""
FPGA Hello world of one switch controlling one LED based on
https://timetoexplore.net/blog/arty-fpga-verilog-01
Target:
ZYNQ 7000 Board (Arty, PYNQ-Z1, PYNQ-Z2) with at least 2
switchs and 4 leds
Input:
sw(2bitVec):switch input
clk(bool): clock input
Ouput:
led(4bitVec): led output
"""
@always(clk.posedge)
def logic():
if sw[0]==0:
led.next[0]=True
else:
led.next[0]=False
return instances()
In [4]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
sw=Signal(intbv(0)[2:]); Peeker(sw, 'sw')
led=Signal(intbv(0)[4:]); Peeker(led, 'led')
np.random.seed(18)
swTVals=[int(i) for i in np.random.randint(0,2, 10)]
DUT=S0L0(sw, clk, led)
def S0L0_TB():
@always(delay(1))
def ClkGen():
clk.next=not clk
@instance
def stimules():
for i in range(10):
sw.next[0]=swTVals[i]
yield clk.posedge
raise StopSimulation()
return instances()
sim=Simulation(DUT, S0L0_TB(), *Peeker.instances()).run()
In [5]:
Peeker.to_wavedrom()
In [6]:
Peeker.to_dataframe()
Out[6]:
In [7]:
DUT.convert()
VerilogTextReader('S0L0');
In [8]:
swTVal=intbv(int(''.join([str(i) for i in swTVals]), 2))[len(swTVals):]
print(f'swTest: {swTVals}, {swTVal}, {[int(i) for i in swTVal]}')
In [9]:
@block
def S0L0_TBV():
clk=Signal(bool(0))
sw=Signal(intbv(0)[2:])
led=Signal(intbv(0)[4:])
#test stimuli
swTVals=Signal(swTVal)
@always_comb
def print_data():
print(sw, clk, led)
DUT=S0L0(sw, clk, led)
@instance
def clk_signal():
while True:
clk.next = not clk
yield delay(1)
@instance
def stimules():
for i in range(10):
sw.next[0]=swTVals[i]
yield clk.posedge
raise StopSimulation()
return instances()
TB=S0L0_TBV()
TB.convert(hdl="Verilog", initial_values=True)
VerilogTextReader('S0L0_TBV');
In [10]:
@block
def S2L4(sw, clk, led):
"""
FPGA Hello world of two switchs controlling four LED based on
https://timetoexplore.net/blog/arty-fpga-verilog-01
Target:
ZYNQ 7000 Board (Arty, PYNQ-Z1, PYNQ-Z2) with at least 2
switchs and 4 leds
Input:
sw(2bitVec):switch input
clk(bool): clock input
Ouput:
led(4bitVec): led output
"""
@always(clk.posedge)
def logic():
if sw[0]==0:
led.next[2:]=0
else:
led.next[2:]=3
if sw[1]==0:
led.next[4:2]=0
else:
led.next[4:2]=3
return instances()
In [11]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
sw=Signal(intbv(0)[2:]); Peeker(sw, 'sw')
led=Signal(intbv(0)[4:]); Peeker(led, 'led')
np.random.seed(18)
swTVals=[int(i) for i in np.random.randint(0,4, 10)]
DUT=S2L4(sw, clk, led)
def S2L4_TB():
@always(delay(1))
def ClkGen():
clk.next=not clk
@instance
def stimules():
for i in range(10):
sw.next=swTVals[i]
yield clk.posedge
raise StopSimulation()
return instances()
sim=Simulation(DUT, S2L4_TB(), *Peeker.instances()).run()
In [12]:
Peeker.to_wavedrom()
In [13]:
Peeker.to_dataframe()
Out[13]:
In [14]:
DUT.convert()
VerilogTextReader('S2L4');
In [ ]:
In [15]:
@block
def countLED(clk, led):
counter=Signal(modbv(0)[33:])
@always(clk.posedge)
def logic():
counter.next=counter+1
led.next[0]=counter[26]
led.next[1]=counter[24]
led.next[3]=counter[22]
led.next[4]=counter[20]
return instances()
In [16]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
led=Signal(intbv(0)[4:]); Peeker(led, 'led')
DUT=countLED(clk, led)
'''
def countLED_TB():
@always(delay(1))
def ClkGen():
clk.next=not clk
@instance
def stimules():
i=0
while True:
if i==2**33:
raise StopSimulation()
if 1%100==0:
print(i)
i+=1
yield clk.posedge
return instances()
sim=Simulation(DUT, countLED_TB(), *Peeker.instances()).run()
'''
;
Out[16]:
Need to figure out how to write/run these long simulations better in python
In [17]:
DUT.convert()
VerilogTextReader('countLED');
In [18]:
@block
def BDCLed(clk, led):
counter=Signal(modbv(0)[8:])
duty_led=Signal(modbv(8)[8:])
@always(clk.posedge)
def logic():
counter.next=counter+1
if counter<duty_led:
led.next=15
else:
led.next=0
return instances()
In [26]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
led=Signal(intbv(0)[4:]); Peeker(led, 'led')
DUT=BDCLed(clk, led)
def BDCLed_TB():
@always(delay(1))
def ClkGen():
clk.next=not clk
@instance
def stimules():
i=0
while True:
if i==1000:
raise StopSimulation()
i+=1
yield clk.posedge
return instances()
sim=Simulation(DUT, BDCLed_TB(), *Peeker.instances()).run()
In [27]:
Peeker.to_wavedrom()
In [28]:
BDCLedData=Peeker.to_dataframe()
BDCLedData=BDCLedData[BDCLedData['clk']==1]
BDCLedData.plot(y='led');
In [30]:
DUT.convert()
VerilogTextReader('BDCLed');
In [31]:
@block
def BDCLed_TBV():
clk=Signal(bool(0))
led=Signal(intbv(0)[4:])
@always_comb
def print_data():
print(sw, clk, led)
DUT=BDCLed(clk, led)
@instance
def clk_signal():
while True:
clk.next = not clk
yield delay(1)
@instance
def stimules():
i=0
while True:
if i==1000:
raise StopSimulation()
i+=1
yield clk.posedge
return instances()
TB=BDCLed_TBV()
TB.convert(hdl="Verilog", initial_values=True)
VerilogTextReader('BDCLed_TBV');
In [32]:
@block
def pwm(clk, dutyCount, o_state):
counter=Signal(modbv(0)[8:])
@always(clk.posedge)
def logic():
counter.next=counter+1
o_state.next=counter<dutyCount
return instances()
In [33]:
Peeker.clear()
clk=Signal(bool(0)); Peeker(clk, 'clk')
dutyCount=Signal(intbv(4)[8:]); Peeker(dutyCount, 'dutyCount')
o_state=Signal(bool(0)); Peeker(o_state, 'o_state')
DUT=pwm(clk, dutyCount, o_state)
def pwm_TB():
pass
In [ ]:
In [ ]: