test08.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  test08.cpp --
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38// test of sc_event's methods; with four different receivers
39
40#include "systemc.h"
41
42SC_MODULE( mod_a )
43{
44    sc_event e;
45
46    void write( const char* msg )
47    {
48        cout << sc_delta_count() << ":" << sc_time_stamp()
49             << " " << msg << "\n";
50    }
51
52    void sender()
53    {
54        // wait one delta cycle
55        wait( SC_ZERO_TIME );
56
57        while( true ) {
58
59            // test cancel()
60            cout << "*** cancel()\n";
61
62            // immediate notification
63            e.notify();
64            write( "sender - immediate" );
65            wait( SC_ZERO_TIME );
66
67            // immediate notification -- canceled (no effect)
68            e.notify();
69            write( "sender - immediate" );
70            e.cancel();
71            write( "sender - canceled" );
72            wait( SC_ZERO_TIME );
73
74            // delta notification
75            e.notify( SC_ZERO_TIME );
76            write( "sender - delta" );
77            wait( SC_ZERO_TIME );
78            wait( SC_ZERO_TIME );
79
80            // delta notification -- canceled
81            e.notify( SC_ZERO_TIME );
82            write( "sender - delta" );
83            e.cancel();
84            write( "sender - canceled" );
85            wait( SC_ZERO_TIME );
86            wait( SC_ZERO_TIME );
87
88            // timed notification
89            e.notify( 1, SC_NS );
90            write( "sender - timed 1 ns" );
91            wait( 1, SC_NS );
92            wait( SC_ZERO_TIME );
93
94            // timed notification -- canceled
95            e.notify( 1, SC_NS );
96            write( "sender - timed 1 ns" );
97            e.cancel();
98            write( "sender - canceled" );
99            wait( 1, SC_NS );
100            wait( SC_ZERO_TIME );
101
102            // timed notifiation -- canceled
103            e.notify( 2, SC_NS );
104            write( "sender - timed 2 ns" );
105            wait( 1, SC_NS );
106            e.cancel();
107            write( "sender - canceled" );
108            wait( 1, SC_NS );
109            wait( SC_ZERO_TIME );
110
111            // test notify() -- the exception test is in test03.cpp
112            cout << "*** notify()\n";
113
114            // delta notification -- made immediate
115            e.notify( SC_ZERO_TIME );
116            write( "sender - delta" );
117            e.notify();
118            write( "sender - immediate" );
119            wait( SC_ZERO_TIME );
120            wait( SC_ZERO_TIME );
121
122            // timed notification -- made immediate
123            e.notify( 1, SC_NS );
124            write( "sender - timed 1 ns" );
125            e.notify();
126            write( "sender - immediate" );
127            wait( 1, SC_NS );
128            wait( SC_ZERO_TIME );
129
130            // timed notification -- made immediate
131            e.notify( 2, SC_NS );
132            write( "sender - timed 2 ns" );
133            wait( 1, SC_NS );
134            e.notify();
135            write( "sender - immediate" );
136            wait( 1, SC_NS );
137            wait( SC_ZERO_TIME );
138
139            // test notify(t)
140            cout << "*** notify(t)\n";
141
142            e.notify( SC_ZERO_TIME );
143            write( "sender - delta" );
144            e.notify( 1, SC_NS );
145            write( "sender - timed 1 ns" );
146            wait( 1, SC_NS );
147            wait( SC_ZERO_TIME );
148
149            e.notify( 1, SC_NS );
150            write( "sender - timed 1 ns" );
151            e.notify( SC_ZERO_TIME );
152            write( "sender - delta" );
153            wait( 1, SC_NS );
154            wait( SC_ZERO_TIME );
155
156            e.notify( 2, SC_NS );
157            write( "sender - timed 2 ns" );
158            e.notify( 1, SC_NS );
159            write( "sender - timed 1 ns" );
160            wait( 2, SC_NS );
161            wait( SC_ZERO_TIME );
162
163            e.notify( 1, SC_NS );
164            write( "sender - timed 1 ns" );
165            e.notify( 2, SC_NS );
166            write( "sender - timed 2 ns" );
167            wait( 2, SC_NS );
168            wait( SC_ZERO_TIME );
169
170            sc_stop();
171            write( "sender - stop" );
172            wait( SC_ZERO_TIME );
173        }
174    }
175
176    bool receiver_static_method_first;
177    bool receiver_dynamic_method_first;
178
179    void receiver_static_method()
180    {
181        if( receiver_static_method_first ) {
182            receiver_static_method_first = false;
183            return;
184        }
185        write( "receiver_static_method" );
186    }
187
188    void receiver_dynamic_method()
189    {
190        next_trigger( e );
191        if( receiver_dynamic_method_first ) {
192            receiver_dynamic_method_first = false;
193            return;
194        }
195        write( "receiver_dynamic_method" );
196    }
197
198    void receiver_static_thread()
199    {
200        while( true ) {
201            wait();
202            write( "receiver_static_thread" );
203        }
204    }
205
206    void receiver_dynamic_thread()
207    {
208        while( true ) {
209            wait( e );
210            write( "receiver_dynamic_thread" );
211        }
212    }
213
214    SC_CTOR( mod_a )
215    {
216        SC_THREAD( sender );
217
218        SC_METHOD( receiver_static_method );
219        sensitive << e;
220        receiver_static_method_first = true;
221
222        SC_METHOD( receiver_dynamic_method );
223        receiver_dynamic_method_first = true;
224
225        SC_THREAD( receiver_static_thread );
226        sensitive << e;
227
228        SC_THREAD( receiver_dynamic_thread );
229    }
230};
231
232int
233sc_main( int, char*[] )
234{
235    mod_a a( "a" );
236
237    sc_start();
238
239    return 0;
240}
241