Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deferred constant in package

327 views
Skip to first unread message

hssig

unread,
Nov 30, 2009, 12:32:25 PM11/30/09
to
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

Mike Treseler

unread,
Nov 30, 2009, 12:55:25 PM11/30/09
to
hssig wrote:

> 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

Kenn Heinrich

unread,
Nov 30, 2009, 1:34:32 PM11/30/09
to
Mike Treseler <mtre...@gmail.com> writes:

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


--
---------------------------------

KJ

unread,
Dec 1, 2009, 12:20:23 AM12/1/09
to

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

hssig

unread,
Dec 1, 2009, 3:55:25 AM12/1/09
to
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

Mike Treseler

unread,
Dec 2, 2009, 8:06:40 PM12/2/09
to
Kenn Heinrich wrote:

> 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.

But I don't have to.
http://groups.google.com/group/comp.lang.vhdl/search?q=xyz_pkg+defer

-- Mike Treseler

Kenn Heinrich

unread,
Dec 2, 2009, 10:50:31 PM12/2/09
to
Mike Treseler <mtre...@gmail.com> writes:


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.

- Kenn

--

Mike Treseler

unread,
Dec 3, 2009, 12:55:01 PM12/3/09
to
Kenn Heinrich wrote:

> 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

0 new messages