fpga design with python and myhdl

48
21.05.2014 1 FPGA Design with Python and MyHDL Guy Eschemann

Upload: guy-eschemann

Post on 14-Jun-2015

1.818 views

Category:

Engineering


6 download

DESCRIPTION

These are the slides from a talk I gave at the ALL PROGRAMMABLE PLC2 Days 2014 conference in Stuttgart, Germany.

TRANSCRIPT

Page 1: FPGA Design with Python and MyHDL

21.05.2014 1

FPGA Design withPython and MyHDL

Guy Eschemann

Page 2: FPGA Design with Python and MyHDL

21.05.2014 2

Content

• About us• A short introduction to Python• Introduction to MyHDL• Demo• Questions

Page 3: FPGA Design with Python and MyHDL

21.05.2014 3

About noasic

• Quick Facts– Founded in 2014– 14+ years experience in FPGA design– Located in Kehl, Germany

• Areas of expertise– FPGA & IP core design and verification– VHDL code generators

Page 4: FPGA Design with Python and MyHDL

21.05.2014 4

VHDL Code Generators (1)

Page 5: FPGA Design with Python and MyHDL

21.05.2014 5

VHDL Code Generators (2)

Page 6: FPGA Design with Python and MyHDL

21.05.2014 6

Page 7: FPGA Design with Python and MyHDL

21.05.2014 7

A short introduction to

Page 8: FPGA Design with Python and MyHDL

21.05.2014 8

Python

• Created 1991 by Guido van Rossum at CWI (Netherlands)

• General-purpose, high-levelprogramming language

• Emphasizes code readability• Write less code• Strongly, dynamically typed• Object-oriented• Open-source• Runs on many platforms

Page 9: FPGA Design with Python and MyHDL

21.05.2014 9

Sample Python code

Page 10: FPGA Design with Python and MyHDL

21.05.2014 10

Python – Syntax

• Blocks are specified by indentation• No mandatory statement termination• Comments start with #• Assign values using =• Test equality using ==• Increment/decrement using += or -=

Page 11: FPGA Design with Python and MyHDL

21.05.2014 11

Python – Data Structures

• Lists– like one-dimensional arrays

• Tuples– immutable, one-dimensional arrays

• Dictionaries– associative arrays („hash tables“)

Page 12: FPGA Design with Python and MyHDL

21.05.2014 12

Python – Strings

• Can use either single or double quotationmarks

• Multi-line strings enclosed in triple quotes• Use modulo (%) operator and a tuple to

format a string

Page 13: FPGA Design with Python and MyHDL

21.05.2014 13

Python – Flow Control

• Flow control statements:– if– for– while– there is no case/switch. Use „if“ instead.

Page 14: FPGA Design with Python and MyHDL

21.05.2014 14

Python – Functions

• Declared with the „def“ keyword• Optional parameters supported• Functions can return multiple values

Page 15: FPGA Design with Python and MyHDL

21.05.2014 15

Python – Importing

• External libaries (such as MyHDL) must beimported before they can be used:

from myhdl import intbvORfrom myhdl import *

Page 16: FPGA Design with Python and MyHDL

21.05.2014 16

Python Libraries

• String handling• Regular expressions• Random number generation• Unit-test framework• OS interfacing• GUI development• Modules for math, database connections,

network interfacing etc.• Hardware design (MyHDL)

Page 17: FPGA Design with Python and MyHDL

21.05.2014 17

Introduction to

Page 18: FPGA Design with Python and MyHDL

21.05.2014 18

MyHDL

• Python-based hardwaredescription language

• Created 2003 by Jan Decaluwe

• Open source

Page 19: FPGA Design with Python and MyHDL

21.05.2014 19

Why MyHDL?

• Write less code• Avoid common VHDL and Verilog pitfalls• Apply modern SW development techniques to

HW design• Can generate both VHDL and Verilog• Simulator included• Open source• Runs on many platforms (Windows, OSX, Linux)• Python ecosystem

Page 20: FPGA Design with Python and MyHDL

21.05.2014 20

What MyHDL is not

• Not a way to turn arbitrary Python into silicon

• Not a radically new approach• Not a synthesis tool• Not an IP block library• Not only for implementation• Not well suited for accurate timing

simulations

Page 21: FPGA Design with Python and MyHDL

21.05.2014 21

MyHDL Design Flow

Page 22: FPGA Design with Python and MyHDL

21.05.2014 22

Example – 8 bit counter

Page 23: FPGA Design with Python and MyHDL

21.05.2014 23

Modeling Components

• Components are modeled using functions• Function parameters map to ports

Page 24: FPGA Design with Python and MyHDL

21.05.2014 24

Modeling Processes

• Processes are modeled using special„generator“ functions

• Decorators are used to create generatorfunctions

Page 25: FPGA Design with Python and MyHDL

21.05.2014 25

The @instance decorator

• Most general, creates generator from a generator function

a 1

0b

z

Page 26: FPGA Design with Python and MyHDL

21.05.2014 26

The @always decorator

• Abstracts an outer „while True“ loop followedby a „yield“ statement

a 1

0b

z

Page 27: FPGA Design with Python and MyHDL

21.05.2014 27

Combinational logic: @always_comb

• Automatically infers the sensitivity list

a 1

0b

z

Page 28: FPGA Design with Python and MyHDL

21.05.2014 28

Sequential logic: @always_seq

• Infers the reset functionality

• Equivalent code:

Page 29: FPGA Design with Python and MyHDL

21.05.2014 29

Function Decorators

• @instance: most general, multiple yields• @always: single yield, abstracts „while

True“ loop• @always_comb: for asynchronous logic,

automatically infers sensitivity list• @always_seq: for sequential logic,

automatically infers the reset functionality

Page 30: FPGA Design with Python and MyHDL

21.05.2014 30

MyHDL Signals

• Similar to VHDL signals• Signal declaration:

s_empty = Signal(0)

s_count = Signal(intbv(0)[8:])

s_clk = Signal(bool(0))

• Signal assignment:

s_count.next = 0

Page 31: FPGA Design with Python and MyHDL

21.05.2014 31

MyHDL intbv class

• Similiar to standard Python „int“ type withadded indexing and slicing capabilities

• intbv = „integer with bit-vector flavor“• Provides access to underlying two‘s

complement representation• Range of allowed values can be

constrained

Page 32: FPGA Design with Python and MyHDL

21.05.2014 32

MyHDL intbv creation

Create an intbv: intbv([val=None] [, min=None] [, max=None])

Example:>>> a = intbv(24, min=0, max=25)>>> a.min0>>> a.max25>>> len(a)5

Page 33: FPGA Design with Python and MyHDL

21.05.2014 33

MyHDL intbv indexing and slicing

Indexing:>>> a = intbv(0x12)>>> bin(a)'10010'>>> a[0]False>>> a[1]True

Slicing:>>> a = intbv(0x12)>>> a[4:0]intbv(2L)

Page 34: FPGA Design with Python and MyHDL

21.05.2014 34

MyHDL intbv creation

Create an intbv, specifying its width:

>>> a = intbv(24)[5:]

>>> a.min

0

>>> a.max

32

>>> len(a)

5

Page 35: FPGA Design with Python and MyHDL

21.05.2014 35

Modeling hierarchy

Page 36: FPGA Design with Python and MyHDL

21.05.2014 36

Generated VHDL code

Page 37: FPGA Design with Python and MyHDL

21.05.2014 37

Verification

• Verification is MyHDL´s strongest point• Arguably the hardest part of hardware

design• There are no restrictions for verification:

can use the full power of Python• Can do „agile“ hardware design• The Foundation is an event-driven

simulator in the MyHDL library

Page 38: FPGA Design with Python and MyHDL

21.05.2014 38

MyHDL Simulator

Page 39: FPGA Design with Python and MyHDL

21.05.2014 39

A basic MyHDL simulation

Page 40: FPGA Design with Python and MyHDL

21.05.2014 40

Debugging in MyHDL

• Regular Python debugging• Waveform viewer output

Page 41: FPGA Design with Python and MyHDL

21.05.2014 41

Conversion

• A subset of MyHDL can be automaticallyconverted to VHDL and Verilog

• The converter maintains the abstractionlevel

• Supports some unique MyHDL features• Creates readable VHDL/Verilog• Convertible subset much broader than

synthesizable subset

Page 42: FPGA Design with Python and MyHDL

21.05.2014 42

Conversion example

• MyHDL Code

• Conversion to Verilog

• Conversion to VHDL

• The converter does the casts and resizingsautomatically

Page 43: FPGA Design with Python and MyHDL

21.05.2014 43

User-defined code

Page 44: FPGA Design with Python and MyHDL

21.05.2014 44

Demo

Page 45: FPGA Design with Python and MyHDL

21.05.2014 45

Caveats

• Dynamic nature of Python needs somegetting used to

• Error messages can be cryptic• Cannot import legacy VHDL/Verilog code

– Write own behaviorals models for simulation– Use user-defined code for conversion

• Cannot generate parameterizable HDL code

Page 46: FPGA Design with Python and MyHDL

21.05.2014 46

MyHDL future

• Fixed-point support in the converter• Interfaces• Python 3.0 support

Page 47: FPGA Design with Python and MyHDL

21.05.2014 47

Resources

• http://python.org• http://myhdl.org

– Manual & examples– Download & installation instructions– Mailing list– Tutorials & publications

• @MyHDL on twitter• Development with mercurial on Bitbucket

Page 48: FPGA Design with Python and MyHDL

21.05.2014 48

Contact

Guy Eschemannnoasic GmbHSundheimer Feld 677694 Kehl

[email protected]://noasic.comTwitter: @geschema