Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Declaring an unconstraind varaible inside a function

Status
Not open for further replies.

shaiko

Advanced Member level 5
Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Visit site
Activity points
18,302
In VHDL 2008
Is it possible to declare an unconsrained unsigned variable in a function?

For example:

Code:
function some_function ( x : unsigned ; y : unsigned ) return unsigned is
variable result : unsigned ; -- "result" is defined as an unconstrained unsigned.
begin
if x > y then
result := x ; -- "result" gets constrained according to the size of x  
else 
result := y ; -- "result" gets constrained according to the size of y  
end if ;
return result ;
end function some_function ;
 

I don't see that VHDL 2008 provides a means to "copy" constraints by a signal or variable assignment as assumed in your code. I don't think that the syntax will be accepted by VHDL 2008.

On the other side, there's a well established method to set the range of internal procedure variables by attributes, which is used e.g. throughout IEEE package functions. Why should we use "unconstrained" bit vectors in this place?
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Unconstrained objects other than ports or function parameters are illegal. They make no sense anyway.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
In VHDL 2008
Is it possible to declare an unconsrained unsigned variable in a function?
No.

For example:
Code:
function some_function ( x : unsigned ; y : unsigned ) return unsigned is
variable result : unsigned ; -- "result" is defined as an unconstrained unsigned.  [COLOR="#FF0000"]KJ:  If left as unconstrained, then how would you expect it to eventually get the size defined?[/COLOR]
begin
if x > y then
result := x ; -- "result" gets constrained according to the size of x  [COLOR="#FF0000"]KJ:  No it does not.  What happens is that VHDL checks to see that 'result' and 'x' are both of the same type and the same size.  If they are not compatible an error is declared when compiling.[/COLOR]
else 
result := y ; -- "result" gets constrained according to the size of y  [COLOR="#FF0000"]KJ:  Ditto[/COLOR]
end if ;
return result ;
end function some_function ;

There are a couple of solutions: Since the function above is so simple, you could write it without any intermediate variables like this:

Code:
function some_function ( x : unsigned ; y : unsigned ) return unsigned is
begin
if x > y then
    return x ;
else 
    return y ;
end if ;
end function some_function ;

More generally, if you did need to use intermediate variables and the size of those variables is different depending on the processing logic, you could use a function to compute the vector range. Imagine that you have a function called 'sel' like this...
Code:
function sel(Cond: Boolean; If_True: integer; If_False: integer) return integer is
begin
   if Cond then
      return If_True;
   else
      return If_False;
   end if;
end function sel;

Then in your original code you could have the following to define the bits of the variable x

Code:
function some_function ( x : unsigned ; y : unsigned ) return unsigned is
variable result : unsigned(sel(x>y, x'high, y'high) downto sel(x>y, x'low, y'low));

Kevin Jennings
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top