Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
intro_example:chp_fifo [2022/05/17 09:48]
rajit [A one-place buffer]
intro_example:chp_fifo [2022/06/26 14:37] (current)
rajit [Simulating the buffer]
Line 90: Line 90:
 (i : 9 : b[i].R = b[i+1].L;) (i : 9 : b[i].R = b[i+1].L;)
 </code> </code>
-Here, ''i'' is a fresh variable that takes on values ''0'', ''1'', ..., ''8'' (one less than 9 specified between the two colons). +Here, ''i'' is a fresh variable that takes on values ''0'', ''1'', ..., ''8'' (one less than 9 specified between the two colons).  This is a special case of a general syntax that ACT uses for //syntactic replication//. The syntactic replication construct is written as follows: 
 +<code act> 
 +(sym id : range : body(id) ) 
 +</code> 
 +The ''sym'' (symbol) might be empty. ''id'' is a variable that can be used in ''body(id)'', and takes the range specified by ''range''. ''range'' can be either an integer-valued expression or ''start .. end'' to indicate a start and end index. The result of the replication is 
 +<code act> 
 + body(lo) sym body(lo+1) sym ... sym body(hi) 
 +</code> 
 +where ''lo'' is the starting index of the range, and ''hi'' is the ending index.
  
 The complete ten-place buffer with its primary input and output channels is given by: The complete ten-place buffer with its primary input and output channels is given by:
Line 133: Line 141:
 Note once again the use of angle brackets to specify template parameters. Note once again the use of angle brackets to specify template parameters.
  
 +===== Simulating the buffer =====
 +
 +Assuming all the process definitions above are in a file ''buffer.act'', we can create a test environment to run a simulation. Below, we create a ''test_source'' that sends ten data values on an output channel, and a ''test_sink'' that reads inputs and prints them to the screen using the built-in ''log()'' command. We then create a top-level test process (called ''test'' here) that connects a ''test_source'' and ''test_sink'' to the buffer.
 +
 +<file act test.act>
 +import "buffer.act"; // import process definitions
 +
 +defproc test_source (chan!(int) X)
 +{
 +    int i;
 +    chp {
 +        i := 0;
 +       *[ i < 10 -> X!i; i := i + 1 ]
 +   }
 +}
 +
 +defproc test_sink (chan?(int) X)
 +{
 +   int x;
 +   chp {
 +      *[ X?x; log ("received ", x) ]
 +   }
 +}
 +
 +defproc test()
 +{
 +    one_place_buffer b;
 +    test_source tsrc;
 +    test_sink tsink;
 +    b.L = tsrc.X;
 +    b.R = tsink.X;
 +}
 +</file>
 +
 +This can be simulated using ''[[tools:actsim|actsim]]'' as follows. The command-line specifies the ACT file to read, as well as the top-level process name.
 +
 +<code>
 +% actsim test.act test
 +
 +actsim> cycle
 +WARNING: test_sink<>: substituting chp model (requested prs, not found)
 +WARNING: test_source<>: substituting chp model (requested prs, not found)
 +WARNING: one_place_buffer<>: substituting chp model (requested prs, not found)
 +actsim> cycle
 +[                  30] <tsink>  received 0
 +[                  50] <tsink>  received 1
 +[                  70] <tsink>  received 2
 +[                  90] <tsink>  received 3
 +[                 110] <tsink>  received 4
 +[                 130] <tsink>  received 5
 +[                 150] <tsink>  received 6
 +[                 170] <tsink>  received 7
 +[                 190] <tsink>  received 8
 +[                 210] <tsink>  received 9
 +actsim>
 +</code>
 +The first set of numbers is the time (default delays are 10 time units for each step in the CHP program). Next, the instance name is specified in angle brackets. Finally the log message is displayed.