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/*****************************************************************************
2112855Sgabeblack@google.com
2212855Sgabeblack@google.com  event_triggered.cpp -- test sc_event::triggered
2312855Sgabeblack@google.com
2412855Sgabeblack@google.com  Original Author: Philipp A. Hartmann, Intel Corporation - 2017-08-06
2512855Sgabeblack@google.com
2612855Sgabeblack@google.com *****************************************************************************/
2712855Sgabeblack@google.com
2812855Sgabeblack@google.com/*****************************************************************************
2912855Sgabeblack@google.com
3012855Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3112855Sgabeblack@google.com  changes you are making here.
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com      Name, Affiliation, Date:
3412855Sgabeblack@google.com  Description of Modification:
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com *****************************************************************************/
3712855Sgabeblack@google.com
3812855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES
3912855Sgabeblack@google.com#include <systemc>
4012855Sgabeblack@google.com#include <iomanip>
4112855Sgabeblack@google.com
4212855Sgabeblack@google.com#ifdef BENCHMARK
4312855Sgabeblack@google.com  static const sc_dt::uint64 num_events     = 128;
4412855Sgabeblack@google.com  static const sc_dt::uint64 num_triggers   = 4;
4512855Sgabeblack@google.com  static const sc_dt::uint64 num_iterations = 10000000;
4612855Sgabeblack@google.com# define CHECK(expr) ((void)0)
4712855Sgabeblack@google.com#else
4812855Sgabeblack@google.com  static const sc_dt::uint64 num_events     = 16;
4912855Sgabeblack@google.com  static const sc_dt::uint64 num_triggers   = 4;
5012855Sgabeblack@google.com  static const sc_dt::uint64 num_iterations = 4;
5112855Sgabeblack@google.com# define CHECK(expr) sc_assert(expr)
5212855Sgabeblack@google.com#endif
5312855Sgabeblack@google.com
5412855Sgabeblack@google.com#ifndef UINT64_C
5512855Sgabeblack@google.com#if defined(_WIN32) && !defined(__MINGW32__)
5612855Sgabeblack@google.com# define UINT64_C(v) v ## ui64
5712855Sgabeblack@google.com#else
5812855Sgabeblack@google.com# define UINT64_C(v) v ## ULL
5912855Sgabeblack@google.com#endif
6012855Sgabeblack@google.com#endif // UINT64_C
6112855Sgabeblack@google.com
6212855Sgabeblack@google.comusing namespace sc_core;
6312855Sgabeblack@google.com
6412855Sgabeblack@google.comSC_MODULE( module )
6512855Sgabeblack@google.com{
6612855Sgabeblack@google.com  sc_vector<sc_event> events;
6712855Sgabeblack@google.com  sc_event            event_return;
6812855Sgabeblack@google.com
6912855Sgabeblack@google.com  SC_CTOR( module )
7012855Sgabeblack@google.com    : events("ev", num_events)
7112855Sgabeblack@google.com    , m_rng_state()
7212855Sgabeblack@google.com  {
7312855Sgabeblack@google.com    SC_THREAD(driver);
7412855Sgabeblack@google.com
7512855Sgabeblack@google.com    SC_THREAD(consumer_dynamic);
7612855Sgabeblack@google.com    SC_THREAD(consumer_static); // odd events only
7712855Sgabeblack@google.com      for(unsigned i = 1; i<events.size(); i+=2)
7812855Sgabeblack@google.com        sensitive << events[i];
7912855Sgabeblack@google.com  }
8012855Sgabeblack@google.comprivate:
8112855Sgabeblack@google.com
8212855Sgabeblack@google.com  void driver()
8312855Sgabeblack@google.com  {
8412855Sgabeblack@google.com    CHECK( !event_return.triggered() );
8512855Sgabeblack@google.com    wait(1, SC_NS);
8612855Sgabeblack@google.com    CHECK( !event_return.triggered() );
8712855Sgabeblack@google.com
8812855Sgabeblack@google.com    random_notify();
8912855Sgabeblack@google.com    random_notify();
9012855Sgabeblack@google.com    wait(event_return);
9112855Sgabeblack@google.com
9212855Sgabeblack@google.com    random_notify(SC_ZERO_TIME);
9312855Sgabeblack@google.com    random_notify(SC_ZERO_TIME);
9412855Sgabeblack@google.com    wait(event_return);
9512855Sgabeblack@google.com
9612855Sgabeblack@google.com    random_notify(sc_time(1, SC_NS));
9712855Sgabeblack@google.com    random_notify(sc_time(1, SC_NS));
9812855Sgabeblack@google.com    wait(event_return);
9912855Sgabeblack@google.com    wait(2, SC_NS);
10012855Sgabeblack@google.com    CHECK( !event_return.triggered() );
10112855Sgabeblack@google.com
10212855Sgabeblack@google.com    for(unsigned i = 0; i < num_triggers; ++i)
10312855Sgabeblack@google.com      random_notify();
10412855Sgabeblack@google.com    wait(event_return);
10512855Sgabeblack@google.com    CHECK( event_return.triggered() );
10612855Sgabeblack@google.com
10712855Sgabeblack@google.com    for(unsigned i = 0; i < num_triggers; ++i)
10812855Sgabeblack@google.com      random_notify(SC_ZERO_TIME);
10912855Sgabeblack@google.com    wait(event_return);
11012855Sgabeblack@google.com    CHECK( event_return.triggered() );
11112855Sgabeblack@google.com
11212855Sgabeblack@google.com    for(unsigned iter = 0; iter < num_iterations; ++iter) {
11312855Sgabeblack@google.com      for(unsigned i = 0; i < num_triggers; ++i) {
11412855Sgabeblack@google.com        random_notify(sc_time(1, SC_NS));
11512855Sgabeblack@google.com      }
11612855Sgabeblack@google.com      wait(event_return);
11712855Sgabeblack@google.com    }
11812855Sgabeblack@google.com    CHECK( event_return.triggered() );
11912855Sgabeblack@google.com  }
12012855Sgabeblack@google.com
12112855Sgabeblack@google.com  void consumer_dynamic()
12212855Sgabeblack@google.com  {
12312855Sgabeblack@google.com    sc_event_or_list events_or; // even events only
12412855Sgabeblack@google.com    for(unsigned i = 0; i < events.size(); i+=2)
12512855Sgabeblack@google.com      events_or |= events[i];
12612855Sgabeblack@google.com
12712855Sgabeblack@google.com    while(true) {
12812855Sgabeblack@google.com      wait(events_or);
12912855Sgabeblack@google.com      print_triggered();
13012855Sgabeblack@google.com      event_return.notify();
13112855Sgabeblack@google.com    }
13212855Sgabeblack@google.com  }
13312855Sgabeblack@google.com
13412855Sgabeblack@google.com  void consumer_static()
13512855Sgabeblack@google.com  {
13612855Sgabeblack@google.com    while(true) {
13712855Sgabeblack@google.com      wait();
13812855Sgabeblack@google.com      print_triggered();
13912855Sgabeblack@google.com      event_return.notify();
14012855Sgabeblack@google.com    }
14112855Sgabeblack@google.com  }
14212855Sgabeblack@google.com
14312855Sgabeblack@google.com  void print_triggered()
14412855Sgabeblack@google.com  {
14512855Sgabeblack@google.com#ifndef BENCHMARK
14612855Sgabeblack@google.com    using namespace std;
14712855Sgabeblack@google.com    cout
14812855Sgabeblack@google.com      << setw(6) << sc_time_stamp()
14912855Sgabeblack@google.com      << " (" << sc_delta_count() << "): "
15012855Sgabeblack@google.com      << sc_get_current_process_handle().name() << ": ";
15112855Sgabeblack@google.com    for(unsigned i =0; i< events.size(); ++i)
15212855Sgabeblack@google.com      if (events[i].triggered())
15312855Sgabeblack@google.com        std::cout << events[i].basename() << " ";
15412855Sgabeblack@google.com    cout << endl;
15512855Sgabeblack@google.com#endif
15612855Sgabeblack@google.com  }
15712855Sgabeblack@google.com
15812855Sgabeblack@google.com  void random_notify()
15912855Sgabeblack@google.com    { random_notify(SC_ZERO_TIME, true); }
16012855Sgabeblack@google.com
16112855Sgabeblack@google.com  void random_notify(const sc_time& t, bool immediate = false)
16212855Sgabeblack@google.com  {
16312855Sgabeblack@google.com    sc_event& ev = events[ lcg_rng() % num_events ];
16412855Sgabeblack@google.com#ifndef BENCHMARK
16512855Sgabeblack@google.com    using namespace std;
16612855Sgabeblack@google.com    cout
16712855Sgabeblack@google.com      << setw(6) << sc_time_stamp()
16812855Sgabeblack@google.com      << " (" << sc_delta_count() << "): "
16912855Sgabeblack@google.com      << sc_get_current_process_handle().name() << ": "
17012855Sgabeblack@google.com      << ev.basename();
17112855Sgabeblack@google.com    if (immediate) {
17212855Sgabeblack@google.com      cout << ".notify()";
17312855Sgabeblack@google.com    } else {
17412855Sgabeblack@google.com      cout << ".notify(" << t << ")";
17512855Sgabeblack@google.com    }
17612855Sgabeblack@google.com    cout << endl;
17712855Sgabeblack@google.com#endif
17812855Sgabeblack@google.com    if (immediate) {
17912855Sgabeblack@google.com      ev.notify();
18012855Sgabeblack@google.com    } else {
18112855Sgabeblack@google.com      ev.notify(t);
18212855Sgabeblack@google.com    }
18312855Sgabeblack@google.com  }
18412855Sgabeblack@google.com
18512855Sgabeblack@google.com  unsigned lcg_rng()
18612855Sgabeblack@google.com  {
18712855Sgabeblack@google.com    m_rng_state = UINT64_C(2862933555777941757) * m_rng_state
18812855Sgabeblack@google.com                + UINT64_C(3037000493);
18912855Sgabeblack@google.com    return ( m_rng_state >> 48 );
19012855Sgabeblack@google.com  }
19112855Sgabeblack@google.com
19212855Sgabeblack@google.com  sc_dt::uint64 m_rng_state;
19312855Sgabeblack@google.com};
19412855Sgabeblack@google.com
19512855Sgabeblack@google.com
19612855Sgabeblack@google.comint
19712855Sgabeblack@google.comsc_main( int, char*[] )
19812855Sgabeblack@google.com{
19912855Sgabeblack@google.com    module m("m");
20012855Sgabeblack@google.com    sc_start();
20112855Sgabeblack@google.com    sc_stop();
20212855Sgabeblack@google.com    return 0;
20312855Sgabeblack@google.com}
204