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// include_descendants.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: include_descendants.cpp,v $
2712855Sgabeblack@google.com// Revision 1.2  2011/05/08 19:18:46  acg
2812855Sgabeblack@google.com//  Andy Goodrich: remove extraneous + prefixes from git diff.
2912855Sgabeblack@google.com//
3012855Sgabeblack@google.com
3112855Sgabeblack@google.com// Process control methods include_descendants argument
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES
3412855Sgabeblack@google.com
3512855Sgabeblack@google.com#include <systemc>
3612855Sgabeblack@google.com
3712855Sgabeblack@google.comusing namespace sc_core;
3812855Sgabeblack@google.comusing std::cout;
3912855Sgabeblack@google.comusing std::endl;
4012855Sgabeblack@google.com
4112855Sgabeblack@google.comstruct Top: sc_module
4212855Sgabeblack@google.com{
4312855Sgabeblack@google.com  Top(sc_module_name _name)
4412855Sgabeblack@google.com  {
4512855Sgabeblack@google.com    SC_THREAD(calling);
4612855Sgabeblack@google.com
4712855Sgabeblack@google.com    SC_THREAD(target1);
4812855Sgabeblack@google.com      t1 = sc_get_current_process_handle();
4912855Sgabeblack@google.com
5012855Sgabeblack@google.com    SC_THREAD(target4);
5112855Sgabeblack@google.com      t4 = sc_get_current_process_handle();
5212855Sgabeblack@google.com
5312855Sgabeblack@google.com    SC_THREAD(target7);
5412855Sgabeblack@google.com      t7 = sc_get_current_process_handle();
5512855Sgabeblack@google.com
5612855Sgabeblack@google.com    count = 0;
5712855Sgabeblack@google.com    f1 = f2 = f3 = f4 = f5 = f6 = f7 = f8 = f9 = f10 = 0;
5812855Sgabeblack@google.com    f11 = f12 = f13 = f14 = f15 = f16 = f17 = f18 = 0;
5912855Sgabeblack@google.com  }
6012855Sgabeblack@google.com
6112855Sgabeblack@google.com  sc_process_handle t1, ch2, ch3, t4, ch5, ch6, t7, ch8, ch9;
6212855Sgabeblack@google.com  sc_process_handle gch10, gch11, gch12, gch13;
6312855Sgabeblack@google.com  sc_event ev;
6412855Sgabeblack@google.com  int count;
6512855Sgabeblack@google.com  int f1, f2, f3, f4, f5, f6, f7, f8, f9, f10;
6612855Sgabeblack@google.com  int f11, f12, f13, f14, f15, f16, f17, f18;
6712855Sgabeblack@google.com
6812855Sgabeblack@google.com  std::exception ex;
6912855Sgabeblack@google.com
7012855Sgabeblack@google.com  void calling()
7112855Sgabeblack@google.com  {
7212855Sgabeblack@google.com    wait(SC_ZERO_TIME);
7312855Sgabeblack@google.com
7412855Sgabeblack@google.com    count = 1;
7512855Sgabeblack@google.com    t1.suspend(SC_INCLUDE_DESCENDANTS);
7612855Sgabeblack@google.com    ev.notify(5, SC_NS);
7712855Sgabeblack@google.com    wait(10, SC_NS);
7812855Sgabeblack@google.com
7912855Sgabeblack@google.com    count = 2;
8012855Sgabeblack@google.com    t1.resume(SC_INCLUDE_DESCENDANTS);
8112855Sgabeblack@google.com    wait(10, SC_NS);
8212855Sgabeblack@google.com
8312855Sgabeblack@google.com    count = 3;
8412855Sgabeblack@google.com    t1.disable(SC_INCLUDE_DESCENDANTS);
8512855Sgabeblack@google.com    ev.notify(5, SC_NS);
8612855Sgabeblack@google.com    wait(10, SC_NS);
8712855Sgabeblack@google.com
8812855Sgabeblack@google.com    count = 4;
8912855Sgabeblack@google.com    t1.enable(SC_INCLUDE_DESCENDANTS);
9012855Sgabeblack@google.com    wait(10, SC_NS);
9112855Sgabeblack@google.com
9212855Sgabeblack@google.com    count = 5;
9312855Sgabeblack@google.com    ev.notify();
9412855Sgabeblack@google.com    wait(10, SC_NS);
9512855Sgabeblack@google.com
9612855Sgabeblack@google.com    count = 6;
9712855Sgabeblack@google.com    t1.sync_reset_on(SC_INCLUDE_DESCENDANTS);
9812855Sgabeblack@google.com    wait(10, SC_NS);
9912855Sgabeblack@google.com
10012855Sgabeblack@google.com    count = 7;
10112855Sgabeblack@google.com    ev.notify();
10212855Sgabeblack@google.com    wait(10, SC_NS);
10312855Sgabeblack@google.com
10412855Sgabeblack@google.com    count = 8;
10512855Sgabeblack@google.com    t1.sync_reset_off(SC_INCLUDE_DESCENDANTS);
10612855Sgabeblack@google.com    wait(sc_time(110, SC_NS) - sc_time_stamp());
10712855Sgabeblack@google.com
10812855Sgabeblack@google.com    count = 10;
10912855Sgabeblack@google.com    t4.reset(SC_INCLUDE_DESCENDANTS);
11012855Sgabeblack@google.com    wait(sc_time(210, SC_NS) - sc_time_stamp());
11112855Sgabeblack@google.com
11212855Sgabeblack@google.com    t7.throw_it(ex, SC_INCLUDE_DESCENDANTS);
11312855Sgabeblack@google.com  }
11412855Sgabeblack@google.com
11512855Sgabeblack@google.com  void target1()
11612855Sgabeblack@google.com  {
11712855Sgabeblack@google.com    sc_assert(count == 0);
11812855Sgabeblack@google.com    ch2 = sc_spawn(sc_bind(&Top::child2, this));
11912855Sgabeblack@google.com    ch3 = sc_spawn(sc_bind(&Top::child3, this));
12012855Sgabeblack@google.com    wait(ch2.terminated_event() & ch3.terminated_event());
12112855Sgabeblack@google.com    f5 = 1;
12212855Sgabeblack@google.com  }
12312855Sgabeblack@google.com
12412855Sgabeblack@google.com  void child2()
12512855Sgabeblack@google.com  {
12612855Sgabeblack@google.com    if (count == 0) // Initialization
12712855Sgabeblack@google.com    {
12812855Sgabeblack@google.com      wait(ev);
12912855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(10, SC_NS) );
13012855Sgabeblack@google.com      wait(ev);
13112855Sgabeblack@google.com      sc_assert(count == 5);
13212855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(40, SC_NS) );
13312855Sgabeblack@google.com      f1 = 1;
13412855Sgabeblack@google.com      wait(ev);
13512855Sgabeblack@google.com    }
13612855Sgabeblack@google.com    else if (count == 7) // Sync reset
13712855Sgabeblack@google.com    {
13812855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(60, SC_NS) );
13912855Sgabeblack@google.com      f3 = 1;
14012855Sgabeblack@google.com      wait(20, SC_NS);
14112855Sgabeblack@google.com    }
14212855Sgabeblack@google.com  }
14312855Sgabeblack@google.com
14412855Sgabeblack@google.com  void child3()
14512855Sgabeblack@google.com  {
14612855Sgabeblack@google.com    if (count == 0) // Initialization
14712855Sgabeblack@google.com    {
14812855Sgabeblack@google.com      wait(ev);
14912855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(10, SC_NS) );
15012855Sgabeblack@google.com      wait(ev);
15112855Sgabeblack@google.com      sc_assert(count == 5);
15212855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(40, SC_NS) );
15312855Sgabeblack@google.com      f2 = 1;
15412855Sgabeblack@google.com      wait(ev);
15512855Sgabeblack@google.com    }
15612855Sgabeblack@google.com    else if (count == 7) // Sync reset
15712855Sgabeblack@google.com    {
15812855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(60, SC_NS) );
15912855Sgabeblack@google.com      f4 = 1;
16012855Sgabeblack@google.com      wait(20, SC_NS);
16112855Sgabeblack@google.com    }
16212855Sgabeblack@google.com  }
16312855Sgabeblack@google.com
16412855Sgabeblack@google.com  void target4()
16512855Sgabeblack@google.com  {
16612855Sgabeblack@google.com    if (count == 0)
16712855Sgabeblack@google.com    {
16812855Sgabeblack@google.com      wait(100, SC_NS);
16912855Sgabeblack@google.com      count = 9;
17012855Sgabeblack@google.com      ch5 = sc_spawn(sc_bind(&Top::child5, this));
17112855Sgabeblack@google.com      ch6 = sc_spawn(sc_bind(&Top::child6, this));
17212855Sgabeblack@google.com    }
17312855Sgabeblack@google.com    else // Hard reset
17412855Sgabeblack@google.com    {
17512855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(110, SC_NS) );
17612855Sgabeblack@google.com      f11 = 1;
17712855Sgabeblack@google.com    }
17812855Sgabeblack@google.com    wait(ch5.terminated_event() & ch6.terminated_event());
17912855Sgabeblack@google.com    f6 = 1;
18012855Sgabeblack@google.com  }
18112855Sgabeblack@google.com
18212855Sgabeblack@google.com  void child5()
18312855Sgabeblack@google.com  {
18412855Sgabeblack@google.com    switch (count)
18512855Sgabeblack@google.com    {
18612855Sgabeblack@google.com      case  9: sc_assert( sc_time_stamp() == sc_time(100, SC_NS) ); f7=1; break;
18712855Sgabeblack@google.com      case 10: sc_assert( sc_time_stamp() == sc_time(110, SC_NS) ); f8=1; break;
18812855Sgabeblack@google.com      default: sc_assert( false ); break;
18912855Sgabeblack@google.com    }
19012855Sgabeblack@google.com    wait(20, SC_NS);
19112855Sgabeblack@google.com  }
19212855Sgabeblack@google.com
19312855Sgabeblack@google.com  void child6()
19412855Sgabeblack@google.com  {
19512855Sgabeblack@google.com    switch (count)
19612855Sgabeblack@google.com    {
19712855Sgabeblack@google.com      case  9: sc_assert( sc_time_stamp() == sc_time(100, SC_NS) ); f9=1; break;
19812855Sgabeblack@google.com      case 10: sc_assert( sc_time_stamp() == sc_time(110, SC_NS) ); f10=1; break;
19912855Sgabeblack@google.com      default: sc_assert( false ); break;
20012855Sgabeblack@google.com    }
20112855Sgabeblack@google.com    wait(20, SC_NS);
20212855Sgabeblack@google.com  }
20312855Sgabeblack@google.com
20412855Sgabeblack@google.com  void target7()
20512855Sgabeblack@google.com  {
20612855Sgabeblack@google.com    wait(200, SC_NS);
20712855Sgabeblack@google.com    count = 11;
20812855Sgabeblack@google.com    ch8 = sc_spawn(sc_bind(&Top::child8, this));
20912855Sgabeblack@google.com    ch9 = sc_spawn(sc_bind(&Top::child9, this));
21012855Sgabeblack@google.com    try {
21112855Sgabeblack@google.com      wait(20, SC_NS);
21212855Sgabeblack@google.com    }
21312855Sgabeblack@google.com    catch (std::exception e) {
21412855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(210, SC_NS) );
21512855Sgabeblack@google.com    }
21612855Sgabeblack@google.com    wait(ch8.terminated_event() & ch9.terminated_event());
21712855Sgabeblack@google.com    sc_assert( sc_time_stamp() == sc_time(214, SC_NS) );
21812855Sgabeblack@google.com    f12 = 1;
21912855Sgabeblack@google.com  }
22012855Sgabeblack@google.com
22112855Sgabeblack@google.com  void child8()
22212855Sgabeblack@google.com  {
22312855Sgabeblack@google.com    gch10 = sc_spawn(sc_bind(&Top::grandchild10, this));
22412855Sgabeblack@google.com    gch11 = sc_spawn(sc_bind(&Top::grandchild11, this));
22512855Sgabeblack@google.com    try {
22612855Sgabeblack@google.com      wait(20, SC_NS);
22712855Sgabeblack@google.com    }
22812855Sgabeblack@google.com    catch (std::exception e) {
22912855Sgabeblack@google.com      f13 = 1;
23012855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(210, SC_NS) );
23112855Sgabeblack@google.com    }
23212855Sgabeblack@google.com    wait(gch10.terminated_event() & gch11.terminated_event());
23312855Sgabeblack@google.com  }
23412855Sgabeblack@google.com
23512855Sgabeblack@google.com  void child9()
23612855Sgabeblack@google.com  {
23712855Sgabeblack@google.com    gch12 = sc_spawn(sc_bind(&Top::grandchild12, this));
23812855Sgabeblack@google.com    gch13 = sc_spawn(sc_bind(&Top::grandchild13, this));
23912855Sgabeblack@google.com    try {
24012855Sgabeblack@google.com      wait(20, SC_NS);
24112855Sgabeblack@google.com    }
24212855Sgabeblack@google.com    catch (std::exception e) {
24312855Sgabeblack@google.com      f14 = 1;
24412855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(210, SC_NS) );
24512855Sgabeblack@google.com    }
24612855Sgabeblack@google.com    wait(gch12.terminated_event() & gch13.terminated_event());
24712855Sgabeblack@google.com  }
24812855Sgabeblack@google.com
24912855Sgabeblack@google.com  void grandchild10()
25012855Sgabeblack@google.com  {
25112855Sgabeblack@google.com    try {
25212855Sgabeblack@google.com      wait(20, SC_NS);
25312855Sgabeblack@google.com    }
25412855Sgabeblack@google.com    catch (std::exception e) {
25512855Sgabeblack@google.com      f15 = 1;
25612855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(210, SC_NS) );
25712855Sgabeblack@google.com    }
25812855Sgabeblack@google.com    wait(1, SC_NS);
25912855Sgabeblack@google.com  }
26012855Sgabeblack@google.com
26112855Sgabeblack@google.com  void grandchild11()
26212855Sgabeblack@google.com  {
26312855Sgabeblack@google.com    try {
26412855Sgabeblack@google.com      wait(20, SC_NS);
26512855Sgabeblack@google.com    }
26612855Sgabeblack@google.com    catch (std::exception e) {
26712855Sgabeblack@google.com      f16 = 1;
26812855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(210, SC_NS) );
26912855Sgabeblack@google.com    }
27012855Sgabeblack@google.com    wait(4, SC_NS);
27112855Sgabeblack@google.com  }
27212855Sgabeblack@google.com
27312855Sgabeblack@google.com  void grandchild12()
27412855Sgabeblack@google.com  {
27512855Sgabeblack@google.com    try {
27612855Sgabeblack@google.com      wait(20, SC_NS);
27712855Sgabeblack@google.com    }
27812855Sgabeblack@google.com    catch (std::exception e) {
27912855Sgabeblack@google.com      f17 = 1;
28012855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(210, SC_NS) );
28112855Sgabeblack@google.com    }
28212855Sgabeblack@google.com    wait(2, SC_NS);
28312855Sgabeblack@google.com  }
28412855Sgabeblack@google.com
28512855Sgabeblack@google.com  void grandchild13()
28612855Sgabeblack@google.com  {
28712855Sgabeblack@google.com    try {
28812855Sgabeblack@google.com      wait(20, SC_NS);
28912855Sgabeblack@google.com    }
29012855Sgabeblack@google.com    catch (std::exception e) {
29112855Sgabeblack@google.com      f18 = 1;
29212855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(210, SC_NS) );
29312855Sgabeblack@google.com    }
29412855Sgabeblack@google.com    wait(3, SC_NS);
29512855Sgabeblack@google.com  }
29612855Sgabeblack@google.com
29712855Sgabeblack@google.com  SC_HAS_PROCESS(Top);
29812855Sgabeblack@google.com};
29912855Sgabeblack@google.com
30012855Sgabeblack@google.comint sc_main(int argc, char* argv[])
30112855Sgabeblack@google.com{
30212855Sgabeblack@google.com  Top top("top");
30312855Sgabeblack@google.com
30412855Sgabeblack@google.com  sc_start();
30512855Sgabeblack@google.com
30612855Sgabeblack@google.com  sc_assert( top.f1 );
30712855Sgabeblack@google.com  sc_assert( top.f2 );
30812855Sgabeblack@google.com  sc_assert( top.f3 );
30912855Sgabeblack@google.com  sc_assert( top.f4 );
31012855Sgabeblack@google.com  sc_assert( top.f5 );
31112855Sgabeblack@google.com  sc_assert( top.f6 );
31212855Sgabeblack@google.com  sc_assert( top.f7 );
31312855Sgabeblack@google.com  sc_assert( top.f8 );
31412855Sgabeblack@google.com  sc_assert( top.f9 );
31512855Sgabeblack@google.com  sc_assert( top.f10 );
31612855Sgabeblack@google.com  sc_assert( top.f11 );
31712855Sgabeblack@google.com  sc_assert( top.f12 );
31812855Sgabeblack@google.com  sc_assert( top.f13 );
31912855Sgabeblack@google.com  sc_assert( top.f14 );
32012855Sgabeblack@google.com  sc_assert( top.f15 );
32112855Sgabeblack@google.com  sc_assert( top.f16 );
32212855Sgabeblack@google.com  sc_assert( top.f17 );
32312855Sgabeblack@google.com  sc_assert( top.f18 );
32412855Sgabeblack@google.com
32512855Sgabeblack@google.com  cout << endl << "Success" << endl;
32612855Sgabeblack@google.com  return 0;
32712855Sgabeblack@google.com}
32812855Sgabeblack@google.com
329