This chapter contains some small examples for quickly getting started and verifying that the simulator is working properly.
The examples in this chapter are taken out of the test suite and its testing library.
Enter the following definitions into a HAC file bool-lib.hac:
defproc bool_buf (chan?(bool) L; chan!(bool) R) { bool x; chp { *[L?(x); R!(x)] } } template <pbool B> defproc bool_buf_init (chan?(bool) L; chan!(bool) R) { bool x; chp { x:=B; *[R!(x); L?(x)] } } template <><pint N; pbool B[N]> defproc bool_source_once(chan!(bool) S) { chp { {;i:N: S!(B[i]) } } } template <><pint N; pbool B[N]> defproc bool_source(chan!(bool) S) { chp { *[ {;i:N: S!(B[i]) } ] } } defproc bool_sink(chan?(bool) B) { bool b; chp { *[ B?(b) ] } }
We will be reusing this file in many examples. Instantiate some of these types in another file bool-test-1.hac.
import "bool-lib.hac"; chan(bool) L, R; bool_source SRC<4,{false,false,true,true}>(L); bool_buf B(L, R); bool_sink SNK(R);
Compile the file using haco (or one of the template Makefiles):
$ haco bool-test-1.hac bool-test-1.haco $ haccreate bool-test-1.haco bool-test-1.haco-c
and run hacchpsim:
$ hacchpsim bool-test-1.haco-c
The hacchpsim simulator is interactive. You should see the following prompt:
chpsim>
The following session is an example of stepping through the simulator while watching data values on channels:
chpsim> get L L : chan(bool<>) L = (0) [empty] chpsim> get R R : chan(bool<>) R = (0) [empty]
get queries the current state of a channel or data variable. All channels are initially empty (current values are meaningless), which is indicated by [empty]. Executed events are printed in a table format, whose headings are given by print-event-header.
chpsim> cause chpsim> watchall-events chpsim> print-event-header time eid pid event cause chpsim> step 20 0 0 4 null 10 5 2 B.L?(B.x) 10 1 1 SRC.S!(false) [by:5] 20 6 2 B.R!(B.x) [by:5] 20 7 3 SNK.B?(SNK.b) [by:6] 30 5 2 B.L?(B.x) [by:6] 30 4 1 SRC.S!(false) [by:5] 40 6 2 B.R!(B.x) [by:5] 40 7 3 SNK.B?(SNK.b) [by:6] 50 5 2 B.L?(B.x) [by:6] 50 3 1 SRC.S!(true) [by:5] 60 6 2 B.R!(B.x) [by:5] 60 7 3 SNK.B?(SNK.b) [by:6] 70 5 2 B.L?(B.x) [by:6] 70 2 1 SRC.S!(true) [by:5] 80 6 2 B.R!(B.x) [by:5] 80 7 3 SNK.B?(SNK.b) [by:6] 90 5 2 B.L?(B.x) [by:6] 90 1 1 SRC.S!(false) [by:5] 100 6 2 B.R!(B.x) [by:5]
Above, the simulator displays a trace of 20 events as they are executed. The first event is always the null event, which starts all processes. The source repeated sends values to the buffer, and the buffer sends values to the receiver. Send and receive actions execute atomically in pairs. (B.L and SRC.S both refer to channel L.) This trace does not actually show what values were sent over the channels. We restart this example, watching the channel data values this time:
chpsim> initialize chpsim> unwatchall-events chpsim> watch-value L R chpsim> step 20 updated channel(s): chan(bool<>) L = (0) [recvd] watch: 10 5 2 B.L?(B.x) updated channel(s): chan(bool<>) L = (0) [empty] watch: 10 1 1 SRC.S!(false) [by:5] updated channel(s): chan(bool<>) R = (0) [sent] watch: 20 6 2 B.R!(B.x) [by:5] updated channel(s): chan(bool<>) R = (0) [empty] watch: 20 7 3 SNK.B?(SNK.b) [by:6] updated channel(s): chan(bool<>) L = (0) [recvd] watch: 30 5 2 B.L?(B.x) [by:6] updated channel(s): chan(bool<>) L = (0) [empty] watch: 30 4 1 SRC.S!(false) [by:5] updated channel(s): chan(bool<>) R = (0) [sent] watch: 40 6 2 B.R!(B.x) [by:5] updated channel(s): chan(bool<>) R = (0) [empty] watch: 40 7 3 SNK.B?(SNK.b) [by:6] updated channel(s): chan(bool<>) L = (1) [recvd] watch: 50 5 2 B.L?(B.x) [by:6] updated channel(s): chan(bool<>) L = (1) [empty] watch: 50 3 1 SRC.S!(true) [by:5] updated channel(s): chan(bool<>) R = (1) [sent] watch: 60 6 2 B.R!(B.x) [by:5] updated channel(s): chan(bool<>) R = (1) [empty] watch: 60 7 3 SNK.B?(SNK.b) [by:6] updated channel(s): chan(bool<>) L = (1) [recvd] watch: 70 5 2 B.L?(B.x) [by:6] updated channel(s): chan(bool<>) L = (1) [empty] watch: 70 2 1 SRC.S!(true) [by:5] updated channel(s): chan(bool<>) R = (1) [sent] watch: 80 6 2 B.R!(B.x) [by:5] updated channel(s): chan(bool<>) R = (1) [empty] watch: 80 7 3 SNK.B?(SNK.b) [by:6] updated channel(s): chan(bool<>) L = (0) [recvd] watch: 90 5 2 B.L?(B.x) [by:6] updated channel(s): chan(bool<>) L = (0) [empty] watch: 90 1 1 SRC.S!(false) [by:5] updated channel(s): chan(bool<>) R = (0) [sent] watch: 100 6 2 B.R!(B.x) [by:5]
A list of all commands with brief descriptions is printed with ‘help all’. Please refer to Commands for a comprehensive description of all hacchpsim commands.
Exercises.
Tracing.