Conditional Assignments in Constants

K

kevin.neilson

Did VHDL-2008 ever add a way to do this?

constant k : integer := 5 when BOOLEAN_GENERIC else 8;

I.e, to do the Verilog ?: operator? Having to write a function to make this work is awkward and verbose.
 
K

KJ

Did VHDL-2008 ever add a way to do this?
constant k : integer := 5 when BOOLEAN_GENERIC else 8;
I.e, to do the Verilog ?: operator? Having to write a function to
make this work is awkward and verbose.

The awkward and verbose function is at the end of the post. However, once you've taken the trouble to write (or copy/paste) this code, you should putit into a package of 'useful, general purpose functions that you can then use whenever you need them' and you can use those functions without ever having to look at that code again.

Effectively, you extended the language in your own preferred direction and there is nothing negative about that. You won't even need to wonder if some version of the language now supports what you want or, just as importantly, wonder if the tool you're using supports that new construct. Plus, whatif down the road, you'd like to have a function that works with std_logic conditions (or input parameters), you can simply copy/paste the code, modify two lines and be ready to go thanks to VHDL's function overloading. The ability to basically customize the language is a 'good thing', waiting for the standards folks and the tool supplier folks to get onboard...not so mucah a 'good thing'.

Using the code that you'll write once and then (probably) never have to look at again is as simple as using any std_logic or numeric_std or other library function that you already take for granted. Now you'll have the start of your own library of functions that you can also take for granted.

library work.my_pkg_of_vhdl_stuff.all;
....
constant k: integer := sel(BOOLEAN_GENERIC, 5, 8);
or ...
constant k: integer := sel(Cond => BOOLEAN_GENERIC, If_True => 5, If_False => 8);

Kevin Jennings

---*** START CODE ***---
----------------------------------------------------------------
-- Functions to select one or the other based on a boolean (or
-- std_ulogic or std_logic) input.
-- Analogous to the C statement x = Cond ? a : b
----------------------------------------------------------------
function sel(Cond: BOOLEAN; If_True, If_False: integer) return integer is
begin
if (Cond = TRUE) then
return(If_True);
else
return(If_False);
end if;
end function sel;
---*** END CODE ***---
 
A

Andy

VHDL-2008 allows conditional and selected assignments to variables or signals in sequential statements, but not in the initialization of a declaration.

It would be nice to be able to use a conditional assignment in an initialization.

I'm assuming that this boolean generic controls several things, otherwise you could just pass in the value of K as a generic instead.

Try the following, slightly less "awkward and verbose" method:

type switch_t is array (boolean) of integer;
constant switch : switch_t := (true => 5, false => 8);
constant k : integer := switch(boolean_generic);

Andy
 
K

kevin.neilson

That's a good function. I can use that. I would prefer it, though, if very basic things were built into the language. If I'm reading somebody else's code I shouldn't have to find that package that 'sel' is defined in just because the language doesn't have a ?: built in. From what I can tell, I can't even convert a Boolean into an integer unless I write my own function.
 
K

kevin.neilson

That's a good one too. It's nice to have something simple that's close to where I'm using it which may be clearer than if it's off in a package somewhere.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top