test02.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  test02.cpp --
23
24  Original Author: Ucar Aziz, Synopsys, Inc., 2002-02-15
25                   Martin Janssen, Synopsys, Inc., 2002-02-15
26
27 *****************************************************************************/
28
29/*****************************************************************************
30
31  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
32  changes you are making here.
33
34      Name, Affiliation, Date:
35  Description of Modification:
36
37 *****************************************************************************/
38
39//test of sc_prim_channel::next_trigger(const sc_event&)
40
41#include <systemc.h>
42
43//write and read interfaces
44class write_if : virtual public
45sc_interface
46 {
47  public:
48   virtual void write() = 0;
49};
50
51class read_if : virtual public
52sc_interface
53{
54 public:
55  virtual void read( ) = 0;
56};
57
58// channel implements write_if and read_if interfaces
59class channel :
60  public sc_channel,
61  public write_if,
62  public read_if
63{
64
65  public :
66
67  //constructor
68  channel(sc_module_name name):sc_channel(name)  , data(0)
69  { }
70
71  //write to channel
72  void write(){
73    static int i = 0;
74    if(i < 10){
75    next_trigger(read_event);
76    data = i;
77    cout<<"writting "<< data <<" to channel";
78    i++;
79    cout<<" and notifying write_event" << endl;
80    write_event.notify();
81  }
82  }
83
84  //read from channel
85  void read( ){
86    int j;
87    next_trigger(write_event);
88    j = data;
89    cout<<"reading  "<<j<<" from channel";
90    cout<<" and notifying read_event" << endl;
91    read_event.notify();
92  }
93
94  private:
95  int data;
96  sc_event write_event, read_event;
97
98};
99
100//source module
101SC_MODULE(mod_a)
102{
103  sc_port<write_if> out;
104
105  void write( )
106  {
107    out->write();
108  }
109
110  SC_CTOR( mod_a ){
111
112    SC_METHOD(write);
113  }
114};
115
116//sink module
117SC_MODULE(mod_b)
118{
119  sc_port<read_if> input;
120  int i;
121
122  void read( )
123  {
124   input->read();
125  }
126
127  SC_CTOR( mod_b ){
128
129    SC_METHOD(read);
130  }
131};
132
133
134int sc_main(int, char*[] )
135{
136  channel a("a");
137  mod_a modul_a("modul_a");
138  mod_b modul_b("modul_b");
139  modul_a.out(a);
140  modul_b.input(a);
141
142  sc_start(10, SC_NS);
143  return 0 ;
144}
145