src/sysc/kernel/sc_simcontext.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2005 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.4 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_simcontext.h -- Definition of the simulation context class.
00021 
00022   Original Author: Stan Y. Liao, Synopsys, Inc.
00023                    Martin Janssen, Synopsys, Inc.
00024 
00025  *****************************************************************************/
00026 
00027 /*****************************************************************************
00028 
00029   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00030   changes you are making here.
00031 
00032       Name, Affiliation, Date: Andy Goodrich, Forte Design Systems 20 May 2003
00033   Description of Modification: - phase callbacks
00034                                - sc_stop mode
00035 
00036       Name, Affiliation, Date: Bishnupriya Bhattacharya, Cadence Design Systems,
00037                                25 August, 2003
00038   Description of Modification: - support for dynamic process
00039                                - support for sc export registry
00040                                - new member methods elaborate(), 
00041                                  prepare_to_simulate(), and initial_crunch()
00042                                  that are invoked by initialize() in that order
00043                                - add sc_get_last_created_process_handle() for 
00044                                  use before simulation starts
00045                                
00046       Name, Affiliation, Date: Bishnupriya Bhattacharya, Cadence Design Systems,
00047                                3 March, 2004
00048   Description of Modification: add sc_get_curr_process_kind()
00049 
00050       Name, Affiliation, Date: 
00051   Description of Modification: 
00052                                
00053  *****************************************************************************/
00054 
00055 #ifndef SC_SIMCONTEXT_H
00056 #define SC_SIMCONTEXT_H
00057 
00058 #include "sysc/kernel/sc_cmnhdr.h"
00059 #include "sysc/kernel/sc_process.h"
00060 #include "sysc/kernel/sc_time.h"
00061 #include "sysc/utils/sc_hash.h"
00062 #include "sysc/utils/sc_vector.h"
00063 #include "sysc/utils/sc_pq.h"
00064 
00065 namespace sc_core {
00066 
00067 // forward declarations
00068 
00069 class sc_cor;
00070 class sc_cor_pkg;
00071 class sc_event;
00072 class sc_event_timed;
00073 class sc_lambda_ptr;
00074 class sc_module;
00075 class sc_module_name;
00076 class sc_module_registry;
00077 class sc_name_gen;
00078 class sc_object;
00079 class sc_object_manager;
00080 class sc_port_registry;
00081 class sc_export_registry;
00082 class sc_prim_channel_registry;
00083 class sc_process_table;
00084 class sc_signal_bool_deval;
00085 class sc_trace_file;
00086 class sc_runnable;
00087 class sc_process_host;
00088 
00089 
00090 
00091 enum sc_curr_proc_kind
00092 {
00093     SC_NO_PROC_,
00094     SC_METHOD_PROC_,
00095     SC_THREAD_PROC_,
00096     SC_CTHREAD_PROC_
00097 };
00098 
00099 
00100 struct sc_curr_proc_info
00101 {
00102     sc_process_b* process_handle;
00103     sc_curr_proc_kind kind;
00104     sc_curr_proc_info() : process_handle( 0 ), kind( SC_NO_PROC_ ) {}
00105 };
00106 
00107 typedef const sc_curr_proc_info* sc_curr_proc_handle;
00108 
00109 
00110 // simulation status codes
00111 
00112 const int SC_SIM_OK        = 0;
00113 const int SC_SIM_ERROR     = 1;
00114 const int SC_SIM_USER_STOP = 2;
00115 
00116 enum sc_stop_mode {          // sc_stop modes:
00117     SC_STOP_FINISH_DELTA,
00118     SC_STOP_IMMEDIATE
00119 };
00120 extern void sc_set_stop_mode( sc_stop_mode mode );
00121 extern sc_stop_mode sc_get_stop_mode();
00122 
00123 
00124 // ----------------------------------------------------------------------------
00125 //  CLASS : sc_simcontext
00126 //
00127 //  The simulation context.
00128 // ----------------------------------------------------------------------------
00129 
00130 class sc_simcontext
00131 {
00132     friend class sc_event;
00133     friend class sc_module;
00134     friend class sc_object;
00135     friend class sc_time;
00136     friend class sc_clock;
00137 
00138     friend bool sc_end_of_simulation_invoked();
00139     friend bool sc_start_of_simulation_invoked();
00140 
00141     void init();
00142     void clean();
00143 
00144 public:
00145 
00146     sc_simcontext();
00147     ~sc_simcontext();
00148 
00149     void initialize( bool = false );
00150     void cycle( const sc_time& );
00151     void simulate( const sc_time& duration );
00152     void stop();
00153     void end();
00154     void reset();
00155 
00156     int sim_status() const;
00157         bool elaboration_done() const;
00158 
00159     sc_object_manager* get_object_manager();
00160 
00161     void hierarchy_push( sc_module* );
00162     sc_module* hierarchy_pop();
00163     sc_module* hierarchy_curr() const;
00164     sc_object* first_object();
00165     sc_object* next_object();
00166     sc_object* find_object( const char* name );
00167 
00168     sc_module_registry* get_module_registry();
00169     sc_port_registry* get_port_registry();
00170     sc_export_registry* get_export_registry();
00171     sc_prim_channel_registry* get_prim_channel_registry();
00172 
00173     // to generate unique names for objects in an MT-Safe way
00174     const char* gen_unique_name( const char* basename_, 
00175                                  bool preserve_first = false 
00176                                );
00177 
00178     sc_method_handle register_method_process( const char* name,
00179                                               SC_ENTRY_FUNC fn,
00180                                               sc_module* );
00181     sc_method_handle create_dynamic_method_process( const char* name,
00182                                                     SC_ENTRY_FUNC fn,
00183                                                     sc_process_host*, 
00184                                                     bool dont_initialize = false);
00185     sc_thread_handle register_thread_process( const char* name,
00186                                               SC_ENTRY_FUNC fn,
00187                                               sc_module* );
00188     sc_thread_handle create_dynamic_thread_process( const char* name,
00189                                                     SC_ENTRY_FUNC fn,
00190                                                     sc_process_host*,
00191                                                     size_t size = 0,
00192                                                     bool dont_initialize = false );
00193     sc_cthread_handle register_cthread_process( const char* name,
00194                                                 SC_ENTRY_FUNC fn,
00195                                                 sc_module* );
00196 
00197     sc_curr_proc_handle get_curr_proc_info();
00198     void set_curr_proc( sc_method_handle );
00199     void set_curr_proc( sc_thread_handle );
00200     void reset_curr_proc();
00201 
00202     int next_proc_id();
00203 
00204     void add_trace_file( sc_trace_file* );
00205 
00206     friend void    sc_set_time_resolution( double, sc_time_unit );
00207     friend sc_time sc_get_time_resolution();
00208     friend void    sc_set_default_time_unit( double, sc_time_unit );
00209     friend sc_time sc_get_default_time_unit();
00210 
00211     const sc_time& time_stamp() const;
00212 
00213     sc_dt::uint64 delta_count() const;
00214     bool is_running() const;
00215     bool update_phase() const;
00216     bool get_error();
00217     void set_error();
00218 
00219     sc_cor_pkg* cor_pkg()
00220         { return m_cor_pkg; }
00221     sc_cor* next_cor();
00222 
00223     const ::std::vector<sc_object*>& get_child_objects() const;
00224 
00225     void elaborate();
00226     void prepare_to_simulate();
00227     inline void initial_crunch( bool no_crunch );
00228 
00229 private:
00230 
00231     void add_child_object( sc_object* );
00232     void remove_child_object( sc_object* );
00233 
00234     void crunch();
00235 
00236     const sc_time next_time(); 
00237 
00238     int add_delta_event( sc_event* );
00239     void remove_delta_event( sc_event* );
00240     void add_timed_event( sc_event_timed* );
00241 
00242     void trace_cycle( bool delta_cycle );
00243 
00244     void push_runnable_method( sc_method_handle );
00245     void push_runnable_thread( sc_thread_handle );
00246 
00247     void push_runnable_method_front( sc_method_handle );
00248     void push_runnable_thread_front( sc_thread_handle );
00249 
00250     sc_method_handle pop_runnable_method();
00251     sc_thread_handle pop_runnable_thread();
00252 
00253     void remove_runnable_method( sc_method_handle );
00254     void remove_runnable_thread( sc_thread_handle );
00255 
00256     void do_sc_stop_action();
00257 
00258     friend void watching(const sc_lambda_ptr&, sc_simcontext*);
00259     friend void watching(const sc_signal_bool_deval&, sc_simcontext*);
00260 
00261 private:
00262 
00263     sc_object_manager*         m_object_manager;
00264 
00265     sc_module_registry*        m_module_registry;
00266     sc_port_registry*          m_port_registry;
00267     sc_export_registry*        m_export_registry;
00268     sc_prim_channel_registry*  m_prim_channel_registry;
00269 
00270     sc_name_gen*               m_name_gen;
00271 
00272     sc_process_table*          m_process_table;
00273     sc_curr_proc_info          m_curr_proc_info;
00274     int                        m_next_proc_id;
00275 
00276     sc_pvector<sc_object*>     m_child_objects;
00277 
00278     sc_pvector<sc_event*>      m_delta_events;
00279     sc_ppq<sc_event_timed*>*   m_timed_events;
00280 
00281     sc_pvector<sc_trace_file*> m_trace_files;
00282     bool                       m_something_to_trace;
00283 
00284     sc_runnable*               m_runnable;
00285 
00286     sc_time_params*            m_time_params;
00287     sc_time                    m_curr_time;
00288 
00289     sc_dt::uint64              m_delta_count;
00290     bool                       m_forced_stop;
00291     bool                       m_ready_to_simulate;
00292     bool                       m_elaboration_done;
00293     bool                       m_update_phase;
00294     bool                       m_error;
00295     bool                       m_in_simulator_control;   
00296     bool                       m_end_of_simulation_called;
00297     bool                       m_start_of_simulation_called;
00298 
00299 
00300     sc_event*                  m_until_event;
00301 
00302     sc_cor_pkg*                m_cor_pkg; // the simcontext's coroutine package
00303     sc_cor*                    m_cor;     // the simcontext's coroutine
00304 
00305     void (*m_watching_fn)( const sc_lambda_ptr&, sc_simcontext* );
00306 
00307 private:
00308 
00309     // disabled
00310     sc_simcontext( const sc_simcontext& );
00311     sc_simcontext& operator = ( const sc_simcontext& );
00312 };
00313 
00314 
00315 // IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
00316 
00317 inline
00318 bool
00319 sc_simcontext::elaboration_done() const
00320 {
00321     return m_elaboration_done;
00322 }
00323 
00324 
00325 inline
00326 int
00327 sc_simcontext::sim_status() const
00328 {
00329     if( m_error ) {
00330         return SC_SIM_ERROR;
00331     }
00332     if( m_forced_stop ) {
00333         return SC_SIM_USER_STOP;
00334     }
00335     return SC_SIM_OK;
00336 }
00337 
00338 
00339 inline
00340 sc_object_manager*
00341 sc_simcontext::get_object_manager()
00342 {
00343     return m_object_manager;
00344 }
00345 
00346 inline
00347 sc_module_registry*
00348 sc_simcontext::get_module_registry()
00349 {
00350     return m_module_registry;
00351 }
00352 
00353 inline
00354 sc_port_registry*
00355 sc_simcontext::get_port_registry()
00356 {
00357     return m_port_registry;
00358 }
00359 
00360 inline
00361 sc_export_registry*
00362 sc_simcontext::get_export_registry()
00363 {
00364     return m_export_registry;
00365 }
00366 
00367 inline
00368 sc_prim_channel_registry*
00369 sc_simcontext::get_prim_channel_registry()
00370 {
00371     return m_prim_channel_registry;
00372 }
00373 
00374 
00375 inline
00376 sc_curr_proc_handle
00377 sc_simcontext::get_curr_proc_info()
00378 {
00379     return &m_curr_proc_info;
00380 }
00381 
00382 
00383 inline
00384 int
00385 sc_simcontext::next_proc_id()
00386 {
00387     return ( ++ m_next_proc_id );
00388 }
00389 
00390 
00391 inline
00392 const sc_time&
00393 sc_simcontext::time_stamp() const
00394 {
00395     return m_curr_time;
00396 }
00397 
00398 
00399 inline
00400 sc_dt::uint64
00401 sc_simcontext::delta_count() const
00402 {
00403     return m_delta_count;
00404 }
00405 
00406 inline
00407 bool
00408 sc_simcontext::is_running() const
00409 {
00410     return m_ready_to_simulate;
00411 }
00412 
00413 inline
00414 bool
00415 sc_simcontext::update_phase() const
00416 {
00417     return m_update_phase;
00418 }
00419 
00420 inline
00421 void
00422 sc_simcontext::set_error()
00423 {
00424     m_error = true;
00425 }
00426 
00427 
00428 inline
00429 bool
00430 sc_simcontext::get_error()
00431 {
00432     return m_error;
00433 }
00434 
00435 inline
00436 int
00437 sc_simcontext::add_delta_event( sc_event* e )
00438 {
00439     m_delta_events.push_back( e );
00440     return ( m_delta_events.size() - 1 );
00441 }
00442 
00443 inline
00444 void
00445 sc_simcontext::add_timed_event( sc_event_timed* et )
00446 {
00447     m_timed_events->insert( et );
00448 }
00449 
00450 // ----------------------------------------------------------------------------
00451 
00452 extern sc_simcontext* sc_get_curr_simcontext();
00453 
00454 inline
00455 sc_process_b*
00456 sc_get_curr_process_handle()
00457 {
00458     return sc_get_curr_simcontext()->get_curr_proc_info()->process_handle;
00459 }
00460 
00461 inline
00462 sc_curr_proc_kind
00463 sc_get_curr_process_kind()
00464 {
00465     return sc_get_curr_simcontext()->get_curr_proc_info()->kind;
00466 }
00467 
00468 extern sc_process_b* sc_get_last_created_process_handle();
00469 
00470 // Generates unique names within each module.
00471 extern
00472 const char*
00473 sc_gen_unique_name( const char* basename_, bool preserve_first = false );
00474 
00475 
00476 // Set the random seed for controlled randomization -- not yet implemented
00477 extern
00478 void
00479 sc_set_random_seed( unsigned int seed_ );
00480 
00481 
00482 extern void sc_start( const sc_time& duration );
00483 extern void sc_start( double duration = -1 );
00484 extern void sc_stop();
00485 
00486 extern void sc_initialize();
00487 extern void sc_cycle( const sc_time& duration );
00488 
00489 extern sc_dt::uint64 sc_delta_count();  // Current simulation delta cycle count.
00490 extern const sc_time& sc_time_stamp();  // Current simulation time.
00491 extern double sc_simulation_time();     // Current time in default time units.
00492 
00493 inline
00494 void
00495 sc_start( double duration, sc_time_unit time_unit )
00496 {
00497     sc_start( sc_time( duration, time_unit ) );
00498 }
00499 
00500 inline
00501 void
00502 sc_cycle( double duration, sc_time_unit time_unit )
00503 {
00504     sc_cycle( sc_time( duration, time_unit ) );
00505 }
00506 
00507 // for backward compatibility with 1.0
00508 inline
00509 void
00510 sc_cycle( double duration )  // in default time units
00511 {
00512     sc_cycle( sc_time( duration, true ) );
00513 }
00514 
00515 inline
00516 bool
00517 sc_end_of_simulation_invoked()
00518 {
00519         return sc_get_curr_simcontext()->m_end_of_simulation_called;
00520 }
00521 
00522 
00523 inline
00524 bool 
00525 sc_start_of_simulation_invoked()
00526 {
00527         return sc_get_curr_simcontext()->m_start_of_simulation_called;
00528 }
00529 
00530 } // namespace sc_core
00531 
00532 #endif

Generated on Wed Apr 25 13:53:27 2007 for SystemC by  doxygen 1.5.1