library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.STD_LOGIC_arith.all; use IEEE.STD_LOGIC_unsigned.all; package function_pkg is function hex2bcd(x : in std_logic_vector;width : in integer) return std_logic_vector ; end package ; package body function_pkg is function hex2bcd(x : in std_logic_vector;width : in integer) return std_logic_vector is variable x_neu : integer ; variable x_alt : integer ; variable sum : integer; variable sum_neu : integer; begin x_alt := conv_integer(unsigned(x)); sum := 0; sum_neu := 0; for i in 0 to (width/4)-1 loop x_neu := x_alt; sum := (x_neu mod 10)*(16**i); sum_neu := sum_neu + sum; x_alt := x_neu / 10; end loop; return conv_std_logic_vector(sum_neu,width); end function hex2bcd; end package body function_pkg ; library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.STD_LOGIC_arith.all; use IEEE.STD_LOGIC_unsigned.all; use work.function_pkg.all; entity generic_hex2bcd is generic ( width : integer := 8 ); port ( hex_i : in std_logic_vector(width-1 downto 0); bcd_o : out std_logic_vector(width-1 downto 0) ); end entity generic_hex2bcd; architecture structure of generic_hex2bcd is begin bcd_o <= hex2bcd(hex_i,width); end structure;