I have written the following code to turn any integer into a string which I manipulate later.
Ignore general coding mistakes. I retyped the code rather than copy/paste, so I might have missed some things. But the code compiles fine.
The code works if I remove the comment tag for the red line. That is, if I force num_tmp2 to be a certain value in the code, that value is returned as a string. However, when I try to feed in a number by calling the code from another vhdl file, the only string that ever gets output is "00000000". What am I doing wrong?
(If you are interested/if it matters, the overall project is to use a DE2 board to read in TTL pulses, count the number of pulses received and display them via the HEX displays on the DE2 board. I can read in the pulses and count how many there are and I can write a string to the HEX displays, but I can't turn the number of pulses received into a string in order to write it out the HEX displays. So this code is to fill in the missing part in the middle.)
Code:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY int2str IS
PORT (number: IN integer;
str : OUT string(1 to 8)
);
END int2str;
ARCHITECTURE Behavior of int2str IS
BEGIN
process(number)
variable subtractor, counter: integer := 0;
variable num_tmp2 : integer;
variable str_tmp : string (1 to 8);
variable str_one : string(1 to 1);
BEGIN
num_tmp2:=number;
[COLOR="Red"]-- num_tmp2: 8675309;[/COLOR]
for j in 8 DOWNTO 1 loop
subtractor := 10**(j-1);
counter :=-1;
poschk : for k in 0 to 9 loop
num_tmp2 := num_tmp2 - subtractor;
counter := k;
exit poschk when num_tmp2 < 0;
end loop poschkl
num_tmp2 := num_tmp2 + subtractor;
if (counter >-1) then
str_one := integer'image(counter);
str_tmp(9-j) := str_one(1);
else
str_tmp(9-j) := 'A';
end if;
end loop;
str <= str_tmp;
end process;
END Behavior;
Ignore general coding mistakes. I retyped the code rather than copy/paste, so I might have missed some things. But the code compiles fine.
The code works if I remove the comment tag for the red line. That is, if I force num_tmp2 to be a certain value in the code, that value is returned as a string. However, when I try to feed in a number by calling the code from another vhdl file, the only string that ever gets output is "00000000". What am I doing wrong?
(If you are interested/if it matters, the overall project is to use a DE2 board to read in TTL pulses, count the number of pulses received and display them via the HEX displays on the DE2 board. I can read in the pulses and count how many there are and I can write a string to the HEX displays, but I can't turn the number of pulses received into a string in order to write it out the HEX displays. So this code is to fill in the missing part in the middle.)