Learn how VHDL attributes make reusable, width-independent code and when to use 'range, 'length, 'high, 'low, and synthesis attributes.
An attribute reads a property of a type or object. For buses, it avoids repeating widths and bounds in calculations.
signal r_data : std_logic_vector(15 downto 0);
constant c_WIDTH : natural := r_data'length;
If r_data changes from 16 to 32 bits, c_WIDTH follows its new width.
Distinguish bounds from direction
Attribute
Meaning
'length
Number of elements
'range
Range in the same direction as the object
'reverse_range
Range in the opposite direction
'high
Highest index
'low
Lowest index
'left
Index written on the left side of the declaration
'right
Index written on the right side of the declaration
For this signal:
signal r_data : std_logic_vector(7 downto 0);
'length is 8, 'high is 7 and 'low is 0. 'range preserves 7 downto 0. 'left and 'right follow declaration order; they do not always equal 'high and 'low in that order.
Let code follow sizes
A fixed-size loop depends on the original declaration:
for i in 0 to 7 loop r_next(i) <= i_data(i);end loop;
Using the signal range follows its width and direction:
for i in i_data'range loop r_next(i) <= i_data(i);end loop;
Also ensure that r_next accepts the visited indexes. The attribute adapts the loop without automatically resizing its destination.