sc_main.cc revision 13317
112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright 2018 Google, Inc.
312837Sgabeblack@google.com *
412837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312837Sgabeblack@google.com * this software without specific prior written permission.
1412837Sgabeblack@google.com *
1512837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612837Sgabeblack@google.com *
2712837Sgabeblack@google.com * Authors: Gabe Black
2812837Sgabeblack@google.com */
2912837Sgabeblack@google.com
3012837Sgabeblack@google.com#include <cstring>
3113081Sgabeblack@google.com#include <string>
3212837Sgabeblack@google.com
3312862Sgabeblack@google.com#include "base/fiber.hh"
3412837Sgabeblack@google.com#include "base/logging.hh"
3512862Sgabeblack@google.com#include "base/types.hh"
3612956Sgabeblack@google.com#include "sim/core.hh"
3712862Sgabeblack@google.com#include "sim/eventq.hh"
3812837Sgabeblack@google.com#include "sim/init.hh"
3912982Sgabeblack@google.com#include "systemc/core/kernel.hh"
4013038Sgabeblack@google.com#include "systemc/core/python.hh"
4112956Sgabeblack@google.com#include "systemc/core/scheduler.hh"
4213317Sgabeblack@google.com#include "systemc/ext/core/messages.hh"
4312837Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4412861Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
4513312Sgabeblack@google.com#include "systemc/utils/report.hh"
4612837Sgabeblack@google.com
4712949Sgabeblack@google.com// A weak symbol to detect if sc_main has been defined, and if so where it is.
4812949Sgabeblack@google.com[[gnu::weak]] int sc_main(int argc, char *argv[]);
4912837Sgabeblack@google.com
5012837Sgabeblack@google.comnamespace sc_core
5112837Sgabeblack@google.com{
5212837Sgabeblack@google.com
5312837Sgabeblack@google.comnamespace
5412837Sgabeblack@google.com{
5512837Sgabeblack@google.com
5612837Sgabeblack@google.combool scMainCalled = false;
5712837Sgabeblack@google.com
5812837Sgabeblack@google.comint _argc = 0;
5912837Sgabeblack@google.comchar **_argv = NULL;
6012837Sgabeblack@google.com
6112862Sgabeblack@google.comclass ScMainFiber : public Fiber
6212862Sgabeblack@google.com{
6313081Sgabeblack@google.com  public:
6413081Sgabeblack@google.com    std::string resultStr;
6513081Sgabeblack@google.com    int resultInt;
6613081Sgabeblack@google.com
6713081Sgabeblack@google.com    ScMainFiber() : resultInt(1) {}
6813081Sgabeblack@google.com
6912862Sgabeblack@google.com    void
7012862Sgabeblack@google.com    main()
7112862Sgabeblack@google.com    {
7212949Sgabeblack@google.com        if (::sc_main) {
7313081Sgabeblack@google.com            try {
7413081Sgabeblack@google.com                resultInt = ::sc_main(_argc, _argv);
7513081Sgabeblack@google.com                if (resultInt)
7613081Sgabeblack@google.com                    resultStr = "sc_main returned non-zero";
7713081Sgabeblack@google.com                else
7813081Sgabeblack@google.com                    resultStr = "sc_main finished";
7913081Sgabeblack@google.com                // Make sure no systemc events/notifications are scheduled
8013081Sgabeblack@google.com                // after sc_main returns.
8113081Sgabeblack@google.com            } catch (const sc_report &r) {
8213081Sgabeblack@google.com                // There was an exception nobody caught.
8313301Sgabeblack@google.com                resultStr = "uncaught sc_report";
8413312Sgabeblack@google.com                sc_gem5::reportHandlerProc(
8513301Sgabeblack@google.com                        r, sc_report_handler::get_catch_actions());
8613182Sgabeblack@google.com            } catch (...) {
8713182Sgabeblack@google.com                // There was some other type of exception we need to wrap.
8813301Sgabeblack@google.com                resultStr = "uncaught exception";
8913312Sgabeblack@google.com                sc_gem5::reportHandlerProc(
9013301Sgabeblack@google.com                        ::sc_gem5::reportifyException(),
9113301Sgabeblack@google.com                        sc_report_handler::get_catch_actions());
9213081Sgabeblack@google.com            }
9313077Sgabeblack@google.com            ::sc_gem5::Kernel::scMainFinished(true);
9413077Sgabeblack@google.com            ::sc_gem5::scheduler.clear();
9512949Sgabeblack@google.com        } else {
9612949Sgabeblack@google.com            // If python tries to call sc_main but no sc_main was defined...
9712949Sgabeblack@google.com            fatal("sc_main called but not defined.\n");
9812949Sgabeblack@google.com        }
9912862Sgabeblack@google.com    }
10012862Sgabeblack@google.com};
10112862Sgabeblack@google.com
10212862Sgabeblack@google.comScMainFiber scMainFiber;
10312862Sgabeblack@google.com
10412837Sgabeblack@google.com// This wrapper adapts the python version of sc_main to the c++ version.
10512837Sgabeblack@google.comvoid
10612837Sgabeblack@google.comsc_main(pybind11::args args)
10712837Sgabeblack@google.com{
10812837Sgabeblack@google.com    panic_if(scMainCalled, "sc_main called more than once.");
10912837Sgabeblack@google.com
11012837Sgabeblack@google.com    _argc = args.size();
11112837Sgabeblack@google.com    _argv = new char *[_argc];
11212837Sgabeblack@google.com
11312837Sgabeblack@google.com    // Initialize all the _argvs to NULL so we can delete [] them
11412837Sgabeblack@google.com    // unconditionally.
11512837Sgabeblack@google.com    for (int idx = 0; idx < _argc; idx++)
11612837Sgabeblack@google.com        _argv[idx] = NULL;
11712837Sgabeblack@google.com
11812837Sgabeblack@google.com    // Attempt to convert all the arguments to strings. If that fails, clean
11912837Sgabeblack@google.com    // up after ourselves. Also don't count this as a call to sc_main since
12012837Sgabeblack@google.com    // we never got to the c++ version of that function.
12112837Sgabeblack@google.com    try {
12212837Sgabeblack@google.com        for (int idx = 0; idx < _argc; idx++) {
12312837Sgabeblack@google.com            std::string arg = args[idx].cast<std::string>();
12412837Sgabeblack@google.com            _argv[idx] = new char[arg.length() + 1];
12512837Sgabeblack@google.com            strcpy(_argv[idx], arg.c_str());
12612837Sgabeblack@google.com        }
12712837Sgabeblack@google.com    } catch (...) {
12812837Sgabeblack@google.com        // If that didn't work for some reason (probably a conversion error)
12912837Sgabeblack@google.com        // blow away _argv and _argc and pass on the exception.
13012837Sgabeblack@google.com        for (int idx = 0; idx < _argc; idx++)
13112837Sgabeblack@google.com            delete [] _argv[idx];
13212837Sgabeblack@google.com        delete [] _argv;
13312837Sgabeblack@google.com        _argc = 0;
13412837Sgabeblack@google.com        throw;
13512837Sgabeblack@google.com    }
13612837Sgabeblack@google.com
13712837Sgabeblack@google.com    // At this point we're going to call the c++ sc_main, so we can't try
13812837Sgabeblack@google.com    // again later.
13912837Sgabeblack@google.com    scMainCalled = true;
14012837Sgabeblack@google.com
14112862Sgabeblack@google.com    scMainFiber.run();
14212837Sgabeblack@google.com}
14312837Sgabeblack@google.com
14413081Sgabeblack@google.comint
14513081Sgabeblack@google.comsc_main_result_code()
14613081Sgabeblack@google.com{
14713081Sgabeblack@google.com    return scMainFiber.resultInt;
14813081Sgabeblack@google.com}
14913081Sgabeblack@google.com
15013081Sgabeblack@google.comstd::string
15113081Sgabeblack@google.comsc_main_result_str()
15213081Sgabeblack@google.com{
15313081Sgabeblack@google.com    return scMainFiber.resultStr;
15413081Sgabeblack@google.com}
15513081Sgabeblack@google.com
15612837Sgabeblack@google.com// Make our sc_main wrapper available in the internal _m5 python module under
15712837Sgabeblack@google.com// the systemc submodule.
15813038Sgabeblack@google.com
15913038Sgabeblack@google.comstruct InstallScMain : public ::sc_gem5::PythonInitFunc
16012837Sgabeblack@google.com{
16113038Sgabeblack@google.com    void
16213038Sgabeblack@google.com    run(pybind11::module &systemc) override
16313038Sgabeblack@google.com    {
16413038Sgabeblack@google.com        systemc.def("sc_main", &sc_main);
16513081Sgabeblack@google.com        systemc.def("sc_main_result_code", &sc_main_result_code);
16613081Sgabeblack@google.com        systemc.def("sc_main_result_str", &sc_main_result_str);
16713038Sgabeblack@google.com    }
16813038Sgabeblack@google.com} installScMain;
16912837Sgabeblack@google.com
17012861Sgabeblack@google.comsc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA;
17112861Sgabeblack@google.com
17212837Sgabeblack@google.com} // anonymous namespace
17312837Sgabeblack@google.com
17412837Sgabeblack@google.comint
17512837Sgabeblack@google.comsc_argc()
17612837Sgabeblack@google.com{
17712837Sgabeblack@google.com    return _argc;
17812837Sgabeblack@google.com}
17912837Sgabeblack@google.com
18012837Sgabeblack@google.comconst char *const *
18112837Sgabeblack@google.comsc_argv()
18212837Sgabeblack@google.com{
18312837Sgabeblack@google.com    return _argv;
18412837Sgabeblack@google.com}
18512837Sgabeblack@google.com
18612860Sgabeblack@google.comvoid
18712860Sgabeblack@google.comsc_start()
18812860Sgabeblack@google.com{
18912962Sgabeblack@google.com    Tick now = ::sc_gem5::scheduler.getCurTick();
19012961Sgabeblack@google.com    sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION);
19112860Sgabeblack@google.com}
19212860Sgabeblack@google.com
19312860Sgabeblack@google.comvoid
19412860Sgabeblack@google.comsc_pause()
19512860Sgabeblack@google.com{
19612990Sgabeblack@google.com    if (::sc_gem5::Kernel::status() == SC_RUNNING)
19712961Sgabeblack@google.com        ::sc_gem5::scheduler.schedulePause();
19812860Sgabeblack@google.com}
19912860Sgabeblack@google.com
20012860Sgabeblack@google.comvoid
20112860Sgabeblack@google.comsc_start(const sc_time &time, sc_starvation_policy p)
20212860Sgabeblack@google.com{
20313061Sgabeblack@google.com    if (time.value() == 0) {
20413061Sgabeblack@google.com        ::sc_gem5::scheduler.oneCycle();
20513061Sgabeblack@google.com    } else {
20613061Sgabeblack@google.com        Tick now = ::sc_gem5::scheduler.getCurTick();
20713317Sgabeblack@google.com        if (MaxTick - now < time.value())
20813317Sgabeblack@google.com            SC_REPORT_ERROR(SC_ID_SIMULATION_TIME_OVERFLOW_, "");
20913061Sgabeblack@google.com        ::sc_gem5::scheduler.start(now + time.value(), p == SC_RUN_TO_TIME);
21013061Sgabeblack@google.com    }
21112860Sgabeblack@google.com}
21212860Sgabeblack@google.com
21312860Sgabeblack@google.comvoid
21412860Sgabeblack@google.comsc_set_stop_mode(sc_stop_mode mode)
21512860Sgabeblack@google.com{
21612861Sgabeblack@google.com    if (sc_is_running()) {
21713317Sgabeblack@google.com        SC_REPORT_ERROR(SC_ID_STOP_MODE_AFTER_START_, "");
21812861Sgabeblack@google.com        return;
21912861Sgabeblack@google.com    }
22012861Sgabeblack@google.com    _stop_mode = mode;
22112860Sgabeblack@google.com}
22212860Sgabeblack@google.com
22312860Sgabeblack@google.comsc_stop_mode
22412860Sgabeblack@google.comsc_get_stop_mode()
22512860Sgabeblack@google.com{
22612861Sgabeblack@google.com    return _stop_mode;
22712860Sgabeblack@google.com}
22812860Sgabeblack@google.com
22912860Sgabeblack@google.comvoid
23012860Sgabeblack@google.comsc_stop()
23112860Sgabeblack@google.com{
23213212Sgabeblack@google.com    static bool stop_called = false;
23313212Sgabeblack@google.com    if (stop_called) {
23413212Sgabeblack@google.com        static bool stop_warned = false;
23513212Sgabeblack@google.com        if (!stop_warned)
23613317Sgabeblack@google.com            SC_REPORT_WARNING(SC_ID_SIMULATION_STOP_CALLED_TWICE_, "");
23713212Sgabeblack@google.com        stop_warned = true;
23813212Sgabeblack@google.com        return;
23913212Sgabeblack@google.com    }
24013212Sgabeblack@google.com    stop_called = true;
24113212Sgabeblack@google.com
24212990Sgabeblack@google.com    if (::sc_gem5::Kernel::status() == SC_STOPPED)
24312961Sgabeblack@google.com        return;
24412961Sgabeblack@google.com
24513097Sgabeblack@google.com    if ((sc_get_status() & SC_RUNNING)) {
24612961Sgabeblack@google.com        bool finish_delta = (_stop_mode == SC_STOP_FINISH_DELTA);
24712961Sgabeblack@google.com        ::sc_gem5::scheduler.scheduleStop(finish_delta);
24812961Sgabeblack@google.com    } else {
24912990Sgabeblack@google.com        ::sc_gem5::Kernel::stop();
25012961Sgabeblack@google.com    }
25112860Sgabeblack@google.com}
25212860Sgabeblack@google.com
25312860Sgabeblack@google.comconst sc_time &
25412860Sgabeblack@google.comsc_time_stamp()
25512860Sgabeblack@google.com{
25613255Sgabeblack@google.com    static sc_time tstamp(1.0, SC_SEC);
25713255Sgabeblack@google.com    tstamp = sc_time::from_value(::sc_gem5::scheduler.getCurTick());
25812956Sgabeblack@google.com    return tstamp;
25912860Sgabeblack@google.com}
26012860Sgabeblack@google.com
26112860Sgabeblack@google.comsc_dt::uint64
26212860Sgabeblack@google.comsc_delta_count()
26312860Sgabeblack@google.com{
26412956Sgabeblack@google.com    return sc_gem5::scheduler.numCycles();
26512860Sgabeblack@google.com}
26612860Sgabeblack@google.com
26712860Sgabeblack@google.combool
26812860Sgabeblack@google.comsc_is_running()
26912860Sgabeblack@google.com{
27012982Sgabeblack@google.com    return sc_get_status() & (SC_RUNNING | SC_PAUSED);
27112860Sgabeblack@google.com}
27212860Sgabeblack@google.com
27312860Sgabeblack@google.combool
27412860Sgabeblack@google.comsc_pending_activity_at_current_time()
27512860Sgabeblack@google.com{
27612962Sgabeblack@google.com    return ::sc_gem5::scheduler.pendingCurr();
27712860Sgabeblack@google.com}
27812860Sgabeblack@google.com
27912860Sgabeblack@google.combool
28012860Sgabeblack@google.comsc_pending_activity_at_future_time()
28112860Sgabeblack@google.com{
28212962Sgabeblack@google.com    return ::sc_gem5::scheduler.pendingFuture();
28312860Sgabeblack@google.com}
28412860Sgabeblack@google.com
28512860Sgabeblack@google.combool
28612860Sgabeblack@google.comsc_pending_activity()
28712860Sgabeblack@google.com{
28812861Sgabeblack@google.com    return sc_pending_activity_at_current_time() ||
28912861Sgabeblack@google.com           sc_pending_activity_at_future_time();
29012860Sgabeblack@google.com}
29112860Sgabeblack@google.com
29212860Sgabeblack@google.comsc_time
29312860Sgabeblack@google.comsc_time_to_pending_activity()
29412860Sgabeblack@google.com{
29512962Sgabeblack@google.com    return sc_time::from_value(::sc_gem5::scheduler.timeToPending());
29612860Sgabeblack@google.com}
29712860Sgabeblack@google.com
29812860Sgabeblack@google.comsc_status
29912860Sgabeblack@google.comsc_get_status()
30012860Sgabeblack@google.com{
30112982Sgabeblack@google.com    return ::sc_gem5::kernel ? ::sc_gem5::kernel->status() : SC_ELABORATION;
30212860Sgabeblack@google.com}
30312860Sgabeblack@google.com
30413043Sgabeblack@google.comstd::ostream &
30513043Sgabeblack@google.comoperator << (std::ostream &os, sc_status s)
30613043Sgabeblack@google.com{
30713043Sgabeblack@google.com    switch (s) {
30813043Sgabeblack@google.com      case SC_ELABORATION:
30913043Sgabeblack@google.com        os << "SC_ELABORATION";
31013043Sgabeblack@google.com        break;
31113043Sgabeblack@google.com      case SC_BEFORE_END_OF_ELABORATION:
31213043Sgabeblack@google.com        os << "SC_BEFORE_END_OF_ELABORATION";
31313043Sgabeblack@google.com        break;
31413043Sgabeblack@google.com      case SC_END_OF_ELABORATION:
31513043Sgabeblack@google.com        os << "SC_END_OF_ELABORATION";
31613043Sgabeblack@google.com        break;
31713043Sgabeblack@google.com      case SC_START_OF_SIMULATION:
31813043Sgabeblack@google.com        os << "SC_START_OF_SIMULATION";
31913043Sgabeblack@google.com        break;
32013043Sgabeblack@google.com      case SC_RUNNING:
32113043Sgabeblack@google.com        os << "SC_RUNNING";
32213043Sgabeblack@google.com        break;
32313043Sgabeblack@google.com      case SC_PAUSED:
32413043Sgabeblack@google.com        os << "SC_PAUSED";
32513043Sgabeblack@google.com        break;
32613043Sgabeblack@google.com      case SC_STOPPED:
32713043Sgabeblack@google.com        os << "SC_STOPPED";
32813043Sgabeblack@google.com        break;
32913043Sgabeblack@google.com      case SC_END_OF_SIMULATION:
33013043Sgabeblack@google.com        os << "SC_END_OF_SIMULATION";
33113043Sgabeblack@google.com        break;
33213043Sgabeblack@google.com
33313043Sgabeblack@google.com        // Nonstandard
33413043Sgabeblack@google.com      case SC_END_OF_INITIALIZATION:
33513043Sgabeblack@google.com        os << "SC_END_OF_INITIALIZATION";
33613043Sgabeblack@google.com        break;
33713043Sgabeblack@google.com      case SC_END_OF_UPDATE:
33813043Sgabeblack@google.com        os << "SC_END_OF_UPDATE";
33913043Sgabeblack@google.com        break;
34013043Sgabeblack@google.com      case SC_BEFORE_TIMESTEP:
34113043Sgabeblack@google.com        os << "SC_BEFORE_TIMESTEP";
34213043Sgabeblack@google.com        break;
34313043Sgabeblack@google.com
34413043Sgabeblack@google.com      default:
34513043Sgabeblack@google.com        if (s & SC_STATUS_ANY) {
34613043Sgabeblack@google.com            const char *prefix = "(";
34713043Sgabeblack@google.com            for (sc_status m = (sc_status)0x1;
34813043Sgabeblack@google.com                    m < SC_STATUS_ANY; m = (sc_status)(m << 1)) {
34913043Sgabeblack@google.com                if (m & s) {
35013043Sgabeblack@google.com                    os << prefix;
35113043Sgabeblack@google.com                    prefix = "|";
35213043Sgabeblack@google.com                    os << m;
35313043Sgabeblack@google.com                }
35413043Sgabeblack@google.com            }
35513043Sgabeblack@google.com            os << ")";
35613043Sgabeblack@google.com        } else {
35713043Sgabeblack@google.com            ccprintf(os, "%#x", s);
35813043Sgabeblack@google.com        }
35913043Sgabeblack@google.com    }
36013043Sgabeblack@google.com
36113043Sgabeblack@google.com    return os;
36213043Sgabeblack@google.com}
36313043Sgabeblack@google.com
36412837Sgabeblack@google.com} // namespace sc_core
365