Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
intro_example:chp_fifo2 [2025/04/21 23:12] – [Using auxillary variables] rajitintro_example:chp_fifo2 [2025/05/01 10:47] (current) – [Alternate Buffer] rajit
Line 1: Line 1:
 ====== Alternate Buffer ====== ====== Alternate Buffer ======
  
-We start with the previous definition of a templated buffer:+We start with the earlier example of a templated buffer:
  
 <code act> <code act>
Line 82: Line 82:
 } }
 </code> </code>
 +
 +This approach relies on the order of ports being known; if instead it is preferable to connect ports by name, the following syntax is also valid.
 +
 +
 +<code act>
 +template<pint N>
 +defproc buffer (chan?(int) L; chan!(int) R)
 +{
 +   one_place_buffer b[N];
 +   chan(int) ch[N-1];
 +   (i : N :  
 +      [ i = 0 ->    b[i](.L=L, .R=ch[i]);
 +     [] i = N-1 ->  b[i](.L=ch[i-1], .R=R);
 +     [] else -> b[i](.L=ch[i-1], .R=ch[i]);
 +      ]
 +   )
 +}
 +</code>
 +
 +===== Dynamic creation of buffer instances =====
 +
 +ACT also permits instances to be created one at a time. So rather than declaring a single buffer array of size ''N'', we can create it one segment of the array at a time.
 +
 +<code act>
 +template<pint N>
 +defproc buffer (chan?(int) L; chan!(int) R)
 +{
 +   chan(int) ch[N-1];
 +   (i : N :  
 +       one_place_buffer b[i..i];
 +      [ i = 0 ->    b[i](.L=L, .R=ch[i]);
 +     [] i = N-1 ->  b[i](.L=ch[i-1], .R=R);
 +     [] else -> b[i](.L=ch[i-1], .R=ch[i]);
 +      ]
 +   )
 +}
 +</code>
 +
 +Each ''b[i..i]'' slice augments the array ''b''.
 +
 +Note that when connecting to ports, you can leave connections dangling and then refer to them later. 
 +For example, the following avoids using the extra channel array.
 +
 +<code act>
 +template<pint N>
 +defproc buffer (chan?(int) L; chan!(int) R)
 +{
 +   chan(int) ch[N-1];
 +   (i : N :  
 +       one_place_buffer b[i..i];
 +      [ i = 0 ->    b[i](.L=L);
 +     [] i = N-1 ->  b[i](.L=b[i-1].R, .R=R);
 +     [] else -> b[i](.L=b[i-1].R);
 +      ]
 +   )
 +}
 +</code>
 +
 +... which may be easier to read as follows:
 +
 +<code act>
 +template<pint N>
 +defproc buffer (chan?(int) L; chan!(int) R)
 +{
 +   chan(int) ch[N-1];
 +   (i : N :  
 +       one_place_buffer b[i..i];
 +      [ i = 0 ->    b[i].L=L;
 +     [] i = N-1 ->  b[i](.L=b[i-1].R, .R=R);
 +     [] else -> b[i].L=b[i-1].R;
 +      ]
 +   )
 +}
 +</code>
 +
 +This version is similar to the original version we started with.
 +