0 votes
in VHDL by (240 points)

What is VHDL type, and how do I use them?

3 Answers

0 votes
by (1.8k points)

VHDL Types

Quick Syntax

Here's the most common VHDL types that you will come across:
library ieee;
use ieee.std_logic_1164.all;

constant CONSTANT_REAL : real := 2.0;

signal input_boolean : boolean;
signal input_sl : std_logic;
signal input_slv : std_logic_vector(15 downto 0);
signal count_integer : integer;
signal count_natural : natural;
signal count_positive : positive;


VHDL is a strictly typed language. That means, that you have to be very precise in dealing with type and how those types interact. While it seems "harder" to learn, it also means that you remove the majority of your bugs with a successful compile at synthesis.

Of course, you will encounter other bugs due to wrong assumptions, flawed logic, bad coding practices, clocking, timing, etc. But at least it won't be because your signals aren't compatible with each other.

You get quite a few predefined data types right out of the box. Every design file typically adds the IEEE library std_logic_1164, which also gives you types std_logic and std_logic_vector.

Keep in mind that there are other types as well, which are less commonly used, such as: bit, bit_vector, char, etc. You can read up on how these are a little different if you want. Just keep in mind that they are far less common.

With the types given above, you can do around 99.9% of designs.

As far as range of these types, check out the list below:

real: 128-bits, range = -1.0E38 to +1.0E38
boolean: True or False
std_logic: single bit '1' or '0', including other states
std_logic_vector(N downto 0): multiple N+1 bits at '1' or '0', including other states
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

By using the ieee.numeric_std library, you can also get access to the signed and unsigned type, as well as do math arithmetic operations.

If you want to define your own type, you can do it like this:

-- define state machine states and enumerate them
type my_states is (idle, start, done);
signal sm_state : my_states;

-- define array of std_logic_vector
type type_array_slv is array (0 to 31) of std_logic_vector(7 downto 0);
signal output_bus : type_array_slv;

Best Practices

1. Try to stick to the predefined types and also std_logic and std_logic_vector that you get with the ieee.std_logic_1164 library. There are many other libraries that offer different types. However, watch out for library conflicts if you use too many.

2. You can define your own custom type, which is pretty common. You can use this to define arrays of predefined types easily. It's also a great way to define states in a state machine.
0 votes
by (500 points)

In VHDL, objects such as input, signal, constant, variable, etc... must have a type. The language introduces predefined standard types.

Example :

signal some_signal : integer ; -- integer is the type of some_signal.
constant some_constant : std_logic := '0' ; -- std_logic is the type of some_constant.
The language also supports user defined custom types. Example :
type strange_user_defined_type is
good_morning ,
nice_car ,
tasty_pizza ,
) ;

signal x : strange_user_defined_type ;
0 votes
by (500 points)

This is a classification of VHDL objects that defines the set of values, properties, attributes and behavior that these objects can take in accordance with the type to which these objects belong.

I use types in object declarations: signals, variables, constants, user types and subtypes, ports and generics, function return type. I do it to define a link between processed data (in its algorithmical and/or real sense) and its VHDL representation.

The common cases:

  signal sig0 : <type>;
variable var0 : <type>;
constant const0 : <type>;
input0 : in <type>;

function func0 (param : in <type>) return <type> is ...
subtype stype0 is <subtype_indication> (for example std_logic_vector(7 downto 0), or integer 1 to 3);
type my_array is array (integer range <>) of stype0;

type my_record is record
field0 : integer;
field1 : stype0;
field2 : my_array;
end record;
And so on.
Hardware Coder Community

© 2022 by Hardware Coder. User contributions are licensed under cc by-sa 4.0 with attribution required. Attribution means a link to the question, answer, user, etc on this site.

This site is owned and operated by Hardware Coder in McKinney, Texas.

Send Us A Message
About Us

By using this site, you agree to the following:

Privacy Policy
Terms and Conditions
DMCA Policy
Earnings Disclaimer
Legal Disclaimer