summaryrefslogtreecommitdiff
path: root/src/systemc/tests/systemc/1666-2011-compliance/immed_self_notif/immed_self_notif.cpp
blob: d29582a38149c46e76b37c5c3b11c7fc16f3211c (plain)
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
/*****************************************************************************

  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.

 *****************************************************************************/

// immed_self_notif.cpp -- test for 
//
//  Original Author: John Aynsley, Doulos, Inc.
//
// MODIFICATION LOG - modifiers, enter your name, affiliation, date and
//
// $Log: immed_self_notif.cpp,v $
// Revision 1.2  2011/05/08 19:18:46  acg
//  Andy Goodrich: remove extraneous + prefixes from git diff.
//

// Change to the semantics of immediate self-notification to match Verilog semantics

#define SC_INCLUDE_DYNAMIC_PROCESSES
#include <systemc>

using namespace sc_core;
using std::cout;
using std::endl;

struct Top: sc_module
{
  Top(sc_module_name _name)
  {
    SC_THREAD(T1);
      sensitive << ev1;
      
    SC_THREAD(T2);
      sensitive << ev1;
      
    SC_THREAD(T3);
      
    SC_THREAD(T4);
      sensitive << ev3;
      
    SC_METHOD(M1);
      sensitive << ev5;
      
    SC_METHOD(M2);
    
    SC_METHOD(M3);
    
    SC_THREAD(yield_test);
      
    end_of_t2   = false;
    m1_run      = false;
    m2_run      = false;
    m3_run      = false;
    first_yield = true;
    yield_count = 0;
    yield_test_run = false;
  }
  
  sc_event ev1, ev2, ev3, ev4, ev5, ev6, ev7;
  bool end_of_t2;
  bool m1_run;
  bool m2_run;
  bool m3_run;
  
  sc_event yield_event_1, yield_event_2;
  bool first_yield;
  int yield_count;
  bool yield_test_run;
  
  void T1()
  {
    wait(SC_ZERO_TIME);
    ev1.notify();
    sc_assert( sc_delta_count() == 1 );
    wait(ev1);
    sc_assert( false );
  }
  
  void T2()
  {
    wait(ev1);
    sc_assert( sc_delta_count() == 1 );
    end_of_t2 = true;
  }

  void T3()
  {
    wait(SC_ZERO_TIME);
    ev2.notify();
    sc_assert( sc_delta_count() == 1 );
    wait(ev2);
    sc_assert( false );
  }
  
  void T4()
  {
    wait(SC_ZERO_TIME);
    ev3.notify();
    sc_assert( sc_delta_count() == 1 );
    wait(ev4);
    sc_assert( false );
  }
  
  void M1()
  {
    sc_assert( !m1_run );
    ev5.notify();
    m1_run = true;
  }
  
  void M2()
  {
    sc_assert( !m2_run );
    ev6.notify();
    next_trigger(ev6);
    m2_run = true;
  }

  void M3()
  {
    sc_assert( !m3_run );
    next_trigger(ev7);
    ev7.notify();
    m3_run = true;
  }
  
  void yield_test()
  {
    sc_assert( sc_delta_count() == 0 );
    wait(SC_ZERO_TIME);
    sc_assert( sc_delta_count() == 1 );
    
    yield();
    sc_spawn(sc_bind( &Top::yield_test_child, this));
    yield();
    sc_spawn(sc_bind( &Top::yield_test_child, this));
    yield();
    
    sc_assert( sc_delta_count() == 1 );
    wait(1, SC_MS);
    unsigned int delta_count = sc_delta_count();
    
    yield();
    sc_spawn(sc_bind( &Top::yield_test_child, this));
    yield();
    sc_spawn(sc_bind( &Top::yield_test_child, this));
    yield();

    sc_assert( sc_delta_count() == delta_count );
    yield_test_run = true;
  }
  
  void yield_test_child()
  {
    yield();
  }
  
  void yield()
  {
    ++yield_count;
    if (first_yield)
    {
      sc_spawn(sc_bind(&Top::yield_helper, this));
      first_yield = false;
    }
    else
      yield_event_1.notify();
    wait(yield_event_2);
  }
  
  void yield_helper()
  {
    yield_event_2.notify();
    while (true)
    {
      wait(yield_event_1);
      yield_event_2.notify();
    }
  }

  SC_HAS_PROCESS(Top);
};


int sc_main(int argc, char* argv[])
{
  Top top("top");
  
  sc_start();
  
  sc_assert( top.end_of_t2 );
  sc_assert( top.m1_run );
  sc_assert( top.m2_run );
  sc_assert( top.m3_run );
  sc_assert( top.first_yield == false );
  sc_assert( top.yield_count == 10 );
  sc_assert( top.yield_test_run );
  
  cout << endl << "Success" << endl;
  return 0;
}