It is commonly necessary to select the first element of an array satisfying a certain condition, or to perform some other computation involving prioritizing an array. A looping construct called ``chain'' is provided for this purpose. It acts exactly like a ``for'' loop, except that the assignments in one iteration of the loop act as defaults for the assignments in subsequent iterations. This makes it possible to assign a signal in more than one iteration of the loop, with the later assignment taking priority over the earlier assignment.
For example, suppose we want to specify a ``priority encoder'', that inputs an array of signals, and outputs the index of the highest numbered signal that is true. Here is a priority encoder that inputs an array of boolean signals of size ``n'':
MODULE priority_n(n,inp,out) { INPUT inp : array 0..(n - 1) of boolean; OUTPUT out : 0..(n-1); chain(i = 0; i < n; i = i + 1) if (inp[i]) out := i; }
Depending on the contents of the array ``inp'', the signal ``out'' might be assigned many times in different iterations of the loop. In this case, the last assignment is given precedence.
The construct:
chain(i = 0; i < 4; i = i + 1) <stmt>is in every way equivalent to:
default <stmt with i = 0> in default <stmt with i = 1> in default <stmt with i = 2> in <stmt with i = 3>