diff --git a/libraries/base/common/src/vhdl/common_pkg.vhd b/libraries/base/common/src/vhdl/common_pkg.vhd index 17bf94944d10e3fdea42e92135fdc73afd07ef0b..fb94e8a8be10c9e13cf2670bee6c774e8ea87d35 100644 --- a/libraries/base/common/src/vhdl/common_pkg.vhd +++ b/libraries/base/common/src/vhdl/common_pkg.vhd @@ -139,6 +139,9 @@ PACKAGE common_pkg IS TYPE t_natural_2arr_2 IS ARRAY (INTEGER RANGE <>) OF t_natural_arr(1 DOWNTO 0); + TYPE t_complex_integer_arr2 IS ARRAY (NATURAL RANGE <>) OF t_integer_arr(1 DOWNTO 0); + TYPE t_complex_real_arr2 IS ARRAY (NATURAL RANGE <>) OF t_real_arr(1 DOWNTO 0); + -- STRUCTURE DECLARATIONS --------------------------------------------------- -- Clock and Reset @@ -201,6 +204,12 @@ PACKAGE common_pkg IS FUNCTION not_int(n: IN INTEGER) RETURN INTEGER; -- if 0 then return 1 else 0 + FUNCTION pack_complex(re, im : INTEGER; w : NATURAL) RETURN INTEGER; -- pack order: im & re + FUNCTION unpack_complex_re(data : INTEGER; w : NATURAL) RETURN INTEGER; -- pack order: im & re + FUNCTION unpack_complex_re(data : STD_LOGIC_VECTOR; w : NATURAL) RETURN INTEGER; -- pack order: im & re + FUNCTION unpack_complex_im(data : INTEGER; w : NATURAL) RETURN INTEGER; -- pack order: im & re + FUNCTION unpack_complex_im(data : STD_LOGIC_VECTOR; w : NATURAL) RETURN INTEGER; -- pack order: im & re + FUNCTION to_natural_arr(n : t_integer_arr; to_zero : BOOLEAN) RETURN t_natural_arr; -- if to_zero=TRUE then negative numbers are forced to zero, otherwise they will give a compile range error FUNCTION to_natural_arr(n : t_nat_natural_arr) RETURN t_natural_arr; FUNCTION to_integer_arr(n : t_natural_arr) RETURN t_integer_arr; @@ -711,6 +720,43 @@ PACKAGE BODY common_pkg IS RETURN sel_a_b(n = 0, 1, 0); END; + FUNCTION pack_complex(re, im : INTEGER; w : NATURAL) RETURN INTEGER IS + CONSTANT c_complex_w : NATURAL := 2 * w; + CONSTANT c_complex_slv : STD_LOGIC_VECTOR(c_complex_w-1 DOWNTO 0) := TO_SVEC(im, w) & TO_SVEC(re, w); + BEGIN + ASSERT c_complex_w <= c_word_w REPORT "common_pkg: Complex value to large to pack into 32 bit integer" SEVERITY FAILURE; + IF c_complex_w < c_word_w THEN -- fits in 31 bit unsigned NATURAL + RETURN TO_UINT(c_complex_slv); + ELSE -- need to use 32 bit signed INTEGER + RETURN TO_SINT(c_complex_slv); + END IF; + END; + + FUNCTION unpack_complex_re(data : STD_LOGIC_VECTOR; w : NATURAL) RETURN INTEGER IS + BEGIN + RETURN TO_SINT(data(w-1 DOWNTO 0)); -- Re in LS part + END; + + FUNCTION unpack_complex_re(data : INTEGER; w : NATURAL) RETURN INTEGER IS + CONSTANT c_complex_w : NATURAL := 2 * w; + CONSTANT c_complex_slv : STD_LOGIC_VECTOR(c_complex_w-1 DOWNTO 0) := TO_SVEC(data, c_complex_w); + BEGIN + RETURN TO_SINT(c_complex_slv(w-1 DOWNTO 0)); -- Re in LS part + END; + + FUNCTION unpack_complex_im(data : STD_LOGIC_VECTOR; w : NATURAL) RETURN INTEGER IS + BEGIN + RETURN TO_SINT(data(2*w-1 DOWNTO w)); -- Im in MS part + END; + + FUNCTION unpack_complex_im(data : INTEGER; w : NATURAL) RETURN INTEGER IS + CONSTANT c_complex_w : NATURAL := 2 * w; + CONSTANT c_complex_slv : STD_LOGIC_VECTOR(c_complex_w-1 DOWNTO 0) := TO_SVEC(data, c_complex_w); + BEGIN + RETURN TO_SINT(c_complex_slv(c_complex_w-1 DOWNTO w)); -- Im in MS part + END; + + FUNCTION to_natural_arr(n : t_integer_arr; to_zero : BOOLEAN) RETURN t_natural_arr IS VARIABLE vN : t_integer_arr(n'LENGTH-1 DOWNTO 0); VARIABLE vR : t_natural_arr(n'LENGTH-1 DOWNTO 0);