112841Sgabeblack@google.com/*
212841Sgabeblack@google.com * Copyright 2018 Google, Inc.
312841Sgabeblack@google.com *
412841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512841Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012841Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312841Sgabeblack@google.com * this software without specific prior written permission.
1412841Sgabeblack@google.com *
1512841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012841Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112841Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312841Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512841Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612841Sgabeblack@google.com *
2712841Sgabeblack@google.com * Authors: Gabe Black
2812841Sgabeblack@google.com */
2912841Sgabeblack@google.com
3012841Sgabeblack@google.com#include "base/logging.hh"
3113065Sgabeblack@google.com#include "base/types.hh"
3213065Sgabeblack@google.com#include "sim/core.hh"
3313065Sgabeblack@google.com#include "sim/eventq.hh"
3413065Sgabeblack@google.com#include "systemc/core/kernel.hh"
3513135Sgabeblack@google.com#include "systemc/core/process_types.hh"
3613065Sgabeblack@google.com#include "systemc/core/sched_event.hh"
3713065Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3813324Sgabeblack@google.com#include "systemc/ext/channel/messages.hh"
3912841Sgabeblack@google.com#include "systemc/ext/channel/sc_clock.hh"
4013200Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4112841Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
4213297Sgabeblack@google.com#include "systemc/ext/utils/sc_report_handler.hh"
4312841Sgabeblack@google.com
4413065Sgabeblack@google.comnamespace sc_gem5
4513065Sgabeblack@google.com{
4613065Sgabeblack@google.com
4713065Sgabeblack@google.comclass ClockTick : public ScEvent
4813065Sgabeblack@google.com{
4913065Sgabeblack@google.com  private:
5013065Sgabeblack@google.com    ::sc_core::sc_time _period;
5113190Sgabeblack@google.com    std::string name;
5213065Sgabeblack@google.com    Process *p;
5313065Sgabeblack@google.com    ProcessMemberFuncWrapper<::sc_core::sc_clock> funcWrapper;
5413065Sgabeblack@google.com
5513065Sgabeblack@google.com  public:
5613065Sgabeblack@google.com    ClockTick(::sc_core::sc_clock *clock, bool to,
5713065Sgabeblack@google.com            ::sc_core::sc_time _period) :
5813065Sgabeblack@google.com        ScEvent([this]() { tick(); }),
5913379Sgabeblack@google.com        _period(_period), name(clock->basename()), p(nullptr),
6013065Sgabeblack@google.com        funcWrapper(clock, to ? &::sc_core::sc_clock::tickUp :
6113065Sgabeblack@google.com                                &::sc_core::sc_clock::tickDown)
6213065Sgabeblack@google.com    {
6313190Sgabeblack@google.com        name += std::string(to ? "_posedge_action" : "_negedge_action");
6413190Sgabeblack@google.com        name = ::sc_core::sc_gen_unique_name(name.c_str());
6513190Sgabeblack@google.com    }
6613190Sgabeblack@google.com
6713190Sgabeblack@google.com    void
6813190Sgabeblack@google.com    createProcess()
6913190Sgabeblack@google.com    {
7013190Sgabeblack@google.com        p = new Method(name.c_str(), &funcWrapper, true);
7113194Sgabeblack@google.com        p->dontInitialize(true);
7213135Sgabeblack@google.com        scheduler.reg(p);
7313065Sgabeblack@google.com    }
7413065Sgabeblack@google.com
7513065Sgabeblack@google.com    ~ClockTick()
7613065Sgabeblack@google.com    {
7713065Sgabeblack@google.com        if (scheduled())
7813065Sgabeblack@google.com            scheduler.deschedule(this);
7913190Sgabeblack@google.com        if (p)
8013190Sgabeblack@google.com            p->popListNode();
8113065Sgabeblack@google.com    }
8213065Sgabeblack@google.com
8313065Sgabeblack@google.com    void
8413065Sgabeblack@google.com    tick()
8513065Sgabeblack@google.com    {
8613065Sgabeblack@google.com        scheduler.schedule(this, _period);
8713065Sgabeblack@google.com        p->ready();
8813065Sgabeblack@google.com    }
8913065Sgabeblack@google.com};
9013065Sgabeblack@google.com
9113065Sgabeblack@google.com};
9213065Sgabeblack@google.com
9312841Sgabeblack@google.comnamespace sc_core
9412841Sgabeblack@google.com{
9512841Sgabeblack@google.com
9612841Sgabeblack@google.comsc_clock::sc_clock() :
9713065Sgabeblack@google.com    sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS),
9813065Sgabeblack@google.com            0.5, SC_ZERO_TIME, true)
9913065Sgabeblack@google.com{}
10012841Sgabeblack@google.com
10113065Sgabeblack@google.comsc_clock::sc_clock(const char *name) :
10213065Sgabeblack@google.com    sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true)
10313065Sgabeblack@google.com{}
10412841Sgabeblack@google.com
10512841Sgabeblack@google.comsc_clock::sc_clock(const char *name, const sc_time &period,
10612841Sgabeblack@google.com                   double duty_cycle, const sc_time &start_time,
10713065Sgabeblack@google.com                   bool posedge_first) :
10813065Sgabeblack@google.com    sc_interface(), sc_signal<bool>(name, posedge_first ? false : true),
10913065Sgabeblack@google.com    _period(period), _dutyCycle(duty_cycle), _startTime(start_time),
11013065Sgabeblack@google.com    _posedgeFirst(posedge_first)
11112841Sgabeblack@google.com{
11213297Sgabeblack@google.com    if (period == SC_ZERO_TIME) {
11313297Sgabeblack@google.com        std::string msg =
11413297Sgabeblack@google.com            "increase the period: clock '" +
11513297Sgabeblack@google.com            std::string(name) + "'";
11613324Sgabeblack@google.com        SC_REPORT_ERROR(SC_ID_CLOCK_PERIOD_ZERO_, msg.c_str());
11713297Sgabeblack@google.com    }
11813297Sgabeblack@google.com
11913297Sgabeblack@google.com    if (duty_cycle * period == SC_ZERO_TIME) {
12013297Sgabeblack@google.com        std::string msg =
12113297Sgabeblack@google.com            "increase the period or increase the duty cycle: clock '" +
12213297Sgabeblack@google.com            std::string(name) + "'";
12313324Sgabeblack@google.com        SC_REPORT_ERROR(SC_ID_CLOCK_HIGH_TIME_ZERO_, msg.c_str());
12413297Sgabeblack@google.com    }
12513297Sgabeblack@google.com
12613297Sgabeblack@google.com    if (duty_cycle * period == period) {
12713297Sgabeblack@google.com        std::string msg =
12813297Sgabeblack@google.com            "increase the period or decrease the duty cycle: clock '" +
12913297Sgabeblack@google.com            std::string(name) + "'";
13013324Sgabeblack@google.com        SC_REPORT_ERROR(SC_ID_CLOCK_LOW_TIME_ZERO_, msg.c_str());
13113297Sgabeblack@google.com    }
13213297Sgabeblack@google.com
13313065Sgabeblack@google.com    _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period);
13413065Sgabeblack@google.com    _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period);
13512841Sgabeblack@google.com}
13612841Sgabeblack@google.com
13712841Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
13813065Sgabeblack@google.com                   double duty_cycle) :
13913065Sgabeblack@google.com    sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME,
14013065Sgabeblack@google.com            true)
14113065Sgabeblack@google.com{}
14212841Sgabeblack@google.com
14312841Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
14412841Sgabeblack@google.com                   double duty_cycle, double start_time_v,
14513065Sgabeblack@google.com                   sc_time_unit start_time_tu, bool posedge_first) :
14613065Sgabeblack@google.com    sc_clock(name, sc_time(period_v, period_tu), duty_cycle,
14713065Sgabeblack@google.com            sc_time(start_time_v, start_time_tu), posedge_first)
14813065Sgabeblack@google.com{}
14912841Sgabeblack@google.com
15012878Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period, double duty_cycle,
15113065Sgabeblack@google.com                   double start_time, bool posedge_first) :
15213065Sgabeblack@google.com    sc_clock(name, sc_time(period, true), duty_cycle,
15313065Sgabeblack@google.com            sc_time(start_time, true), posedge_first)
15413065Sgabeblack@google.com{}
15513065Sgabeblack@google.com
15613065Sgabeblack@google.comsc_clock::~sc_clock()
15712878Sgabeblack@google.com{
15813065Sgabeblack@google.com    if (_gem5UpEdge->scheduled())
15913065Sgabeblack@google.com        ::sc_gem5::scheduler.deschedule(_gem5UpEdge);
16013065Sgabeblack@google.com    if (_gem5DownEdge->scheduled())
16113065Sgabeblack@google.com        ::sc_gem5::scheduler.deschedule(_gem5DownEdge);
16213065Sgabeblack@google.com    delete _gem5UpEdge;
16313065Sgabeblack@google.com    delete _gem5DownEdge;
16412878Sgabeblack@google.com}
16512878Sgabeblack@google.com
16612841Sgabeblack@google.comvoid
16712841Sgabeblack@google.comsc_clock::write(const bool &)
16812841Sgabeblack@google.com{
16913065Sgabeblack@google.com    panic("write() called on sc_clock.");
17012841Sgabeblack@google.com}
17112841Sgabeblack@google.com
17213065Sgabeblack@google.comconst sc_time &sc_clock::period() const { return _period; }
17313065Sgabeblack@google.comdouble sc_clock::duty_cycle() const { return _dutyCycle; }
17413065Sgabeblack@google.comconst sc_time &sc_clock::start_time() const { return _startTime; }
17513065Sgabeblack@google.combool sc_clock::posedge_first() const { return _posedgeFirst; }
17612841Sgabeblack@google.com
17712931Sgabeblack@google.comconst sc_time &
17812931Sgabeblack@google.comsc_clock::time_stamp()
17912931Sgabeblack@google.com{
18013200Sgabeblack@google.com    return sc_time_stamp();
18112931Sgabeblack@google.com}
18212931Sgabeblack@google.com
18313065Sgabeblack@google.comvoid
18413065Sgabeblack@google.comsc_clock::before_end_of_elaboration()
18513065Sgabeblack@google.com{
18613190Sgabeblack@google.com    _gem5UpEdge->createProcess();
18713190Sgabeblack@google.com    _gem5DownEdge->createProcess();
18813065Sgabeblack@google.com    if (_posedgeFirst) {
18913065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5UpEdge, _startTime);
19013065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5DownEdge,
19113065Sgabeblack@google.com                _startTime + _period * _dutyCycle);
19213065Sgabeblack@google.com    } else {
19313065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5DownEdge, _startTime);
19413065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5UpEdge,
19513065Sgabeblack@google.com                _startTime + _period * (1.0 - _dutyCycle));
19613065Sgabeblack@google.com    }
19713065Sgabeblack@google.com}
19812841Sgabeblack@google.com
19912841Sgabeblack@google.com} // namespace sc_core
200