Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| intro_example:chp_fifo2 [2025/04/21 23:07] – rajit | intro_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 | + | We start with the earlier example |
| <code act> | <code act> | ||
| Line 46: | Line 46: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | Another version of this is the following: | ||
| + | |||
| + | <code act> | ||
| + | template< | ||
| + | defproc buffer (chan?(int) L; chan!(int) R) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | (i : N : | ||
| + | [ i = 0 -> b[i].L = L; b[i].R = ch[i]; | ||
| + | [] i = N-1 -> b[i].L = ch[i-1]; b[i].R = R; | ||
| + | [] else -> b[i].L = ch[i-1]; b[i].R = ch[i]; | ||
| + | ] | ||
| + | ) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Using port connections directly | ||
| + | |||
| + | Instead of using the dot notation to explicitly connect ports, we observe that each set of connections operates on the same instance '' | ||
| + | |||
| + | <code act> | ||
| + | template< | ||
| + | defproc buffer (chan?(int) L; chan!(int) R) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | (i : N : | ||
| + | [ i = 0 -> b[i](L, | ||
| + | [] i = N-1 -> b[i](ch[i-1], | ||
| + | [] else -> b[i](ch[i-1], | ||
| + | ] | ||
| + | ) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 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< | ||
| + | defproc buffer (chan?(int) L; chan!(int) R) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | (i : N : | ||
| + | [ i = 0 -> b[i](.L=L, .R=ch[i]); | ||
| + | [] i = N-1 -> b[i](.L=ch[i-1], | ||
| + | [] else -> b[i](.L=ch[i-1], | ||
| + | ] | ||
| + | ) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== 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 '' | ||
| + | |||
| + | <code act> | ||
| + | template< | ||
| + | defproc buffer (chan?(int) L; chan!(int) R) | ||
| + | { | ||
| + | | ||
| + | (i : N : | ||
| + | | ||
| + | [ i = 0 -> b[i](.L=L, .R=ch[i]); | ||
| + | [] i = N-1 -> b[i](.L=ch[i-1], | ||
| + | [] else -> b[i](.L=ch[i-1], | ||
| + | ] | ||
| + | ) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Each '' | ||
| + | |||
| + | 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< | ||
| + | defproc buffer (chan?(int) L; chan!(int) R) | ||
| + | { | ||
| + | | ||
| + | (i : N : | ||
| + | | ||
| + | [ i = 0 -> b[i](.L=L); | ||
| + | [] i = N-1 -> b[i](.L=b[i-1].R, | ||
| + | [] else -> b[i](.L=b[i-1].R); | ||
| + | ] | ||
| + | ) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ... which may be easier to read as follows: | ||
| + | |||
| + | <code act> | ||
| + | template< | ||
| + | defproc buffer (chan?(int) L; chan!(int) R) | ||
| + | { | ||
| + | | ||
| + | (i : N : | ||
| + | | ||
| + | [ i = 0 -> b[i].L=L; | ||
| + | [] i = N-1 -> b[i](.L=b[i-1].R, | ||
| + | [] else -> b[i].L=b[i-1].R; | ||
| + | ] | ||
| + | ) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | This version is similar to the original version we started with. | ||