Electrical – How does to reverse items in an array type

modelsimstringvhdl

I have a function that works with string passed to it. The string should be defined as "a to b" rather than "a downto b". I have the following questions:

  1. How to find out if the string passed is defined in ascending or descending order?
  2. Why do strings not have index 0? The minimum index is 1.
  3. I have string p is 1 to 10, while string q is 10 downto 1, assigning one to the other generates error:

    Fatal: (vsim-3607) Slice range direction (downto) does not match slice prefix direction (to).

    Why?

  4. How do I assign p to q or q to p in this case?

Best Answer

  1. There are a few attributes that can be used to determine the bounds of ranges :

    • T'right and T'left
    • T'low and T'high
    • T'ascending : boolean : indicates a TO=true or DOWNTO=false range.
  2. No idea why VHDL strings usually start at 1

  3. The problem is what means a string defined as (10 downto 1)? Is it some text in a right-to-left language as Arabic? It would make no sense to reverse the order of letters!

  4. If you want to inverse the order, you can use a loop :

    FOR i in p'range LOOP
         q(i):=p(i);
    END LOOP;
    

If it is just because the vector is badly defined, you can write

    q:=p;