112855Sgabeblack@google.com/*****************************************************************************
212855Sgabeblack@google.com
312855Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412855Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512855Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612855Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712855Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812855Sgabeblack@google.com  License.  You may obtain a copy of the License at
912855Sgabeblack@google.com
1012855Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112855Sgabeblack@google.com
1212855Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312855Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412855Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512855Sgabeblack@google.com  implied.  See the License for the specific language governing
1612855Sgabeblack@google.com  permissions and limitations under the License.
1712855Sgabeblack@google.com
1812855Sgabeblack@google.com *****************************************************************************/
1912855Sgabeblack@google.com
2012855Sgabeblack@google.com// event_list.cpp -- test for
2112855Sgabeblack@google.com//
2212855Sgabeblack@google.com//  Original Author: John Aynsley, Doulos, Inc.
2312855Sgabeblack@google.com//
2412855Sgabeblack@google.com// MODIFICATION LOG - modifiers, enter your name, affiliation, date and
2512855Sgabeblack@google.com//
2612855Sgabeblack@google.com// $Log: event_list.cpp,v $
2712855Sgabeblack@google.com// Revision 1.3  2011/09/05 21:23:30  acg
2812855Sgabeblack@google.com//  Philipp A. Hartmann: eliminate compiler warnings.
2912855Sgabeblack@google.com//
3012855Sgabeblack@google.com// Revision 1.2  2011/05/08 19:18:46  acg
3112855Sgabeblack@google.com//  Andy Goodrich: remove extraneous + prefixes from git diff.
3212855Sgabeblack@google.com//
3312855Sgabeblack@google.com
3412855Sgabeblack@google.com// Event lists. A thread waits on a list of events built dynamically from a multiport
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com#include <systemc>
3712855Sgabeblack@google.com
3812855Sgabeblack@google.comusing namespace sc_core;
3912855Sgabeblack@google.comusing std::cout;
4012855Sgabeblack@google.comusing std::endl;
4112855Sgabeblack@google.com
4212855Sgabeblack@google.comstruct Mod: sc_module
4312855Sgabeblack@google.com{
4412855Sgabeblack@google.com  sc_port<sc_signal_in_if<int>, 0> p; // Multiport
4512855Sgabeblack@google.com
4612855Sgabeblack@google.com  Mod(sc_module_name _name)
4712855Sgabeblack@google.com  {
4812855Sgabeblack@google.com    SC_THREAD(T1);
4912855Sgabeblack@google.com    SC_THREAD(T2);
5012855Sgabeblack@google.com  }
5112855Sgabeblack@google.com
5212855Sgabeblack@google.com  void T1()
5312855Sgabeblack@google.com  {
5412855Sgabeblack@google.com    for (;;)
5512855Sgabeblack@google.com    {
5612855Sgabeblack@google.com      wait(all_events());
5712855Sgabeblack@google.com      cout << "M::T1 awoke with " << p[0]->read() << p[1]->read() << p[2]->read()
5812855Sgabeblack@google.com           << " at " << sc_time_stamp() << " on list" << endl;
5912855Sgabeblack@google.com    }
6012855Sgabeblack@google.com  }
6112855Sgabeblack@google.com  void T2()
6212855Sgabeblack@google.com  {
6312855Sgabeblack@google.com    for (;;)
6412855Sgabeblack@google.com    {
6512855Sgabeblack@google.com      wait( p[0]->default_event() | p[1]->default_event() );
6612855Sgabeblack@google.com      cout << "M::T2 awoke with " << p[0]->read() << p[1]->read() << p[2]->read()
6712855Sgabeblack@google.com           << " at " << sc_time_stamp() << " on list" << endl;
6812855Sgabeblack@google.com    }
6912855Sgabeblack@google.com  }
7012855Sgabeblack@google.com  sc_event_or_list all_events() const
7112855Sgabeblack@google.com  {
7212855Sgabeblack@google.com    sc_assert( p.size() == 3 );
7312855Sgabeblack@google.com
7412855Sgabeblack@google.com    sc_event_or_list or_list;
7512855Sgabeblack@google.com    for (int i = 0; i < p.size(); i++)
7612855Sgabeblack@google.com      or_list |= p[i]->default_event();
7712855Sgabeblack@google.com
7812855Sgabeblack@google.com    sc_assert( or_list.size() == 3 );
7912855Sgabeblack@google.com    return or_list;
8012855Sgabeblack@google.com  }
8112855Sgabeblack@google.com
8212855Sgabeblack@google.com  SC_HAS_PROCESS(Mod);
8312855Sgabeblack@google.com};
8412855Sgabeblack@google.com
8512855Sgabeblack@google.comstruct Top: sc_module
8612855Sgabeblack@google.com{
8712855Sgabeblack@google.com  Top(sc_module_name _name)
8812855Sgabeblack@google.com  : finished(false)
8912855Sgabeblack@google.com  , count(0)
9012855Sgabeblack@google.com  {
9112855Sgabeblack@google.com    m = new Mod("m");
9212855Sgabeblack@google.com    m->p.bind(sig1);
9312855Sgabeblack@google.com    m->p.bind(sig2);
9412855Sgabeblack@google.com    m->p.bind(sig3);
9512855Sgabeblack@google.com    SC_THREAD(T);
9612855Sgabeblack@google.com    SC_METHOD(M);
9712855Sgabeblack@google.com  }
9812855Sgabeblack@google.com
9912855Sgabeblack@google.com  ~Top()
10012855Sgabeblack@google.com  {
10112855Sgabeblack@google.com    sc_assert( finished );
10212855Sgabeblack@google.com    sc_assert( count == 4 );
10312855Sgabeblack@google.com    sc_assert( sc_get_status() == SC_PAUSED );
10412855Sgabeblack@google.com    sc_assert( sc_is_running() == true );
10512855Sgabeblack@google.com    cout << "~T() called" << endl;
10612855Sgabeblack@google.com  }
10712855Sgabeblack@google.com
10812855Sgabeblack@google.com  sc_signal<int> sig1, sig2, sig3;
10912855Sgabeblack@google.com  Mod* m;
11012855Sgabeblack@google.com
11112855Sgabeblack@google.com  sc_event e1, e2, e3, e4;
11212855Sgabeblack@google.com  bool finished;
11312855Sgabeblack@google.com
11412855Sgabeblack@google.com  void T()
11512855Sgabeblack@google.com  {
11612855Sgabeblack@google.com    sig1.write(0);
11712855Sgabeblack@google.com    sig2.write(0);
11812855Sgabeblack@google.com    sig3.write(0);
11912855Sgabeblack@google.com
12012855Sgabeblack@google.com    wait(10, SC_NS);
12112855Sgabeblack@google.com    sig1.write(1);
12212855Sgabeblack@google.com    wait(10, SC_NS);
12312855Sgabeblack@google.com    sig1.write(2);
12412855Sgabeblack@google.com
12512855Sgabeblack@google.com    wait(10, SC_NS);
12612855Sgabeblack@google.com    sig2.write(1);
12712855Sgabeblack@google.com    wait(10, SC_NS);
12812855Sgabeblack@google.com    sig2.write(2);
12912855Sgabeblack@google.com
13012855Sgabeblack@google.com    wait(10, SC_NS);
13112855Sgabeblack@google.com    sig3.write(1);
13212855Sgabeblack@google.com    wait(10, SC_NS);
13312855Sgabeblack@google.com    sig3.write(2);
13412855Sgabeblack@google.com
13512855Sgabeblack@google.com    sc_time start;
13612855Sgabeblack@google.com    start = sc_time_stamp();
13712855Sgabeblack@google.com
13812855Sgabeblack@google.com    e1.notify(10, SC_NS);
13912855Sgabeblack@google.com    e2.notify(20, SC_NS);
14012855Sgabeblack@google.com
14112855Sgabeblack@google.com    sc_event_and_list list1 = e1 & e2;
14212855Sgabeblack@google.com    sc_assert( list1.size() == 2 );
14312855Sgabeblack@google.com
14412855Sgabeblack@google.com    wait( list1 );
14512855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
14612855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(20, SC_NS) );
14712855Sgabeblack@google.com
14812855Sgabeblack@google.com    start = sc_time_stamp();
14912855Sgabeblack@google.com    e1.notify(11, SC_NS);
15012855Sgabeblack@google.com    e2.notify(21, SC_NS);
15112855Sgabeblack@google.com
15212855Sgabeblack@google.com    sc_event_and_list list2;
15312855Sgabeblack@google.com    list2 &= e1;
15412855Sgabeblack@google.com    list2 &= e2;
15512855Sgabeblack@google.com    sc_assert( list2.size() == 2 );
15612855Sgabeblack@google.com
15712855Sgabeblack@google.com    wait( list2);
15812855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
15912855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(21, SC_NS) );
16012855Sgabeblack@google.com
16112855Sgabeblack@google.com    start = sc_time_stamp();
16212855Sgabeblack@google.com    e1.notify(14, SC_NS);
16312855Sgabeblack@google.com    e2.notify(24, SC_NS);
16412855Sgabeblack@google.com
16512855Sgabeblack@google.com    sc_event_and_list list3 = list2 & e2;
16612855Sgabeblack@google.com    sc_assert( list3.size() == 2 );
16712855Sgabeblack@google.com
16812855Sgabeblack@google.com    wait( list3 );
16912855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
17012855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(24, SC_NS) );
17112855Sgabeblack@google.com
17212855Sgabeblack@google.com    start = sc_time_stamp();
17312855Sgabeblack@google.com    e1.notify(100, SC_NS);
17412855Sgabeblack@google.com    e2.notify(200, SC_NS);
17512855Sgabeblack@google.com
17612855Sgabeblack@google.com    sc_event_and_list list4;
17712855Sgabeblack@google.com    list4 = list3 & e2;
17812855Sgabeblack@google.com    sc_assert( list4.size() == 2 );
17912855Sgabeblack@google.com    wait( list4 );
18012855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
18112855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(200, SC_NS) );
18212855Sgabeblack@google.com
18312855Sgabeblack@google.com    start = sc_time_stamp();
18412855Sgabeblack@google.com    e1.notify(101, SC_NS);
18512855Sgabeblack@google.com    e2.notify(202, SC_NS);
18612855Sgabeblack@google.com
18712855Sgabeblack@google.com    sc_event_and_list list5 = list3 & list4;
18812855Sgabeblack@google.com    sc_assert( list5.size() == 2 );
18912855Sgabeblack@google.com    wait( list5 );
19012855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
19112855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(202, SC_NS) );
19212855Sgabeblack@google.com
19312855Sgabeblack@google.com    sc_event_and_list list6 = e1;
19412855Sgabeblack@google.com    sc_assert( list6.size() == 1 );
19512855Sgabeblack@google.com
19612855Sgabeblack@google.com    start = sc_time_stamp();
19712855Sgabeblack@google.com    e1.notify(500, SC_NS);
19812855Sgabeblack@google.com    wait(list6);
19912855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(500, SC_NS) );
20012855Sgabeblack@google.com
20112855Sgabeblack@google.com
20212855Sgabeblack@google.com    start = sc_time_stamp();
20312855Sgabeblack@google.com    e1.notify(1000, SC_NS);
20412855Sgabeblack@google.com    wait(e1 | e2 | e3);
20512855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
20612855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(1000, SC_NS) );
20712855Sgabeblack@google.com
20812855Sgabeblack@google.com    start = sc_time_stamp();
20912855Sgabeblack@google.com    e1.notify(1002, SC_NS);
21012855Sgabeblack@google.com    e2.notify(1001, SC_NS);
21112855Sgabeblack@google.com    e3.notify(1000, SC_NS);
21212855Sgabeblack@google.com
21312855Sgabeblack@google.com    wait(e1 & e2 & e3);
21412855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
21512855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(1002, SC_NS) );
21612855Sgabeblack@google.com
21712855Sgabeblack@google.com    start = sc_time_stamp();
21812855Sgabeblack@google.com    e1.notify(2000, SC_NS);
21912855Sgabeblack@google.com
22012855Sgabeblack@google.com    sc_event_or_list list7;
22112855Sgabeblack@google.com    list7 = e1 | e2;
22212855Sgabeblack@google.com    list7 = list7 | e3;
22312855Sgabeblack@google.com    list7 |= e4;
22412855Sgabeblack@google.com    wait(list7);
22512855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
22612855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(2000, SC_NS) );
22712855Sgabeblack@google.com
22812855Sgabeblack@google.com    start = sc_time_stamp();
22912855Sgabeblack@google.com    e1.notify(3000, SC_NS);
23012855Sgabeblack@google.com    list7 = e2 | e3;
23112855Sgabeblack@google.com    list7 = e1 | list7;
23212855Sgabeblack@google.com    wait(list7);
23312855Sgabeblack@google.com    cout << "Awoke at " << sc_time_stamp() << endl;
23412855Sgabeblack@google.com    sc_assert( sc_time_stamp() - start == sc_time(3000, SC_NS) );
23512855Sgabeblack@google.com
23612855Sgabeblack@google.com    list1 = e1;
23712855Sgabeblack@google.com    list2 = e2;
23812855Sgabeblack@google.com    e1.notify(SC_ZERO_TIME);
23912855Sgabeblack@google.com    wait(list1);
24012855Sgabeblack@google.com    e2.notify(SC_ZERO_TIME);
24112855Sgabeblack@google.com    wait(list2);
24212855Sgabeblack@google.com
24312855Sgabeblack@google.com    list1.swap(list2);
24412855Sgabeblack@google.com
24512855Sgabeblack@google.com    e1.notify(SC_ZERO_TIME);
24612855Sgabeblack@google.com    wait(list2);
24712855Sgabeblack@google.com    e2.notify(SC_ZERO_TIME);
24812855Sgabeblack@google.com    wait(list1);
24912855Sgabeblack@google.com
25012855Sgabeblack@google.com    finished = true;
25112855Sgabeblack@google.com  }
25212855Sgabeblack@google.com
25312855Sgabeblack@google.com  sc_event v1, v2, v3, v4;
25412855Sgabeblack@google.com  sc_event_or_list  or_list1,  or_list2;
25512855Sgabeblack@google.com  sc_event_and_list and_list1, and_list2;
25612855Sgabeblack@google.com  int count;
25712855Sgabeblack@google.com
25812855Sgabeblack@google.com  void M()
25912855Sgabeblack@google.com  {
26012855Sgabeblack@google.com    switch (count)
26112855Sgabeblack@google.com    {
26212855Sgabeblack@google.com      case 0:
26312855Sgabeblack@google.com      {
26412855Sgabeblack@google.com        sc_assert( sc_time_stamp() == sc_time(0, SC_NS) );
26512855Sgabeblack@google.com        or_list1 = v1 | v2;
26612855Sgabeblack@google.com        or_list2 = v3 | v4;
26712855Sgabeblack@google.com        v2.notify(10, SC_NS);
26812855Sgabeblack@google.com        next_trigger(or_list1 | or_list2);
26912855Sgabeblack@google.com        break;
27012855Sgabeblack@google.com      }
27112855Sgabeblack@google.com      case 1:
27212855Sgabeblack@google.com      {
27312855Sgabeblack@google.com        sc_assert( sc_time_stamp() == sc_time(10, SC_NS) );
27412855Sgabeblack@google.com        and_list1 = v1 & v2;
27512855Sgabeblack@google.com        and_list2 = v3 & v4;
27612855Sgabeblack@google.com        v1.notify(1, SC_NS);
27712855Sgabeblack@google.com        v2.notify(2, SC_NS);
27812855Sgabeblack@google.com        v3.notify(3, SC_NS);
27912855Sgabeblack@google.com        v4.notify(4, SC_NS);
28012855Sgabeblack@google.com        next_trigger(and_list1 & and_list2);
28112855Sgabeblack@google.com        break;
28212855Sgabeblack@google.com      }
28312855Sgabeblack@google.com      case 2:
28412855Sgabeblack@google.com      {
28512855Sgabeblack@google.com        sc_assert( sc_time_stamp() == sc_time(14, SC_NS) );
28612855Sgabeblack@google.com        and_list1 = v1 & v2 & v2 & v1;
28712855Sgabeblack@google.com        sc_assert( and_list1.size() == 2 );
28812855Sgabeblack@google.com        v1.notify(1, SC_NS);
28912855Sgabeblack@google.com        v2.notify(2, SC_NS);
29012855Sgabeblack@google.com        next_trigger(and_list1);
29112855Sgabeblack@google.com        break;
29212855Sgabeblack@google.com      }
29312855Sgabeblack@google.com      case 3:
29412855Sgabeblack@google.com      {
29512855Sgabeblack@google.com        sc_assert( sc_time_stamp() == sc_time(16, SC_NS) );
29612855Sgabeblack@google.com        break;
29712855Sgabeblack@google.com      }
29812855Sgabeblack@google.com    }
29912855Sgabeblack@google.com    ++count;
30012855Sgabeblack@google.com  }
30112855Sgabeblack@google.com
30212855Sgabeblack@google.com  SC_HAS_PROCESS(Top);
30312855Sgabeblack@google.com};
30412855Sgabeblack@google.com
30512855Sgabeblack@google.comint sc_main(int argc, char* argv[])
30612855Sgabeblack@google.com{
30712855Sgabeblack@google.com  Top top("top");
30812855Sgabeblack@google.com
30912855Sgabeblack@google.com  sc_start();
31012855Sgabeblack@google.com
31112855Sgabeblack@google.com  sc_assert( sc_get_status() == SC_PAUSED );
31212855Sgabeblack@google.com  sc_assert( sc_is_running() );
31312855Sgabeblack@google.com  sc_assert( sc_pending_activity() == false );
31412855Sgabeblack@google.com
31512855Sgabeblack@google.com  cout << endl << "Success" << endl;
31612855Sgabeblack@google.com  return 0;
31712855Sgabeblack@google.com}
31812855Sgabeblack@google.com
319