test05.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  test05.cpp --
23
24  Original Author: Martin Janssen, Synopsys, Inc., 2002-03-23
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_fifo event finders
39
40#include "systemc.h"
41
42#define W_INFO(msg) \
43    cout << sc_time_stamp() << "," << sc_delta_count() \
44         << ": writer: " << msg << endl;
45
46#define R_INFO(msg) \
47    cout << sc_time_stamp() << "," << sc_delta_count() \
48         << ": reader: " << msg << endl;
49
50SC_MODULE( writer )
51{
52    // port(s)
53    sc_fifo_out<int> out;
54
55    // process(es)
56    void main_action()
57    {
58        int val = 0;
59        while( true ) {
60            wait( 10, SC_NS ); // wait for 10 ns
61            W_INFO( "blocking write" );
62            for( int i = 0; i < 20; i ++ ) {
63                out.write( val ++ ); // blocking write
64            }
65            wait( 10, SC_NS );
66            W_INFO( out.num_free() << " free spaces" );
67            W_INFO( "non-blocking write" );
68            for( int i = 0; i < 20; i ++ ) {
69                while( ! out.nb_write( val ++ ) ) { // non-blocking write
70                    W_INFO( "waiting" );
71                    wait();
72                    W_INFO( "data read event" );
73                }
74            }
75        }
76    }
77
78    SC_CTOR( writer )
79    {
80        SC_THREAD( main_action );
81        sensitive << out.data_read();
82    }
83};
84
85SC_MODULE( reader )
86{
87    // port(s)
88    sc_fifo_in<int> in;
89
90    // process(es)
91    void main_action()
92    {
93        int val;
94        while( true ) {
95            wait( 10, SC_NS ); // wait for 10 ns
96            R_INFO( "blocking read 1" );
97            for( int i = 0; i < 15; i ++ ) {
98                in.read( val ); // blocking read
99                R_INFO( val );
100            }
101            wait( 10, SC_NS );
102            R_INFO( in.num_available() << " available samples" );
103            R_INFO( "blocking read 2" );
104            for( int i = 0; i < 15; i ++ ) {
105                val = in.read(); // blocking read
106                R_INFO( val );
107            }
108            wait( 10, SC_NS );
109            R_INFO( in.num_available() << " available samples" );
110            R_INFO( "non-blocking read" );
111            for( int i = 0; i < 15; i ++ ) {
112                while( ! in.nb_read( val ) ) { // non-blocking read
113                    R_INFO( "waiting" );
114                    wait();
115                    R_INFO( "data written event" );
116                }
117                R_INFO( val );
118            }
119        }
120    }
121
122    SC_CTOR( reader )
123    {
124        SC_THREAD( main_action );
125        sensitive << in.data_written();
126    }
127};
128
129int sc_main( int, char*[] )
130{
131    // sc_clock c;
132
133    // declare channel(s)
134    sc_fifo<int> fifo( 10 );
135
136    // instantiate block(s) and connect to channel(s)
137    writer w( "writer" );
138    reader r( "reader" );
139
140    w.out( fifo );
141    r.in( fifo );
142
143    // run the simulation
144    sc_start( 100, SC_NS );
145
146    return 0;
147}
148