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
2112855Sgabeblack@google.com  test07.cpp -- Test handling of process objects with living descendants
2212855Sgabeblack@google.com
2312855Sgabeblack@google.com  Original Author: Philipp A. Hartmann, OFFIS
2412855Sgabeblack@google.com
2512855Sgabeblack@google.com *****************************************************************************/
2612855Sgabeblack@google.com/*****************************************************************************
2712855Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
2812855Sgabeblack@google.com  changes you are making here.
2912855Sgabeblack@google.com
3012855Sgabeblack@google.com      Name, Affiliation, Date:
3112855Sgabeblack@google.com  Description of Modification:
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com *****************************************************************************/
3412855Sgabeblack@google.com// $Log: test07.cpp,v $
3512855Sgabeblack@google.com// Revision 1.2  2011/02/14 17:00:00  acg
3612855Sgabeblack@google.com//  Andy Goodrich: updated copyright and added cvs logging information inline.
3712855Sgabeblack@google.com//
3812855Sgabeblack@google.com
3912855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES
4012855Sgabeblack@google.com#include <systemc.h>
4112855Sgabeblack@google.com
4212855Sgabeblack@google.comvoid
4312855Sgabeblack@google.comdump_hierarchy(
4412855Sgabeblack@google.com    std::vector< sc_object* > const & objs = sc_get_top_level_objects()
4512855Sgabeblack@google.com  , unsigned level = 0
4612855Sgabeblack@google.com)
4712855Sgabeblack@google.com{
4812855Sgabeblack@google.com  if (!level)
4912855Sgabeblack@google.com    std::cout << "------ " << "(" << sc_time_stamp() << ")" << " ------"
5012855Sgabeblack@google.com      << std::endl;
5112855Sgabeblack@google.com
5212855Sgabeblack@google.com  std::vector<sc_object*>::const_iterator it = objs.begin();
5312855Sgabeblack@google.com  for( ; it != objs.end(); it++ )
5412855Sgabeblack@google.com  {
5512855Sgabeblack@google.com    std::cout << std::string( level + 1, ' ' )
5612855Sgabeblack@google.com              << (*it)->name() << " (" << (*it)->kind() << ")";
5712855Sgabeblack@google.com
5812855Sgabeblack@google.com    sc_process_handle h(*it);
5912855Sgabeblack@google.com
6012855Sgabeblack@google.com    std::cout << ( h.valid() // is it a process? -> print state
6112855Sgabeblack@google.com                 ? (! h.terminated() ? " (running)" : " (terminated)"  )
6212855Sgabeblack@google.com                 : "" )
6312855Sgabeblack@google.com              << std::endl;
6412855Sgabeblack@google.com
6512855Sgabeblack@google.com    dump_hierarchy( (*it)->get_child_objects(), level+1 );
6612855Sgabeblack@google.com  }
6712855Sgabeblack@google.com
6812855Sgabeblack@google.com  if (!level)
6912855Sgabeblack@google.com    std::cout << "---------------------- " << std::endl;
7012855Sgabeblack@google.com}
7112855Sgabeblack@google.com
7212855Sgabeblack@google.comstruct my_object : sc_object {
7312855Sgabeblack@google.com  my_object( const char* name ) : sc_object( name ) {}
7412855Sgabeblack@google.com  ~my_object()
7512855Sgabeblack@google.com  {
7612855Sgabeblack@google.com    std::cout << "+++ " << this->name() << " deleted" << std::endl;
7712855Sgabeblack@google.com  }
7812855Sgabeblack@google.com};
7912855Sgabeblack@google.com
8012855Sgabeblack@google.comSC_MODULE(DUT)
8112855Sgabeblack@google.com{
8212855Sgabeblack@google.com  SC_CTOR(DUT)
8312855Sgabeblack@google.com    : leaf_(0)
8412855Sgabeblack@google.com  {
8512855Sgabeblack@google.com    SC_THREAD(parent);
8612855Sgabeblack@google.com  }
8712855Sgabeblack@google.com
8812855Sgabeblack@google.com  enum start_options
8912855Sgabeblack@google.com  {
9012855Sgabeblack@google.com    no_children      = 0,
9112855Sgabeblack@google.com    start_child_proc = 1,
9212855Sgabeblack@google.com    create_child_obj = 2,
9312855Sgabeblack@google.com    both_children    = 3
9412855Sgabeblack@google.com  };
9512855Sgabeblack@google.com
9612855Sgabeblack@google.com  void child( start_options opt ){
9712855Sgabeblack@google.com    start();
9812855Sgabeblack@google.com
9912855Sgabeblack@google.com    my_object local( "local" );
10012855Sgabeblack@google.com
10112855Sgabeblack@google.com    if( opt & create_child_obj )
10212855Sgabeblack@google.com      leaf_=new my_object("dyn_obj");
10312855Sgabeblack@google.com
10412855Sgabeblack@google.com    wait( 100, SC_NS );
10512855Sgabeblack@google.com
10612855Sgabeblack@google.com    if( opt & start_child_proc )
10712855Sgabeblack@google.com      sc_spawn( sc_bind( &DUT::child, this, no_children )
10812855Sgabeblack@google.com              , "grandchild" );
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com    wait( 100, SC_NS );
11112855Sgabeblack@google.com    end();
11212855Sgabeblack@google.com  }
11312855Sgabeblack@google.com
11412855Sgabeblack@google.com  void parent()
11512855Sgabeblack@google.com  {
11612855Sgabeblack@google.com    // only parent alive
11712855Sgabeblack@google.com    wait( 50, SC_NS );
11812855Sgabeblack@google.com    dump_hierarchy();
11912855Sgabeblack@google.com
12012855Sgabeblack@google.com    sc_spawn( sc_bind( &DUT::child, this, start_child_proc ), "child0" );
12112855Sgabeblack@google.com    sc_spawn( sc_bind( &DUT::child, this, both_children ), "child1" );
12212855Sgabeblack@google.com
12312855Sgabeblack@google.com    // direct children up and running
12412855Sgabeblack@google.com    wait( 50, SC_NS );
12512855Sgabeblack@google.com    dump_hierarchy();
12612855Sgabeblack@google.com
12712855Sgabeblack@google.com    // grandchildren started, child object created
12812855Sgabeblack@google.com    wait( 100, SC_NS );
12912855Sgabeblack@google.com    dump_hierarchy();
13012855Sgabeblack@google.com
13112855Sgabeblack@google.com    // direct children ended (zombies kept)
13212855Sgabeblack@google.com    wait( 100, SC_NS );
13312855Sgabeblack@google.com    dump_hierarchy();
13412855Sgabeblack@google.com
13512855Sgabeblack@google.com    // grandhildren ended (zombie with child object kept)
13612855Sgabeblack@google.com    wait( 100, SC_NS );
13712855Sgabeblack@google.com    dump_hierarchy();
13812855Sgabeblack@google.com
13912855Sgabeblack@google.com    // child object removed, zombies deleted
14012855Sgabeblack@google.com    delete leaf_; leaf_ = 0;
14112855Sgabeblack@google.com    wait( 100, SC_NS );
14212855Sgabeblack@google.com    dump_hierarchy();
14312855Sgabeblack@google.com
14412855Sgabeblack@google.com    {
14512855Sgabeblack@google.com      // create another pair of children
14612855Sgabeblack@google.com      sc_process_handle
14712855Sgabeblack@google.com        h0 = sc_spawn( sc_bind( &DUT::child, this, start_child_proc ), "child0" ),
14812855Sgabeblack@google.com        h1 = sc_spawn( sc_bind( &DUT::child, this, start_child_proc ), "child1" );
14912855Sgabeblack@google.com
15012855Sgabeblack@google.com      wait( 100, SC_NS );
15112855Sgabeblack@google.com      dump_hierarchy();
15212855Sgabeblack@google.com
15312855Sgabeblack@google.com      // and kill them, after it has spawned its grandchild
15412855Sgabeblack@google.com      wait( 50, SC_NS );
15512855Sgabeblack@google.com      h0.kill();
15612855Sgabeblack@google.com      h1.kill( SC_INCLUDE_DESCENDANTS );
15712855Sgabeblack@google.com
15812855Sgabeblack@google.com      std::cout << "+++ kills sent ... "
15912855Sgabeblack@google.com                << "(" << sc_time_stamp() << ")"
16012855Sgabeblack@google.com                << std::endl;
16112855Sgabeblack@google.com
16212855Sgabeblack@google.com      // needed to avoid segfault
16312855Sgabeblack@google.com      //wait(SC_ZERO_TIME);
16412855Sgabeblack@google.com
16512855Sgabeblack@google.com    } // drop handles
16612855Sgabeblack@google.com
16712855Sgabeblack@google.com    wait( 50, SC_NS );
16812855Sgabeblack@google.com    dump_hierarchy();
16912855Sgabeblack@google.com
17012855Sgabeblack@google.com    end();
17112855Sgabeblack@google.com  }
17212855Sgabeblack@google.com
17312855Sgabeblack@google.com  void start()
17412855Sgabeblack@google.com  {
17512855Sgabeblack@google.com    std::cout
17612855Sgabeblack@google.com      << "+++ "
17712855Sgabeblack@google.com      << sc_get_current_process_handle().name()
17812855Sgabeblack@google.com      << " starting "
17912855Sgabeblack@google.com      << "(" << sc_time_stamp() << ")"
18012855Sgabeblack@google.com      << std::endl;
18112855Sgabeblack@google.com  }
18212855Sgabeblack@google.com  void end()
18312855Sgabeblack@google.com  {
18412855Sgabeblack@google.com    std::cout
18512855Sgabeblack@google.com      << "+++ "
18612855Sgabeblack@google.com      << sc_get_current_process_handle().name()
18712855Sgabeblack@google.com      << " exiting "
18812855Sgabeblack@google.com      << "(" << sc_time_stamp() << ")"
18912855Sgabeblack@google.com      << std::endl;
19012855Sgabeblack@google.com  }
19112855Sgabeblack@google.com
19212855Sgabeblack@google.com  my_object* leaf_;
19312855Sgabeblack@google.com};
19412855Sgabeblack@google.com
19512855Sgabeblack@google.com
19612855Sgabeblack@google.comint sc_main( int, char*[] )
19712855Sgabeblack@google.com{
19812855Sgabeblack@google.com  DUT dut("dut");
19912855Sgabeblack@google.com  sc_start(900, SC_NS );
20012855Sgabeblack@google.com  // everything cleaned up (only module still alive)
20112855Sgabeblack@google.com  dump_hierarchy();
20212855Sgabeblack@google.com
20312855Sgabeblack@google.com  return 0;
20412855Sgabeblack@google.com}
205