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  test15.cpp -- sc_writer_policy: check conflicts within an evaluation phase
2312855Sgabeblack@google.com
2412855Sgabeblack@google.com  Original Author: Philipp A. Hartmann, Intel, 2017-05-12
2512855Sgabeblack@google.com
2612855Sgabeblack@google.com *****************************************************************************/
2712855Sgabeblack@google.com
2812855Sgabeblack@google.com// see https://github.com/OSCI-WG/systemc/issues/222
2912855Sgabeblack@google.com
3012855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES
3112855Sgabeblack@google.com#include <systemc>
3212855Sgabeblack@google.com#include <iomanip>
3312855Sgabeblack@google.com#include <sstream>
3412855Sgabeblack@google.com
3512855Sgabeblack@google.comusing sc_core::SC_NS;
3612855Sgabeblack@google.comusing sc_core::SC_ZERO_TIME;
3712855Sgabeblack@google.com
3812855Sgabeblack@google.comSC_MODULE(dut)
3912855Sgabeblack@google.com{
4012855Sgabeblack@google.com    static const int num_drivers = 4;
4112855Sgabeblack@google.com
4212855Sgabeblack@google.com    SC_CTOR(dut)
4312855Sgabeblack@google.com      : sig("sig")
4412855Sgabeblack@google.com    {
4512855Sgabeblack@google.com        SC_THREAD(run);
4612855Sgabeblack@google.com        for(int id=1; id <= num_drivers; ++id) {
4712855Sgabeblack@google.com          // first and last driver write the same values
4812855Sgabeblack@google.com          spawn_driver( id, (id-1) % (num_drivers-1) + 1 );
4912855Sgabeblack@google.com        }
5012855Sgabeblack@google.com    }
5112855Sgabeblack@google.com
5212855Sgabeblack@google.comprivate:
5312855Sgabeblack@google.com    void spawn_driver(int id, int value) {
5412855Sgabeblack@google.com      sc_assert( id > 0 && id <= num_drivers );
5512855Sgabeblack@google.com      sc_core::sc_spawn_options sp;
5612855Sgabeblack@google.com      sp.set_sensitivity(&ev[id-1]);
5712855Sgabeblack@google.com      sp.spawn_method();
5812855Sgabeblack@google.com      sp.dont_initialize();
5912855Sgabeblack@google.com
6012855Sgabeblack@google.com      std::stringstream nm;
6112855Sgabeblack@google.com      nm << "p" << id;
6212855Sgabeblack@google.com      sc_spawn( sc_bind(&dut::driver, this, value), nm.str().c_str(), &sp );
6312855Sgabeblack@google.com    }
6412855Sgabeblack@google.com
6512855Sgabeblack@google.com    void run() {
6612855Sgabeblack@google.com      wait(1, SC_NS);
6712855Sgabeblack@google.com
6812855Sgabeblack@google.com      std::cout << "\n*** trigger each driver in individual delta cycles" << std::endl;
6912855Sgabeblack@google.com      for(int id=1; id <= num_drivers; ++id) {
7012855Sgabeblack@google.com        trigger(id);
7112855Sgabeblack@google.com        wait(SC_ZERO_TIME);
7212855Sgabeblack@google.com        log();
7312855Sgabeblack@google.com      }
7412855Sgabeblack@google.com
7512855Sgabeblack@google.com      wait(1, SC_NS);
7612855Sgabeblack@google.com      std::cout << "\n*** trigger 1-2-3 in the same delta cycle" << std::endl;
7712855Sgabeblack@google.com      trigger(1);
7812855Sgabeblack@google.com      trigger(2); // error expected
7912855Sgabeblack@google.com      trigger(3); // error expected
8012855Sgabeblack@google.com      wait(SC_ZERO_TIME);
8112855Sgabeblack@google.com      log();
8212855Sgabeblack@google.com
8312855Sgabeblack@google.com      wait(1, SC_NS);
8412855Sgabeblack@google.com      std::cout << "\n*** trigger 2-3-1 in the same delta cycle" << std::endl;
8512855Sgabeblack@google.com      trigger(2);
8612855Sgabeblack@google.com      trigger(3); // error expected
8712855Sgabeblack@google.com      trigger(1); // error expected
8812855Sgabeblack@google.com      wait(SC_ZERO_TIME);
8912855Sgabeblack@google.com      log();
9012855Sgabeblack@google.com
9112855Sgabeblack@google.com      wait(1, SC_NS);
9212855Sgabeblack@google.com      std::cout << "\n*** trigger 3-1-2 in the same delta cycle" << std::endl;
9312855Sgabeblack@google.com      trigger(3);
9412855Sgabeblack@google.com      trigger(1); // error expected
9512855Sgabeblack@google.com      trigger(2); // error expected
9612855Sgabeblack@google.com      wait(SC_ZERO_TIME);
9712855Sgabeblack@google.com      log();
9812855Sgabeblack@google.com
9912855Sgabeblack@google.com      wait(1, SC_NS);
10012855Sgabeblack@google.com      std::cout << "\n*** trigger 1-2-1-2 in the same delta cycle" << std::endl;
10112855Sgabeblack@google.com      trigger(1);
10212855Sgabeblack@google.com      trigger(2); // error expected
10312855Sgabeblack@google.com      trigger(1); // NO error expected (original process)
10412855Sgabeblack@google.com      trigger(2); // error expected
10512855Sgabeblack@google.com      wait(SC_ZERO_TIME);
10612855Sgabeblack@google.com      log();
10712855Sgabeblack@google.com
10812855Sgabeblack@google.com      wait(1, SC_NS);
10912855Sgabeblack@google.com      std::cout << "\n*** trigger 2-1 in the same delta cycle" << std::endl;
11012855Sgabeblack@google.com      trigger(2);
11112855Sgabeblack@google.com      trigger(1); // error expected
11212855Sgabeblack@google.com      wait(SC_ZERO_TIME);
11312855Sgabeblack@google.com      log();
11412855Sgabeblack@google.com
11512855Sgabeblack@google.com      wait(1, SC_NS);
11612855Sgabeblack@google.com      std::cout << "\n*** trigger 1-3 in the same delta cycle" << std::endl;
11712855Sgabeblack@google.com      trigger(1);
11812855Sgabeblack@google.com      trigger(3); // error expected
11912855Sgabeblack@google.com      wait(SC_ZERO_TIME);
12012855Sgabeblack@google.com      log();
12112855Sgabeblack@google.com
12212855Sgabeblack@google.com      wait(1, SC_NS);
12312855Sgabeblack@google.com      std::cout << "\n*** trigger 1-4-1 in the same delta cycle" << std::endl;
12412855Sgabeblack@google.com      trigger(1);
12512855Sgabeblack@google.com      trigger(4); // error expected? (same value)
12612855Sgabeblack@google.com      trigger(1); // NO error expected (original process)
12712855Sgabeblack@google.com      wait(SC_ZERO_TIME);
12812855Sgabeblack@google.com      log();
12912855Sgabeblack@google.com
13012855Sgabeblack@google.com      wait(1, SC_NS);
13112855Sgabeblack@google.com      std::cout << "\n*** trigger 4-1 in the same delta cycle" << std::endl;
13212855Sgabeblack@google.com      trigger(4);
13312855Sgabeblack@google.com      trigger(1); // error expected? (same value)
13412855Sgabeblack@google.com      wait(SC_ZERO_TIME);
13512855Sgabeblack@google.com      log();
13612855Sgabeblack@google.com    }
13712855Sgabeblack@google.com
13812855Sgabeblack@google.com    void trigger(int id) {
13912855Sgabeblack@google.com      sc_assert( id > 0 && id <= num_drivers );
14012855Sgabeblack@google.com      ev[id-1].notify();
14112855Sgabeblack@google.com      wait(ev_schedule);
14212855Sgabeblack@google.com    }
14312855Sgabeblack@google.com
14412855Sgabeblack@google.com    void driver(int value) {
14512855Sgabeblack@google.com        log(value);
14612855Sgabeblack@google.com        try {
14712855Sgabeblack@google.com            sig.write(value);
14812855Sgabeblack@google.com        } catch (const sc_core::sc_report& msg ) {
14912855Sgabeblack@google.com            std::cout << "\n" << msg.what() << "\n" << std::endl;
15012855Sgabeblack@google.com        }
15112855Sgabeblack@google.com        ev_schedule.notify();
15212855Sgabeblack@google.com    }
15312855Sgabeblack@google.com
15412855Sgabeblack@google.com    void log(int value = -1) {
15512855Sgabeblack@google.com        std::cout
15612855Sgabeblack@google.com          << std::setw(8) << sc_core::sc_get_current_process_handle().name() << ": "
15712855Sgabeblack@google.com          << std::setw(5) << sc_core::sc_time_stamp()
15812855Sgabeblack@google.com          << " @ " << std::setw(2) << sc_core::sc_delta_count() << ": "
15912855Sgabeblack@google.com          << ( (value!=-1) ? "writing " : "reading " )
16012855Sgabeblack@google.com          << sig.name() << " = "
16112855Sgabeblack@google.com          << ( (value!=-1) ? value : sig.read() )
16212855Sgabeblack@google.com          << std::endl;
16312855Sgabeblack@google.com    }
16412855Sgabeblack@google.com
16512855Sgabeblack@google.com    sc_core::sc_signal<int, sc_core::SC_MANY_WRITERS> sig;
16612855Sgabeblack@google.com    sc_core::sc_event ev_schedule, ev[num_drivers];
16712855Sgabeblack@google.com};
16812855Sgabeblack@google.com
16912855Sgabeblack@google.comint sc_main( int, char*[] )
17012855Sgabeblack@google.com{
17112855Sgabeblack@google.com    dut top("dut");
17212855Sgabeblack@google.com    sc_core::sc_start();
17312855Sgabeblack@google.com    std::cout << "\nProgram completed" << std::endl;
17412855Sgabeblack@google.com    return 0;
17512855Sgabeblack@google.com}
176