test04.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  test04.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 events
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( out.data_read_event() );
72                    W_INFO( "data read event" );
73                }
74            }
75        }
76    }
77
78    SC_CTOR( writer )
79    {
80        SC_THREAD( main_action );
81    }
82};
83
84SC_MODULE( reader )
85{
86    // port(s)
87    sc_fifo_in<int> in;
88
89    // process(es)
90    void main_action()
91    {
92        int val;
93        while( true ) {
94            wait( 10, SC_NS ); // wait for 10 ns
95            R_INFO( "blocking read 1" );
96            for( int i = 0; i < 15; i ++ ) {
97                in.read( val ); // blocking read
98                R_INFO( val );
99            }
100            wait( 10, SC_NS );
101            R_INFO( in.num_available() << " available samples" );
102            R_INFO( "blocking read 2" );
103            for( int i = 0; i < 15; i ++ ) {
104                val = in.read(); // blocking read
105                R_INFO( val );
106            }
107            wait( 10, SC_NS );
108            R_INFO( in.num_available() << " available samples" );
109            R_INFO( "non-blocking read" );
110            for( int i = 0; i < 15; i ++ ) {
111                while( ! in.nb_read( val ) ) { // non-blocking read
112                    R_INFO( "waiting" );
113                    wait( in.data_written_event() );
114                    R_INFO( "data written event" );
115                }
116                R_INFO( val );
117            }
118        }
119    }
120
121    SC_CTOR( reader )
122    {
123        SC_THREAD( main_action );
124    }
125};
126
127int sc_main( int, char*[] )
128{
129    // sc_clock c;
130
131    // declare channel(s)
132    sc_fifo<int> fifo( 10 );
133
134    // instantiate block(s) and connect to channel(s)
135    writer w( "writer" );
136    reader r( "reader" );
137
138    w.out( fifo );
139    r.in( fifo );
140
141    // run the simulation
142    sc_start( 100, SC_NS );
143
144    return 0;
145}
146