sc_main.cc revision 13403
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"
4113403Sgabeblack@google.com#include "systemc/core/sc_main_fiber.hh"
4212956Sgabeblack@google.com#include "systemc/core/scheduler.hh"
4313317Sgabeblack@google.com#include "systemc/ext/core/messages.hh"
4412837Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4512861Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
4613312Sgabeblack@google.com#include "systemc/utils/report.hh"
4712837Sgabeblack@google.com
4812837Sgabeblack@google.comnamespace sc_core
4912837Sgabeblack@google.com{
5012837Sgabeblack@google.com
5112837Sgabeblack@google.comnamespace
5212837Sgabeblack@google.com{
5312837Sgabeblack@google.com
5412837Sgabeblack@google.com// This wrapper adapts the python version of sc_main to the c++ version.
5512837Sgabeblack@google.comvoid
5612837Sgabeblack@google.comsc_main(pybind11::args args)
5712837Sgabeblack@google.com{
5813403Sgabeblack@google.com    panic_if(::sc_gem5::scMainFiber.called(),
5913403Sgabeblack@google.com            "sc_main called more than once.");
6012837Sgabeblack@google.com
6113403Sgabeblack@google.com    int argc = args.size();
6213403Sgabeblack@google.com    char **argv = new char *[argc];
6312837Sgabeblack@google.com
6413403Sgabeblack@google.com    // Initialize all the argvs to NULL so we can delete [] them
6512837Sgabeblack@google.com    // unconditionally.
6613403Sgabeblack@google.com    for (int idx = 0; idx < argc; idx++)
6713403Sgabeblack@google.com        argv[idx] = NULL;
6812837Sgabeblack@google.com
6912837Sgabeblack@google.com    // Attempt to convert all the arguments to strings. If that fails, clean
7012837Sgabeblack@google.com    // up after ourselves. Also don't count this as a call to sc_main since
7112837Sgabeblack@google.com    // we never got to the c++ version of that function.
7212837Sgabeblack@google.com    try {
7313403Sgabeblack@google.com        for (int idx = 0; idx < argc; idx++) {
7412837Sgabeblack@google.com            std::string arg = args[idx].cast<std::string>();
7513403Sgabeblack@google.com            argv[idx] = new char[arg.length() + 1];
7613403Sgabeblack@google.com            strcpy(argv[idx], arg.c_str());
7712837Sgabeblack@google.com        }
7812837Sgabeblack@google.com    } catch (...) {
7912837Sgabeblack@google.com        // If that didn't work for some reason (probably a conversion error)
8013403Sgabeblack@google.com        // blow away argv and argc and pass on the exception.
8113403Sgabeblack@google.com        for (int idx = 0; idx < argc; idx++)
8213403Sgabeblack@google.com            delete [] argv[idx];
8313403Sgabeblack@google.com        delete [] argv;
8413403Sgabeblack@google.com        argc = 0;
8512837Sgabeblack@google.com        throw;
8612837Sgabeblack@google.com    }
8712837Sgabeblack@google.com
8813403Sgabeblack@google.com    ::sc_gem5::scMainFiber.setArgs(argc, argv);
8913403Sgabeblack@google.com    ::sc_gem5::scMainFiber.run();
9012837Sgabeblack@google.com}
9112837Sgabeblack@google.com
9213081Sgabeblack@google.comint
9313081Sgabeblack@google.comsc_main_result_code()
9413081Sgabeblack@google.com{
9513403Sgabeblack@google.com    return ::sc_gem5::scMainFiber.resultInt();
9613081Sgabeblack@google.com}
9713081Sgabeblack@google.com
9813081Sgabeblack@google.comstd::string
9913081Sgabeblack@google.comsc_main_result_str()
10013081Sgabeblack@google.com{
10113403Sgabeblack@google.com    return ::sc_gem5::scMainFiber.resultStr();
10213081Sgabeblack@google.com}
10313081Sgabeblack@google.com
10412837Sgabeblack@google.com// Make our sc_main wrapper available in the internal _m5 python module under
10512837Sgabeblack@google.com// the systemc submodule.
10613038Sgabeblack@google.com
10713038Sgabeblack@google.comstruct InstallScMain : public ::sc_gem5::PythonInitFunc
10812837Sgabeblack@google.com{
10913038Sgabeblack@google.com    void
11013038Sgabeblack@google.com    run(pybind11::module &systemc) override
11113038Sgabeblack@google.com    {
11213038Sgabeblack@google.com        systemc.def("sc_main", &sc_main);
11313081Sgabeblack@google.com        systemc.def("sc_main_result_code", &sc_main_result_code);
11413081Sgabeblack@google.com        systemc.def("sc_main_result_str", &sc_main_result_str);
11513038Sgabeblack@google.com    }
11613038Sgabeblack@google.com} installScMain;
11712837Sgabeblack@google.com
11812861Sgabeblack@google.comsc_stop_mode _stop_mode = SC_STOP_FINISH_DELTA;
11912861Sgabeblack@google.com
12012837Sgabeblack@google.com} // anonymous namespace
12112837Sgabeblack@google.com
12212837Sgabeblack@google.comint
12312837Sgabeblack@google.comsc_argc()
12412837Sgabeblack@google.com{
12513403Sgabeblack@google.com    return ::sc_gem5::scMainFiber.argc();
12612837Sgabeblack@google.com}
12712837Sgabeblack@google.com
12812837Sgabeblack@google.comconst char *const *
12912837Sgabeblack@google.comsc_argv()
13012837Sgabeblack@google.com{
13113403Sgabeblack@google.com    return ::sc_gem5::scMainFiber.argv();
13212837Sgabeblack@google.com}
13312837Sgabeblack@google.com
13412860Sgabeblack@google.comvoid
13512860Sgabeblack@google.comsc_start()
13612860Sgabeblack@google.com{
13712962Sgabeblack@google.com    Tick now = ::sc_gem5::scheduler.getCurTick();
13812961Sgabeblack@google.com    sc_start(sc_time::from_value(MaxTick - now), SC_EXIT_ON_STARVATION);
13912860Sgabeblack@google.com}
14012860Sgabeblack@google.com
14112860Sgabeblack@google.comvoid
14212860Sgabeblack@google.comsc_pause()
14312860Sgabeblack@google.com{
14412990Sgabeblack@google.com    if (::sc_gem5::Kernel::status() == SC_RUNNING)
14512961Sgabeblack@google.com        ::sc_gem5::scheduler.schedulePause();
14612860Sgabeblack@google.com}
14712860Sgabeblack@google.com
14812860Sgabeblack@google.comvoid
14912860Sgabeblack@google.comsc_start(const sc_time &time, sc_starvation_policy p)
15012860Sgabeblack@google.com{
15113061Sgabeblack@google.com    if (time.value() == 0) {
15213061Sgabeblack@google.com        ::sc_gem5::scheduler.oneCycle();
15313061Sgabeblack@google.com    } else {
15413061Sgabeblack@google.com        Tick now = ::sc_gem5::scheduler.getCurTick();
15513317Sgabeblack@google.com        if (MaxTick - now < time.value())
15613317Sgabeblack@google.com            SC_REPORT_ERROR(SC_ID_SIMULATION_TIME_OVERFLOW_, "");
15713061Sgabeblack@google.com        ::sc_gem5::scheduler.start(now + time.value(), p == SC_RUN_TO_TIME);
15813061Sgabeblack@google.com    }
15912860Sgabeblack@google.com}
16012860Sgabeblack@google.com
16112860Sgabeblack@google.comvoid
16212860Sgabeblack@google.comsc_set_stop_mode(sc_stop_mode mode)
16312860Sgabeblack@google.com{
16412861Sgabeblack@google.com    if (sc_is_running()) {
16513317Sgabeblack@google.com        SC_REPORT_ERROR(SC_ID_STOP_MODE_AFTER_START_, "");
16612861Sgabeblack@google.com        return;
16712861Sgabeblack@google.com    }
16812861Sgabeblack@google.com    _stop_mode = mode;
16912860Sgabeblack@google.com}
17012860Sgabeblack@google.com
17112860Sgabeblack@google.comsc_stop_mode
17212860Sgabeblack@google.comsc_get_stop_mode()
17312860Sgabeblack@google.com{
17412861Sgabeblack@google.com    return _stop_mode;
17512860Sgabeblack@google.com}
17612860Sgabeblack@google.com
17712860Sgabeblack@google.comvoid
17812860Sgabeblack@google.comsc_stop()
17912860Sgabeblack@google.com{
18013212Sgabeblack@google.com    static bool stop_called = false;
18113212Sgabeblack@google.com    if (stop_called) {
18213212Sgabeblack@google.com        static bool stop_warned = false;
18313212Sgabeblack@google.com        if (!stop_warned)
18413317Sgabeblack@google.com            SC_REPORT_WARNING(SC_ID_SIMULATION_STOP_CALLED_TWICE_, "");
18513212Sgabeblack@google.com        stop_warned = true;
18613212Sgabeblack@google.com        return;
18713212Sgabeblack@google.com    }
18813212Sgabeblack@google.com    stop_called = true;
18913212Sgabeblack@google.com
19012990Sgabeblack@google.com    if (::sc_gem5::Kernel::status() == SC_STOPPED)
19112961Sgabeblack@google.com        return;
19212961Sgabeblack@google.com
19313097Sgabeblack@google.com    if ((sc_get_status() & SC_RUNNING)) {
19412961Sgabeblack@google.com        bool finish_delta = (_stop_mode == SC_STOP_FINISH_DELTA);
19512961Sgabeblack@google.com        ::sc_gem5::scheduler.scheduleStop(finish_delta);
19612961Sgabeblack@google.com    } else {
19712990Sgabeblack@google.com        ::sc_gem5::Kernel::stop();
19812961Sgabeblack@google.com    }
19912860Sgabeblack@google.com}
20012860Sgabeblack@google.com
20112860Sgabeblack@google.comconst sc_time &
20212860Sgabeblack@google.comsc_time_stamp()
20312860Sgabeblack@google.com{
20413255Sgabeblack@google.com    static sc_time tstamp(1.0, SC_SEC);
20513255Sgabeblack@google.com    tstamp = sc_time::from_value(::sc_gem5::scheduler.getCurTick());
20612956Sgabeblack@google.com    return tstamp;
20712860Sgabeblack@google.com}
20812860Sgabeblack@google.com
20912860Sgabeblack@google.comsc_dt::uint64
21012860Sgabeblack@google.comsc_delta_count()
21112860Sgabeblack@google.com{
21212956Sgabeblack@google.com    return sc_gem5::scheduler.numCycles();
21312860Sgabeblack@google.com}
21412860Sgabeblack@google.com
21512860Sgabeblack@google.combool
21612860Sgabeblack@google.comsc_is_running()
21712860Sgabeblack@google.com{
21812982Sgabeblack@google.com    return sc_get_status() & (SC_RUNNING | SC_PAUSED);
21912860Sgabeblack@google.com}
22012860Sgabeblack@google.com
22112860Sgabeblack@google.combool
22212860Sgabeblack@google.comsc_pending_activity_at_current_time()
22312860Sgabeblack@google.com{
22412962Sgabeblack@google.com    return ::sc_gem5::scheduler.pendingCurr();
22512860Sgabeblack@google.com}
22612860Sgabeblack@google.com
22712860Sgabeblack@google.combool
22812860Sgabeblack@google.comsc_pending_activity_at_future_time()
22912860Sgabeblack@google.com{
23012962Sgabeblack@google.com    return ::sc_gem5::scheduler.pendingFuture();
23112860Sgabeblack@google.com}
23212860Sgabeblack@google.com
23312860Sgabeblack@google.combool
23412860Sgabeblack@google.comsc_pending_activity()
23512860Sgabeblack@google.com{
23612861Sgabeblack@google.com    return sc_pending_activity_at_current_time() ||
23712861Sgabeblack@google.com           sc_pending_activity_at_future_time();
23812860Sgabeblack@google.com}
23912860Sgabeblack@google.com
24012860Sgabeblack@google.comsc_time
24112860Sgabeblack@google.comsc_time_to_pending_activity()
24212860Sgabeblack@google.com{
24312962Sgabeblack@google.com    return sc_time::from_value(::sc_gem5::scheduler.timeToPending());
24412860Sgabeblack@google.com}
24512860Sgabeblack@google.com
24612860Sgabeblack@google.comsc_status
24712860Sgabeblack@google.comsc_get_status()
24812860Sgabeblack@google.com{
24912982Sgabeblack@google.com    return ::sc_gem5::kernel ? ::sc_gem5::kernel->status() : SC_ELABORATION;
25012860Sgabeblack@google.com}
25112860Sgabeblack@google.com
25213043Sgabeblack@google.comstd::ostream &
25313043Sgabeblack@google.comoperator << (std::ostream &os, sc_status s)
25413043Sgabeblack@google.com{
25513043Sgabeblack@google.com    switch (s) {
25613043Sgabeblack@google.com      case SC_ELABORATION:
25713043Sgabeblack@google.com        os << "SC_ELABORATION";
25813043Sgabeblack@google.com        break;
25913043Sgabeblack@google.com      case SC_BEFORE_END_OF_ELABORATION:
26013043Sgabeblack@google.com        os << "SC_BEFORE_END_OF_ELABORATION";
26113043Sgabeblack@google.com        break;
26213043Sgabeblack@google.com      case SC_END_OF_ELABORATION:
26313043Sgabeblack@google.com        os << "SC_END_OF_ELABORATION";
26413043Sgabeblack@google.com        break;
26513043Sgabeblack@google.com      case SC_START_OF_SIMULATION:
26613043Sgabeblack@google.com        os << "SC_START_OF_SIMULATION";
26713043Sgabeblack@google.com        break;
26813043Sgabeblack@google.com      case SC_RUNNING:
26913043Sgabeblack@google.com        os << "SC_RUNNING";
27013043Sgabeblack@google.com        break;
27113043Sgabeblack@google.com      case SC_PAUSED:
27213043Sgabeblack@google.com        os << "SC_PAUSED";
27313043Sgabeblack@google.com        break;
27413043Sgabeblack@google.com      case SC_STOPPED:
27513043Sgabeblack@google.com        os << "SC_STOPPED";
27613043Sgabeblack@google.com        break;
27713043Sgabeblack@google.com      case SC_END_OF_SIMULATION:
27813043Sgabeblack@google.com        os << "SC_END_OF_SIMULATION";
27913043Sgabeblack@google.com        break;
28013043Sgabeblack@google.com
28113043Sgabeblack@google.com        // Nonstandard
28213043Sgabeblack@google.com      case SC_END_OF_INITIALIZATION:
28313043Sgabeblack@google.com        os << "SC_END_OF_INITIALIZATION";
28413043Sgabeblack@google.com        break;
28513043Sgabeblack@google.com      case SC_END_OF_UPDATE:
28613043Sgabeblack@google.com        os << "SC_END_OF_UPDATE";
28713043Sgabeblack@google.com        break;
28813043Sgabeblack@google.com      case SC_BEFORE_TIMESTEP:
28913043Sgabeblack@google.com        os << "SC_BEFORE_TIMESTEP";
29013043Sgabeblack@google.com        break;
29113043Sgabeblack@google.com
29213043Sgabeblack@google.com      default:
29313043Sgabeblack@google.com        if (s & SC_STATUS_ANY) {
29413043Sgabeblack@google.com            const char *prefix = "(";
29513043Sgabeblack@google.com            for (sc_status m = (sc_status)0x1;
29613043Sgabeblack@google.com                    m < SC_STATUS_ANY; m = (sc_status)(m << 1)) {
29713043Sgabeblack@google.com                if (m & s) {
29813043Sgabeblack@google.com                    os << prefix;
29913043Sgabeblack@google.com                    prefix = "|";
30013043Sgabeblack@google.com                    os << m;
30113043Sgabeblack@google.com                }
30213043Sgabeblack@google.com            }
30313043Sgabeblack@google.com            os << ")";
30413043Sgabeblack@google.com        } else {
30513043Sgabeblack@google.com            ccprintf(os, "%#x", s);
30613043Sgabeblack@google.com        }
30713043Sgabeblack@google.com    }
30813043Sgabeblack@google.com
30913043Sgabeblack@google.com    return os;
31013043Sgabeblack@google.com}
31113043Sgabeblack@google.com
31212837Sgabeblack@google.com} // namespace sc_core
313