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
2212855Sgabeblack@google.com  simulation_callbacks.cpp -- Test of simulation phase callbacks
2312855Sgabeblack@google.com
2412855Sgabeblack@google.com  Note: requires simulation phase callback support enabled in the kernel
2512855Sgabeblack@google.com        SC_ENABLE_SIMULATION_PHASE_CALLBACKS / --enable-phase-callbacks
2612855Sgabeblack@google.com
2712855Sgabeblack@google.com  Original Author: Philipp A. Hartmann, OFFIS, 2013-05-17
2812855Sgabeblack@google.com
2912855Sgabeblack@google.com *****************************************************************************/
3012855Sgabeblack@google.com
3112855Sgabeblack@google.com/*****************************************************************************
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3412855Sgabeblack@google.com  changes you are making here.
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com      Name, Affiliation, Date:
3712855Sgabeblack@google.com  Description of Modification:
3812855Sgabeblack@google.com
3912855Sgabeblack@google.com *****************************************************************************/
4012855Sgabeblack@google.com
4112855Sgabeblack@google.com#include <systemc.h>
4212855Sgabeblack@google.com
4312855Sgabeblack@google.com#ifdef BENCHMARK
4412855Sgabeblack@google.com#  include "rusage_timer.h"
4512855Sgabeblack@google.com#  define  ROUNDS 20
4612855Sgabeblack@google.com#  define  NUM_TIMED_TRIGGERS 2000000
4712855Sgabeblack@google.com#  define  NUM_DELTA_TRIGGERS 50
4812855Sgabeblack@google.com#  define  VERBOSE 0
4912855Sgabeblack@google.com#else
5012855Sgabeblack@google.com#  define  ROUNDS 2
5112855Sgabeblack@google.com#  define  NUM_TIMED_TRIGGERS 10
5212855Sgabeblack@google.com#  define  NUM_DELTA_TRIGGERS 1
5312855Sgabeblack@google.com#  define  VERBOSE 1
5412855Sgabeblack@google.com#endif
5512855Sgabeblack@google.com
5612855Sgabeblack@google.com#ifndef REGISTER_CALLBACKS
5712855Sgabeblack@google.com#  define REGISTER_CALLBACKS 1
5812855Sgabeblack@google.com#endif
5912855Sgabeblack@google.com#ifndef EXTRA_METHOD
6012855Sgabeblack@google.com#  define EXTRA_METHOD 0
6112855Sgabeblack@google.com#endif
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com#define TIMED_THREAD 1
6412855Sgabeblack@google.com
6512855Sgabeblack@google.com#if TIMED_THREAD
6612855Sgabeblack@google.com#  define TIMED_WAIT    wait
6712855Sgabeblack@google.com#  define TIMED_PROCESS SC_THREAD
6812855Sgabeblack@google.com#else
6912855Sgabeblack@google.com#  define TIMED_WAIT    next_trigger
7012855Sgabeblack@google.com#  define TIMED_PROCESS SC_METHOD
7112855Sgabeblack@google.com#endif
7212855Sgabeblack@google.com
7312855Sgabeblack@google.com#if REGISTER_CALLBACKS
7412855Sgabeblack@google.com//#  define CALLBACK_MASK ( SC_END_OF_EVALUATION )
7512855Sgabeblack@google.com//#  define CALLBACK_MASK ( SC_END_OF_UPDATE )
7612855Sgabeblack@google.com//#  define CALLBACK_MASK ( SC_BEFORE_TIMESTEP )
7712855Sgabeblack@google.com#  define CALLBACK_MASK ( SC_END_OF_UPDATE | SC_BEFORE_TIMESTEP )
7812855Sgabeblack@google.com#else
7912855Sgabeblack@google.com// SC_RUNNING (for EXTRA_METHOD)
8012855Sgabeblack@google.com#  define CALLBACK_MASK ( SC_RUNNING )
8112855Sgabeblack@google.com#endif
8212855Sgabeblack@google.com
8312855Sgabeblack@google.comstatic const sc_dt::uint64 max_rounds         = ROUNDS;
8412855Sgabeblack@google.comstatic const sc_dt::uint64 max_timed_triggers = NUM_TIMED_TRIGGERS;
8512855Sgabeblack@google.comstatic const sc_dt::uint64 max_delta_triggers = NUM_DELTA_TRIGGERS;
8612855Sgabeblack@google.comstatic const sc_time  delay(1, SC_NS);
8712855Sgabeblack@google.com
8812855Sgabeblack@google.comSC_MODULE(phase_tracer)
8912855Sgabeblack@google.com{
9012855Sgabeblack@google.com  SC_HAS_PROCESS(phase_tracer);
9112855Sgabeblack@google.com  phase_tracer( sc_module_name = sc_core::sc_gen_unique_name("phase_tracer") )
9212855Sgabeblack@google.com    : cb_mask(CALLBACK_MASK), cb_count(0)
9312855Sgabeblack@google.com  {
9412855Sgabeblack@google.com#if REGISTER_CALLBACKS
9512855Sgabeblack@google.com    cb_mask = register_simulation_phase_callback( CALLBACK_MASK );
9612855Sgabeblack@google.com#endif
9712855Sgabeblack@google.com  }
9812855Sgabeblack@google.com
9912855Sgabeblack@google.com  virtual void simulation_phase_callback()
10012855Sgabeblack@google.com  {
10112855Sgabeblack@google.com    cb_count++;
10212855Sgabeblack@google.com
10312855Sgabeblack@google.com#   if VERBOSE
10412855Sgabeblack@google.com    {
10512855Sgabeblack@google.com      std::string ttp;
10612855Sgabeblack@google.com      if( !sc_pending_activity() ) {
10712855Sgabeblack@google.com        ttp = "MAX";
10812855Sgabeblack@google.com      } else {
10912855Sgabeblack@google.com        ttp = sc_time_to_pending_activity().to_string();
11012855Sgabeblack@google.com      }
11112855Sgabeblack@google.com      std::cout << name()
11212855Sgabeblack@google.com                << ": phase callback "
11312855Sgabeblack@google.com                << sc_get_status()
11412855Sgabeblack@google.com                << ": " << sc_time_stamp()
11512855Sgabeblack@google.com                << " -> pending activity: " << ttp
11612855Sgabeblack@google.com                << std::endl;
11712855Sgabeblack@google.com    }
11812855Sgabeblack@google.com#   endif
11912855Sgabeblack@google.com    sc_assert( cb_mask & sc_get_status() );
12012855Sgabeblack@google.com  }
12112855Sgabeblack@google.com
12212855Sgabeblack@google.com  ~phase_tracer()
12312855Sgabeblack@google.com      { print_static_phase_stats( "[destructor]" ); }
12412855Sgabeblack@google.com
12512855Sgabeblack@google.com  void print_static_phase_stats( const char* phase )
12612855Sgabeblack@google.com  {
12712855Sgabeblack@google.com#if VERBOSE
12812855Sgabeblack@google.com      std::cout << name()
12912855Sgabeblack@google.com                << ": " << phase << ": "
13012855Sgabeblack@google.com                << cb_count << " callbacks called."
13112855Sgabeblack@google.com                << std::endl;
13212855Sgabeblack@google.com#endif
13312855Sgabeblack@google.com  }
13412855Sgabeblack@google.com
13512855Sgabeblack@google.comprivate:
13612855Sgabeblack@google.com  unsigned      cb_mask;
13712855Sgabeblack@google.com  sc_dt::uint64 cb_count;
13812855Sgabeblack@google.com};
13912855Sgabeblack@google.com
14012855Sgabeblack@google.comSC_MODULE(activities)
14112855Sgabeblack@google.com{
14212855Sgabeblack@google.com  SC_CTOR(activities)
14312855Sgabeblack@google.com    : timed_count(), delta_count()
14412855Sgabeblack@google.com  {
14512855Sgabeblack@google.com    TIMED_PROCESS(timed);
14612855Sgabeblack@google.com      sensitive << timed_ev;
14712855Sgabeblack@google.com      dont_initialize();
14812855Sgabeblack@google.com    SC_METHOD(delta);
14912855Sgabeblack@google.com      sensitive << delta_ev;
15012855Sgabeblack@google.com      dont_initialize();
15112855Sgabeblack@google.com#if EXTRA_METHOD
15212855Sgabeblack@google.com    SC_METHOD(extra);
15312855Sgabeblack@google.com      sensitive << timed_ev;
15412855Sgabeblack@google.com      dont_initialize();
15512855Sgabeblack@google.com#endif
15612855Sgabeblack@google.com  }
15712855Sgabeblack@google.com
15812855Sgabeblack@google.com  void notify_round()
15912855Sgabeblack@google.com    { timed_ev.notify(SC_ZERO_TIME); }
16012855Sgabeblack@google.com
16112855Sgabeblack@google.comprivate:
16212855Sgabeblack@google.com  void timed()
16312855Sgabeblack@google.com  {
16412855Sgabeblack@google.com#   if TIMED_THREAD
16512855Sgabeblack@google.com    while(1)
16612855Sgabeblack@google.com#   endif
16712855Sgabeblack@google.com    {
16812855Sgabeblack@google.com      if( timed_count >= max_timed_triggers ) {
16912855Sgabeblack@google.com        timed_count = 0;
17012855Sgabeblack@google.com        TIMED_WAIT();
17112855Sgabeblack@google.com      } else {
17212855Sgabeblack@google.com        verbose();
17312855Sgabeblack@google.com        ++timed_count;
17412855Sgabeblack@google.com        if( max_delta_triggers )
17512855Sgabeblack@google.com          delta_ev.notify(SC_ZERO_TIME);
17612855Sgabeblack@google.com        timed_ev.notify(delay);
17712855Sgabeblack@google.com        TIMED_WAIT();
17812855Sgabeblack@google.com      }
17912855Sgabeblack@google.com    }
18012855Sgabeblack@google.com  }
18112855Sgabeblack@google.com
18212855Sgabeblack@google.com  void delta()
18312855Sgabeblack@google.com  {
18412855Sgabeblack@google.com    ++delta_count;
18512855Sgabeblack@google.com    verbose();
18612855Sgabeblack@google.com    if( delta_count >= max_delta_triggers ) {
18712855Sgabeblack@google.com      delta_count = 0;
18812855Sgabeblack@google.com    } else {
18912855Sgabeblack@google.com      delta_ev.notify(SC_ZERO_TIME);
19012855Sgabeblack@google.com    }
19112855Sgabeblack@google.com  }
19212855Sgabeblack@google.com
19312855Sgabeblack@google.com  void extra()
19412855Sgabeblack@google.com  {
19512855Sgabeblack@google.com    if( sc_pending_activity_at_current_time() ) {
19612855Sgabeblack@google.com      pt.simulation_phase_callback();
19712855Sgabeblack@google.com      next_trigger(SC_ZERO_TIME);
19812855Sgabeblack@google.com    } else if (sc_time_to_pending_activity()== sc_max_time()-sc_time_stamp() ) {
19912855Sgabeblack@google.com      next_trigger();
20012855Sgabeblack@google.com    } else {
20112855Sgabeblack@google.com      pt.simulation_phase_callback();
20212855Sgabeblack@google.com      next_trigger(sc_time_to_pending_activity());
20312855Sgabeblack@google.com    }
20412855Sgabeblack@google.com  }
20512855Sgabeblack@google.com
20612855Sgabeblack@google.com  void verbose()
20712855Sgabeblack@google.com  {
20812855Sgabeblack@google.com#if VERBOSE
20912855Sgabeblack@google.com    std::cout
21012855Sgabeblack@google.com      << sc_get_current_process_handle().name()
21112855Sgabeblack@google.com      << ": " << sc_time_stamp()
21212855Sgabeblack@google.com      << ": " << timed_count << "/" << delta_count
21312855Sgabeblack@google.com      << std::endl;
21412855Sgabeblack@google.com#endif
21512855Sgabeblack@google.com  }
21612855Sgabeblack@google.com
21712855Sgabeblack@google.comprivate:
21812855Sgabeblack@google.com  phase_tracer pt;
21912855Sgabeblack@google.com  sc_dt::uint64 timed_count, delta_count;
22012855Sgabeblack@google.com  sc_event timed_ev, delta_ev;
22112855Sgabeblack@google.com};
22212855Sgabeblack@google.com
22312855Sgabeblack@google.com
22412855Sgabeblack@google.comint sc_main(int, char*[])
22512855Sgabeblack@google.com{
22612855Sgabeblack@google.com  activities   top("top");
22712855Sgabeblack@google.com
22812855Sgabeblack@google.com  sc_start(SC_ZERO_TIME);
22912855Sgabeblack@google.com  for(unsigned i=0; i<max_rounds; ++i)
23012855Sgabeblack@google.com  {
23112855Sgabeblack@google.com#ifdef BENCHMARK
23212855Sgabeblack@google.com    rusage_timer timer;
23312855Sgabeblack@google.com#endif
23412855Sgabeblack@google.com    top.notify_round();
23512855Sgabeblack@google.com    sc_start();
23612855Sgabeblack@google.com#ifdef BENCHMARK
23712855Sgabeblack@google.com    std::cout << timer.to_seconds() << std::endl;
23812855Sgabeblack@google.com#endif
23912855Sgabeblack@google.com  }
24012855Sgabeblack@google.com  sc_stop();
24112855Sgabeblack@google.com  return 0;
24212855Sgabeblack@google.com}
243