Integer to slv

S

stephen henry

Hi all,

I'm having difficulty figuring out how to fix an integer to
std_logic_vector function that I threw together:

function integer_to_slv (
invect, bits : integer)
return std_logic_vector is


variable n : std_logic_vector(bits-1 downto 0) := (others => '0');
variable temp : integer := invect;
begin

for i in n'reverse_range loop
if temp mod 2 = 1 then
n(i) := '1';
end if;
temp := temp / 2;
end loop;

return n;

end function integer_to_slv;

The function works okay; however, when it comes to converting negative
integers to std_logic_vector, the sign bit is lost (the result always
being positive). It seems that in the final iteration of the loop, the
temp variable corresponds to 0000....0000(sign bit), this however
doesn't work with the mod function so the result is always negative.
How could I fix this?

Thanks for any help

Steve
 
P

Parikshit Kumar

any particular reason you don't want to use already available functions in
numeric_std package

std_logic_vector(to_signed(invect, bits) )
 
S

stephen henry

Parikshit Kumar said:
any particular reason you don't want to use already available functions in
numeric_std package

std_logic_vector(to_signed(invect, bits) )

Yes, I'm aware that function that do this already exist. The reason I
am not using them is to further my knowledge of VHDL, hence the reason
I not having too much success getting it to work! :)
 
R

Ralf Hildebrandt

stephen henry wrote:


I'm having difficulty figuring out how to fix an integer to
std_logic_vector function that I threw together:

Why don't you use the build-in-functions?

Library IEEE;
use IEEE.numeric.std.all;


some_slv<=std_logic_vector( to_unsigned(some_integer,bitwidth) );



Ralf
 
M

Mike Treseler

stephen said:
Yes, I'm aware that function that do this already exist. The reason I
am not using them is to further my knowledge of VHDL, hence the reason
I not having too much success getting it to work! :)

Have a look at the numeric_std source that comes with your simulator.

-- Mike Treseler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top