Reverse function - unconstrained types

Discussion in 'VHDL' started by ALuPin@web.de, Apr 6, 2009.

  1. Guest

    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
     
    , Apr 6, 2009
    #1
    1. Advertising

  2. Guest

    On Apr 6, 9:45 am, "" <> 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
     
    , Apr 6, 2009
    #2
    1. Advertising

  3. Guest

    Hi Kenn,

    thank you for your answer.

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

    Rgds,
    ALuPin
     
    , Apr 6, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FE
    Replies:
    0
    Views:
    1,665
  2. Alex Rast
    Replies:
    3
    Views:
    5,238
    Jim Lewis
    Oct 6, 2003
  3. Victor Hannak
    Replies:
    1
    Views:
    548
    Mike Treseler
    Nov 25, 2003
  4. Amal
    Replies:
    5
    Views:
    8,769
    Brandon
    Mar 8, 2006
  5. dogbite
    Replies:
    4
    Views:
    702
    osmium
    Oct 10, 2003
Loading...

Share This Page