1
\$\begingroup\$

In VHDL, is there any way to convert an integer into bit_vector of length of 14?

Currently, I am first converting the integer into std_logic_vector and then into bit_vector.

for i in 0 to 5 loop
    x_std(i) <= conv_std_logic_vector(x_int(i),14);
    x_bit(i) <= to_bitvector(x_std(i),14);   --here comes error
end loop;

In behavioral check syntax, following error comes while converting std_logic into bit:

Type bit does not match with the integer literal

I am using following libraries, ieee.std_logic_1164, std_logic_arith and std_logic_unsigned.

How can I do this conversion without an error?

\$\endgroup\$
1
\$\begingroup\$

To answer your specific question: the to_bitvector() function from std_logic_1164 only takes one input, a signal of type std_ulogic_vector or std_logic_vector. If you search for the function name you find documentation like this which gives the interface

function to_bitvector ( s : std_ulogic_vector ) return bit_vector;

So change your code to this and it should work (though I haven't tested):

for i in 0 to 5 loop
    x_std(i) <= conv_std_logic_vector(x_int(i),14);
    x_bit(i) <= to_bitvector(x_std(i));  --should compile
end loop;

As to why the functions take different inputs, the conv_std_logic_vector function requires a length parameter as the integer type has no specific length. The length parameter tells the synthesiser how wide a std_logic_vector is created by the function. VHDL is strongly typed and the length of the signal returned from the function is then checked against the left hand sign on the assignment (x_std(i)) to ensure they match.

Conversely, the to_bitvector function simply goes through each element of the array in turn performing a mapping from std_logic to bit and the returned array width is therefore defined by the width of the input array.

It's also worth looking into using VHDL attributes as the length input to conversion functions. This code won't break if you decide to change the width of x_std() and x_bit() later:

x_std(i) <= conv_std_logic_vector(x_int(i),x_std(i)'LENGTH);
\$\endgroup\$
  • \$\begingroup\$ Also many people have moved away from using the std_logic_arith package and its conversion functions in favour of ieee.numeric_std. I've found this presentation very helpful \$\endgroup\$ – Xcodo Jun 29 '15 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.