-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsc_simcontext.cpp
2357 lines (2054 loc) · 72.3 KB
/
sc_simcontext.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
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.
*****************************************************************************/
/*****************************************************************************
sc_simcontext.cpp -- Provides a simulation context for use with multiple
simulations.
Original Author: Stan Y. Liao, Synopsys, Inc.
Martin Janssen, Synopsys, Inc.
CHANGE LOG AT THE END OF THE FILE
*****************************************************************************/
#include "sysc/kernel/sc_simcontext.h"
#include "sysc/kernel/sc_simcontext_int.h"
#include "sysc/kernel/sc_cor_fiber.h"
#include "sysc/kernel/sc_cor_pthread.h"
#include "sysc/kernel/sc_cor_qt.h"
#include "sysc/kernel/sc_event.h"
#include "sysc/kernel/sc_kernel_ids.h"
#include "sysc/kernel/sc_module.h"
#include "sysc/kernel/sc_module_registry.h"
#include "sysc/kernel/sc_name_gen.h"
#include "sysc/kernel/sc_object_manager.h"
#include "sysc/kernel/sc_cthread_process.h"
#include "sysc/kernel/sc_method_process.h"
#include "sysc/kernel/sc_thread_process.h"
#include "sysc/kernel/sc_process_handle.h"
#include "sysc/kernel/sc_reset.h"
#include "sysc/kernel/sc_ver.h"
#include "sysc/kernel/sc_dynamic_processes.h"
#include "sysc/kernel/sc_stage_callback_registry.h"
#include "sysc/communication/sc_port.h"
#include "sysc/communication/sc_export.h"
#include "sysc/communication/sc_prim_channel.h"
#include "sysc/communication/sc_stub.h"
#include "sysc/tracing/sc_trace.h"
#include "sysc/utils/sc_mempool.h"
#include "sysc/utils/sc_list.h"
#include "sysc/utils/sc_string_view.h"
#include "sysc/utils/sc_utils_ids.h"
#include <algorithm>
#include <cstring>
#include <sstream>
// DEBUGGING MACROS:
//
// DEBUG_MSG(NAME,P,MSG)
// MSG = message to print
// NAME = name that must match the process for the message to print, or
// null if the message should be printed unconditionally.
// P = pointer to process message is for, or NULL in which case the
// message will not print.
#if 0
# include <cstring>
# define DEBUG_NAME ""
# define DEBUG_MSG(NAME,P,MSG) \
{ \
if ( P && ( (std::strlen(NAME)==0) || !std::strcmp(NAME,P->name())) ) \
std::cout << "**** " << sc_time_stamp() << " (" \
<< sc_get_current_process_name("** NONE **") << "): " << MSG \
<< " - " << P->name() << std::endl; \
}
#else
# define DEBUG_MSG(NAME,P,MSG)
#endif
#define SC_DO_STAGE_CALLBACK_( Kind ) \
m_stage_cb_registry->Kind()
// disable callback based tracing
#define SC_SIMCONTEXT_TRACING_ 0
namespace sc_core {
sc_stop_mode stop_mode = SC_STOP_FINISH_DELTA;
// ----------------------------------------------------------------------------
// CLASS : sc_process_table
//
// Container class that keeps track of all method processes,
// (c)thread processes.
// ----------------------------------------------------------------------------
class sc_process_table
{
public:
template<typename ProcessHandle> struct queue
{
queue() : m_head() {}
~queue();
ProcessHandle head() const { return m_head; }
void push_front(ProcessHandle h);
ProcessHandle remove(ProcessHandle h);
private:
ProcessHandle m_head;
};
void push_front( sc_method_handle handle )
{ m_methods.push_front(handle); }
void push_front( sc_thread_handle handle )
{ m_threads.push_front(handle); }
sc_method_handle method_q_head() const
{ return m_methods.head(); }
sc_thread_handle thread_q_head() const
{ return m_threads.head(); }
sc_method_handle remove( sc_method_handle handle )
{ return m_methods.remove(handle); }
sc_thread_handle remove( sc_thread_handle handle )
{ return m_threads.remove(handle); }
private:
queue<sc_method_handle> m_methods; // Queue of existing method processes.
queue<sc_thread_handle> m_threads; // Queue of existing thread processes.
};
template<typename ProcessHandle>
sc_process_table::queue<ProcessHandle>::~queue()
{
while( m_head ) {
ProcessHandle now_p = m_head;
m_head = m_head->next_exist();
now_p->reference_decrement();
}
}
template<typename ProcessHandle>
void
sc_process_table::queue<ProcessHandle>::push_front( ProcessHandle handle )
{
handle->set_next_exist(m_head);
m_head = handle;
}
template<typename ProcessHandle>
ProcessHandle
sc_process_table::queue<ProcessHandle>::remove( ProcessHandle handle )
{
ProcessHandle now_p = m_head; // Entry now examining.
ProcessHandle prior_p = NULL; // Entry prior to one now examining.
while( now_p )
{
if ( now_p == handle )
{
if ( prior_p )
prior_p->set_next_exist( now_p->next_exist() );
else
m_head = now_p->next_exist();
return handle;
}
prior_p = now_p;
now_p = now_p->next_exist();
}
return NULL;
}
// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
sc_method_handle
sc_simcontext::remove_process( sc_method_handle handle )
{
return m_process_table->remove(handle);
}
sc_thread_handle
sc_simcontext::remove_process( sc_thread_handle handle )
{
return m_process_table->remove(handle);
}
SC_API int
sc_notify_time_compare( const void* p1, const void* p2 )
{
const sc_event_timed* et1 = static_cast<const sc_event_timed*>( p1 );
const sc_event_timed* et2 = static_cast<const sc_event_timed*>( p2 );
const sc_time& t1 = et1->notify_time();
const sc_time& t2 = et2->notify_time();
if( t1 < t2 ) {
return 1;
} else if( t1 > t2 ) {
return -1;
} else {
return 0;
}
}
// +============================================================================
// | CLASS sc_invoke_method - class to invoke sc_method's to support
// | sc_simcontext::preempt_with().
// +============================================================================
class sc_invoke_method : public sc_module
{
public:
SC_CTOR(sc_invoke_method)
{
// remove from object hierarchy
detach();
}
virtual ~sc_invoke_method()
{}
// Method to call to execute a method's semantics.
void invoke_method( sc_method_handle method_h )
{
sc_process_handle invoker_h; // handle for invocation thread to use.
std::vector<sc_process_handle>::size_type invokers_n; // number of invocation threads available.
m_method = method_h;
// There is not an invocation thread to use, so allocate one.
invokers_n = m_invokers.size();
if ( invokers_n == 0 )
{
sc_hierarchy_scope scope( get_hierarchy_scope() );
sc_spawn_options options;
options.dont_initialize();
options.set_stack_size(0x100000);
options.set_sensitivity(&m_dummy);
invoker_h = sc_spawn(sc_bind(&sc_invoke_method::invoker,this),
sc_gen_unique_name("invoker"), &options);
((sc_process_b*)invoker_h)->detach();
}
// There is an invocation thread to use, use the last one on the list.
else
{
invoker_h = m_invokers[invokers_n-1];
m_invokers.pop_back();
}
// Fire off the invocation thread to invoke the method's semantics,
// When it blocks put it onto the list of invocation threads that
// are available.
sc_get_curr_simcontext()->preempt_with( (sc_thread_handle)invoker_h );
DEBUG_MSG( DEBUG_NAME, m_method, "back from preemption" );
m_invokers.push_back(invoker_h);
}
// Thread to call method from:
void invoker()
{
sc_simcontext* csc_p = sc_get_curr_simcontext();
sc_process_b* me = sc_get_current_process_b();
DEBUG_MSG( DEBUG_NAME, me, "invoker initialization" );
for (;; )
{
DEBUG_MSG( DEBUG_NAME, m_method, "invoker executing method" );
csc_p->set_curr_proc( (sc_process_b*)m_method );
csc_p->get_active_invokers().push_back((sc_thread_handle)me);
m_method->run_process();
csc_p->set_curr_proc( me );
csc_p->get_active_invokers().pop_back();
DEBUG_MSG( DEBUG_NAME, m_method, "back from executing method" );
wait();
}
}
sc_event m_dummy; // dummy event to wait on.
sc_method_handle m_method; // method to be invoked.
std::vector<sc_process_handle> m_invokers; // list of invoking threads.
};
// ----------------------------------------------------------------------------
// CLASS : sc_simcontext
//
// The simulation context.
// ----------------------------------------------------------------------------
void
sc_simcontext::init()
{
// ALLOCATE VARIOUS MANAGERS AND REGISTRIES:
m_object_manager = new sc_object_manager;
m_module_registry = new sc_module_registry( *this );
m_port_registry = new sc_port_registry( *this );
m_export_registry = new sc_export_registry( *this );
m_prim_channel_registry = new sc_prim_channel_registry( *this );
m_stage_cb_registry = new sc_stage_callback_registry( *this );
m_stub_registry = new sc_stub_registry( *this );
m_name_gen = new sc_name_gen;
m_process_table = new sc_process_table;
m_current_writer = 0;
// CHECK FOR ENVIRONMENT VARIABLES THAT MODIFY SIMULATOR EXECUTION:
const char* write_check = std::getenv("SC_SIGNAL_WRITE_CHECK");
sc_string_view write_check_s = (write_check != NULL) ? write_check : "";
if ( write_check_s == "DISABLE" )
m_write_check = SC_SIGNAL_WRITE_CHECK_DISABLE_;
else if ( write_check_s == "CONFLICT" )
m_write_check = SC_SIGNAL_WRITE_CHECK_CONFLICT_;
else
m_write_check = SC_SIGNAL_WRITE_CHECK_DEFAULT_;
// FINISH INITIALIZATIONS:
reset_curr_proc();
m_next_proc_id = -1;
m_timed_events = new sc_ppq<sc_event_timed*>( 128, sc_notify_time_compare );
m_null_event_p = NULL;
m_something_to_trace = false;
m_runnable = new sc_runnable;
m_collectable = new sc_process_list;
m_time_params = new sc_time_params;
m_curr_time = SC_ZERO_TIME;
m_max_time = SC_ZERO_TIME;
m_change_stamp = 0;
m_delta_count = 0;
m_initial_delta_count_at_current_time = 0;
m_forced_stop = false;
m_paused = false;
m_ready_to_simulate = false;
m_elaboration_done = false;
m_execution_phase = phase_initialize;
m_error = NULL;
m_cor_pkg = 0;
m_method_invoker_p = NULL;
m_cor = 0;
m_reset_finder_q = 0;
m_in_simulator_control = false;
m_start_of_simulation_called = false;
m_end_of_simulation_called = false;
set_simulation_status(SC_ELABORATION);
m_stage = (sc_stage)(0);
m_suspend = 0;
m_unsuspendable = 0;
}
void
sc_simcontext::clean()
{
// remove remaining zombie processes
do_collect_processes();
delete m_stub_registry;
delete m_method_invoker_p;
delete m_error;
delete m_cor_pkg;
delete m_time_params;
delete m_collectable;
delete m_runnable;
delete m_null_event_p;
delete m_timed_events;
delete m_process_table;
delete m_name_gen;
delete m_stage_cb_registry;
delete m_prim_channel_registry;
delete m_export_registry;
delete m_port_registry;
delete m_module_registry;
delete m_object_manager;
m_delta_events.clear();
m_child_objects.clear();
m_trace_files.clear();
while( m_reset_finder_q ) {
sc_reset_finder* rf = m_reset_finder_q;
m_reset_finder_q = rf->m_next_p;
delete rf;
}
}
sc_simcontext::sc_simcontext() :
m_object_manager(0), m_module_registry(0), m_port_registry(0),
m_export_registry(0), m_prim_channel_registry(0),
m_stage_cb_registry(0), m_stub_registry(0), m_name_gen(0),
m_process_table(0), m_curr_proc_info(), m_current_writer(0),
m_write_check(SC_SIGNAL_WRITE_CHECK_DEFAULT_), m_next_proc_id(-1),
m_child_events(), m_child_objects(), m_delta_events(), m_timed_events(0),
m_trace_files(), m_something_to_trace(false), m_runnable(0), m_collectable(0),
m_time_params(), m_curr_time(SC_ZERO_TIME), m_max_time(SC_ZERO_TIME),
m_change_stamp(0), m_delta_count(0), m_initial_delta_count_at_current_time(0),
m_forced_stop(false), m_paused(false),
m_ready_to_simulate(false), m_elaboration_done(false),
m_execution_phase(phase_initialize), m_error(0),
m_in_simulator_control(false), m_end_of_simulation_called(false),
m_simulation_status(SC_ELABORATION), m_start_of_simulation_called(false),
m_cor_pkg(0), m_cor(0), m_reset_finder_q(0),
m_suspend(0), m_unsuspendable(0)
{
init();
}
sc_simcontext::~sc_simcontext()
{
clean();
}
// +------------------------------------------------------------------------------------------------
// |"sc_simcontext::get_thread_safe_status"
// |
// | This method returns the current simulator status, and uses a mutex mechanism to guarantee
// | thread safety. It may be called from pthreads other than the simulator's.
// |
// | Notes:
// | (1) Return from this function releases the mutex.
// | (2) Internal to the simulator thread get_status() should be used to avoid the overhead of
// | the mutex.
// | Result:
// | Current simulator status (see the sc_status enum).
// +------------------------------------------------------------------------------------------------
sc_status sc_simcontext::get_thread_safe_status()
{
sc_scoped_lock lock( m_simulation_status_mutex );
return m_simulation_status != SC_RUNNING ?
m_simulation_status :
(m_in_simulator_control ? SC_RUNNING : SC_PAUSED);
}
// +----------------------------------------------------------------------------
// |"sc_simcontext::null_event"
// |
// | This method returns a "null" event that can be used with sc_event::none()
// | and sc_
// +----------------------------------------------------------------------------
sc_event& sc_simcontext::null_event()
{
if ( NULL == m_null_event_p ) {
m_null_event_p = new sc_event( sc_event::kernel_event, "null" );
}
return *m_null_event_p;
}
// +----------------------------------------------------------------------------
// |"sc_simcontext::active_object"
// |
// | This method returns the currently active object with respect to
// | additions to the hierarchy. It will be the top of the object hierarchy
// | stack if it is non-empty, or it will be the active process, or NULL
// | if there is no active process.
// +----------------------------------------------------------------------------
sc_object_host*
sc_simcontext::active_object()
{
if( m_object_manager->hierarchy_size() > 0 )
return m_object_manager->hierarchy_curr();
return get_curr_proc_info()->process_handle;
}
// +----------------------------------------------------------------------------
// |"sc_simcontext::crunch"
// |
// | This method implements the simulator's execution of processes. It performs
// | one or more "delta" cycles. Each delta cycle consists of an evaluation,
// | an update phase, and a notification phase. During the evaluation phase any
// | processes that are ready to run are executed. After all the processes have
// | been executed the update phase is entered. During the update phase the
// | values of any signals that have changed are updated. After the updates
// | have been performed the notification phase is entered. During that phase
// | any notifications that need to occur because of of signal values changes
// | are performed. This will result in the queueing of processes for execution
// | that are sensitive to those notifications. At that point a delta cycle
// | is complete, and the process is started again unless 'once' is true.
// |
// | Arguments:
// | once = true if only one delta cycle is to be performed.
// +----------------------------------------------------------------------------
inline void
sc_simcontext::crunch( bool once )
{
#ifdef DEBUG_SYSTEMC
int num_deltas = 0; // number of delta cycles
#endif
while ( true )
{
// EVALUATE PHASE
m_execution_phase = phase_evaluate;
bool empty_eval_phase = true;
while( true )
{
// execute method processes
m_runnable->toggle_methods();
sc_method_handle method_h = pop_runnable_method();
while( method_h != 0 ) {
empty_eval_phase = false;
if ( !method_h->run_process() )
{
goto out;
}
method_h = pop_runnable_method();
}
// execute (c)thread processes
m_runnable->toggle_threads();
sc_thread_handle thread_h = pop_runnable_thread();
while( thread_h != 0 ) {
if ( thread_h->m_cor_p != NULL ) break;
thread_h = pop_runnable_thread();
}
if( thread_h != 0 ) {
empty_eval_phase = false;
m_cor_pkg->yield( thread_h->m_cor_p );
}
if( m_error ) {
goto out;
}
// check for call(s) to sc_stop
if( m_forced_stop ) {
if ( stop_mode == SC_STOP_IMMEDIATE ) goto out;
}
// no more runnable processes
if( m_runnable->is_empty() ) {
break;
}
}
// remove finally dead zombies:
do_collect_processes();
// UPDATE PHASE
//
// The change stamp must be updated first so that event_occurred()
// will work.
m_execution_phase = phase_update;
if ( !empty_eval_phase )
{
m_change_stamp++;
}
m_prim_channel_registry->perform_update();
SC_DO_STAGE_CALLBACK_(update_done); // SC_POST_UPDATE
m_execution_phase = phase_notify;
#if SC_SIMCONTEXT_TRACING_
if( m_something_to_trace ) {
trace_cycle( /* delta cycle? */ true );
}
#endif
// check for call(s) to sc_stop
if( m_forced_stop ) {
break;
}
#ifdef DEBUG_SYSTEMC
// check for possible infinite loops
if( ++ num_deltas > SC_MAX_NUM_DELTA_CYCLES ) {
::std::cerr << "SystemC warning: "
<< "the number of delta cycles exceeds the limit of "
<< SC_MAX_NUM_DELTA_CYCLES
<< ", defined in sc_constants.h.\n"
<< "This is a possible sign of an infinite loop.\n"
<< "Increase the limit if this warning is invalid.\n";
break;
}
#endif
// NOTIFICATION PHASE:
//
// Process delta notifications which will queue processes for
// subsequent execution.
int size = m_delta_events.size();
if ( size != 0 )
{
sc_event** l_events = &m_delta_events[0];
int i = size - 1;
do {
l_events[i]->trigger();
} while( -- i >= 0 );
m_delta_events.clear();
}
if ( !empty_eval_phase )
m_delta_count ++;
if( m_runnable->is_empty() ) {
// no more runnable processes
break;
}
// if sc_pause() was called we are done.
if ( m_paused ) break;
// IF ONLY DOING ONE CYCLE, WE ARE DONE. OTHERWISE EXECUTE NEW CALLBACKS
if ( once ) break;
}
// When this point is reached the processing of delta cycles is complete,
// if the completion was because of an error throw the exception specified
// by '*m_error'.
out:
this->reset_curr_proc();
do_collect_processes();
if( m_error ) throw *m_error; // re-throw propagated error
}
inline
void
sc_simcontext::cycle( const sc_time& t)
{
sc_time next_event_time;
m_in_simulator_control = true;
crunch();
do_timestep( m_curr_time + t );
if ( next_time(next_event_time) && next_event_time <= m_curr_time) {
SC_REPORT_WARNING(SC_ID_CYCLE_MISSES_EVENTS_, "");
}
m_in_simulator_control = false;
}
void
sc_simcontext::sc_tool_elaboration()
{
if( m_elaboration_done || sim_status() != SC_SIM_OK ) {
return;
}
// Instantiate the method invocation module
// (not added to public object hierarchy)
m_method_invoker_p =
new sc_invoke_method("$$$$kernel_module$$$$_invoke_method" );
m_simulation_status = SC_BEFORE_END_OF_ELABORATION;
m_module_registry->construction_done();
}
void
sc_simcontext::elaborate()
{
if( m_elaboration_done || sim_status() != SC_SIM_OK ) {
return;
}
// Instantiate the method invocation module
// (not added to public object hierarchy)
m_method_invoker_p =
new sc_invoke_method("$$$$kernel_module$$$$_invoke_method" );
set_simulation_status(SC_BEFORE_END_OF_ELABORATION);
for( int cd = 0; cd != 4; /* empty */ )
{
cd = m_port_registry->construction_done();
cd += m_export_registry->construction_done();
cd += m_prim_channel_registry->construction_done();
cd += m_module_registry->construction_done();
// check for call(s) to sc_stop
if( m_forced_stop ) {
do_sc_stop_action();
return;
}
}
SC_DO_STAGE_CALLBACK_(construction_done); // SC_POST_BEFORE_END_OF_ELABORATION
// SIGNAL THAT ELABORATION IS DONE
//
// We set the switch before the calls in case someone creates a process
// in an end_of_elaboration callback. We need the information to flag
// the process as being dynamic.
m_elaboration_done = true;
set_simulation_status(SC_END_OF_ELABORATION);
m_port_registry->elaboration_done();
m_export_registry->elaboration_done();
m_prim_channel_registry->elaboration_done();
m_module_registry->elaboration_done();
SC_DO_STAGE_CALLBACK_(elaboration_done); // SC_POST_END_OF_ELABORATION
sc_reset::reconcile_resets(m_reset_finder_q);
m_reset_finder_q = NULL;
// check for call(s) to sc_stop
if( m_forced_stop ) {
do_sc_stop_action();
return;
}
}
void
sc_simcontext::prepare_to_simulate()
{
sc_method_handle method_p; // Pointer to method process accessing.
sc_thread_handle thread_p; // Pointer to thread process accessing.
if( m_ready_to_simulate || sim_status() != SC_SIM_OK ) {
return;
}
// instantiate the coroutine package
m_cor_pkg = new sc_cor_pkg_t( this );
m_cor = m_cor_pkg->get_main();
// NOTIFY ALL OBJECTS THAT SIMULATION IS ABOUT TO START:
set_simulation_status(SC_START_OF_SIMULATION);
m_port_registry->start_simulation();
m_export_registry->start_simulation();
m_prim_channel_registry->start_simulation();
m_module_registry->start_simulation();
SC_DO_STAGE_CALLBACK_(start_simulation); // SC_POST_START_OF_SIMULATION
m_start_of_simulation_called = true;
// CHECK FOR CALL(S) TO sc_stop
if( m_forced_stop ) {
do_sc_stop_action();
return;
}
// PREPARE ALL (C)THREAD PROCESSES FOR SIMULATION:
for ( thread_p = m_process_table->thread_q_head();
thread_p; thread_p = thread_p->next_exist() )
{
thread_p->prepare_for_simulation();
}
set_simulation_status(SC_RUNNING);
m_ready_to_simulate = true;
m_runnable->init();
// update phase
m_execution_phase = phase_update;
m_prim_channel_registry->perform_update();
m_execution_phase = phase_notify;
int size;
// make all method processes runnable
for ( method_p = m_process_table->method_q_head();
method_p; method_p = method_p->next_exist() )
{
if ( ((method_p->m_state & sc_process_b::ps_bit_disabled) != 0) ||
method_p->dont_initialize() )
{
if ( method_p->m_static_events.size() == 0 )
{
SC_REPORT_WARNING( SC_ID_DISABLE_WILL_ORPHAN_PROCESS_,
method_p->name() );
}
}
else if ( (method_p->m_state & sc_process_b::ps_bit_suspended) == 0)
{
if ( !method_p->is_runnable() ) // already scheduled?
push_runnable_method_front( method_p );
}
else
{
method_p->m_state |= sc_process_b::ps_bit_ready_to_run;
}
}
// make thread processes runnable
// (cthread processes always have the dont_initialize flag set)
for ( thread_p = m_process_table->thread_q_head();
thread_p; thread_p = thread_p->next_exist() )
{
if ( ((thread_p->m_state & sc_process_b::ps_bit_disabled) != 0) ||
thread_p->dont_initialize() )
{
if ( thread_p->m_static_events.size() == 0 )
{
SC_REPORT_WARNING( SC_ID_DISABLE_WILL_ORPHAN_PROCESS_,
thread_p->name() );
}
}
else if ( (thread_p->m_state & sc_process_b::ps_bit_suspended) == 0)
{
if ( !thread_p->is_runnable() ) // already scheduled?
push_runnable_thread_front( thread_p );
}
else
{
thread_p->m_state |= sc_process_b::ps_bit_ready_to_run;
}
}
// process delta notifications
if( ( size = m_delta_events.size() ) != 0 ) {
sc_event** l_delta_events = &m_delta_events[0];
int i = size - 1;
do {
l_delta_events[i]->trigger();
} while( -- i >= 0 );
m_delta_events.clear();
}
}
void
sc_simcontext::initial_crunch( bool no_crunch )
{
if( no_crunch || m_runnable->is_empty() ) {
return;
}
// run the delta cycle loop
crunch();
if( m_error ) {
return;
}
// check for call(s) to sc_stop
if( m_forced_stop ) {
do_sc_stop_action();
}
}
void
sc_simcontext::initialize( bool no_crunch )
{
m_in_simulator_control = true;
elaborate();
prepare_to_simulate();
initial_crunch(no_crunch);
m_in_simulator_control = false;
}
// +----------------------------------------------------------------------------
// |"sc_simcontext::simulate"
// |
// | This method runs the simulation for the specified amount of time.
// |
// | Notes:
// | (1) This code always run with an SC_EXIT_ON_STARVATION starvation policy,
// | so the simulation time on return will be the minimum of the
// | simulation on entry plus the duration, and the maximum time of any
// | event present in the simulation. If the simulation policy is
// | SC_RUN_TO_TIME starvation it is implemented by the caller of this
// | method, e.g., sc_start(), by artificially setting the simulation
// | time forward after this method completes.
// |
// | Arguments:
// | duration = amount of time to simulate.
// +----------------------------------------------------------------------------
void
sc_simcontext::simulate( const sc_time& duration )
{
initialize( true );
if (sim_status() != SC_SIM_OK) {
return;
}
sc_time non_overflow_time = max_time() - m_curr_time;
if ( duration > non_overflow_time )
{
SC_REPORT_ERROR(SC_ID_SIMULATION_TIME_OVERFLOW_, "");
return;
}
m_in_simulator_control = true;
m_paused = false;
sc_time until_t = m_curr_time + duration;
sc_time t; // current simulaton time.
// IF DURATION WAS ZERO WE ONLY CRUNCH ONCE:
//
// We duplicate the code so that we don't add the overhead of the
// check to each loop in the do below.
if ( duration == SC_ZERO_TIME )
{
m_in_simulator_control = true;
crunch( true );
if( m_error ) {
m_in_simulator_control = false;
return;
}
if( m_forced_stop ) {
do_sc_stop_action();
return;
}
// return via implicit pause
goto exit_pause;
}
// NON-ZERO DURATION: EXECUTE UP TO THAT TIME, OR UNTIL EVENT STARVATION:
do {
crunch();
if( m_error ) {
m_in_simulator_control = false;
return;
}
// check for call(s) to sc_stop() or sc_pause().
if( m_forced_stop ) {
do_sc_stop_action();
return;
}
if( m_paused ) {
SC_DO_STAGE_CALLBACK_(simulation_paused); // SC_PRE_PAUSE
goto exit_pause; // return explicit pause
}
t = m_curr_time;
do {
// See note 1 above:
if ( !next_time(t) || (t > until_t) ) {
if ( (t > until_t) || m_prim_channel_registry->async_suspend() ) {
// requested simulation time completed or no external updates
goto exit_time;
}
// received external updates, continue simulation
break;
}
if ( t > m_curr_time )
do_timestep(t);
// PROCESS TIMED NOTIFICATIONS AT THE CURRENT TIME
do {
sc_event_timed* et = m_timed_events->extract_top();
sc_event* e = et->event();
delete et;
if( e != 0 ) {
e->trigger();
}
} while( m_timed_events->size() &&
m_timed_events->top()->notify_time() == t );
} while( m_runnable->is_empty() );
} while ( t < until_t ); // hold off on the delta for the until_t time.
exit_time: // final simulation time update, if needed
if ( t > m_curr_time && t <= until_t )
do_timestep(t);
exit_pause:
m_execution_phase = phase_evaluate;
m_in_simulator_control = false;
}
void
sc_simcontext::do_timestep(const sc_time& t)
{
sc_assert( m_curr_time < t );
SC_DO_STAGE_CALLBACK_(before_timestep); // SC_PRE_TIMESTEP
#if SC_SIMCONTEXT_TRACING_
if( m_something_to_trace ) {
trace_cycle( false );
}
#endif