SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_phase_callback_registry.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements. See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License. You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied. See the License for the specific language governing
16  permissions and limitations under the License.
17 
18  *****************************************************************************/
19 
20 /*****************************************************************************
21 
22  sc_phase_callback_registry.h -- Definition of the simulation phase callbacks
23 */
37 #ifndef SC_PHASE_CALLBACK_REGISTRY_H_INCLUDED_
38 #define SC_PHASE_CALLBACK_REGISTRY_H_INCLUDED_
39 
40 #if defined( SC_ENABLE_SIMULATION_PHASE_CALLBACKS ) \
41  || defined( SC_ENABLE_SIMULATION_PHASE_CALLBACKS_TRACING )
42 # define SC_HAS_PHASE_CALLBACKS_ 1
43 #else
44 # define SC_HAS_PHASE_CALLBACKS_ 0
45 #endif
46 
49 #include "sysc/kernel/sc_status.h"
50 
51 #include <vector>
52 
53 namespace sc_core {
54 
55 class sc_simcontext;
56 class sc_object;
57 
59 {
60 public:
62  typedef sc_object cb_type;
64 
65  struct entry
66  {
67  cb_type* target;
68  mask_type mask;
69  };
70 
71  friend class sc_simcontext;
72  friend class sc_object;
73 
74 private: // interface completely internal
75 
76  explicit
78 
80 
81  // --- callback forwarders
82 
83  bool construction_done() const; //< returns false
84  void elaboration_done() const;
85  void initialization_done() const;
86  void start_simulation() const;
87 
88  void evaluation_done() const;
89  void update_done() const;
90  void before_timestep() const;
91 
92  void simulation_paused() const;
93  void simulation_stopped() const;
94  void simulation_done() const;
95 
96 
97  // --- callback registration and implementation
98 
99  mask_type register_callback( cb_type&, mask_type mask );
100  mask_type unregister_callback( cb_type&, mask_type mask );
101 
102  // generic caller
103  void do_callback( sc_status ) const;
104 
105 private:
106  typedef std::vector<entry> storage_type;
107  typedef std::vector<cb_type*> single_storage_type;
108 
109 #if SC_HAS_PHASE_CALLBACKS_
110 
111  // set and restore simulation status
112  struct scoped_status
113  {
114  scoped_status( sc_status& ref, sc_status s )
115  : ref_(ref), prev_(ref) { ref_ = s;}
116  ~scoped_status() { ref_ = prev_; }
117  private:
118  sc_status& ref_;
119  sc_status prev_;
120  }; // scoped_status
121 
122  mask_type validate_mask( cb_type&, mask_type, bool warn );
123 
124 private:
125 
126  sc_simcontext* m_simc;
127  storage_type m_cb_vec; // all callbacks
128 #if 0
129  single_storage_type m_cb_eval_vec; // - eval cb shortcut
130 #endif
131  single_storage_type m_cb_update_vec; // - update cb shortcut
132  single_storage_type m_cb_timestep_vec; // - timestep cb shortcut
133 
134 #endif // SC_HAS_PHASE_CALLBACKS_
135 
136 private:
137  // disabled
138  sc_phase_callback_registry( const this_type& );
139  this_type& operator=(const this_type&);
140 
141 }; // sc_phase_callback_registry
142 
143 
144 // -------------------- callback implementations --------------------
145 // (empty, if feature is disabled)
146 
147 inline bool
148 sc_phase_callback_registry::construction_done() const
149 {
150 #if SC_HAS_PHASE_CALLBACKS_
151  do_callback( SC_BEFORE_END_OF_ELABORATION );
152 #endif
153  return false;
154 }
155 
156 inline void
157 sc_phase_callback_registry::elaboration_done() const
158 {
159 #if SC_HAS_PHASE_CALLBACKS_
160  do_callback( SC_END_OF_ELABORATION );
161 #endif
162 }
163 
164 inline void
165 sc_phase_callback_registry::start_simulation() const
166 {
167 #if SC_HAS_PHASE_CALLBACKS_
168  do_callback( SC_START_OF_SIMULATION );
169 #endif
170 }
171 
172 inline void
173 sc_phase_callback_registry::initialization_done() const
174 {
175 #if SC_HAS_PHASE_CALLBACKS_
176  scoped_status scope( m_simc->m_simulation_status
178 
179  do_callback( SC_END_OF_INITIALIZATION );
180 #endif
181 }
182 
183 inline void
184 sc_phase_callback_registry::simulation_paused() const
185 {
186 #if SC_HAS_PHASE_CALLBACKS_
187  do_callback( SC_PAUSED );
188 #endif
189 }
190 
191 inline void
192 sc_phase_callback_registry::simulation_stopped() const
193 {
194 #if SC_HAS_PHASE_CALLBACKS_
195  do_callback( SC_STOPPED );
196 #endif
197 }
198 
199 inline void
200 sc_phase_callback_registry::simulation_done() const
201 {
202 #if SC_HAS_PHASE_CALLBACKS_
203  do_callback( SC_END_OF_SIMULATION );
204 #endif
205 }
206 
207 // -------------- specialized callback implementations --------------
208 
209 #if 0
210 inline void
211 sc_phase_callback_registry::evaluation_done() const
212 {
213 #if SC_HAS_PHASE_CALLBACKS_
214 
215  if( !m_cb_eval_vec.size() ) return;
216 
217  typedef single_storage_type::const_iterator it_type;
218  single_storage_type const & vec = m_cb_eval_vec;
219 
220  scoped_status scope( m_simc->m_simulation_status
221  , SC_END_OF_EVALUATION );
222 
223  for(it_type it = vec.begin(), end = vec.end(); it != end; ++it)
224  (*it)->do_simulation_phase_callback();
225 #endif
226 }
227 #endif
228 
229 inline void
230 sc_phase_callback_registry::update_done() const
231 {
232 #if SC_HAS_PHASE_CALLBACKS_
233 
234  if( !m_cb_update_vec.size() ) return;
235 
236  typedef single_storage_type::const_iterator it_type;
237  single_storage_type const & vec = m_cb_update_vec;
238 
239  scoped_status scope( m_simc->m_simulation_status
240  , SC_END_OF_UPDATE );
241 
242  for(it_type it = vec.begin(), end = vec.end(); it != end; ++it)
243  (*it)->do_simulation_phase_callback();
244 #endif
245 }
246 
247 inline void
248 sc_phase_callback_registry::before_timestep() const
249 {
250 #if SC_HAS_PHASE_CALLBACKS_
251 
252  if( !m_cb_timestep_vec.size() ) return;
253 
254  typedef single_storage_type::const_iterator it_type;
255  single_storage_type const & vec = m_cb_timestep_vec;
256 
257  scoped_status scope( m_simc->m_simulation_status
258  , SC_BEFORE_TIMESTEP );
259 
260  for(it_type it = vec.begin(), end = vec.end(); it != end; ++it)
261  (*it)->do_simulation_phase_callback();
262 #endif
263 }
264 
265 } // namespace sc_core
266 
267 /*****************************************************************************
268 
269  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
270  changes you are making here.
271 
272  Name, Affiliation, Date:
273  Description of Modification:
274 
275  *****************************************************************************/
276 #endif /* SC_PHASE_CALLBACK_REGISTRY_H_INCLUDED_ */
277 // Taf!
278 
Abstract base class of all SystemC `simulation&#39; objects.
Definition: sc_object.h:61
Definition of the simulation phases.
For inline definitions of some utility functions.
unsigned phase_cb_mask
Definition: sc_object.h:75
during start_of_simulation()
Definition: sc_status.h:55
when scheduler stopped by sc_stop()
Definition: sc_status.h:59
after update/notify phase
Definition: sc_status.h:65
during before_end_of_elaboration()
Definition: sc_status.h:53
The simulation context.
during end_of_simulation()
Definition: sc_status.h:60
after initialization
Definition: sc_status.h:63
when scheduler stopped by sc_pause()
Definition: sc_status.h:58
Definition of the simulation context class.
during end_of_elaboration()
Definition: sc_status.h:54
before next time step
Definition: sc_status.h:66
class SC_API sc_simcontext
Definition: sc_object.h:51