S
stephen henry
Guest
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
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