I am trying to write a generic function in vhdl for converting from vector to array.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
package test is
type slv_arr is array (natural range <>) of std_logic_vector;
type unsigned_arr is array (natural range <>) of unsigned;
type signed_arr is array (natural range <>) of signed;
end package test;
package body test is
function f_to_arr_g
-- generic (type arg_type; type ret_type)
-- parameter (arg : arg_type; element_size : positive) return ret_type is
generic (type ret_type)
parameter (arg : std_logic_vector; element_size : positive) return ret_type is
-- Function Declaration Region
constant c_ARG_LEN : positive := arg'length;
constant c_ARRAY_LEN : positive := c_ARG_LEN / element_size;
variable v_return : ret_type(c_ARRAY_LEN-1 downto 0)(element_size-1 downto 0);
begin -- start of function
---- Splitting
for i in 0 to c_ARRAY_LEN-1 loop
v_return(i) := arg(((i+1)*element_size)-1 downto i*element_size);
end loop;
----
return v_return;
end function f_to_arr_g;
function f_to_arr is new f_to_arr_g generic map (slv_arr);
end package body test;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.test.all;
entity f_to_arr_tb is
end entity f_to_arr_tb;
architecture tb of f_to_arr_tb is
constant c_slv : std_logic_vector(31 downto 0) := x"AABBCCDD";
constant c_expected : slv_arr(3 downto 0)(7 downto 0) := (x"AA", x"BB", x"CC", x"DD");
signal s_result : slv_arr(3 downto 0)(7 downto 0);
begin
process
begin
s_result <= f_to_arr(c_slv);
assert s_result = c_expected
report "Test failed"
severity error;
wait;
end process;
end architecture tb;
the problem is that the compiler does not accepting the "length" attribute and issuing this error
" prefix of attribute length should be a discrete or physical type or subtype"
Is there a way to do this or I should overload the function for each type?