summaryrefslogtreecommitdiff
path: root/src/systemc/tests/systemc/kernel/phase_callbacks/test05/simulation_callbacks.cpp
blob: 03dde6ccc23317340800f680b2df639a299a2e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*****************************************************************************

  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
  more contributor license agreements.  See the NOTICE file distributed
  with this work for additional information regarding copyright ownership.
  Accellera licenses this file to you under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with the
  License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.  See the License for the specific language governing
  permissions and limitations under the License.

 *****************************************************************************/

/*****************************************************************************

  simulation_callbacks.cpp -- Test of simulation phase callbacks

  Note: requires simulation phase callback support enabled in the kernel
        SC_ENABLE_SIMULATION_PHASE_CALLBACKS / --enable-phase-callbacks

  Original Author: Philipp A. Hartmann, OFFIS, 2013-05-17

 *****************************************************************************/

/*****************************************************************************

  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
  changes you are making here.

      Name, Affiliation, Date:
  Description of Modification:

 *****************************************************************************/

#include <systemc.h>

#ifdef BENCHMARK
#  include "rusage_timer.h"
#  define  ROUNDS 20
#  define  NUM_TIMED_TRIGGERS 2000000
#  define  NUM_DELTA_TRIGGERS 50
#  define  VERBOSE 0
#else
#  define  ROUNDS 2
#  define  NUM_TIMED_TRIGGERS 10
#  define  NUM_DELTA_TRIGGERS 1
#  define  VERBOSE 1
#endif

#ifndef REGISTER_CALLBACKS
#  define REGISTER_CALLBACKS 1
#endif
#ifndef EXTRA_METHOD
#  define EXTRA_METHOD 0
#endif

#define TIMED_THREAD 1

#if TIMED_THREAD
#  define TIMED_WAIT    wait
#  define TIMED_PROCESS SC_THREAD
#else
#  define TIMED_WAIT    next_trigger
#  define TIMED_PROCESS SC_METHOD
#endif

#if REGISTER_CALLBACKS
//#  define CALLBACK_MASK ( SC_END_OF_EVALUATION )
//#  define CALLBACK_MASK ( SC_END_OF_UPDATE )
//#  define CALLBACK_MASK ( SC_BEFORE_TIMESTEP )
#  define CALLBACK_MASK ( SC_END_OF_UPDATE | SC_BEFORE_TIMESTEP )
#else
// SC_RUNNING (for EXTRA_METHOD)
#  define CALLBACK_MASK ( SC_RUNNING )
#endif

static const sc_dt::uint64 max_rounds         = ROUNDS;
static const sc_dt::uint64 max_timed_triggers = NUM_TIMED_TRIGGERS;
static const sc_dt::uint64 max_delta_triggers = NUM_DELTA_TRIGGERS;
static const sc_time  delay(1, SC_NS);

SC_MODULE(phase_tracer)
{
  SC_HAS_PROCESS(phase_tracer);
  phase_tracer( sc_module_name = sc_core::sc_gen_unique_name("phase_tracer") )
    : cb_mask(CALLBACK_MASK), cb_count(0)
  {
#if REGISTER_CALLBACKS
    cb_mask = register_simulation_phase_callback( CALLBACK_MASK );
#endif
  }

  virtual void simulation_phase_callback()
  {
    cb_count++;

#   if VERBOSE
    {
      std::string ttp;
      if( !sc_pending_activity() ) {
        ttp = "MAX";
      } else {
        ttp = sc_time_to_pending_activity().to_string();
      }
      std::cout << name()
                << ": phase callback "
                << sc_get_status()
                << ": " << sc_time_stamp()
                << " -> pending activity: " << ttp
                << std::endl;
    }
#   endif
    sc_assert( cb_mask & sc_get_status() );
  }

  ~phase_tracer()
      { print_static_phase_stats( "[destructor]" ); }

  void print_static_phase_stats( const char* phase )
  {
#if VERBOSE
      std::cout << name()
                << ": " << phase << ": "
                << cb_count << " callbacks called."
                << std::endl;
#endif
  }

private:
  unsigned      cb_mask;
  sc_dt::uint64 cb_count;
};

SC_MODULE(activities)
{
  SC_CTOR(activities)
    : timed_count(), delta_count()
  {
    TIMED_PROCESS(timed);
      sensitive << timed_ev;
      dont_initialize();
    SC_METHOD(delta);
      sensitive << delta_ev;
      dont_initialize();
#if EXTRA_METHOD
    SC_METHOD(extra);
      sensitive << timed_ev;
      dont_initialize();
#endif
  }

  void notify_round()
    { timed_ev.notify(SC_ZERO_TIME); }

private:
  void timed()
  {
#   if TIMED_THREAD
    while(1)
#   endif
    {
      if( timed_count >= max_timed_triggers ) {
        timed_count = 0;
        TIMED_WAIT();
      } else {
        verbose();
        ++timed_count;
        if( max_delta_triggers )
          delta_ev.notify(SC_ZERO_TIME);
        timed_ev.notify(delay);
        TIMED_WAIT();
      }
    }
  }

  void delta()
  {
    ++delta_count;
    verbose();
    if( delta_count >= max_delta_triggers ) {
      delta_count = 0;
    } else {
      delta_ev.notify(SC_ZERO_TIME);
    }
  }

  void extra()
  {
    if( sc_pending_activity_at_current_time() ) {
      pt.simulation_phase_callback();
      next_trigger(SC_ZERO_TIME);
    } else if (sc_time_to_pending_activity()== sc_max_time()-sc_time_stamp() ) {
      next_trigger();
    } else {
      pt.simulation_phase_callback();
      next_trigger(sc_time_to_pending_activity());
    }
  }

  void verbose()
  {
#if VERBOSE
    std::cout
      << sc_get_current_process_handle().name()
      << ": " << sc_time_stamp()
      << ": " << timed_count << "/" << delta_count
      << std::endl;
#endif
  }

private:
  phase_tracer pt;
  sc_dt::uint64 timed_count, delta_count;
  sc_event timed_ev, delta_ev;
};


int sc_main(int, char*[])
{
  activities   top("top");

  sc_start(SC_ZERO_TIME);
  for(unsigned i=0; i<max_rounds; ++i)
  {
#ifdef BENCHMARK
    rusage_timer timer;
#endif
    top.notify_round();
    sc_start();
#ifdef BENCHMARK
    std::cout << timer.to_seconds() << std::endl;
#endif
  }
  sc_stop();
  return 0;
}