VHDL Math Tricks PDF
VHDL Math Tricks PDF
the Trade
by
Jim Lewis
Director of Training, SynthWorks Design Inc
[email protected]
http://www.SynthWorks.com
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003
SynthWorks
VHDL Math Tricks of the Trade
VHDL is a strongly typed language. Success in VHDL
depends on understanding the types and overloaded
operators provided by the standard and numeric
packages.
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P2
SynthWorks
Common VHDL Types
TYPE Value Origin
std_ulogic 'U', 'X', '0', '1', 'Z', 'W', 'L', 'H', '-' std_logic_1164
std_ulogic_vector array of std_ulogic std_logic_1164
std_logic resolved std_ulogic std_logic_1164
std_logic_vector array of std_logic std_logic_1164
unsigned array of std_logic numeric_std,
std_logic_arith
signed array of std_logic numeric_std,
std_logic_arith
boolean true, false standard
character 191 / 256 characters standard
string array of character standard
integer -(231 -1) to (231 - 1) standard
real -1.0E38 to 1.0E38 standard
time 1 fs to 1 hr standard
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P3
SynthWorks
Packages for Numeric Operations
● numeric_std -- IEEE standard
● Defines types signed, unsigned
Recommendation:
Use numeric_std for new designs
Ok to use std_logic_unsigned with numeric_std*
* Currently, IEEE 1076.3 plans to have a numeric package that permits
unsigned math with std_logic_vector
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P4
SynthWorks
Packages for Numeric Operations
● Using IEEE Numeric_Std Recommendation:
Recommendation:
Use
Use numeric_std
numeric_std for
for new
new designs
designs
library
library ieee
ieee ;;
use
use ieee.std_logic_1164.all
ieee.std_logic_1164.all ;;
use
use ieee.numeric_std.all
numeric_std
ieee.numeric_std.all ;; Use
Use numeric_std
numeric_std oror
std_logic_arith,
std_logic_arith, but
but
never
never both
both
● Using Synopsys Std_Logic_Arith
library
library ieee
ieee ;;
use
use ieee.std_logic_1164.all ;;
ieee.std_logic_1164.all
use
use ieee.std_logic_arith.all
std_logic_arith
ieee.std_logic_arith.all ;;
use
use ieee.std_logic_unsigned.all
ieee.std_logic_unsigned.all ;;
Recommendation,
Recommendation, ifif you
you use
use Synopsys
Synopsys Packages:
Packages:
Use
Use std_logic_arith
std_logic_arith for
for numeric
numeric operations
operations
Use
Use std_logic_unsigned
std_logic_unsigned only
only for
for counters
counters and
and testbenches
testbenches
Don't
Don't use
use the
the package
package std_logic_signed.
std_logic_signed.
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P5
SynthWorks
Unsigned and Signed Types
● Used to represent numeric values:
TYPE Value Notes
unsigned 0 to 2NN - 1
signed - 2(N-1)
(N-1) to 2(N-1)
(N-1) - 1 2's Complement number
== 15
15 decimal
decimal only
only ifif using
using
C_slv <= "1111" ; std_logic_unsigned
std_logic_unsigned
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P6
SynthWorks
Unsigned and Signed Types
● Type definitions identical to std_logic_vector
type UNSIGNED is array (natural range <>) of std_logic;
type SIGNED is array (natural range <>) of std_logic;
SynthWorks
Overloading Basics
● Simplified view of overloading provided by VHDL packages
Operator
Operator Left
Left Right
Right Result
Result
Logic
Logic TypeA
TypeA TypeA
TypeA TypeA
TypeA
Numeric Array Array Array1
Array Integer Array1
Integer Array Array1
Notes:
Notes:
Array
Array == unsigned,
unsigned, signed, std_logic_vector22
signed, std_logic_vector
TypeA
TypeA == boolean,
boolean, std_logic,
std_logic, std_ulogic,
std_ulogic, bit_vector
bit_vector
std_logic_vector, std_ulogic_vector,
std_logic_vector, std_ulogic_vector,
signed33,, unsigned
signed unsigned33
Array
Array and
and TypeA
TypeA types
types used
used in
in an
an expression
expression must
must be
be the
the same.
same.
1) for comparison operators the result is boolean
2) only for std_logic_unsigned.
3) only for numeric_std and not std_logic_arith
● For a detailed view of VHDL's overloading, get the VHDL Types and
Operators Quick Reference card at: http://www.SynthWorks.com/papers
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P8
SynthWorks
Overloading Examples
Signal
Signal A_uv,
A_uv, B_uv,
B_uv, C_uv,
C_uv, D_uv,
D_uv, E_uv
E_uv :: unsigned(7
unsigned(7 downto
downto 0)
0) ;;
Signal
Signal R_sv,
R_sv, S_sv,
S_sv, T_sv,
T_sv, U_sv,
U_sv, V_sv
V_sv :: signed(7
signed(7 downto
downto 0)0) ;;
Signal
Signal J_slv,
J_slv, K_slv,
K_slv, L_slv
L_slv :: std_logic_vector(7
std_logic_vector(7 downto
downto 0)
0) ;;
signal
signal Y_sv
Y_sv :: signed(8
signed(8 downto
downto 0)
0) ;;
.. .. ..
--
-- Permitted
Permitted
A_uv
A_uv <=
<= B_uv
B_uv ++ C_uv
C_uv ;; --
-- Unsigned
Unsigned ++ Unsigned
Unsigned == Unsigned
Unsigned
D_uv
D_uv <=
<= B_uv
B_uv ++ 11 ;; --
-- Unsigned
Unsigned ++ Integer
Integer == Unsigned
Unsigned
E_uv
E_uv <=
<= 11 ++ C_uv;
C_uv; --
-- Integer
Integer ++ Unsigned
Unsigned == Unsigned
Unsigned
R_sv
R_sv <=
<= S_sv
S_sv ++ T_sv
T_sv ;; --
-- Signed
Signed ++ Signed
Signed == Signed
Signed
U_sv
U_sv <=
<= S_sv
S_sv + 1 ;;
+ 1 --
-- Signed
Signed ++ Integer
Integer == Signed
Signed
V_sv
V_sv <=
<= 11 ++ T_sv;
T_sv; --
-- Integer
Integer ++ Signed
Signed == Signed
Signed
J_slv
J_slv <=
<= K_slv
K_slv ++ L_slv
L_slv ;; --
-- if
if using
using std_logic_unsigned
std_logic_unsigned
--
-- Illegal
Illegal Cannot
Cannot mix
mix different
different array
array types
types
--
-- Solution persented later in type conversions
Solution persented later in type conversions
--
-- Y_sv
Y_sv <=
<= A_uv
A_uv -- B_uv
B_uv ;; --
-- want
want signed
signed result
result
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P9
SynthWorks
Strong Typing Implications
● Size and type of target (left) = size and type of expression (right)
● Each operation returns a result that has a specific size based on
rules of the operation. The table below summarizes these rules.
Operation Size of Y = Size of Expression
Y <= "10101010" ; number of digits in literal
Y <= X"AA" ; 4 * (number of digits)
Y <= A ; A'Length = Length of array A
Y <= A and B ; A'Length = B'Length
W <= A > B ; Boolean
Y <= A + B ; Maximum (A'Length, B'Length)
Y <= A + 10 ; A'Length
V <= A * B ; A'Length + B'Length
SynthWorks
Type Conversions
● VHDL is dependent on overloaded operators and conversions
● What conversion functions are needed?
● Signed & Unsigned (elements) <=> Std_Logic
● Signed & Unsigned <=> Std_Logic_Vector
● Signed & Unsigned <=> Integer
● Std_Logic_vector <=> Integer
● VHDL Built-In Conversions
● Automatic Type Conversion
● Conversion by Type Casting
● Conversion functions located in Numeric_Std
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P12
SynthWorks
Automatic Type Conversion:
Unsigned, Signed <=> Std_Logic
● Two types convert automatically when both are subtypes of the same type.
subtype
subtype std_logic
std_logic is
is resolved
resolved std_ulogic
std_ulogic ;;
Legal A_sl
A_sl <=
<= J_uv(0)
J_uv(0) ;;
Assignments B_sul <= K_sv(7) ;
L_uv(0) <= C_sl ;
M_slv(2) <= N_sv(2) ;
Implication: Y_sl
Y_sl <=
<= A_sl
A_sl and
and B_sul
B_sul and
and
J_uv(2)
J_uv(2) and
and K_sv(7)
K_sv(7) and
and M_slv(2);
M_slv(2);
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P13
SynthWorks
Type Casting:
Unsigned, Signed <=> Std_Logic_Vector
● Use type casting to convert equal sized arrays when:
● Elements have a common base type (i.e. std_logic)
● Indices have a common base type (i.e. Integer)
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P14
SynthWorks
Numeric_Std Conversions:
Unsigned, Signed <=> Integer
● Converting to and from integer requires a conversion function.
● Unsigned, Signed => Integer
Unsigned_int
Unsigned_int <=
<= TO_INTEGER
TO_INTEGER (( A_uv
A_uv )) ;;
Signed_int
Signed_int <=
<= TO_INTEGER
TO_INTEGER (( B_sv
B_sv )) ;;
signal
signal A_uv,
A_uv, C_uv
C_uv :: unsigned
unsigned (7
(7 downto
downto 0)
0) ;;
signal
signal Unsigned_int
Unsigned_int :: integer
integer range
range 00 to
to 255
255 ;;
signal
signal B_sv,
B_sv, D_sv
D_sv :: signed(
signed( 77 downto
downto 0)
0) ;;
signal
signal Signed_int
Signed_int :: integer
integer range
range -128
-128 to
to 127;
127;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P15
SynthWorks
Std_Logic_Arith Conversions:
Unsigned, Signed <=> Integer
● Converting to and from integer requires a conversion function.
● Unsigned, Signed => Integer
Unsigned_int
Unsigned_int <=
<= Conv_INTEGER
Conv_INTEGER (( A_uv
A_uv )) ;;
Signed_int
Signed_int <=
<= Conv_INTEGER
Conv_INTEGER (( B_sv
B_sv )) ;;
signal
signal A_uv,
A_uv, C_uv
C_uv :: unsigned
unsigned (7
(7 downto
downto 0)
0) ;;
signal
signal Unsigned_int
Unsigned_int :: integer
integer range
range 00 to
to 255
255 ;;
signal
signal B_sv,
B_sv, D_sv
D_sv :: signed(
signed( 77 downto
downto 0)
0) ;;
signal
signal Signed_int
Signed_int :: integer
integer range
range -128
-128 to
to 127;
127;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P16
SynthWorks
Std_Logic_Vector <=> Integer
● Converting between std_logic_vector and integer is a two step process:
signal
signal A_slv,
A_slv, C_slv
C_slv :: std_logic_vector
std_logic_vector (7
(7 downto
downto 0)
0) ;;
signal
signal Unsigned_int
Unsigned_int :: integer
integer range
range 00 to
to 255
255 ;;
signal
signal B_slv,
B_slv, D_slv
D_slv :: std_logic_vector(
std_logic_vector( 77 downto
downto 0)
0) ;;
signal
signal Signed_int
Signed_int :: integer
integer range
range -128
-128 to
to 127;
127;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P17
SynthWorks
Ambiguous Expressions
● An expression / statement is ambiguous if more than one operator
symbol or subprogram can match its arguments.
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P18
SynthWorks
Std_Logic_Arith:
Ambiguous Expressions
● VHDL type qualifier (type_name') is a mechanism that specifies the type
of an operand or return value of a subprogram (or operator).
Z_sv
Z_sv <=
<= A_sv
A_sv ++ signed'("1010")
signed'("1010") ;; Effects
Effects all
all numeric
numeric
operators
operators inin
● Leaving out the ' is an error: std_logic_arith
std_logic_arith
--
-- Z_sv
Z_sv <=
<= A_sv
A_sv ++ signed("1010")
signed("1010") ;;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P19
SynthWorks
Addition Operators
Addition Operators: + -
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P20
SynthWorks
Use Integers with Care
● Synthesis tools create a 32-bit wide resources for unconstrained integers
signal
signal Y_int,
Y_int, A_int,
A_int, B_int
B_int :: integer
integer ;;
.. .. ..
Y_int
Y_int <=<= A_int
A_int ++ B_int
B_int ;;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P21
SynthWorks
Comparison Operators
Comparison Operators: = /= > >= < <=
Result
Result == Boolean
Boolean Input
Input arrays
arrays are
are extended
extended to
to be
be the
the same
same length
length
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P22
SynthWorks
Multiplication and Division
Multiplication Operators: * / mod rem
● Array Multiplication
signal
signal A_uv,
A_uv, B_uv
B_uv :: unsigned(
unsigned( 77 downto
downto 0) 0) ;;
signal
signal Z_uv
Z_uv :: unsigned(15
unsigned(15 downto
downto 0) 0) ;;
.. .. ..
Z_uv
Z_uv <=<= A_uv
A_uv ** B_uv;
B_uv; •• Size
Size of
of result
result ==
•• Sum
Sum ofof the
the two
two input
input arrays
arrays
Note:
Note: "/
"/ mod
mod rem"
rem" not
not well
well supported
supported by
by synthesis
synthesis tools.
tools.
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P23
SynthWorks
Adder with Carry Out
'0',
'0', A(3:0)
A(3:0) Y5
Y5 <=
<=
++ '0',
'0', B(3:0)
B(3:0) ('0'
('0' && A)
A) ++ ('0'
('0' && B);
B);
-------------------
-------------------
CarryOut,
CarryOut, Result(3:0)
Result(3:0) YY <=
<= Y5(3
Y5(3 downto
downto 0)
0) ;;
Co
Co <=
<= Y5(4)
Y5(4) ;;
signal
signal A,
A, B,
B, YY :: unsigned(3
unsigned(3 downto
downto 0);
0);
signal
signal Y5
Y5 :: unsigned(4
unsigned(4 downto
downto 0)
0) ;;
signal
signal Co
Co : std_logic
: std_logic ;;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P24
SynthWorks
Adder with Carry In
signal
signal A,
A, B,
B, YY :: unsigned(3
unsigned(3 downto
downto 0);
0);
signal
signal Y5
Y5 :: unsigned(4
unsigned(4 downto
downto 0)
0) ;;
signal
signal CarryIn
CarryIn :: std_logic
std_logic ;;
Code:
Y5
Y5 <=
<= (A
(A && '1')
'1') ++ (B
(B && CarryIn);
CarryIn);
YY <=
<= Y5(4
Y5(4 downto
downto 1)
1) ;;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P25
SynthWorks
ALU Functions
● ALU1: ● Three implementations
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P26
SynthWorks
Possible Solutions to ALU 1
As Specified: Optimal results:
A A i0
C i1
B i2 o
E
C G i3
i0 sel
D i1
O Z OpSel Z
E i2
i3
F sel B i0
G D i1
F i2 o
H OpSel i3
H
sel
OpSel
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P27
SynthWorks
ALU 1: Tool Driven
ToolDrvnProc
ToolDrvnProc :: process
process (OpSel,A,B,C,D,E,F,G,H)
(OpSel,A,B,C,D,E,F,G,H)
begin
begin
case
case OpSel
OpSel is
is
when
when "00"
"00" =>
=> ZZ <=
<= AA ++ BB ;;
when
when "01"
"01" =>
=> ZZ <=
<= CC ++ DD ;;
when "10"
when "10" =>=> ZZ <= EE ++ FF ;;
<=
when
when "11"
"11" =>
=> ZZ <=
<= GG ++ HH ;;
when others
when others =>=> ZZ <= (others
<= (others => => 'X')
'X') ;;
end
end case
case ;;
end
end process
process ;; ---- ToolDrvnProc
ToolDrvnProc
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P28
SynthWorks
ALU 1: Code Driven
XX <=
<= Mux4(OpSel,
Mux4(OpSel, A,
A, C,
C, E,
E, G)
G) ;;
YY <=
<= Mux4(OpSel,
Mux4(OpSel, B,
B, D,
D, F,
F, H)
H) ;;
ZZ <=
<= XX ++ YY ;;
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P29
SynthWorks
ALU 1:
Defeating Resource Sharing *
● Bad Code will defeat Resource Sharing.
BadAluProc:
BadAluProc: process
process (OpSel,
(OpSel, A,
A, B,
B, C,
C, D,
D, E,
E, F,
F, G,
G, H)
H)
begin
begin
if
if (OpSel
(OpSel == "00")
"00") then
then ZZ <=
<= AA ++ B;
B; end
end if;
if;
if
if (OpSel
(OpSel == "01")
"01") then
then ZZ <=
<= CC ++ D;
D; end
end if;
if;
if (OpSel
if (OpSel == "10")
"10") then
then ZZ <=
<= EE ++ F;
F; end
end if;
if;
if
if (OpSel
(OpSel == "11")
"11") then
then ZZ <=
<= GG ++ H;
H; end
end if;
if;
end
end process
process ;;
Uses
Uses "end
"end if",
if", rather
rather than
than "elsif"
"elsif"
● * Not Recommended,
synthesis tool may create a separate resource for each adder.
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P30
SynthWorks
Defeating Resource Sharing
● When does this happen? ● Separate statemachines and
resources
case
case StateReg
StateReg is is
when
when S1S1 =>=> Statemach
Statemach :: process(...)
process(...)
if
if (in1
(in1 == '1')
'1') then
then begin
begin
ZZ <=
<= AA ++ BB ;; --
-- generate
generate function
function
.. .. .. --
-- select
select logic
logic (OpSel)
(OpSel)
end
end ifif ;; end
end process
process ;;
when
when S2S2 =>=>
if
if (in2
(in2 == '1')
'1') then
then
ZZ <=
<= CC ++ DD ;; Resources
Resources :: process(...)
process(...)
.. .. .. begin
begin
end
end ifif ;; --
-- code:
code:
.. .. .. --
-- arithmetic operators
arithmetic operators
when
when SnSn =>=> --
-- comparison
comparison operators
operators
.. .. .. end
end process
process ;;
when
when others
others => =>
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P31
SynthWorks
More Information
There is work in progress to extend VHDL's math capability.
For more information see the following IEEE working groups
websites:
Group Website
IEEE 1164 http://www.eda.org/vhdl-std-logic
IEEE 1076.3/numeric std http://www.eda.org/vhdlsynth
IEEE 1076.3/floating point http://www.eda.org/fphdl
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P32
SynthWorks
Author Biography
Jim Lewis, Director of Training, SynthWorks Design Inc.
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P33
SynthWorks
SynthWorks VHDL Training
Comprehensive VHDL Introduction 4 Days
http://www.synthworks.com/comprehensive_vhdl_introduction.htm
A design and verification engineers introduction to VHDL syntax,
RTL coding, and testbenches.
Our designer focus ensures that your engineers will be productive
in a VHDL design environment.
Lewis Copyright © 2003 SynthWorks Design Inc. All Rights Reserved. MAPLD 2003 P34