1
0
mirror of https://github.com/myhdl/myhdl.git synced 2025-01-24 21:52:56 +08:00
myhdl/olddoc/whatsnew03/whatsnew03.tex

425 lines
14 KiB
TeX
Raw Normal View History

2003-07-17 18:21:27 +00:00
\documentclass{howto}
2003-07-18 10:02:53 +00:00
% \usepackage{distutils}
\usepackage{palatino}
\renewcommand{\ttdefault}{cmtt}
\renewcommand{\sfdefault}{cmss}
\newcommand{\myhdl}{\protect \mbox{MyHDL}}
2003-07-17 18:21:27 +00:00
\usepackage{graphicx}
% $Id$
2003-07-18 10:02:53 +00:00
\title{What's New in \myhdl\ 0.3}
2003-07-17 20:52:34 +00:00
\release{0.3}
\author{Jan Decaluwe}
\authoraddress{\email{jan@jandecaluwe.com}}
2003-07-17 18:21:27 +00:00
\begin{document}
\maketitle
\tableofcontents
2003-07-17 20:52:34 +00:00
\section{VCD output for waveform viewing\label{section-wave}}
2003-07-17 18:21:27 +00:00
\ifpdf
\includegraphics{tbfsm.png}
\fi
2003-07-18 10:02:53 +00:00
\myhdl\ now has support for waveform viewing. During simulation, signal
2003-08-22 08:06:45 +00:00
changes can be written to a VCD output file that can be loaded
into a waveform viewer tool such as \program{gtkwave}.
2003-07-17 18:21:27 +00:00
2003-07-17 22:50:47 +00:00
The user interface of this feature consists of a single function,
2003-07-30 11:48:48 +00:00
\function{traceSignals()}. To explain how it works, recall that in
2003-08-22 08:06:45 +00:00
\myhdl{}, an instance is created by assigning
the result of a function call to an instance name. For example:
2003-07-17 18:21:27 +00:00
\begin{verbatim}
2003-07-17 20:52:34 +00:00
tb_fsm = testbench()
2003-07-17 18:21:27 +00:00
\end{verbatim}
2003-07-28 19:17:17 +00:00
To enable VCD tracing, the instance should be created as follows
instead:
2003-07-17 18:21:27 +00:00
\begin{verbatim}
2003-07-30 11:48:48 +00:00
tb_fsm = traceSignals(testbench)
2003-07-17 18:21:27 +00:00
\end{verbatim}
2003-08-22 08:06:45 +00:00
All signals in the instance hierarchy will be traced in a VCD
2003-08-03 16:03:39 +00:00
file called \file{tb_fsm.vcd}. Note that first the argument of
\function{traceSignals()} consists of the uncalled function. By
calling the function under its control, \function{traceSignals()}
gathers information about the hierarchy and the signals to be traced.
In addition to a function argument, \function{traceSignals()} accepts
an arbitrary number of non-keyword and keyword arguments that will be
2003-07-22 22:02:20 +00:00
passed to the function call.
2003-07-18 09:21:40 +00:00
Signals are dumped in a suitable format. This format is inferred at
the \class{Signal} construction time, from the type of the initial
value. In particular, \class{bool} signals are dumped as single
2003-08-29 13:55:31 +00:00
bits. (This only works starting with Python~2.3, when \class{bool} has
2003-07-18 09:21:40 +00:00
become a separate type). Likewise, \class{intbv} signals with a
defined bit width are dumped as bit vectors. To support the general
case, other types of signals are dumped as a string representation, as
returned by the standard \function{str()} function.
2003-07-17 18:21:27 +00:00
2003-07-28 19:17:17 +00:00
\begin{notice}[warning]
2003-07-30 09:18:43 +00:00
Support for literal string representations is not part of the VCD
standard. It is specific to \program{gtkwave}. To generate a
standard VCD file, you need to use signals with a defined bit width
only.
2003-07-28 19:17:17 +00:00
\end{notice}
2003-08-04 17:28:10 +00:00
\section{Enumeration types\label{section-enum}}
2003-07-22 22:02:20 +00:00
It is often desirable to define a set of identifiers. A standard
2003-07-28 18:43:38 +00:00
Python idiom for this purpose is to assign a range of integers to a
tuple of identifiers, like so:
2003-07-22 22:02:20 +00:00
\begin{verbatim}
>>> SEARCH, CONFIRM, SYNC = range(3)
>>> CONFIRM
1
\end{verbatim}
2003-07-30 09:18:43 +00:00
However, this technique has some drawbacks. Though it is clearly
2003-07-22 22:02:20 +00:00
the intention that the identifiers belong together, this information
is lost as soon as they are defined. Also, the identifiers evaluate to
integers, whereas a string representation of the identifiers
would be preferable. To solve these issues, we need an
\emph{enumeration type}.
\myhdl\ 0.3 supports enumeration types by providing a function
\function{enum()}. The arguments to \function{enum()} are the string
representations of the identifiers, and its return value is an
enumeration type. The identifiers are available as attributes of the
type. For example:
\begin{verbatim}
>>> from myhdl import enum
>>> t_State = enum('SEARCH', 'CONFIRM', 'SYNC')
>>> t_State
<Enum: SEARCH, CONFIRM, SYNC>
>>> t_State.CONFIRM
CONFIRM
\end{verbatim}
2003-07-23 12:16:34 +00:00
Enumeration types are often used for the state variable in a finite
state machine. In the waveform in Section~\ref{section-wave}, you see
2003-08-01 14:13:30 +00:00
a \class{Signal} called \var{state}. Note how the waveforms show the
string representation of the enumeration type identifiers The
\var{state} signal has been constructed with an enumeration type
identifier as its initial value, as follows:
2003-07-22 22:02:20 +00:00
\begin{verbatim}
state = Signal(t_State.SEARCH)
\end{verbatim}
2003-07-25 06:35:55 +00:00
\section{Inferring the sensitivity list for combinatorial
logic\label{section-combinatorial}}
2003-07-30 09:18:43 +00:00
In \myhdl{}, combinatorial logic is described by a generator function with
2003-07-25 06:35:55 +00:00
a sensitivity list that contains all inputs signals (the signals that
are read inside the function).
2003-07-30 09:18:43 +00:00
It may be easy to forget some input signals, especially it there are a
lot of them or if the code is being modified. There are various ways
to solve this. One way is to use a sophisticated editor. Another way
is direct language support. For example, recent versions of Verilog
have the \code{always~@*} construct, that infers all input
2003-08-29 14:02:48 +00:00
signals. The SystemVerilog~3.1 standard improves on this by
2003-07-26 19:23:03 +00:00
introducing the \code{always_comb} block with slightly enhanced
2003-07-25 06:35:55 +00:00
semantics.
2003-07-24 10:34:47 +00:00
2003-08-29 14:01:33 +00:00
\myhdl{}~0.3 provides a function called \function{always_comb()} which
2003-07-26 19:23:03 +00:00
is named and modeled after the SystemVerilog counterpart.
2003-08-22 08:06:45 +00:00
\function{always_comb()} takes a classic local function as its
argument. This function should specify the combinatorial logic behavior.
\function{always_comb()} returns a generator
that is sensitive to all inputs, and that will run the function
whenever an input changes.
2003-07-24 10:34:47 +00:00
2003-07-28 18:43:38 +00:00
For example, suppose that we have a mux module described as follows:
2003-07-24 10:34:47 +00:00
\begin{verbatim}
def mux(z, a, b, sel):
""" Multiplexer.
z -- mux output
a, b -- data inputs
sel -- control input
"""
def logic()
while 1:
yield a, b, sel
if sel == 1:
z.next = a
else:
z.next = b
mux_logic = logic()
return mux_logic
\end{verbatim}
Using \function{always_comb()}, we can describe it as follows instead:
\begin{verbatim}
def mux(z, a, b, sel):
""" Multiplexer.
z -- mux output
a, b -- data inputs
sel -- control input
"""
def logic()
if sel == 1:
z.next = a
else:
z.next = b
mux_logic = always_comb(logic)
return mux_logic
\end{verbatim}
2003-08-01 14:22:44 +00:00
Note that in the first version, the sensitivity list is at the beginning of
2003-08-01 14:13:30 +00:00
the generator function code. This is traditionally done in
synthesizable RTL style modeling. However, the semantics of this style
are not entirely correct: at the start of the simulation, the
combinatorial output will not reflect the initial state of the
2003-08-22 08:06:45 +00:00
inputs. \function{always_comb()} solves this by putting the sensitivity
2003-08-01 14:13:30 +00:00
list at the end of the code.
2003-07-23 20:21:40 +00:00
2003-08-22 08:06:45 +00:00
\section{Inferring the list of instances\label{section-instances}}
2003-07-23 12:16:34 +00:00
In \myhdl{}, the instances defined in a top level function
2003-07-30 09:18:43 +00:00
need to be returned explicitly. The following is a schematic
example:
2003-07-23 12:16:34 +00:00
\begin{verbatim}
def top(...):
2003-07-23 20:21:40 +00:00
...
2003-07-23 12:16:34 +00:00
instance_1 = module_1(...)
instance_2 = module_2(...)
...
instance_n = module_n(...)
2003-07-23 20:21:40 +00:00
...
2003-07-23 12:16:34 +00:00
return instance_1, instance_2, ... , instance_n
\end{verbatim}
2003-08-03 16:03:39 +00:00
It may be convenient to assemble the list of instances automatically,
2003-08-22 08:06:45 +00:00
especially if there are many instances. For this purpose,
2003-08-29 14:01:33 +00:00
\myhdl{}~0.3 provides the function \function{instances()}.
2003-08-03 16:03:39 +00:00
It is used as follows:
2003-07-23 12:16:34 +00:00
\begin{verbatim}
from myhdl import instances
def top(...):
2003-07-23 20:21:40 +00:00
...
2003-07-23 12:16:34 +00:00
instance_1 = module_1(...)
instance_2 = module_2(...)
...
instance_n = module_n(...)
2003-07-23 20:21:40 +00:00
...
2003-07-23 12:16:34 +00:00
return instances()
\end{verbatim}
2003-07-30 09:18:43 +00:00
Function \function{instances()} uses introspection to inspect the type
2003-08-08 09:12:55 +00:00
of the local variables defined by the calling function. All variables
that comply with the definition of an instance are assembled in
a list, and that list is returned.
2003-07-23 12:16:34 +00:00
2003-08-22 08:06:45 +00:00
\section{Inferring the list of processes\label{section-processes}}
2003-07-23 12:16:34 +00:00
2003-07-23 20:21:40 +00:00
In addition to instances, a top level function may
also define local generators functions, which I will
call \emph{processes} because of the analogy with VHDL.
Like instances, processes need to be returned explicitly,
with the qualification that they have to be called first
2003-07-30 09:18:43 +00:00
to turn them into generators. The following is a schematic
example:
2003-07-23 12:16:34 +00:00
2003-07-23 20:21:40 +00:00
\begin{verbatim}
def top(...):
...
def process_1():
...
def process_2():
...
2003-07-30 09:18:43 +00:00
...
2003-07-23 20:21:40 +00:00
def process_n():
...
...
return process_1(), process_2(), ..., process_n()
\end{verbatim}
As for instances, it may be more convenient to assemble the list of
processes automatically. One option is to turn each process into an
2003-07-25 06:35:55 +00:00
instance by calling it and assigning the returned generator to a local
variable. Those instances will then be found by the
2003-07-23 20:21:40 +00:00
\function{instances()} function described in
Section~\ref{section-instances}.
Another option is to use the function \function{processes()} provided
2003-08-29 14:42:42 +00:00
by \myhdl{}~0.3. This function uses introspection to find the
2003-07-23 20:21:40 +00:00
processes, calls each of them, and assembles the returned generators
into a list. It can be used as follows:
2003-07-23 12:16:34 +00:00
2003-07-23 20:21:40 +00:00
\begin{verbatim}
2003-07-28 18:43:38 +00:00
from myhdl import processes
2003-07-23 20:21:40 +00:00
def top(...):
...
def process_1():
...
def process_2():
...
2003-07-30 09:18:43 +00:00
...
2003-07-23 20:21:40 +00:00
def process_n():
...
...
return processes()
\end{verbatim}
2003-07-25 06:35:55 +00:00
To conclude, a top level function with both instances and processes
can use the following idiomatic code to return all of them:
2003-07-23 20:21:40 +00:00
\begin{verbatim}
return instances(), processes()
\end{verbatim}
2003-07-23 12:16:34 +00:00
2003-07-25 06:35:55 +00:00
\section{Class \class{intbv} enhancements\label{section-intbv}}
Class \class{intbv} has been enhanced with new features.
It is now possible to leave the left index of a slicing operation
unspecified. The meaning is to access ``all'' higher order bits. For
example:
\begin{verbatim}
>>> from myhdl import intbv
>>> n = intbv()
>>> hex(n)
'0x0'
>>> n[:] = 0xde
>>> hex(n)
'0xde'
>>> n[:8] = 0xfa
>>> hex(n)
'0xfade'
>>> n[8:] = 0xb4
>>> hex(n)
'0xfab4'
\end{verbatim}
2003-07-30 14:16:04 +00:00
\class{intbv} objects now have \var{min} and \var{max} attributes
2003-08-29 11:55:55 +00:00
that can be specified at construction time. The meaning is that
2003-07-25 06:35:55 +00:00
only values within \code{range(min, max)} are permitted. The default
2003-08-29 11:55:55 +00:00
value for these attributes is \var{None}, meaning ``infinite''. For
2003-07-25 06:35:55 +00:00
example (traceback output shortened for clarity):
\begin{verbatim}
>>> n = intbv(min=-17, max=53)
>>> n
intbv(0)
>>> n.min
-17
>>> n.max
53
>>> n[:] = 28
>>> n
intbv(28)
>>> n[:] = -18
Traceback (most recent call last):
....
ValueError: intbv value -18 < minimum -17
>>> n[:] = 53
Traceback (most recent call last):
....
ValueError: intbv value 53 >= maximum 53
\end{verbatim}
2003-07-30 14:16:04 +00:00
When a slice is taken from an \class{intbv} object, the return value
is a new \class{intbv} object with a defined bit width. As in
Verilog, the value of the new \class{intbv} object is always
2003-07-25 06:35:55 +00:00
positive, regardless of the sign of the original value. In addition,
the \var{min} and \var{max} attributes are set implicitly:
\begin{verbatim}
>>> v = intbv()[6:]
>>> v
intbv(0)
>>> v.min
0
>>> v.max
64
\end{verbatim}
2003-07-23 12:16:34 +00:00
2003-07-28 18:43:38 +00:00
Lastly, a small change was implemented with regard to
binary operations. In previous versions, both numeric
and bit-wise operations always returned a new \class{intbv}
2003-07-30 14:16:04 +00:00
object, even in mixed-mode operations with \class{int}
2003-07-28 18:43:38 +00:00
objects. This has changed: numeric operations
return an \class{int}, and bitwise operations return
a \class{intbv}. In this way, the return value corresponds
better to the nature of the operation.
\section{Function \function{concat()} \label{section-concat}}
In previous versions, the \class{intbv} class provided a
\method{concat()} method. This method is no longer
2003-08-22 08:06:45 +00:00
available. Instead, there is now a \function{concat()} function
that supports a much broader range of objects.
2003-07-28 18:43:38 +00:00
A function is more natural because \myhdl\ objects of various types
2003-07-31 18:09:24 +00:00
can be concatenated: \class{intbv} objects with a defined bit width,
\class{bool} objects, the corresponding signal objects, and bit
strings. All these objects have a defined bit width. Moreover, the
first argument doesn't need to have a defined bit width. It can also be
an unsized \class{intbv}, an \class{int}, a \class{long}, or a
corresponding signal object. Function \function{concat()} returns an
\class{intbv} object.
2003-07-28 18:43:38 +00:00
2003-07-22 22:02:20 +00:00
\section{Python 2.3 support\label{section-Python}}
2003-08-29 13:55:31 +00:00
Python~2.3 was released on July 29, 2003, and as of this writing, it
is the latest stable Python release. \myhdl{}~0.3 works with both
Python~2.2 and Python~2.3. In good Python tradition, \myhdl\ code
developed with Python~2.2 should run without changes or problems in
Python~2.3.
2003-07-22 22:02:20 +00:00
2003-07-28 18:43:38 +00:00
In general, I am not that keen on early upgrading. However, as it
2003-07-22 22:02:20 +00:00
happens, the evolution of Python enables features that are really
2003-08-29 13:55:31 +00:00
important or even crucial to \myhdl{}. Python~2.2 generators are the
best example: they are the cornerstone of \myhdl{}. But Python~2.3
2003-07-22 22:02:20 +00:00
also has significant benefits, which I will summarize below.
2003-08-29 13:55:31 +00:00
First, generators and the \code{yield} statement are a default
Python~2.3 feature. This means that \code{from __future__ import generators}
2003-07-22 22:02:20 +00:00
statements are no longer required.
2003-08-29 13:55:31 +00:00
Second, Python~2.3 has a \class{bool} type, which is implemented as a
2003-07-22 22:02:20 +00:00
subtype of \class{int}. For general Python use, the implications are
2003-07-25 06:35:55 +00:00
rather limited - the main difference is that logical result values
will print as \code{False} and \code{True} instead of \code{0} and
\code{1}. However, in \myhdl{}, I can use the \class{bool} type to
infer a bit width. If a \class{Signal} is constructed with a
\class{bool} value, it is a single bit \class{Signal}. One application
is waveform viewing as in Section~\ref{section-wave}. In the waveform,
note how single bit signals are displayed as level changes. With
Python 2.2, the waveforms of these signals would only show value
changes, which is not as clear for single bits.
2003-07-22 22:02:20 +00:00
2003-08-29 13:55:31 +00:00
Finally, Python~2.3 is significantly faster. \myhdl\ code runs
25--35\% faster in Python~2.3. This is a very nice speedup compared to
2003-07-30 09:18:43 +00:00
the small burden of a straightforward upgrade.
2003-07-22 22:02:20 +00:00
2003-08-29 13:55:31 +00:00
Python is a very stable language, so upgrading to Python~2.3 is
2003-07-22 22:02:20 +00:00
virtually risk free. Given the additional benefits, I recommend
2003-08-22 08:06:45 +00:00
\myhdl\ users to do so as soon as possible. For the next major \myhdl\
2003-08-29 13:55:31 +00:00
release, the new features will become required and only Python~2.3
2003-08-22 08:06:45 +00:00
(and higher) will be supported.
2003-07-22 22:02:20 +00:00
2003-07-17 18:21:27 +00:00
\end{document}