0 votes
in VHDL by (240 points)

How do you convert different types in VHDL? What are some examples?

2 Answers

0 votes
by (300 points)

VHDL type conversion is called casting. It's done by writing the type we want to cast to followed by the object we want to cast in parentheses.

Example :

signal a : std_logic_vector ( 7 downto 0 ) ;
signal b : signed ( 7 downto 0 ) ;
signal c : unsigned ( 7 downto 0 ) ;

signal unsigned_result : unsigned ( 7 downto 0 ) ;
signal signed_result : signed ( 7 downto 0 ) ;

unsigned_result <= unsigned ( a ) + unsigned ( b ) + c ; -- a and b require casting signed_result <= signed ( a ) + b + signed ( c ) ; -- a and c require casting
Note : Not all types are castable from one to another.
0 votes
by (300 points)

The type of an object defines the kind of values it can have and the operations that can be done on it. Type conversion is an important concept to learn because usually when two objects are used together, they need to be of the exact same type.

The syntax for type conversion is as follows:


Type is not a keyword, it is a generic word used for the purpose of explaining the syntax. In real code, the word type is replaced by the keyword of the type to which the expression needs to be converted. For example:

y_val := Integer(90.4 * real(x_val));

In the above code, suppose that intially both variables, x_val, and y_val are of integer type. Now look at the code carefully and it can be seen that two type conversions are being done. Firstly, real(x_val) means that x_val is converted to a real number so that it can be multiplied by 90.4.

The result of multiplication, the real number is again converted to integer type so that it can be assigned to integer type variable y_val. This is done by: Integer(90.4 * real(x_val)).

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