Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   VHDL (http://www.velocityreviews.com/forums/f18-vhdl.html)
-   -   Reverse function - unconstrained types (http://www.velocityreviews.com/forums/t678780-reverse-function-unconstrained-types.html)

ALuPin@web.de 04-06-2009 01:45 PM

Reverse function - unconstrained types
 
Hi,

one possible solution to reverse an unconstrained std_logic_vector is
to use
the following function:

function reverse(p: std_logic_vector) return std_logic_vector is
variable result: std_logic_vector(p'reverse_range);
begin
for i in p'range loop
result(i) := p(i);
end loop;
return result;
end;


Now I have the following type and signal declaration:

subtype stype_data is std_logic_vector(7 downto 0);
type type_array is array (natural range <>) of stype_data;

signal ls_test_data : type_array(3 downto 0);

How would a function have to look like to be capable of
reverse the order of the array elements ? Is it legal to use
the following unconstrained function to achieve that reversing ?

function reverse2(p: type_array) return type_array is
variable result: type_array(p'reverse_range);
begin
for i in p'range loop
result(i) := p(i);
end loop;
return result;
end;

Thank you for your opinion.

Rgds,
ALuPin

kennheinrich@sympatico.ca 04-06-2009 02:22 PM

Re: Reverse function - unconstrained types
 
On Apr 6, 9:45*am, "ALu...@web.de" <ALu...@web.de> wrote:
> Hi,
>
> one possible solution to reverse an unconstrained std_logic_vector is
> to use
> the following function:
>
> function reverse(p: std_logic_vector) return std_logic_vector is
> *variable result: std_logic_vector(p'reverse_range);
> *begin
> * * for i in p'range loop
> * * * result(i) := p(i);
> * * end loop;
> * * return result;
> end;
>
> Now I have the following type and signal declaration:
>
> subtype stype_data *is std_logic_vector(7 downto 0);
> type type_array is array (natural range <>) of stype_data;
>
> signal ls_test_data : type_array(3 downto 0);
>
> How would a function have to look like to be capable of
> reverse the order of the array elements ? Is it legal to use
> the following unconstrained function to achieve that reversing ?
>
> function reverse2(p: type_array) return type_array is
> *variable result: type_array(p'reverse_range);
> *begin
> * * for i in p'range loop
> * * * result(i) := p(i);
> * * end loop;
> * * return result;
> end;
>
> Thank you for your opinion.
>
> Rgds,
> ALuPin


Your std_logic_vector is itself an unconstrained array. Your two
questions are therefore equivalent :-)

As a note on "style" (purely a personal thing, nothing to do with
correctness), your functions could be confusing to some, as they rely
on a logical remapping of the vector indices (through the use of
reverse_range) coupled with the language-defined assignment (when you
actually call the function) that maps left-to-left and right-to-right
indices, thereby undoing the remap. This clever "double negative"
could be made explicit (and also avoid any bizarre user surprises,
such as when passing the result to a function that checks the index
and range of its argument, such as ieee_fixed package functions). I'd
suggest that the following would make the output signature match the
input signature.

function reverse(p: std_logic_vector) return std_logic_vector is
variable result: std_logic_vector(p'reverse_range);
variable equivalent_result: std_logic_vector(p'range);
begin
for i in p'range loop
result(i) := p(i);
end loop;
equivalent_result := result;
return equivalent_result;
end;


- Kenn

ALuPin@web.de 04-06-2009 02:45 PM

Re: Reverse function - unconstrained types
 
Hi Kenn,

thank you for your answer.

Yes, the point concerning matching between input and output signature
is an important one!

Rgds,
ALuPin


All times are GMT. The time now is 09:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.