Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

Generate in vhdl declaration

Hi,

I have a single vhdl package that I would like to use for two projects. The constants in the two projects have the same name but different values. Can I use generate statement in the declaration to generate different values for the constants.

e.g

project1: if proj=xx generate

constant ty : positive := 6;

constant tz : positive := 7;

end generate project1;

project2: if proj=yy generate

constant ty : positive := 10;

constant tz : positive := 17;

end generate project2;

These must go to the declaration part of the package.

Thanks,

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    you cannot use generate inside a package. And you also cannot set a constant inside a generate if it is declared outside the generate.

    But you can use a function:

    
    function set_ty return positive is
    begin
      if proj = XX then
        return 6;
      elsif proj = YY then
         return 7;
       ..... etc
    end function set_TY;
    constant TY : positive := set_TY;
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    What you can do is use a separate package VHDL file for each project, but use the same package name.

    This is how I do it when I have FPGA designs that just use different configuration settings but are based on the same VHDL code.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    Hi,

    I have a single vhdl package that I would like to use for two projects. The constants in the two projects have the same name but different values. Can I use generate statement in the declaration to generate different values for the constants.

    e.g

    project1: if proj=xx generate

    constant ty : positive := 6;

    constant tz : positive := 7;

    end generate project1;

    project2: if proj=yy generate

    constant ty : positive := 10;

    constant tz : positive := 17;

    end generate project2;

    These must go to the declaration part of the package.

    Thanks,

    --- Quote End ---

    In addition to the earlier suggestion of using seperate files here is another method...

    type t_LIST_OF_PROJECTS is (xx, yy);
    type t_MY_CONSTANTS is record
       ty : positive;
       tz : positive;
    end record t_MY_CONSTANTS;
    type arr_t_MY_CONSTANTS is array(t_LIST_OF_PROJECTS) of t_MY_CONSTANTS;
    constant The_Constants: arr_t_MY_CONSTANTS :=
    (
       xx =>
       (
         ty => 6,
         tz => 7
    ),
       yy =>
       (
          ty => 10,
          tz => 17
       )
    );
    

    Now when you go to use these constants, rather than this...

    xyz <= ty * tz;

    You would do this...

    xyz <= The_Constants(Project).ty * The_Constants(Project).tz;

    Where 'Project' is of type t_LIST_OF_PROJECTS which is defined in the projects. Presumably there would be a generic at the top level that sets the value of a generic called 'Project' to a particular value so that it can then be used in the above mentioned calculation for 'xyz'. Depending on how many places you are currently referencing the 'ty' and 'tz' constants, it might be better to structure things like this...

    entity Almost_Top generic(ty : positive; tz : positive; port (...);

    In the top level you would then instantiate 'Almost_Top' like this...

    U1 : entity work.Almost_Top generic map(ty => The_Constants(Project).ty, tz => The_Constants(Project).tz); port (...);

    That way, inside the 'Almost_Top' where you currently use 'ty' and 'tz' you wouldn't have to change anything since 'ty' and 'tz' would be coming in as constants in the form of a generic for the entity. The only place you have to refer to the 'messier' form would be at the top level.

    The basic approach here is that the set of constants are packaged into an array of records. Which set is used is selected by the value of a constant that is uniquely identified for each project.

    The advantage here is from a source file control viewpoint. You can have a single package in a single file that defines all of the constants. You do not have to rely on swapping in or out files on a project basis. Which method is 'best' though is still a judgment call based on the various project requirements.

    Kevin Jennings