I have an entity that has a generic integer parameter fs_in_khz that can be 5, 10 or 2:
entity test_control_source is
generic(
-- This should only be 5, 10 or 20
fs_in_khz : integer := 20
);
It would be nice if I could take advantage of VHDL’s features and simply restrict the type to those values, perhaps using something like:
type control_source_freq is (F5_KHZ, F10_KHZ, F20_KHZ);
...
entity test_control_source is
generic(
-- This should only be 5, 10 or 20
fs_in_khz : control_source_freq := F20_KHZ
);
However, later on in the architecture of this entity, I have
architecture source_behaviour of test_control_source is
constant cs_period : integer := 5000 * clock_rate / fs_in_khz;
begin
...
I prefer having this parameter computed outside of the processes that use it, rather repeating the computation everywhere it’s needed. Can I restrict the allowed values of my fs_in_khz generic parameter and keep my constant cs_period factored out of the processes where it’s used?
Create your enumerated type in a package, as you suggest
Also create a function called
calculate_periodwhich takes inclock_rateand yourfstype and returns the appropriate integer.Alternatively, add some
assertstatements to the component using that generic and verify that it is of the correct values. Test this with your synthesis tool to see what happens if the asserts trigger though – often, even if you useseverity failureit’ll just give a warning!