Natural and Positive Types in VHDL: Explanation and Examples
natural is a predefined integer subtype that includes all whole numbers from 0 upwards, including zero. The positive type is similar but only includes integers greater than zero, excluding zero itself.How It Works
Think of natural and positive types as special buckets for numbers. The natural bucket holds all whole numbers starting from zero and going up (0, 1, 2, 3, ...). The positive bucket is almost the same but starts just after zero, so it holds numbers like 1, 2, 3, and so on.
This helps VHDL designers make sure that only valid numbers are used where negative numbers or zero might cause problems. For example, if you want to count items, zero or more makes sense, so you use natural. But if you want to represent something that must be at least one, like the number of bits in a bus, you use positive.
Example
This example shows how to declare variables using natural and positive types and assign values to them.
library ieee; use ieee.std_logic_1164.all; entity example_types is end example_types; architecture behavior of example_types is signal count : natural := 0; -- can be 0 or more signal size : positive := 1; -- must be greater than 0 begin process begin count <= 10; -- valid assignment size <= 5; -- valid assignment -- count <= -1; -- invalid: negative number not allowed -- size <= 0; -- invalid: zero not allowed wait; end process; end behavior;
When to Use
Use natural when you need to represent counts or indexes that can be zero or more, such as array indexes or loop counters. It ensures your values never go negative, which could cause errors.
Use positive when zero is not a valid value, like specifying the size of a memory block, number of bits in a signal, or any quantity that must be at least one. This prevents accidental zero or negative values that might break your design.
Key Points
- natural includes 0 and all positive integers.
- positive includes only positive integers greater than 0.
- Both help catch invalid values early in VHDL designs.
- They are subtypes of the integer type in VHDL.