Thread: Array Types
View Single Post
  #2 (permalink)  
Old 10-27-2003, 03:39 PM
Jonathan Bromley
Guest
 
Posts: n/a
Default Re: Array Types

"Matt North" <m.r.w.north@NO_SPAMrl.ac.uk> wrote in
message news:[email protected]...

> I have a type which is generated in a package:
> <<type matrix is array(natural range<>) of bit_vector(7 downto 0);
> the functions in the package return constrained arrays of type matrix:
> <<return matrix is variable result: matrix(0 to length);
> When i then go to use the type matrix in my entity:
> <<variable rom:=matrix;
> I get a warning saying that rom cant be an unconstrained type, i

understand
> that matrix is unconstrained (array(natural range<>)) however the results
> returned by the function in the package constrain the array.
>
> Does anyone know of anyway of constraining the type matrix in the package
> depending on the size of the array that each individual function returns?
> In the finished code i would like to have a number of functions which all
> return different sized arrays (all of the same type), which can then be

used
> by the entity.


I suspect you're fighting VHDL here, rather than letting it help you.

First, the answer to your explicit question: Yes, but it's clumsy.
Variables must be given a constrained subtype (so that the compiler
knows how much memory to allocate for them, as a first cut at an
explanation). If each of your functions returns a specific
constrained subtype of "matrix" then you can use it thus:

function fun_matN (...) return matrix(N-1 downto 0);
...
-- Make a constant whose range matches fun_matN().
-- Function parameters can be any old junk, because
-- you are never going to use the value of its result.
constant const_matN: matrix := fun_matN(...);
-- Use that constant's range to create a variable:
variable var_matN: matrix(const_matN'range);

Now that you've done this, of course, you can with confidence
write procedural code thus:

var_matN := fun_matN(...);

BUT................
in my experience it's pretty rare for it to be useful to return
a variety of fixed-width results from a variety of functions.
I can't speak for your application of course, but generally
it's much more useful to have the function return a result whose
range is determined dynamically, based on the properties of its
parameters. For example, the "+" operator in numeric_std
returns a result whose width is the larger of its two operands'
widths. In this kind of situation, you can generally decide
in advance what ranges your various data items need.

If your function is genuinely returning a constant - for example,
some constant vector that needs calculation but is always the
same, such as a Fibonacci sequence or the values of
arctan(2^-n) needed for a CORDIC engine - then you can drop
its result into a VHDL constant. As my example above shows,
you don't need to specify the range of a constant; it's
determined from the constant's initialisation expression.

Finally, one last idea: Consider writing a "resize"
procedure that allows you to shoe-horn the result of
any "matrix" expression into an arbitrary "matrix"
variable. Of course, the correct behaviour of truncation
and extension depends on what you're trying to do - in
my example I fill from left to right, throwing away
unused right-hand elements of the source matrix, and
padding un-filled right-hand elements of the destination
with a dummy value.

procedure coerce (
dst: out matrix;
src: in matrix;
dummy: bit_vector(7 downot 0) := "00000000") is
-- Normalise array ranges to 1..N layout.
alias norm_src: matrix(1 to src'length) is src;
alias norm_dst: matrix(1 to dst'length) is dst;
begin
for i in norm_dst'range loop
if i > src'length then
norm_dst(i) := dummy;
else
norm_dst(i) := norm_src(i);
end if;
end loop;
end; -- procedure coerce

Give us a bit more detail about what you are trying to do,
and perhaps we can be more helpful for your specific needs.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * Perl * Tcl/Tk * Verification * Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail: [email protected]
Fax: +44 (0)1425 471573 Web: http://www.doulos.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.



Reply With Quote