112855Sgabeblack@google.com#include <systemc>
212855Sgabeblack@google.comusing namespace sc_core;
312855Sgabeblack@google.comusing namespace sc_dt;
412855Sgabeblack@google.comusing std::cout;
512855Sgabeblack@google.comusing std::endl;
612855Sgabeblack@google.com
712855Sgabeblack@google.com// 34) event finder on multiport
812855Sgabeblack@google.com
912855Sgabeblack@google.comstruct i_f: virtual sc_interface
1012855Sgabeblack@google.com{
1112855Sgabeblack@google.com  virtual const sc_event& event() const = 0;
1212855Sgabeblack@google.com};
1312855Sgabeblack@google.com
1412855Sgabeblack@google.comstruct Chan: i_f, sc_object
1512855Sgabeblack@google.com{
1612855Sgabeblack@google.com  virtual const sc_event& event() const { return ev; }
1712855Sgabeblack@google.com  sc_event ev;
1812855Sgabeblack@google.com};
1912855Sgabeblack@google.com
2012855Sgabeblack@google.comstruct Port: sc_port<i_f,0>
2112855Sgabeblack@google.com{
2212855Sgabeblack@google.com  sc_event_finder& find_event() const
2312855Sgabeblack@google.com  {
2412855Sgabeblack@google.com    return *new sc_event_finder_t<i_f>( *this, &i_f::event );
2512855Sgabeblack@google.com  }
2612855Sgabeblack@google.com};
2712855Sgabeblack@google.com
2812855Sgabeblack@google.comSC_MODULE(M)
2912855Sgabeblack@google.com{
3012855Sgabeblack@google.com  Port mp;
3112855Sgabeblack@google.com  bool flag, flag2;
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com  SC_CTOR(M)
3412855Sgabeblack@google.com    : flag(false), flag2(false)
3512855Sgabeblack@google.com  {
3612855Sgabeblack@google.com    SC_THREAD(T);
3712855Sgabeblack@google.com    sensitive << mp.find_event();
3812855Sgabeblack@google.com  }
3912855Sgabeblack@google.com  void T()
4012855Sgabeblack@google.com  {
4112855Sgabeblack@google.com    wait();
4212855Sgabeblack@google.com    sc_assert(sc_time_stamp() == sc_time(1, SC_NS));
4312855Sgabeblack@google.com    wait();
4412855Sgabeblack@google.com    sc_assert(sc_time_stamp() == sc_time(11, SC_NS));
4512855Sgabeblack@google.com    wait();
4612855Sgabeblack@google.com    sc_assert(sc_time_stamp() == sc_time(111, SC_NS));
4712855Sgabeblack@google.com    flag = true;
4812855Sgabeblack@google.com    wait();
4912855Sgabeblack@google.com    flag = false;
5012855Sgabeblack@google.com  }
5112855Sgabeblack@google.com  void end_of_elaboration()
5212855Sgabeblack@google.com  {
5312855Sgabeblack@google.com    SC_THREAD(T2);
5412855Sgabeblack@google.com    for (int i = 0; i < mp.size(); i++)
5512855Sgabeblack@google.com      sensitive << mp[i]->event();
5612855Sgabeblack@google.com  }
5712855Sgabeblack@google.com  void T2()
5812855Sgabeblack@google.com  {
5912855Sgabeblack@google.com    wait();
6012855Sgabeblack@google.com    sc_assert(sc_time_stamp() == sc_time(1, SC_NS));
6112855Sgabeblack@google.com    wait();
6212855Sgabeblack@google.com    sc_assert(sc_time_stamp() == sc_time(11, SC_NS));
6312855Sgabeblack@google.com    wait();
6412855Sgabeblack@google.com    sc_assert(sc_time_stamp() == sc_time(111, SC_NS));
6512855Sgabeblack@google.com    flag2 = true;
6612855Sgabeblack@google.com    wait();
6712855Sgabeblack@google.com    flag2 = false;
6812855Sgabeblack@google.com  }
6912855Sgabeblack@google.com  void end_of_simulation()
7012855Sgabeblack@google.com  {
7112855Sgabeblack@google.com    sc_assert( flag );
7212855Sgabeblack@google.com    sc_assert( flag2 );
7312855Sgabeblack@google.com  }
7412855Sgabeblack@google.com};
7512855Sgabeblack@google.com
7612855Sgabeblack@google.comSC_MODULE(Top)
7712855Sgabeblack@google.com{
7812855Sgabeblack@google.com  M *m;
7912855Sgabeblack@google.com  Chan chan1, chan2, chan3;
8012855Sgabeblack@google.com  SC_CTOR(Top)
8112855Sgabeblack@google.com  {
8212855Sgabeblack@google.com    m = new M("m");
8312855Sgabeblack@google.com    m->mp(chan1);
8412855Sgabeblack@google.com    m->mp(chan2);
8512855Sgabeblack@google.com    m->mp(chan3);
8612855Sgabeblack@google.com    SC_THREAD(T);
8712855Sgabeblack@google.com  }
8812855Sgabeblack@google.com  void T()
8912855Sgabeblack@google.com  {
9012855Sgabeblack@google.com    wait(1, SC_NS);
9112855Sgabeblack@google.com    chan1.ev.notify();
9212855Sgabeblack@google.com    wait(10, SC_NS);
9312855Sgabeblack@google.com    chan2.ev.notify();
9412855Sgabeblack@google.com    wait(100, SC_NS);
9512855Sgabeblack@google.com    chan3.ev.notify();
9612855Sgabeblack@google.com    wait(1, SC_NS);
9712855Sgabeblack@google.com    sc_stop();
9812855Sgabeblack@google.com  }
9912855Sgabeblack@google.com};
10012855Sgabeblack@google.com
10112855Sgabeblack@google.comint sc_main(int argc, char* argv[])
10212855Sgabeblack@google.com{
10312855Sgabeblack@google.com  cout << "Should be silent..." << endl;
10412855Sgabeblack@google.com
10512855Sgabeblack@google.com  Top top("top");
10612855Sgabeblack@google.com  sc_start();
10712855Sgabeblack@google.com
10812855Sgabeblack@google.com  cout << endl << "Success" << endl;
10912855Sgabeblack@google.com  return 0;
11012855Sgabeblack@google.com}
111