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_reset.cpp -- Support for reset.
23 
24   Original Author: Andy Goodrich, Forte Design Systems
25 
26   CHANGE LOG AT THE END OF THE FILE
27  *****************************************************************************/
28 
29 
30 #include "sysc/kernel/sc_simcontext.h"
31 #include "sysc/kernel/sc_reset.h"
32 #include "sysc/kernel/sc_process_handle.h"
33 #include "sysc/communication/sc_signal.h"
34 #include "sysc/communication/sc_signal_ports.h"
35 
36 
37 // THE SYSTEMC PROOF OF CONCEPT SIMULATOR RESET SIGNAL IMPLEMENTATION:
38 //
39 // (1) An instance of the sc_reset class is attached to each sc_signal<bool>
40 //     that is used as a reset signal.
41 //
42 // (2) Each process that is senstive to a reset signal will be registered in the
43 //     sc_reset class attached to that reset signal.
44 //
45 // (3) When a change in the value of a reset signal occurs it invokes the
46 //     notify_processes() method of its sc_reset object instance. The
47 //     notify_processes() method will call the reset_changed() method of each
48 //     process that is registered with it to inform the process that
49 //     state of the reset signal has changed.
50 //
51 // (4) A process may have multiple reset signals, so counters are kept for the
52 //     number of active asynchronous, and synchronous, reset signals that are
53 //     active. Those counters are incremented and decremented in the process'
54 //     reset_changed() method.
55 //
56 // (5) When a process' semantics() method is called the current reset state is
57 //     checked, and a reset sequence is initiated if the process is in reset.
58 //     This will occur every time an SC_METHOD is dispatched. SC_CTHREAD and
59 //     and SC_THREAD instances, only go through the semantics() method they
60 //     initially start up. So the reset check  is duplicated in the suspend_me()
61 //     method, the tail of which will execute each time the thread is
62 //     dispatched.
63 
64 namespace sc_core {
65 
sc_reset_finder(bool async,const sc_in<bool> * port_p,bool level,sc_process_b * target_p)66 inline sc_reset_finder::sc_reset_finder(
67     bool async, const sc_in<bool>* port_p, bool level, sc_process_b* target_p) :
68     m_async(async), m_level(level), m_next_p(0), m_in_p(port_p), m_inout_p(0),
69     m_out_p(0), m_target_p(target_p)
70 {
71     sc_get_curr_simcontext()->add_reset_finder(this);
72 }
73 
sc_reset_finder(bool async,const sc_inout<bool> * port_p,bool level,sc_process_b * target_p)74 inline sc_reset_finder::sc_reset_finder(
75     bool async, const sc_inout<bool>* port_p, bool level, sc_process_b* target_p
76 ) :
77     m_async(async), m_level(level), m_next_p(0), m_in_p(0), m_inout_p(port_p),
78     m_out_p(0), m_target_p(target_p)
79 {
80     sc_get_curr_simcontext()->add_reset_finder(this);
81 }
82 
sc_reset_finder(bool async,const sc_out<bool> * port_p,bool level,sc_process_b * target_p)83 inline sc_reset_finder::sc_reset_finder(
84     bool async, const sc_out<bool>* port_p, bool level, sc_process_b* target_p
85 ) :
86     m_async(async), m_level(level), m_next_p(0), m_in_p(0), m_inout_p(0),
87     m_out_p(port_p), m_target_p(target_p)
88 {
89     sc_get_curr_simcontext()->add_reset_finder(this);
90 }
91 
92 
93 //------------------------------------------------------------------------------
94 //"sc_reset::notify_processes"
95 //
96 // Notify processes that there is a change in the reset signal value.
97 //------------------------------------------------------------------------------
notify_processes()98 void sc_reset::notify_processes()
99 {
100     bool                                    active;       // true if reset is active.
101     sc_reset_target*                        entry_p;      // reset entry processing.
102     std::vector<sc_reset_target>::size_type process_i;    // index of process resetting.
103     std::vector<sc_reset_target>::size_type process_n;    // # of processes to reset.
104     bool                                    value;        // value of our signal.
105 
106     value = m_iface_p->read();
107     process_n = m_targets.size();
108     for ( process_i = 0; process_i < process_n; process_i++ )
109     {
110         entry_p = &m_targets[process_i];
111 	active = ( entry_p->m_level == value );
112 	entry_p->m_process_p->reset_changed( entry_p->m_async, active );
113     }
114 }
115 
116 
117 //------------------------------------------------------------------------------
118 //"sc_reset::reconcile_resets"
119 //
120 // This static method processes the sc_reset_finders to establish the actual
121 // reset connections.
122 //
123 // Notes:
124 //   (1) If reset is asserted we tell the process that it is in reset.
125 //------------------------------------------------------------------------------
reconcile_resets(sc_reset_finder * reset_finder_q)126 void sc_reset::reconcile_resets(sc_reset_finder* reset_finder_q)
127 {
128     const sc_signal_in_if<bool>*  iface_p;      // Interface to reset signal.
129     sc_reset_finder*              next_p;       // Next finder to process.
130     sc_reset_finder*              now_p;        // Finder currently processing.
131     sc_reset_target               reset_target; // Target's reset entry.
132     sc_reset*                     reset_p;      // Reset object to use.
133 
134     for ( now_p = reset_finder_q; now_p; now_p = next_p )
135     {
136         next_p = now_p->m_next_p;
137         if ( now_p->m_in_p )
138         {
139             iface_p = dynamic_cast<const sc_signal_in_if<bool>*>(
140                 now_p->m_in_p->get_interface());
141         }
142         else if ( now_p->m_inout_p )
143         {
144             iface_p = dynamic_cast<const sc_signal_in_if<bool>*>(
145                 now_p->m_inout_p->get_interface());
146         }
147         else
148         {
149             iface_p = dynamic_cast<const sc_signal_in_if<bool>*>(
150                 now_p->m_out_p->get_interface());
151         }
152         sc_assert( iface_p != 0 );
153         reset_p = iface_p->is_reset();
154 	now_p->m_target_p->m_resets.push_back(reset_p);
155 	reset_target.m_async = now_p->m_async;
156 	reset_target.m_level = now_p->m_level;
157 	reset_target.m_process_p = now_p->m_target_p;
158 	reset_p->m_targets.push_back(reset_target);
159 	if ( iface_p->read() == now_p->m_level ) // see note 1 above
160 	    now_p->m_target_p->initially_in_reset( now_p->m_async );
161         delete now_p;
162     }
163 }
164 
165 
166 //------------------------------------------------------------------------------
167 //"sc_reset::remove_process"
168 //
169 // This method removes the supplied process from the list of processes that
170 // should be notified when there is a change in the value of the reset signal.
171 //
172 // Arguments:
173 //     process_p -> process to be removed.
174 //------------------------------------------------------------------------------
remove_process(sc_process_b * process_p)175 void sc_reset::remove_process( sc_process_b* process_p )
176 {
177     int process_i; // Index of process resetting.
178     int process_n; // # of processes to reset.
179 
180     process_n = m_targets.size();
181     for ( process_i = 0; process_i < process_n; )
182     {
183         if ( m_targets[process_i].m_process_p == process_p )
184         {
185             m_targets[process_i] = m_targets[process_n-1];
186 	    process_n--;
187             m_targets.resize(process_n);
188         }
189 	else
190 	{
191 	    process_i++;
192 	}
193     }
194 }
195 
196 //------------------------------------------------------------------------------
197 //"sc_reset::reset_signal_is - ports"
198 //
199 // These overloads of the reset_signal_is() method will register the active
200 // process with the sc_reset object instance associated with the supplied port.
201 // If the port does not yet have a pointer to its sc_signal<bool> instance it
202 // will create an sc_reset_finder class object instance that will be used
203 // to set the process' reset information when the port has been bound.
204 //
205 // Arguments:
206 //     async = true if the reset signal is asynchronous, false if not.
207 //     port  = port for sc_signal<bool> that will provide the reset signal.
208 //     level = level at which reset is active, either true or false.
209 //------------------------------------------------------------------------------
reset_signal_is(bool async,const sc_in<bool> & port,bool level)210 void sc_reset::reset_signal_is( bool async, const sc_in<bool>& port, bool level)
211 {
212     const sc_signal_in_if<bool>* iface_p;
213     sc_process_b*                process_p;
214 
215     process_p = (sc_process_b*)sc_get_current_process_handle();
216     sc_assert( process_p );
217     process_p->m_has_reset_signal = true;
218     switch ( process_p->proc_kind() )
219     {
220       case SC_THREAD_PROC_:
221       case SC_METHOD_PROC_:
222       case SC_CTHREAD_PROC_:
223         iface_p = dynamic_cast<const sc_signal_in_if<bool>*>(port.get_interface());
224         if ( iface_p )
225             reset_signal_is( async, *iface_p, level );
226         else
227             new sc_reset_finder( async, &port, level, process_p );
228         break;
229       default:
230         SC_REPORT_ERROR(SC_ID_UNKNOWN_PROCESS_TYPE_, process_p->name());
231         break;
232     }
233 }
234 
reset_signal_is(bool async,const sc_inout<bool> & port,bool level)235 void sc_reset::reset_signal_is(
236     bool async, const sc_inout<bool>& port, bool level )
237 {
238     const sc_signal_in_if<bool>* iface_p;
239     sc_process_b*                process_p;
240 
241     process_p = (sc_process_b*)sc_get_current_process_handle();
242     sc_assert( process_p );
243     process_p->m_has_reset_signal = true;
244     switch ( process_p->proc_kind() )
245     {
246       case SC_THREAD_PROC_:
247       case SC_METHOD_PROC_:
248       case SC_CTHREAD_PROC_:
249         iface_p = dynamic_cast<const sc_signal_in_if<bool>*>(port.get_interface());
250         if ( iface_p )
251             reset_signal_is( async, *iface_p, level );
252         else
253             new sc_reset_finder( async, &port, level, process_p );
254         break;
255       default:
256         SC_REPORT_ERROR(SC_ID_UNKNOWN_PROCESS_TYPE_, process_p->name());
257         break;
258     }
259 }
260 
reset_signal_is(bool async,const sc_out<bool> & port,bool level)261 void sc_reset::reset_signal_is(
262     bool async, const sc_out<bool>& port, bool level )
263 {
264     const sc_signal_in_if<bool>* iface_p;
265     sc_process_b*                process_p;
266 
267     process_p = (sc_process_b*)sc_get_current_process_handle();
268     sc_assert( process_p );
269     process_p->m_has_reset_signal = true;
270     switch ( process_p->proc_kind() )
271     {
272       case SC_THREAD_PROC_:
273       case SC_METHOD_PROC_:
274       case SC_CTHREAD_PROC_:
275         iface_p = dynamic_cast<const sc_signal_in_if<bool>*>(port.get_interface());
276         if ( iface_p )
277             reset_signal_is( async, *iface_p, level );
278         else
279             new sc_reset_finder( async, &port, level, process_p );
280         break;
281       default:
282         SC_REPORT_ERROR(SC_ID_UNKNOWN_PROCESS_TYPE_, process_p->name());
283         break;
284     }
285 }
286 
287 //------------------------------------------------------------------------------
288 //"sc_reset::reset_signal_is"
289 //
290 // This static method will register the active process instance as being
291 // reset by the sc_signal<bool> whose interface has been supplied. If no
292 // sc_reset object instance has been attached to the sc_signal<bool> yet, it
293 // will be created and attached. The active process instance is pushed into
294 // the list of processes that the sc_reset object instance should notify if
295 // the value of the reset signal changes.
296 //
297 // Arguments:
298 //     async = true if the reset signal is asynchronous, false if not.
299 //     iface = interface for the reset signal.
300 //     level = is the level at which reset is active, either true or false.
301 // Notes:
302 //   (1) If reset is asserted we tell the process that it is in reset
303 //       initially.
304 //------------------------------------------------------------------------------
reset_signal_is(bool async,const sc_signal_in_if<bool> & iface,bool level)305 void sc_reset::reset_signal_is(
306     bool async, const sc_signal_in_if<bool>& iface, bool level )
307 {
308     sc_process_b*   process_p;    // process adding reset for.
309     sc_reset_target reset_target; // entry to build for the process.
310     sc_reset*       reset_p;      // reset object.
311 
312     process_p = sc_process_b::last_created_process_base();
313     sc_assert( process_p );
314     process_p->m_has_reset_signal = true;
315     switch ( process_p->proc_kind() )
316     {
317       case SC_METHOD_PROC_:
318       case SC_CTHREAD_PROC_:
319       case SC_THREAD_PROC_:
320 	reset_p = iface.is_reset();
321 	process_p->m_resets.push_back(reset_p);
322         reset_target.m_async = async;
323 	reset_target.m_level = level;
324 	reset_target.m_process_p = process_p;
325 	reset_p->m_targets.push_back(reset_target);
326 	if ( iface.read() == level ) process_p->initially_in_reset( async );
327         break;
328       default:
329         SC_REPORT_ERROR(SC_ID_UNKNOWN_PROCESS_TYPE_, process_p->name());
330         break;
331     }
332 }
333 
334 } // namespace sc_core
335 
336 // $Log: sc_reset.cpp,v $
337 // Revision 1.16  2011/08/26 20:46:10  acg
338 //  Andy Goodrich: moved the modification log to the end of the file to
339 //  eliminate source line number skew when check-ins are done.
340 //
341 // Revision 1.15  2011/08/24 22:05:51  acg
342 //  Torsten Maehne: initialization changes to remove warnings.
343 //
344 // Revision 1.14  2011/04/08 22:37:34  acg
345 //  Andy Goodrich: documentation of the reset mechanism and additional
346 //  documentation of methods. Removal of check for SC_METHODs in
347 //  sc_reset_signal_is() that should not have been there.
348 //
349 // Revision 1.13  2011/03/20 15:13:01  acg
350 //  Andy Goodrich: set the reset flag for async_reset_signal_is to catch
351 //  the suspend() corner case.
352 //
353 // Revision 1.12  2011/03/20 13:43:23  acg
354 //  Andy Goodrich: added async_signal_is() plus suspend() as a corner case.
355 //
356 // Revision 1.11  2011/03/06 19:57:11  acg
357 //  Andy Goodrich: refinements for the illegal suspend - synchronous reset
358 //  interaction.
359 //
360 // Revision 1.10  2011/02/18 20:27:14  acg
361 //  Andy Goodrich: Updated Copyrights.
362 //
363 // Revision 1.9  2011/02/13 21:47:37  acg
364 //  Andy Goodrich: update copyright notice.
365 //
366 // Revision 1.8  2011/02/01 21:08:26  acg
367 //  Andy Goodrich: new multiple reset support.
368 //
369 // Revision 1.7  2011/01/06 18:04:38  acg
370 //  Andy Goodrich: removed commented out code.
371 //
372 // Revision 1.6  2010/12/07 20:09:13  acg
373 // Andy Goodrich: removed sc_signal overloads since already have sc_signal_in_if overloads.
374 //
375 // Revision 1.5  2010/11/20 17:10:56  acg
376 //  Andy Goodrich: reset processing changes for new IEEE 1666 standard.
377 //
378 // Revision 1.4  2009/05/22 16:06:29  acg
379 //  Andy Goodrich: process control updates.
380 //
381 // Revision 1.3  2009/03/12 22:59:58  acg
382 //  Andy Goodrich: updates for 2.4 stuff.
383 //
384 // Revision 1.2  2008/05/22 17:06:26  acg
385 //  Andy Goodrich: updated copyright notice to include 2008.
386 //
387 // Revision 1.1.1.1  2006/12/15 20:20:05  acg
388 // SystemC 2.3
389 //
390 // Revision 1.7  2006/12/02 20:58:19  acg
391 //  Andy Goodrich: updates from 2.2 for IEEE 1666 support.
392 //
393 // Revision 1.5  2006/04/11 23:13:21  acg
394 //   Andy Goodrich: Changes for reduced reset support that only includes
395 //   sc_cthread, but has preliminary hooks for expanding to method and thread
396 //   processes also.
397 //
398 // Revision 1.4  2006/01/24 20:49:05  acg
399 // Andy Goodrich: changes to remove the use of deprecated features within the
400 // simulator, and to issue warning messages when deprecated features are used.
401 //
402 // Revision 1.3  2006/01/13 18:44:30  acg
403 // Added $Log to record CVS changes into the source.
404 //
405