05 Arithmetic
05 Arithmetic
111
Arithmetic Package Overview- I
VHDL offers a number of packages which provide
common arithmetical functions
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Comparison (=, >, <, <=, >=, /=)
By simply adding in a use clause at the top of your code
these become available
Synthesis automatically generates the logic required !!
112
Arithmetic Package Overview- II
There are a number of different packages that exist for
historical reasons
STD_LOGIC_ARITH, std_logic_signed, std_logic_unsigned
NUMERIC_STD (IEEE)
We will only consider NUMERIC_STD as it is the only
standard package which is defined on all commercial
synthesis and simulation tools
Tools must provide a common set of arithmetical functions
Synthesis result (gates and how they are connected) will change
with synthesis tool, but functionality will not
113
Declaring Arithmetic Signals & Variables
NUMERIC_STD offers 2 data types
SIGNED, UNSIGNED
These are declared in a similar method to std_logic_vector
Can be used to declare signals, variables, even ports in an entity
UNSIGNED
Assumes that only positive values are going to be used
Example declaration
signal count: unsigned (3 downto 0)
This creates a signal used for storing values 0 -> 15
114
Declaring Arithmetic Signals & Variables
SIGNED
2s complement form, with MSB used as a sign bit
Example declaration
signal count: signed (3 downto 0)
Creates a signal used for storing the values -8 -> +7
Integer Signed
-8 1000
-1 1111
0 0000
+7 0111
115
Representation of Signed/ Unsigned
Signed/ Unsigned values are represented using a subset of
std_logic_vector
I.e. 0, 1 in each bit
However, cannot perform comparisons, assignments etc.
directly with std_logic
We need to use conversion functions (see later)
116
Arithmetic Package Functions- I
For a detailed list of functions (and their operations) see
the program listing from NUMERIC_STD.VHD this is
the official IEEE package
How do I read the package header ?
Consider the function Id: A.6
function + (L: UNSIGNED; R: NATURAL) return UNSIGNED
117
Arithmetic Package Functions- II
Signed Arithmetic Functions:
118
Arithmetic Package Functions- III
Unsigned Arithmetic Functions:
119
Arithmetic Package Functions- IV
Comparison functions:
= Equal
unsigned unsigned
/= Not equal
120
Arithmetic Package Functions- V
Resize functions
Used for resizing a signed/ unsigned value
Useful if we want to extract carry bit etc.
Example
newvalue = resize(oldvalue, 5)
121
Arithmetic Package Functions- VI
Simple conversion functions
Used to convert integer/ natural numbers to and from signed/
unsigned
unsigned natural
Convert to
to_integer
integer
signed integer
Convert to natural unsigned
to_unsigned natural
unsigned (size) (size)
Convert to natural signed
to_signed integer
signed (size) (size)
122
Arithmetic Package Functions- VII
Conversion to/ from std_logic_vector
Used to convert signed and unsigned values to and from
std_logic_vector
Really just copies each bit
123
Making the Package Visible
At the top of the VHDL source file, the line
use ieee.numeric_std.all
is added
This makes the package (functions, data types, etc.)
visible
124
Arithmetic Package Example- I
Some concrete examples of the use of the arithmetic
package !
Simple counter
Counts up to certain value then resets back
Only increments if input inc is set to 1
CLK
INC
use ieee.numeric_std.all;
127
Arithmetic Packages- Conclusion
Have given an overview of the numeric package and the
functions which it contains
These are widely use for counters etc.
Can be used to generate many different arithmetic circuits
(e.g. multipliers etc.) though it may be better to design
these for the given application as the target after synthesis
may be inefficient
128