Deferred constant in package

H

hssig

Hi,

I am defining a function and a constant in the following manner:


package pkg_test is
function f_calc( inp: integer) return integer;
constant cVAL : integer;
end package pkg_test;


package body pkg_test is
function f_calc( inp: integer) return integer is
begin
...
end function f_calc;

constant cVAL : integer := f_calc(16);

type type_sig is array (natural range <>) of std_logic_vector(cVAL
downto 0);
end package body pkg_test;

The conflict: If I want to make "type_sig" visible for my component,
the type has to be defined in the package and not in the package
body . How can I find a remedy ?

Thank you, cheers
hssig
 
M

Mike Treseler

hssig said:
The conflict: If I want to make "type_sig" visible for my component,
the type has to be defined in the package and not in the package
body .

Make that *declared* in the package and assigned in the body.

How can I find a remedy ?

What's the problem?

That's how it works for deferred constants.

Maybe you want to declare your constant
in the process where it is used.

-- Mike
 
K

Kenn Heinrich

Mike Treseler said:
Make that *declared* in the package and assigned in the body.

How can I find a remedy ?

What's the problem?

That's how it works for deferred constants.

Maybe you want to declare your constant
in the process where it is used.

-- Mike

I think the original problem was that the package should declare the
type (so it can be used un may places, presumably), which requires the
elaboration of the function to compute the width. But you can't put a
function body definition inside a package declaration. There's the
problem.

One possible workaround would be to put the function (alone) into an
auxiliary package, and then use it to make the type public in a
body-less (disembodied???) package:


package aux...
function f(integer) return integer...
end package aux;

package body aux
function f(integer) return integer blah blah...
end package body;

use work.aux.all;
package p
constant cVal = f(16); -- imported f
type v is array... (cVal-1 downto 0);
end package;


- Kenn
 
K

KJ

Hi,

I am defining a function and a constant in the following manner:

package pkg_test is
function f_calc( inp: integer) return integer;
constant cVAL : integer;
end package pkg_test;

package body pkg_test is
function f_calc( inp: integer) return integer is
begin
    ...
end function f_calc;

constant cVAL : integer := f_calc(16);

type type_sig is array (natural range <>) of std_logic_vector(cVAL
downto 0);
end package body pkg_test;

The conflict: If I want to make "type_sig" visible for my component,
the type has to be defined in the package and not in the package
body . How can I find a remedy ?

Rather than an array of std_logic_vectors of some non-constant size,
you could define type_sig as a two dimensional array of std_logic
unconstrained in either dimension.

After you do that though you wont be able to assign an entire "cVAL
downto 0" vector element of your data type (or retrieve an entire
"cVAL downto 0" either).

So what you'll want to do is then some functions to put into your
package like the following:
- A function that given the 2d array input and an index, return an
array of std_logic_vectors from the 'index' column of the 2d array.
- A function (or procedure) that given the 2d array, an array of
std_logic_vectors and an index, copies the array of vectors into the
indexed column of the 2d array.

These functions would be defined to work with unconstrained vectors
and arrays because it can determine the limits within the function
itself.

It's a bit clunky but does work. The alternative is to work directly
with the 2d array. Depending on what you're doing, this might be
better, but in many cases it's not and the helper functions/procedures
are the better way to go.

Kevin Jennings
 
H

hssig

Hi Kenn, Kevin,

thank you for your explanations. I like Kenn's proposal to source the
function(s) out to a second package, Kevin's idea seems to be a good
alternative which I have to think about.

cheers,
hssig
 
M

Mike Treseler

Kenn said:
Irrelevant. That's just what the OP was doing originally. But it does
not show how to make use of either cVal or f_calc inside the package
declaration, which is what the OP specifically asked for in order to
export the constrained type "type_sig" from the package.

My point is that while using two packages works fine,
I can do the same thing using a single package and body.
Deferred functional constants belong in the body.
It is a fact that they are not allowed in the package,
but that does not stop me from using one from the package body.

-- Mike Treseler
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top