For..loop with variable range

Discussion in 'VHDL' started by Dan, Nov 30, 2007.

  1. Dan

    Dan Guest

    Hi,

    I have this piece of code in my design:

    function xxxxxx (...) is

    [...]

    for j in i to length - 1 loop
    v(i) := '0';
    end loop;

    [...]

    end xxxxxxx;

    In this case 'i' is a variable, so I'm having problems syntesizing the
    desing because the for..loop range is variable, not constant.

    To make the context clear, the xxxxxx function converts a natural number to
    a binary vector by making successive divisions. When the quotient is zero,
    the for..loop fills the rest of the vector with 0's.

    Any suggestion?

    Thanks
     
    Dan, Nov 30, 2007
    #1
    1. Advertising

  2. Dan

    KJ Guest

    "Dan" <> wrote in message
    news:finlfc$6h0$-data.net...
    >
    > Hi,
    >
    > I have this piece of code in my design:
    >

    <snip>
    > To make the context clear, the xxxxxx function converts a natural number
    > to a binary vector by making successive divisions. When the quotient is
    > zero, the for..loop fills the rest of the vector with 0's.
    >
    > Any suggestion?
    >


    Yes, don't reinvent the wheel, use the numeric_std package. Usage snippets
    below.

    use ieee.numeric_std.all;
    .....
    signal x: std_logic_vector(...);
    signal y: natural range ...;
    ....
    x <= std_logic_vector(to_unsigned(y, x'length));

    KJ
     
    KJ, Nov 30, 2007
    #2
    1. Advertising

  3. On Fri, 30 Nov 2007 01:28:31 +0100, "Dan" <> wrote:

    >
    >Hi,
    >
    >I have this piece of code in my design:
    >
    >function xxxxxx (...) is
    > for j in i to length - 1 loop
    > v(i) := '0';
    > end loop;
    >end xxxxxxx;
    >
    >In this case 'i' is a variable, so I'm having problems syntesizing the
    >desing because the for..loop range is variable, not constant.


    You have to translate the algorithm into a synthesisable form; and that
    means locally constant loop indices

    > for j in thing'low to thing'high loop
    >-- looping over all bits in "thing" whatever the range

    or simply
    > for j in 0 to length - 1 loop


    Now you have to exclude the bits you don't want using realisable
    hardware;
    fortunately a comparison operator is realisable
    > for j in 0 to length - 1 loop
    > if j >= i then
    > v(i) := '0';
    > end if;


    It clearly performs the same operation.
    Incidentally did you mean v(j) := 0?

    - Brian
     
    Brian Drummond, Nov 30, 2007
    #3
  4. Dan

    Andy Guest

    On Nov 30, 8:39 am, Brian Drummond <>
    wrote:
    > On Fri, 30 Nov 2007 01:28:31 +0100, "Dan" <> wrote:
    >
    > >Hi,

    >
    > >I have this piece of code in my design:

    >
    > >function xxxxxx (...) is
    > > for j in i to length - 1 loop
    > > v(i) := '0';
    > > end loop;
    > >end xxxxxxx;

    >
    > >In this case 'i' is a variable, so I'm having problems syntesizing the
    > >desing because the for..loop range is variable, not constant.

    >
    > You have to translate the algorithm into a synthesisable form; and that
    > means locally constant loop indices
    >
    >
    >
    > > for j in thing'low to thing'high loop
    > >-- looping over all bits in "thing" whatever the range

    > or simply
    > > for j in 0 to length - 1 loop

    >
    > Now you have to exclude the bits you don't want using realisable
    > hardware;
    > fortunately a comparison operator is realisable
    >
    > > for j in 0 to length - 1 loop
    > > if j >= i then
    > > v(i) := '0';
    > > end if;

    >
    > It clearly performs the same operation.
    > Incidentally did you mean v(j) := 0?
    >
    > - Brian


    Another way to get "variable index limit" on a for-loop is with an
    exit statement to terminate the loop early. Put the exit statement in
    a conditional inside the loop.

    Andy
     
    Andy, Dec 3, 2007
    #4
  5. On Mon, 3 Dec 2007 06:40:33 -0800 (PST), Andy <>
    wrote:

    >On Nov 30, 8:39 am, Brian Drummond <>
    >wrote:
    >> On Fri, 30 Nov 2007 01:28:31 +0100, "Dan" <> wrote:


    >> > for j in 0 to length - 1 loop
    >> > if j >= i then
    >> > v(i) := '0';
    >> > end if;


    >Another way to get "variable index limit" on a for-loop is with an
    >exit statement to terminate the loop early. Put the exit statement in
    >a conditional inside the loop.


    Good one, IF you can realise what you want by exiting early.

    Does it synthesise as expected?

    It's not so useful if you need a late entry to the loop, as the example
    implies. In this case you can reverse the loop direction to get your
    early exit, but if both ends were variables, it wouldn't work.

    - Brian
     
    Brian Drummond, Dec 3, 2007
    #5
  6. Dan

    Andy Guest

    On Dec 3, 4:07 pm, Brian Drummond <>
    wrote:
    > On Mon, 3 Dec 2007 06:40:33 -0800 (PST), Andy <>
    > wrote:
    >
    > >On Nov 30, 8:39 am, Brian Drummond <>
    > >wrote:
    > >> On Fri, 30 Nov 2007 01:28:31 +0100, "Dan" <> wrote:
    > >> > for j in 0 to length - 1 loop
    > >> > if j >= i then
    > >> > v(i) := '0';
    > >> > end if;

    > >Another way to get "variable index limit" on a for-loop is with an
    > >exit statement to terminate the loop early. Put the exit statement in
    > >a conditional inside the loop.

    >
    > Good one, IF you can realise what you want by exiting early.
    >
    > Does it synthesise as expected?
    >
    > It's not so useful if you need a late entry to the loop, as the example
    > implies. In this case you can reverse the loop direction to get your
    > early exit, but if both ends were variables, it wouldn't work.
    >
    > - Brian


    Since a for-loop is unrolled in synthesis (that's the reason the index
    bounds have to be static), a for-loop with a conditional unrolls to a
    series of if-then statements (not nested). Putting an exit statement
    in the conditional turns it into an if-elsif-elsif... sequence, so it
    jumps to the end when a condition is hit.

    Andy
     
    Andy, Dec 4, 2007
    #6
    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. Replies:
    46
    Views:
    1,000
    Antoon Pardon
    Jul 25, 2006
  2. Joona I Palaste
    Replies:
    16
    Views:
    534
    Fao, Sean
    Sep 1, 2004
  3. Lambda
    Replies:
    2
    Views:
    425
    James Kanze
    Jul 16, 2008
  4. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    171
    Tomoyuki Kosimizu
    Nov 27, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    460
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page