VHDL use dependent generic type in port specification

Viewed 532

I would like to to have a package that contains types that depend on generics so that I can use them on a project. However I am having trouble using those dependent types in port declarations:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


package dependent_types is
    generic (generic_value : natural);

    subtype dependent_type is std_ulogic_vector (generic_value - 1 downto 0);
end package;


use work.all;

entity dependent_entity_with_dependent_port is
    generic (generic_value : natural);

    package instantiated_types is new dependent_types
        generic map (generic_value => generic_value);
    use instantiated_types.all;

    port (dependent_port : out dependent_type);
end dependent_entity_with_dependent_port;

It seems that entity declarative parts (package instantiation, use clause) must come after the port declaration. This prevents using types defined in generic packages that depend on the entity's generics.

Is there any way to achieve this?

1 Answers

I don't think it is possible to do what you want - set the package generic equal to an ordinary generic and then use that as a port. However, you could use a deferred constant in a package like this:

This code is compiled once and is stable:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package dependent_types is
    generic (generic_value : natural);
    subtype dependent_type is std_ulogic_vector (generic_value - 1 downto 0);
end package;

package constants is
  constant generic_value : natural;
end package;

This code then needs to be recompiled every time the constant changes:

package body constants is
  constant generic_value : natural := 16;
end package body ;

use work.all;
use work.constants.all;
package instantiated_types is new dependent_types
    generic map (generic_value => generic_value);

use work.instantiated_types.all;
entity dependent_entity_with_dependent_port is
    port (dependent_port : out dependent_type);
end dependent_entity_with_dependent_port;

architecture A of dependent_entity_with_dependent_port is
begin
end;

https://www.edaplayground.com/x/3vfd

Related