112855Sgabeblack@google.com/*****************************************************************************
212855Sgabeblack@google.com
312855Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412855Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512855Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612855Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712855Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812855Sgabeblack@google.com  License.  You may obtain a copy of the License at
912855Sgabeblack@google.com
1012855Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112855Sgabeblack@google.com
1212855Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312855Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412855Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512855Sgabeblack@google.com  implied.  See the License for the specific language governing
1612855Sgabeblack@google.com  permissions and limitations under the License.
1712855Sgabeblack@google.com
1812855Sgabeblack@google.com *****************************************************************************/
1912855Sgabeblack@google.com
2012855Sgabeblack@google.com// proc_ctrl_immed.cpp -- test for
2112855Sgabeblack@google.com//
2212855Sgabeblack@google.com//  Original Author: John Aynsley, Doulos, Inc.
2312855Sgabeblack@google.com//
2412855Sgabeblack@google.com// MODIFICATION LOG - modifiers, enter your name, affiliation, date and
2512855Sgabeblack@google.com//
2612855Sgabeblack@google.com// $Log: proc_ctrl_immed.cpp,v $
2712855Sgabeblack@google.com// Revision 1.3  2011/09/01 15:47:15  acg
2812855Sgabeblack@google.com//  John Aynsley: correction for immediate method invocation on reset.
2912855Sgabeblack@google.com//
3012855Sgabeblack@google.com// Revision 1.2  2011/05/08 19:18:46  acg
3112855Sgabeblack@google.com//  Andy Goodrich: remove extraneous + prefixes from git diff.
3212855Sgabeblack@google.com//
3312855Sgabeblack@google.com
3412855Sgabeblack@google.com// Process control methods executed immediately in same eval phase
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES
3712855Sgabeblack@google.com
3812855Sgabeblack@google.com#include <systemc>
3912855Sgabeblack@google.com
4012855Sgabeblack@google.comusing namespace sc_core;
4112855Sgabeblack@google.comusing std::cout;
4212855Sgabeblack@google.comusing std::endl;
4312855Sgabeblack@google.com
4412855Sgabeblack@google.comstruct Top: sc_module
4512855Sgabeblack@google.com{
4612855Sgabeblack@google.com  Top(sc_module_name _name)
4712855Sgabeblack@google.com  : count(0), reset_count(0)
4812855Sgabeblack@google.com  {
4912855Sgabeblack@google.com    SC_THREAD(ctrl);
5012855Sgabeblack@google.com    SC_THREAD(target_thread);
5112855Sgabeblack@google.com      t = sc_get_current_process_handle();
5212855Sgabeblack@google.com
5312855Sgabeblack@google.com    SC_METHOD(target_method);
5412855Sgabeblack@google.com      sensitive << ev;
5512855Sgabeblack@google.com      dont_initialize();
5612855Sgabeblack@google.com      m = sc_get_current_process_handle();
5712855Sgabeblack@google.com      m.disable();
5812855Sgabeblack@google.com
5912855Sgabeblack@google.com    SC_METHOD(thread_reset_handler);
6012855Sgabeblack@google.com      sensitive << t.reset_event();
6112855Sgabeblack@google.com      dont_initialize();
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com    SC_METHOD(method_reset_handler);
6412855Sgabeblack@google.com      sensitive << m.reset_event();
6512855Sgabeblack@google.com      dont_initialize();
6612855Sgabeblack@google.com
6712855Sgabeblack@google.com    SC_METHOD(thread_terminated_handler);
6812855Sgabeblack@google.com      sensitive << t.terminated_event();
6912855Sgabeblack@google.com      dont_initialize();
7012855Sgabeblack@google.com
7112855Sgabeblack@google.com    SC_METHOD(method_terminated_handler);
7212855Sgabeblack@google.com      sensitive << m.terminated_event();
7312855Sgabeblack@google.com      dont_initialize();
7412855Sgabeblack@google.com
7512855Sgabeblack@google.com    SC_METHOD(yield_helper_method);
7612855Sgabeblack@google.com      sensitive << yield_event_1;
7712855Sgabeblack@google.com      dont_initialize();
7812855Sgabeblack@google.com
7912855Sgabeblack@google.com    f0 = f1 = f2 = f3 = f4 = f5 = f6 = f7 = f8 = f9 = 0;
8012855Sgabeblack@google.com    f10 = f11 = f12 = f13 = f14 = f15 = f16 = f17 = f18 = f19 = 0;
8112855Sgabeblack@google.com    f20 = f21 = f22 = f23 = f24 = f25 = f26 = f27 = f28 = f29 = 0;
8212855Sgabeblack@google.com    f30 = f31 = f32 = f33 = f34 = f35 = f36 = f37 = f38 = f39 = 0;
8312855Sgabeblack@google.com  }
8412855Sgabeblack@google.com
8512855Sgabeblack@google.com  sc_event yield_event_1, yield_event_2, target_awoke_event;
8612855Sgabeblack@google.com  int count, reset_count;
8712855Sgabeblack@google.com  int f0, f1, f2, f3, f4, f5, f6, f7, f8, f9;
8812855Sgabeblack@google.com  int f10, f11, f12, f13, f14, f15, f16, f17, f18, f19;
8912855Sgabeblack@google.com  int f20, f21, f22, f23, f24, f25, f26, f27, f28, f29;
9012855Sgabeblack@google.com  int f30, f31, f32, f33, f34, f35, f36, f37, f38, f39;
9112855Sgabeblack@google.com
9212855Sgabeblack@google.com  sc_event ev;
9312855Sgabeblack@google.com  sc_process_handle t, m;
9412855Sgabeblack@google.com  std::exception ex;
9512855Sgabeblack@google.com
9612855Sgabeblack@google.com  void ctrl()
9712855Sgabeblack@google.com  {
9812855Sgabeblack@google.com    wait(SC_ZERO_TIME);
9912855Sgabeblack@google.com    sc_assert( sc_delta_count() == 1 );
10012855Sgabeblack@google.com
10112855Sgabeblack@google.com    count = 1;
10212855Sgabeblack@google.com    ev.notify();
10312855Sgabeblack@google.com    wait(target_awoke_event);
10412855Sgabeblack@google.com
10512855Sgabeblack@google.com    count = 2;
10612855Sgabeblack@google.com    ev.notify();
10712855Sgabeblack@google.com    t.suspend();
10812855Sgabeblack@google.com    yield();
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com    count = 2;
11112855Sgabeblack@google.com    t.resume();
11212855Sgabeblack@google.com    wait(target_awoke_event);
11312855Sgabeblack@google.com
11412855Sgabeblack@google.com    count = 3;
11512855Sgabeblack@google.com    ev.notify();
11612855Sgabeblack@google.com    t.disable();
11712855Sgabeblack@google.com    wait(target_awoke_event);
11812855Sgabeblack@google.com
11912855Sgabeblack@google.com    count = 4;
12012855Sgabeblack@google.com    ev.notify();
12112855Sgabeblack@google.com    yield();
12212855Sgabeblack@google.com
12312855Sgabeblack@google.com    count = 5;
12412855Sgabeblack@google.com    t.enable();
12512855Sgabeblack@google.com    yield();
12612855Sgabeblack@google.com
12712855Sgabeblack@google.com    count = 6;
12812855Sgabeblack@google.com    ev.notify();
12912855Sgabeblack@google.com    wait(target_awoke_event);
13012855Sgabeblack@google.com
13112855Sgabeblack@google.com    count = 7;
13212855Sgabeblack@google.com    t.suspend();
13312855Sgabeblack@google.com    ev.notify();
13412855Sgabeblack@google.com    yield();
13512855Sgabeblack@google.com
13612855Sgabeblack@google.com    count = 8;
13712855Sgabeblack@google.com    t.resume();
13812855Sgabeblack@google.com    wait(target_awoke_event);
13912855Sgabeblack@google.com
14012855Sgabeblack@google.com    count = 9;
14112855Sgabeblack@google.com    reset_count = 9;
14212855Sgabeblack@google.com    t.sync_reset_on();
14312855Sgabeblack@google.com    ev.notify();
14412855Sgabeblack@google.com    wait(target_awoke_event);
14512855Sgabeblack@google.com
14612855Sgabeblack@google.com    count = 10;
14712855Sgabeblack@google.com    reset_count = 10;
14812855Sgabeblack@google.com    ev.notify();
14912855Sgabeblack@google.com    wait(target_awoke_event);
15012855Sgabeblack@google.com
15112855Sgabeblack@google.com    count = 11;
15212855Sgabeblack@google.com    t.sync_reset_off();
15312855Sgabeblack@google.com    ev.notify();
15412855Sgabeblack@google.com    wait(target_awoke_event);
15512855Sgabeblack@google.com
15612855Sgabeblack@google.com    count = 12;
15712855Sgabeblack@google.com    t.resume();
15812855Sgabeblack@google.com    t.enable();
15912855Sgabeblack@google.com    t.sync_reset_off();
16012855Sgabeblack@google.com    yield();
16112855Sgabeblack@google.com
16212855Sgabeblack@google.com    count = 13;
16312855Sgabeblack@google.com    ev.notify();
16412855Sgabeblack@google.com    wait(target_awoke_event);
16512855Sgabeblack@google.com
16612855Sgabeblack@google.com    count = 14;
16712855Sgabeblack@google.com    reset_count = 14;
16812855Sgabeblack@google.com    t.reset();
16912855Sgabeblack@google.com
17012855Sgabeblack@google.com    count = 15;
17112855Sgabeblack@google.com    ev.notify();
17212855Sgabeblack@google.com    wait(target_awoke_event);
17312855Sgabeblack@google.com
17412855Sgabeblack@google.com    count = 16;
17512855Sgabeblack@google.com    reset_count = 16;
17612855Sgabeblack@google.com    t.reset();
17712855Sgabeblack@google.com
17812855Sgabeblack@google.com    count = 17;
17912855Sgabeblack@google.com    t.throw_it(ex);
18012855Sgabeblack@google.com
18112855Sgabeblack@google.com    count = 18;
18212855Sgabeblack@google.com    t.kill();
18312855Sgabeblack@google.com    yield();
18412855Sgabeblack@google.com
18512855Sgabeblack@google.com    count = 19;
18612855Sgabeblack@google.com    m.enable();
18712855Sgabeblack@google.com    ev.notify();
18812855Sgabeblack@google.com    wait(target_awoke_event);
18912855Sgabeblack@google.com
19012855Sgabeblack@google.com    count = 20;
19112855Sgabeblack@google.com    ev.notify();
19212855Sgabeblack@google.com    m.suspend();
19312855Sgabeblack@google.com    yield();
19412855Sgabeblack@google.com
19512855Sgabeblack@google.com    count = 21;
19612855Sgabeblack@google.com    m.resume();
19712855Sgabeblack@google.com    wait(target_awoke_event);
19812855Sgabeblack@google.com
19912855Sgabeblack@google.com    count = 22;
20012855Sgabeblack@google.com    m.suspend();
20112855Sgabeblack@google.com    ev.notify();
20212855Sgabeblack@google.com    yield();
20312855Sgabeblack@google.com
20412855Sgabeblack@google.com    count = 23;
20512855Sgabeblack@google.com    m.resume();
20612855Sgabeblack@google.com    wait(target_awoke_event);
20712855Sgabeblack@google.com
20812855Sgabeblack@google.com    count = 24;
20912855Sgabeblack@google.com    m.suspend();
21012855Sgabeblack@google.com    ev.notify();
21112855Sgabeblack@google.com
21212855Sgabeblack@google.com    count = 25;
21312855Sgabeblack@google.com    m.resume();
21412855Sgabeblack@google.com    wait(target_awoke_event);
21512855Sgabeblack@google.com
21612855Sgabeblack@google.com    count = 26;
21712855Sgabeblack@google.com    reset_count = 26;
21812855Sgabeblack@google.com    m.sync_reset_on();
21912855Sgabeblack@google.com    ev.notify();
22012855Sgabeblack@google.com    wait(target_awoke_event);
22112855Sgabeblack@google.com
22212855Sgabeblack@google.com    count = 27;
22312855Sgabeblack@google.com    m.disable();
22412855Sgabeblack@google.com    ev.notify();
22512855Sgabeblack@google.com    yield();
22612855Sgabeblack@google.com
22712855Sgabeblack@google.com    count = 28;
22812855Sgabeblack@google.com    reset_count = 28;
22912855Sgabeblack@google.com    m.enable();
23012855Sgabeblack@google.com    ev.notify();
23112855Sgabeblack@google.com    wait(target_awoke_event);
23212855Sgabeblack@google.com
23312855Sgabeblack@google.com    count = 29;
23412855Sgabeblack@google.com    m.sync_reset_off();
23512855Sgabeblack@google.com    m.enable();
23612855Sgabeblack@google.com    m.resume();
23712855Sgabeblack@google.com    yield();
23812855Sgabeblack@google.com
23912855Sgabeblack@google.com    count = 30;
24012855Sgabeblack@google.com    reset_count = 30;
24112855Sgabeblack@google.com    m.reset();
24212855Sgabeblack@google.com
24312855Sgabeblack@google.com    count = 31;
24412855Sgabeblack@google.com    m.kill();
24512855Sgabeblack@google.com    yield();
24612855Sgabeblack@google.com
24712855Sgabeblack@google.com    sc_assert( sc_delta_count() == 1 );
24812855Sgabeblack@google.com    f27 = 1;
24912855Sgabeblack@google.com  }
25012855Sgabeblack@google.com
25112855Sgabeblack@google.com  void target_thread()
25212855Sgabeblack@google.com  {
25312855Sgabeblack@google.com    switch (count)
25412855Sgabeblack@google.com    {
25512855Sgabeblack@google.com      case  0: f0=1; break;
25612855Sgabeblack@google.com      case  9: f7=1; break;
25712855Sgabeblack@google.com      case 10: f9=1; break;
25812855Sgabeblack@google.com      case 14: f13=1; break;
25912855Sgabeblack@google.com      case 16: f16=1; break;
26012855Sgabeblack@google.com      default: sc_assert(false); break;
26112855Sgabeblack@google.com    }
26212855Sgabeblack@google.com    while (true)
26312855Sgabeblack@google.com    {
26412855Sgabeblack@google.com      try {
26512855Sgabeblack@google.com        target_awoke_event.notify();
26612855Sgabeblack@google.com        wait(ev);
26712855Sgabeblack@google.com       }
26812855Sgabeblack@google.com      catch (const std::exception& e) {
26912855Sgabeblack@google.com        switch (count)
27012855Sgabeblack@google.com        {
27112855Sgabeblack@google.com          case  9: sc_assert(sc_is_unwinding()); f6=1; break;
27212855Sgabeblack@google.com          case 10: sc_assert(sc_is_unwinding()); f8=1; break;
27312855Sgabeblack@google.com          case 14: sc_assert(sc_is_unwinding()); f12=1; break;
27412855Sgabeblack@google.com          case 16: sc_assert(sc_is_unwinding()); f15=1; break;
27512855Sgabeblack@google.com          case 17: sc_assert( !sc_is_unwinding() ); f17=1; break;
27612855Sgabeblack@google.com          case 18: sc_assert(sc_is_unwinding()); f19=1; break;
27712855Sgabeblack@google.com          default: sc_assert(false); break;
27812855Sgabeblack@google.com        }
27912855Sgabeblack@google.com        if ( sc_is_unwinding() )
28012855Sgabeblack@google.com          throw dynamic_cast<const sc_unwind_exception&>(e);
28112855Sgabeblack@google.com      }
28212855Sgabeblack@google.com      switch (count)
28312855Sgabeblack@google.com      {
28412855Sgabeblack@google.com        case  1: f1=1; break;
28512855Sgabeblack@google.com        case  2: f2=1; break;
28612855Sgabeblack@google.com        case  3: f3=1; break;
28712855Sgabeblack@google.com        case  6: f4=1; break;
28812855Sgabeblack@google.com        case  8: f5=1; break;
28912855Sgabeblack@google.com        case 11: f10=1; break;
29012855Sgabeblack@google.com        case 13: f11=1; break;
29112855Sgabeblack@google.com        case 15: f14=1; break;
29212855Sgabeblack@google.com        case 17: f18=1; break;
29312855Sgabeblack@google.com        default: sc_assert(false); break;
29412855Sgabeblack@google.com      }
29512855Sgabeblack@google.com    }
29612855Sgabeblack@google.com  }
29712855Sgabeblack@google.com
29812855Sgabeblack@google.com  void target_method()
29912855Sgabeblack@google.com  {
30012855Sgabeblack@google.com    switch (count)
30112855Sgabeblack@google.com    {
30212855Sgabeblack@google.com      case 19: f20=1; break;
30312855Sgabeblack@google.com      case 21: f21=1; break;
30412855Sgabeblack@google.com      case 23: f22=1; break;
30512855Sgabeblack@google.com      case 25: f23=1; break;
30612855Sgabeblack@google.com      case 26: f24=1; break;
30712855Sgabeblack@google.com      case 28: f25=1; break;
30812855Sgabeblack@google.com      case 30: f26=1; break;
30912855Sgabeblack@google.com      default: sc_assert(false); break;
31012855Sgabeblack@google.com    }
31112855Sgabeblack@google.com    target_awoke_event.notify();
31212855Sgabeblack@google.com  }
31312855Sgabeblack@google.com
31412855Sgabeblack@google.com  void thread_reset_handler()
31512855Sgabeblack@google.com  {
31612855Sgabeblack@google.com    switch (reset_count)
31712855Sgabeblack@google.com    {
31812855Sgabeblack@google.com      case  9: f30=1; break;
31912855Sgabeblack@google.com      case 10: f31=1; break;
32012855Sgabeblack@google.com      case 14: f32=1; break;
32112855Sgabeblack@google.com      case 16: f33=1; break;
32212855Sgabeblack@google.com      default: sc_assert(false); break;
32312855Sgabeblack@google.com    }
32412855Sgabeblack@google.com  }
32512855Sgabeblack@google.com
32612855Sgabeblack@google.com  void method_reset_handler()
32712855Sgabeblack@google.com  {
32812855Sgabeblack@google.com    switch (reset_count)
32912855Sgabeblack@google.com    {
33012855Sgabeblack@google.com      case 26: f34=1; break;
33112855Sgabeblack@google.com      case 28: f35=1; break;
33212855Sgabeblack@google.com      case 30: f36=1; break;
33312855Sgabeblack@google.com      default: sc_assert(false); break;
33412855Sgabeblack@google.com    }
33512855Sgabeblack@google.com  }
33612855Sgabeblack@google.com
33712855Sgabeblack@google.com  void thread_terminated_handler()
33812855Sgabeblack@google.com  {
33912855Sgabeblack@google.com    sc_assert(count == 18);
34012855Sgabeblack@google.com    sc_assert(sc_delta_count() == 1);
34112855Sgabeblack@google.com    f37 = 1;
34212855Sgabeblack@google.com  }
34312855Sgabeblack@google.com
34412855Sgabeblack@google.com  void method_terminated_handler()
34512855Sgabeblack@google.com  {
34612855Sgabeblack@google.com    sc_assert(count == 31);
34712855Sgabeblack@google.com    sc_assert(sc_delta_count() == 1);
34812855Sgabeblack@google.com    f38 = 1;
34912855Sgabeblack@google.com  }
35012855Sgabeblack@google.com
35112855Sgabeblack@google.com  void yield()
35212855Sgabeblack@google.com  {
35312855Sgabeblack@google.com    yield_event_1.notify();
35412855Sgabeblack@google.com    wait(yield_event_2);
35512855Sgabeblack@google.com  }
35612855Sgabeblack@google.com
35712855Sgabeblack@google.com  void yield_helper_method()
35812855Sgabeblack@google.com  {
35912855Sgabeblack@google.com    yield_event_2.notify();
36012855Sgabeblack@google.com  }
36112855Sgabeblack@google.com
36212855Sgabeblack@google.com  SC_HAS_PROCESS(Top);
36312855Sgabeblack@google.com};
36412855Sgabeblack@google.com
36512855Sgabeblack@google.comint sc_main(int argc, char* argv[])
36612855Sgabeblack@google.com{
36712855Sgabeblack@google.com  Top top("top");
36812855Sgabeblack@google.com
36912855Sgabeblack@google.com  sc_start();
37012855Sgabeblack@google.com
37112855Sgabeblack@google.com  sc_assert( top.f0 );
37212855Sgabeblack@google.com  sc_assert( top.f1 );
37312855Sgabeblack@google.com  sc_assert( top.f2 );
37412855Sgabeblack@google.com  sc_assert( top.f3 );
37512855Sgabeblack@google.com  sc_assert( top.f4 );
37612855Sgabeblack@google.com  sc_assert( top.f5 );
37712855Sgabeblack@google.com  sc_assert( top.f6 );
37812855Sgabeblack@google.com  sc_assert( top.f7 );
37912855Sgabeblack@google.com  sc_assert( top.f8 );
38012855Sgabeblack@google.com  sc_assert( top.f9 );
38112855Sgabeblack@google.com  sc_assert( top.f10 );
38212855Sgabeblack@google.com  sc_assert( top.f11 );
38312855Sgabeblack@google.com  sc_assert( top.f12 );
38412855Sgabeblack@google.com  sc_assert( top.f13 );
38512855Sgabeblack@google.com  sc_assert( top.f14 );
38612855Sgabeblack@google.com  sc_assert( top.f15 );
38712855Sgabeblack@google.com  sc_assert( top.f16 );
38812855Sgabeblack@google.com  sc_assert( top.f17 );
38912855Sgabeblack@google.com  sc_assert( top.f18 );
39012855Sgabeblack@google.com  sc_assert( top.f19 );
39112855Sgabeblack@google.com  sc_assert( top.f20 );
39212855Sgabeblack@google.com  sc_assert( top.f21 );
39312855Sgabeblack@google.com  sc_assert( top.f22 );
39412855Sgabeblack@google.com  sc_assert( top.f23 );
39512855Sgabeblack@google.com  sc_assert( top.f24 );
39612855Sgabeblack@google.com  sc_assert( top.f25 );
39712855Sgabeblack@google.com  sc_assert( top.f26 );
39812855Sgabeblack@google.com  sc_assert( top.f27 );
39912855Sgabeblack@google.com
40012855Sgabeblack@google.com  sc_assert( top.f30 );
40112855Sgabeblack@google.com  sc_assert( top.f31 );
40212855Sgabeblack@google.com  sc_assert( top.f32 );
40312855Sgabeblack@google.com  sc_assert( top.f33 );
40412855Sgabeblack@google.com  sc_assert( top.f34 );
40512855Sgabeblack@google.com  sc_assert( top.f35 );
40612855Sgabeblack@google.com  sc_assert( top.f36 );
40712855Sgabeblack@google.com  sc_assert( top.f37 );
40812855Sgabeblack@google.com  sc_assert( top.f38 );
40912855Sgabeblack@google.com
41012855Sgabeblack@google.com  cout << endl << "Success" << endl;
41112855Sgabeblack@google.com  return 0;
41212855Sgabeblack@google.com}
41312855Sgabeblack@google.com
414