next up previous contents
Next: Creating parameterized modules Up: Constructor loops Previous: Basic for-loops   Contents

Creating arrays of instances

One important use of loops is to create arrays of module instances. For example, to create a three bit counter as an array of counter bits, we could write:

        bits : array 2..0;

        for(i = 0; i < 2; i = i + 1)
          bits[i] : counter_bit(carry[i],clear,count[i],carry[i+1]);

Note that bits is first declared as a generic array. Then the elements of the array are ``filled in'' inside the loop. In this way, each counter bit is connected to the appropriate signal, as a function of the loop index i.

Also note that module instances can be nested inside conditionals, provided that the condition evaluates to a constant at compile time. Since loops are unrolled at compile time, a loop index counts as a constant. Thus, for example, if we want to use a special module ``special_bit'' for bit 0 of the counter, we could write:

  bits : array 2..0;

  for(i = 0; i < 2; i = i + 1){
    if(i = 0)
      bits[i] : special_bit(carry[i],clear,count[i],carry[i+1]);
    else
      bits[i] : counter_bit(carry[i],clear,count[i],carry[i+1]);
  }



2002-10-28