How to convert integer to unsigned in VHDL

I am trying to split two integers as follows:

variable m0Low : integer := 0; variable m1Low : integer := 0; m1Low := divide(m1Low,m0Low); 

Using the function:

 function divide (a : UNSIGNED; b : UNSIGNED) return UNSIGNED is variable a1 : unsigned(a'length-1 downto 0):=a; variable b1 : unsigned(b'length-1 downto 0):=b; variable p1 : unsigned(b'length downto 0):= (others => '0'); variable i : integer:=0; begin for i in 0 to b'length-1 loop p1(b'length-1 downto 1) := p1(b'length-2 downto 0); p1(0) := a1(a'length-1); a1(a'length-1 downto 1) := a1(a'length-2 downto 0); p1 := p1-b1; if(p1(b'length-1) ='1') then a1(0) :='0'; p1 := p1+b1; else a1(0) :='1'; end if; end loop; return a1; end divide; 

However, I get the following error: Divide can not have such operands in this context.

I am trying to pass variables to unsigned m1Low := divide(unsigned(m1Low),unsigned(m0Low));

But I get the following error: The expression can not be converted to type unsigned.

Any idea what I can do? thanks Haris

+4
source share
2 answers

To convert an integer to an unsigned or signed data type,

 use IEEE.NUMERIC_STD.all; 

you should use

 to_unsigned(I,U'length); to_signed(I,S'length) 

where i is the integer value and length U is the unsigned length (number of bits).

I have not tested your code and how it works, but my correction on your code is simple,

 m1Low := to_integer(divide(to_unsigned(m1Low, N),to_unsigned(m0Low, N))); 

you must indicate N where the length of your vector depends on your design. I used to_integer () because your function returns an unsigned value for an integer variable.

Hope these simple notes help you.

+6
source

If you want to pass integers as unsigned vectors, you need to convert them, not their type.

First you need the numeric_std library:

 use ieee.numeric_std.all; 

You can then use to_unsigned to convert integers to unsigned vectors. For this function, you need to know the length of the unsigned vector you are converting, so use the 'length attribute:

 destination_vector := to_unsigned(source_integer, destination_vector'length); 

You can convert back from unsigned to integer (which does not need to specify the input length, since information about the input data is directly available to the function):

 destination_integer := to_integer(source_vector); 
+4
source

Source: https://habr.com/ru/post/1481251/


All Articles