VHDL Integer Range?

in VHDL

How do you specify a VHDL integer range?

by (1.8k points)

VHDL Integer Range

Quick Syntax

signal my_integer : integer;
VHDL assumes that my_integer is 32-bits and can be anything from -2,147,483,647 to +2,147,483,647.
signal my_integer : integer range -5 to 255;
VHDL will limit the range of my_integer from -5 to 255 and will only assign the number of bits required.

Purpose

Integers are a default type in VHDL. Here's a run down of Integer and it's subtypes: Natural and Positive.

Integer: 32-bits, range = -2,147,483,647 to +2,147,483,647
Natural: 31-bits, range = 0 to +2,147,483,647
Positive: 31-bits, range = 1 to +2,147,483,647

You can restrict the range of these types with the range add on, but you need to keep the original limits in mind when doing so (don't try to make a natural or a positive go past their original lower boundary):
signal my_integer : integer range -16 to 16;
signal my_natural : natural range 0 to 16;
signal my_positive : positive range 1 to 16;
Limiting the range of these types is a great idea when using generics or for ports to protect instantiations from driving the design out of it's expected behavior.

Synthesis tools may or may not have the ability to trim out un-used bits from a 32-bit integers depending on the design. It's generally good practice to limit the range of known signals to help the tools optimize the design properly.

Don't forget that you can set the initial value of the integer like this:
signal my_integer : integer range -16 to 16 := 2;
by (260 points)

VHDL has seen its rise when the predominant computer architecture was of 32 bits. Although the standard doesn't explicitly specify this - almost all FPGA design software defines the range of an integer as -2,147,483,647 to +2,147,483,647.

This is the default. So if one writes :

signal x : integer ; -- x supports values from -2,147,483,647 to +2,147,483,647.

There is however a way to limit the range of x inside the boundaries of the default.

Example :

signal date_of_month : integer range 1 to 31 ; -- If we'll try to drive the signal with a value not between 1 to 31 we'll get a compilation error.

date_of_month <= 46 ; -- This line will show as erroneous.