test05.cpp revision 12855:588919e0e4aa
16915SN/A/*****************************************************************************
26915SN/A
36915SN/A  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
46915SN/A  more contributor license agreements.  See the NOTICE file distributed
56915SN/A  with this work for additional information regarding copyright ownership.
66915SN/A  Accellera licenses this file to you under the Apache License, Version 2.0
76915SN/A  (the "License"); you may not use this file except in compliance with the
86915SN/A  License.  You may obtain a copy of the License at
96915SN/A
106915SN/A    http://www.apache.org/licenses/LICENSE-2.0
116915SN/A
126915SN/A  Unless required by applicable law or agreed to in writing, software
136915SN/A  distributed under the License is distributed on an "AS IS" BASIS,
146915SN/A  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
156915SN/A  implied.  See the License for the specific language governing
166915SN/A  permissions and limitations under the License.
176915SN/A
186915SN/A *****************************************************************************/
196915SN/A
206915SN/A/*****************************************************************************
216915SN/A
226915SN/A  test05.cpp --
236915SN/A
246915SN/A  Original Author: Martin Janssen, Synopsys, Inc., 2002-03-23
256915SN/A
266915SN/A *****************************************************************************/
276915SN/A
286915SN/A/*****************************************************************************
296915SN/A
306915SN/A  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
316915SN/A  changes you are making here.
326915SN/A
336915SN/A      Name, Affiliation, Date:
349100SN/A  Description of Modification:
3510529Smorr@cs.wisc.edu
366915SN/A *****************************************************************************/
376915SN/A
3811019Sjthestness@gmail.com// test of sc_fifo event finders
396915SN/A
4011019Sjthestness@gmail.com#include "systemc.h"
4111019Sjthestness@gmail.com
426915SN/A#define W_INFO(msg) \
437538SN/A    cout << sc_time_stamp() << "," << sc_delta_count() \
447538SN/A         << ": writer: " << msg << endl;
457538SN/A
4610519Snilay@cs.wisc.edu#define R_INFO(msg) \
4710007Snilay@cs.wisc.edu    cout << sc_time_stamp() << "," << sc_delta_count() \
4810007Snilay@cs.wisc.edu         << ": reader: " << msg << endl;
4910007Snilay@cs.wisc.edu
506915SN/ASC_MODULE( writer )
516915SN/A{
5210007Snilay@cs.wisc.edu    // port(s)
536915SN/A    sc_fifo_out<int> out;
546915SN/A
556915SN/A    // process(es)
566915SN/A    void main_action()
576915SN/A    {
586915SN/A        int val = 0;
596915SN/A        while( true ) {
606915SN/A            wait( 10, SC_NS ); // wait for 10 ns
616915SN/A            W_INFO( "blocking write" );
626915SN/A            for( int i = 0; i < 20; i ++ ) {
636915SN/A                out.write( val ++ ); // blocking write
646915SN/A            }
656915SN/A            wait( 10, SC_NS );
666915SN/A            W_INFO( out.num_free() << " free spaces" );
678180SN/A            W_INFO( "non-blocking write" );
688180SN/A            for( int i = 0; i < 20; i ++ ) {
6910007Snilay@cs.wisc.edu                while( ! out.nb_write( val ++ ) ) { // non-blocking write
706915SN/A                    W_INFO( "waiting" );
716915SN/A                    wait();
726915SN/A                    W_INFO( "data read event" );
736915SN/A                }
746915SN/A            }
758180SN/A        }
769319SN/A    }
779319SN/A
786915SN/A    SC_CTOR( writer )
798180SN/A    {
809319SN/A        SC_THREAD( main_action );
819319SN/A        sensitive << out.data_read();
826915SN/A    }
839366SN/A};
849366SN/A
858322SN/ASC_MODULE( reader )
869696SN/A{
879696SN/A    // port(s)
888436SN/A    sc_fifo_in<int> in;
8910529Smorr@cs.wisc.edu
909366SN/A    // process(es)
919366SN/A    void main_action()
9210300Scastilloe@unican.es    {
939841SN/A        int val;
949366SN/A        while( true ) {
958322SN/A            wait( 10, SC_NS ); // wait for 10 ns
967015SN/A            R_INFO( "blocking read 1" );
977015SN/A            for( int i = 0; i < 15; i ++ ) {
986915SN/A                in.read( val ); // blocking read
9910300Scastilloe@unican.es                R_INFO( val );
1008436SN/A            }
1016915SN/A            wait( 10, SC_NS );
1028322SN/A            R_INFO( in.num_available() << " available samples" );
1039468SN/A            R_INFO( "blocking read 2" );
10410007Snilay@cs.wisc.edu            for( int i = 0; i < 15; i ++ ) {
1056915SN/A                val = in.read(); // blocking read
1066915SN/A                R_INFO( val );
1076915SN/A            }
10810007Snilay@cs.wisc.edu            wait( 10, SC_NS );
10910311Snilay@cs.wisc.edu            R_INFO( in.num_available() << " available samples" );
11010311Snilay@cs.wisc.edu            R_INFO( "non-blocking read" );
11110311Snilay@cs.wisc.edu            for( int i = 0; i < 15; i ++ ) {
11210311Snilay@cs.wisc.edu                while( ! in.nb_read( val ) ) { // non-blocking read
11310311Snilay@cs.wisc.edu                    R_INFO( "waiting" );
11410311Snilay@cs.wisc.edu                    wait();
11510311Snilay@cs.wisc.edu                    R_INFO( "data written event" );
11610311Snilay@cs.wisc.edu                }
11710311Snilay@cs.wisc.edu                R_INFO( val );
1188180SN/A            }
1198180SN/A        }
1206915SN/A    }
1216915SN/A
1226915SN/A    SC_CTOR( reader )
1236915SN/A    {
1246915SN/A        SC_THREAD( main_action );
1258180SN/A        sensitive << in.data_written();
1268180SN/A    }
1276915SN/A};
1286915SN/A
1299696SN/Aint sc_main( int, char*[] )
1309841SN/A{
1318436SN/A    // sc_clock c;
13210007Snilay@cs.wisc.edu
1339468SN/A    // declare channel(s)
1346915SN/A    sc_fifo<int> fifo( 10 );
13510007Snilay@cs.wisc.edu
13610311Snilay@cs.wisc.edu    // instantiate block(s) and connect to channel(s)
13710311Snilay@cs.wisc.edu    writer w( "writer" );
13810311Snilay@cs.wisc.edu    reader r( "reader" );
13910311Snilay@cs.wisc.edu
14010311Snilay@cs.wisc.edu    w.out( fifo );
14110311Snilay@cs.wisc.edu    r.in( fifo );
14210311Snilay@cs.wisc.edu
14310311Snilay@cs.wisc.edu    // run the simulation
14410311Snilay@cs.wisc.edu    sc_start( 100, SC_NS );
14510311Snilay@cs.wisc.edu
1469826SN/A    return 0;
1479798SN/A}
1486915SN/A