Change a constant value, depending on a generic

B

Barry

Is there a more clever and compact way to do this, rather than writing
a function? The constant INIT_40_VAL is assigned to a generic in a
component instantiation later on.
TIA


entity v5_sysmon is
generic ( SIM_ONLY : integer := 0 ); -- = 1 to set averaging to 1
instead of 16
......

architecture rtl of v5_sysmon is

function set_init_40(sim_only : in integer) return bit_vector is
constant init_40_avg1 : bit_vector := X"0000";
constant init_40_avg16 : bit_vector := X"1000";
begin
if sim_only = 1 then
return init_40_avg1;
else
return init_40_avg16;
end if;
end function set_init_40;

constant INIT_40_VAL : bit_vector := set_init_40(SIM_ONLY);
 
D

Dave

Is there a more clever and compact way to do this, rather than writing
a function?  The constant INIT_40_VAL is assigned to a generic in a
component instantiation later on.
TIA

entity v5_sysmon is
  generic ( SIM_ONLY : integer := 0 );   --  = 1 to set averaging to 1
instead of 16
.....

architecture rtl of v5_sysmon is

  function set_init_40(sim_only : in integer) return bit_vector is
    constant init_40_avg1  : bit_vector := X"0000";
    constant init_40_avg16 : bit_vector := X"1000";
  begin
    if sim_only = 1 then
      return init_40_avg1;
    else
      return init_40_avg16;
    end if;
  end function set_init_40;

  constant INIT_40_VAL : bit_vector := set_init_40(SIM_ONLY);

First, I wouldn't use types bit and bit_vector, but instead std_logic,
std_logic_vector, and signed and unsigned from ieee.numeric_std.

How about:

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

entity v5_sysmon is
generic ( SIM_ONLY : integer range 0 to 1 := 0 );
....
end entity;

architecture rtl of v5_sysmon is

constant INIT_40 : std_logic_vector(15 downto 0) := std_logic_vector
(to_unsigned(4096 * SIM_ONLY), 16);

-- rest of code

Dave
 
B

Barry

First, I wouldn't use types bit and bit_vector, but instead std_logic,
std_logic_vector, and signed and unsigned from ieee.numeric_std.

How about:

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

entity v5_sysmon is
   generic ( SIM_ONLY : integer range 0 to 1 := 0 );
...
end entity;

architecture rtl of v5_sysmon is

constant INIT_40 : std_logic_vector(15 downto 0) := std_logic_vector
(to_unsigned(4096 * SIM_ONLY), 16);

-- rest of code

Dave- Hide quoted text -

- Show quoted text -

Thanks, Dave. Type bit_vector is used to match the type of the
generic in the Xilinx unisim component "SYSMON" (not shown in my
snippet).
 
T

Tricky

Is there a more clever and compact way to do this, rather than writing
a function?  The constant INIT_40_VAL is assigned to a generic in a
component instantiation later on.
TIA

entity v5_sysmon is
  generic ( SIM_ONLY : integer := 0 );   --  = 1 to set averaging to 1
instead of 16
.....

architecture rtl of v5_sysmon is

  function set_init_40(sim_only : in integer) return bit_vector is
    constant init_40_avg1  : bit_vector := X"0000";
    constant init_40_avg16 : bit_vector := X"1000";
  begin
    if sim_only = 1 then
      return init_40_avg1;
    else
      return init_40_avg16;
    end if;
  end function set_init_40;

  constant INIT_40_VAL : bit_vector := set_init_40(SIM_ONLY);

This is the most compact form for anything more complicated than
arithmatic. What is good about this though is it allows you to put
assertions in the function that can check the validity of generics and
or combinations of generics so that in some synthesisers (not that
many actually) you can halt synthesis and simulation.
 
A

Andy

For some applications (maybe not yours, since the generic has 2^32
possible values), a constant array indexed by the generic would be
simpler than a function.

-- the array typedef
type init_40_choice_t is array (integer range <>) of bit_vector(15
downto 0);

-- the (really big) array:
constant init_40_choices : init_40_choice_t(integer'range)
:= (1 => X"0000",
others => X"1000");
-- index the array with the generic
constant init_40_val : bit_vector := init_40_choices(sim_only);

Hope this helps,

Andy
 
B

Barry

The first question is: why on earth would you use an integer where a
boolean is so clearly called for?
(the only reason I can think of is too much brain-rotting C :)

The generic SIM_ONLY originally came from the code produced by the
Xilinx MIG for my SDRAM controller. I am re-using it here for another
purpose, so that I only have a single "only for simulation" generic to
keep track of at the testbench level. I don't care to re-write all of
the MIG-produced code modules for a boolean generic. Otherwise, you
offer a nice solution.

The original solution with a function is fine; sometimes I learn
something really clever in the newsgroups that I had not thought of
before, so I just figured I would ask.

Barry
 
M

Mark McDougall

Barry said:
The generic SIM_ONLY originally came from the code produced by the
Xilinx MIG for my SDRAM controller. I am re-using it here for another
purpose, so that I only have a single "only for simulation" generic to
keep track of at the testbench level.

I use this...

constant in_simulation : BOOLEAN := false
-- synthesis translate_off
or true
-- synthesis translate_on
;
constant in_synthesis : boolean := not in_simulation;

....in a global package so that I don't have to worry about explicitly
setting such constants during simulation/synthesis.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top