test03.cpp revision 12855:588919e0e4aa
1#include "systemc.h"
2
3// Interface
4class C_if : virtual public sc_interface
5{
6public:
7    virtual void run() = 0;
8};
9
10// Channel
11class C : public C_if, public sc_channel
12{
13public:
14    SC_CTOR(C) { }
15    virtual void run()
16    {
17        cout << sc_time_stamp() << " In Channel run() " << endl;
18    }
19};
20
21// --- D: export channel C through IFP --------
22SC_MODULE( D )
23{
24    sc_export<C_if> IFP;
25
26    SC_CTOR( D )
27	: IFP("IFP"),   // explicit name
28	  m_C("C")
29    {
30	IFP( m_C );     // bind sc_export->interface by name
31    }
32 private:
33    C m_C;            // channel
34};
35
36// --- E: module with two interface-ports ---
37SC_MODULE( E )
38{
39 private:
40    C m_C;
41    D m_D;
42
43 public:
44    sc_export<C_if> IFP1;
45    sc_export<C_if> IFP2;
46
47    SC_CTOR( E )
48	: m_C("C"),
49	  m_D("D"),
50          IFP1("IFP1")
51    {
52        IFP1( m_C );
53	IFP2( m_D.IFP );          // bind sc_export->sc_export by name
54        IFP1.get_interface();     // just to see whether it compiles
55    }
56};
57
58// Module X connected to the channels through E
59SC_MODULE( X )
60{
61    sc_port<C_if> P1;
62    sc_port<C_if> P2;
63    SC_CTOR(X) {
64        SC_THREAD(run);
65    }
66    void run() {
67        wait(10, SC_NS);
68	P1->run();
69	wait(10, SC_NS);
70	P2->run();
71    }
72};
73
74int sc_main(int argc, char** argv) {
75  E the_E("E");
76  X the_X("X");
77  // port->IFP
78  the_X.P1( the_E.IFP1 );
79  the_X.P2( the_E.IFP2 );
80
81  sc_start(17, SC_NS);
82  the_E.IFP1->run();  // testing the operator-> of sc_export
83  sc_start(50, SC_NS);
84
85  return 0;
86}
87