0
0
VHDLprogramming~5 mins

Integer and natural types in VHDL

Choose your learning style9 modes available
Introduction

Integer and natural types let you work with whole numbers in VHDL. They help you count, measure, and control things in your digital designs.

When you need to count items or steps in a process.
When you want to represent a value that can be positive or negative, like temperature.
When you want to represent only zero or positive numbers, like the number of clock cycles.
When you want to set limits on values to avoid errors in your design.
Syntax
VHDL
variable_name : integer range low_value to high_value;
variable_name : natural range low_value to high_value;

integer can hold positive and negative whole numbers.

natural is a subtype of integer that only allows zero and positive numbers.

Examples
This signal can hold values from -10 to 10, including negative numbers.
VHDL
signal count : integer range -10 to 10;
This variable can only hold zero or positive numbers up to 100.
VHDL
variable steps : natural range 0 to 100;
This signal can hold any integer value within the default integer range.
VHDL
signal temperature : integer;
Sample Program

This VHDL program counts from 0 to 5 using an integer signal with a range. It reports the count value every 10 nanoseconds.

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity counter_example is
end counter_example;

architecture behavior of counter_example is
  signal count : integer range 0 to 5 := 0;
begin
  process
  begin
    for i in 0 to 5 loop
      count <= i;
      report "Count is " & integer'image(i);
      wait for 10 ns;
    end loop;
    wait;
  end process;
end behavior;
OutputSuccess
Important Notes

Using ranges helps catch errors if a value goes outside expected limits.

Natural type is useful when negative numbers don't make sense, like counts or sizes.

Summary

Integer type holds whole numbers, positive and negative.

Natural type holds only zero and positive whole numbers.

Ranges limit the values variables or signals can hold to keep designs safe.