I want to describe a binary to bcd converter with the follow code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity MAIN is
Port (
BIN : in STD_LOGIC_VECTOR(7 downto 0);
BCDu : out STD_LOGIC_VECTOR(3 downto 0);
BCDd : out STD_LOGIC_VECTOR(3 downto 0);
BCDc : out STD_LOGIC_VECTOR(3 downto 0)
);
end entity MAIN;
architecture Behavioral of MAIN is
begin
process(BIN)
variable aux : natural := to_integer(unsigned(BIN));
variable uni : natural := 0;
variable dec : natural := 0;
variable cent : natural := 0;
begin
if(aux < 256) then
while(aux > 99) loop
aux := aux - 100;
cent := cent + 1;
end loop;
while(aux > 9) loop
aux := aux - 10;
dec := dec + 1;
end loop;
end if;
uni := aux;
BCDu <= std_logic_vector(to_unsigned(uni, 4));
BCDd <= std_logic_vector(to_unsigned(dec, 4));
BCDc <= std_logic_vector(to_unsigned(cent, 4));
end process;
end architecture Behavioral;
But the follow error appears:
Loop has iterated 64 times. Use "set -loop_iteration_limit XX" to iterate more.
I don't know why that error appears, I have two while
s and together it has 7 iterations.
VHDL is a hardware description language, not a procedural language. While in simulation the steps of the loop are executed one by one, on hardware the loop is unrolled such that the whole entire thing is computed in parallel. In your case, the loops probably have to have gates dedicated for all possible values of x from 0 to 255, since the synthesis engine isn’t smart enough to realize that the loops only run up to 7 times. This could likely be solved by precomputing the loop iterations in another variable. A similar solution would be to use division and modulus to eliminate the loops entirely.
However, the best solution would be to use 7 chained single-step BCD conversion modules implemented without a process to minimize the number of gates used. In each of those modules, if the 4 bit input is at least 5, then 3 is added to the output, and it’s output is shifted by one bit such that it introduces one new bit of the original input before the next stage. So the first module operates on 3 bits to produce 4, and you only use a module when there are at least three bits past the previous module. 5 and 3 are the special numbers in this because after the next shift, 5 becomes 10 and 3 becomes the 6 extra needed to make 10 equal to 16, the base of 4-bit numbers (hexadecimal). Adding 3 carries the extra 10 over to the next digit, since a binary 16 becomes a 1 in the next digit.