Reverse function - unconstrained types

A

ALuPin

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
 
K

kennheinrich

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
 
A

ALuPin

Hi Kenn,

thank you for your answer.

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

Rgds,
ALuPin
 

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

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top