test1.cpp revision 12855:588919e0e4aa
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//  test01.cpp -- test self disables on processes
23//
24//  Original Author: Andy Goodrich, Forte Design Systems, Inc.
25//
26//  CVS MODIFICATION LOG - modifiers, enter your name, affiliation, date and
27//  changes you are making here.
28//
29// $Log: test1.cpp,v $
30// Revision 1.2  2009/07/28 01:09:48  acg
31//  Andy Goodrich: replacement test using standardized environment.
32//
33//*****************************************************************************
34
35#define SC_INCLUDE_DYNAMIC_PROCESSES
36#include "systemc.h"
37
38enum my_process_states {
39    ST_DISABLED,
40    ST_NORMAL,
41    ST_SUSPENDED
42};
43
44inline ostream& time_stamp( ostream& os )
45{
46    os << dec << sc_time_stamp() << "[" << sc_delta_count() << "]: ";
47    return os;
48}
49
50SC_MODULE(top) {
51    // constructor:
52
53    SC_CTOR(top) :
54        m_state_cthread0(ST_NORMAL),
55	m_state_method0(ST_NORMAL),
56        m_state_thread0(ST_NORMAL)
57    {
58        SC_THREAD(stimulator0);
59
60        SC_CTHREAD( target_cthread0, m_clk.pos() );
61        m_target_cthread0 = sc_get_current_process_handle();
62
63        SC_METHOD(target_method0);
64	sensitive << m_clk.pos();
65        m_target_method0 = sc_get_current_process_handle();
66
67        SC_THREAD(target_thread0);
68        m_target_thread0 = sc_get_current_process_handle();
69    }
70
71    // processes:
72
73    void stimulator0();
74    void target_cthread0();
75    void target_method0();
76    void target_thread0();
77
78    // Storage:
79
80    sc_in<bool>       m_clk;
81    int               m_state_cthread0;
82    int               m_state_method0;
83    int               m_state_thread0;
84    sc_process_handle m_target_cthread0;
85    sc_process_handle m_target_method0;
86    sc_process_handle m_target_thread0;
87};
88
89void top::stimulator0()
90{
91    const char* name = "stimulator";
92    wait(10, SC_NS);
93    cout << endl;
94    time_stamp(cout) << name << ": enabling target_cthread0" << endl;
95    cout << endl;
96    m_state_cthread0 = ST_NORMAL;
97    m_target_cthread0.enable();
98    wait(10, SC_NS);
99
100    cout << endl;
101    time_stamp(cout) << name << ": enabling target_method0" << endl;
102    cout << endl;
103    m_state_method0 = ST_NORMAL;
104    m_target_method0.enable();
105    wait(10, SC_NS);
106
107    cout << endl;
108    time_stamp(cout) << name << ": enabling target_thread0" << endl;
109    cout << endl;
110    m_state_thread0 = ST_NORMAL;
111    m_target_thread0.enable();
112    ::sc_core::wait(1000, SC_NS);
113
114    cout << endl;
115    time_stamp(cout) << name << ": terminating" << endl;
116    sc_stop();
117}
118
119void top::target_cthread0()
120{
121    int         i;
122    const char* name = "target_cthread0";
123
124    time_stamp(cout) << name  << ": starting" << endl;
125    time_stamp(cout) << name  << ": issuing self disable" << endl;
126    m_state_cthread0 = ST_DISABLED;
127    m_target_cthread0.disable();
128    time_stamp(cout) << name  << ": after issuing self disable" << endl;
129    cout << endl;
130    for ( i = 0; i < 100; i++ )
131    {
132        wait();
133	if ( m_state_cthread0 == ST_DISABLED )
134	{
135	    time_stamp(cout) << name  << ": ERROR should not see this" << endl;
136	}
137    }
138    time_stamp(cout) << name  << ": terminating" << endl;
139}
140
141void top::target_method0()
142{
143    const char* name = "target_method0";
144    static int  state = 0;
145    switch( state )
146    {
147      case 0:
148        time_stamp(cout) << name  << ": starting" << endl;
149        time_stamp(cout) << name  << ": issuing self disable" << endl;
150	m_state_method0 = ST_DISABLED;
151        m_target_method0.disable();
152        time_stamp(cout) << name  << ": after issuing self disable" << endl;
153        cout << endl;
154        break;
155      default:
156	if ( m_state_method0 == ST_DISABLED )
157	{
158	    time_stamp(cout) << name  << ": ERROR should not see this" << endl;
159	}
160        break;
161      case 99:
162        time_stamp(cout) << name  << ": terminating" << endl;
163        break;
164    }
165    state++;
166}
167
168void top::target_thread0()
169{
170    const char* name = "target_thread0";
171
172    time_stamp(cout) << name  << ": starting" << endl;
173    time_stamp(cout) << name  << ": issuing self disable" << endl;
174    m_state_thread0 = ST_DISABLED;
175    m_target_thread0.disable();
176    time_stamp(cout) << name  << ": after issuing self disable" << endl;
177    cout << endl;
178
179    // We wait a long enough time that our event will not occur until
180    // after we are re-enabled. Otherwise this thread will just go away
181    // quietly when the disable cancels the event.
182
183    ::sc_core::wait(80, SC_NS);
184    if ( m_state_thread0 == ST_DISABLED )
185    {
186	time_stamp(cout) << name  << ": ERROR should not see this" << endl;
187    }
188    time_stamp(cout) << name  << ": terminating" << endl;
189}
190
191int sc_main (int argc, char *argv[])
192{
193    sc_clock clock( "clock", 1.0, SC_NS );
194
195    top* top_p = new top("top");
196    top_p->m_clk(clock);
197
198    sc_start();
199    return 0;
200}
201
202