sc_clock.cc revision 13200
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"
3812841Sgabeblack@google.com#include "systemc/ext/channel/sc_clock.hh"
3913200Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh"
4012841Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
4112841Sgabeblack@google.com
4213065Sgabeblack@google.comnamespace sc_gem5
4313065Sgabeblack@google.com{
4413065Sgabeblack@google.com
4513065Sgabeblack@google.comclass ClockTick : public ScEvent
4613065Sgabeblack@google.com{
4713065Sgabeblack@google.com  private:
4813065Sgabeblack@google.com    ::sc_core::sc_clock *clock;
4913065Sgabeblack@google.com    ::sc_core::sc_time _period;
5013190Sgabeblack@google.com    std::string name;
5113065Sgabeblack@google.com    Process *p;
5213065Sgabeblack@google.com    ProcessMemberFuncWrapper<::sc_core::sc_clock> funcWrapper;
5313065Sgabeblack@google.com
5413065Sgabeblack@google.com  public:
5513065Sgabeblack@google.com    ClockTick(::sc_core::sc_clock *clock, bool to,
5613065Sgabeblack@google.com            ::sc_core::sc_time _period) :
5713065Sgabeblack@google.com        ScEvent([this]() { tick(); }),
5813190Sgabeblack@google.com        clock(clock), _period(_period), name(clock->basename()), p(nullptr),
5913065Sgabeblack@google.com        funcWrapper(clock, to ? &::sc_core::sc_clock::tickUp :
6013065Sgabeblack@google.com                                &::sc_core::sc_clock::tickDown)
6113065Sgabeblack@google.com    {
6213190Sgabeblack@google.com        name += std::string(to ? "_posedge_action" : "_negedge_action");
6313190Sgabeblack@google.com        name = ::sc_core::sc_gen_unique_name(name.c_str());
6413190Sgabeblack@google.com    }
6513190Sgabeblack@google.com
6613190Sgabeblack@google.com    void
6713190Sgabeblack@google.com    createProcess()
6813190Sgabeblack@google.com    {
6913190Sgabeblack@google.com        p = new Method(name.c_str(), &funcWrapper, true);
7013194Sgabeblack@google.com        p->dontInitialize(true);
7113135Sgabeblack@google.com        scheduler.reg(p);
7213065Sgabeblack@google.com    }
7313065Sgabeblack@google.com
7413065Sgabeblack@google.com    ~ClockTick()
7513065Sgabeblack@google.com    {
7613065Sgabeblack@google.com        if (scheduled())
7713065Sgabeblack@google.com            scheduler.deschedule(this);
7813190Sgabeblack@google.com        if (p)
7913190Sgabeblack@google.com            p->popListNode();
8013065Sgabeblack@google.com    }
8113065Sgabeblack@google.com
8213065Sgabeblack@google.com    void
8313065Sgabeblack@google.com    tick()
8413065Sgabeblack@google.com    {
8513065Sgabeblack@google.com        scheduler.schedule(this, _period);
8613065Sgabeblack@google.com        p->ready();
8713065Sgabeblack@google.com    }
8813065Sgabeblack@google.com};
8913065Sgabeblack@google.com
9013065Sgabeblack@google.com};
9113065Sgabeblack@google.com
9212841Sgabeblack@google.comnamespace sc_core
9312841Sgabeblack@google.com{
9412841Sgabeblack@google.com
9512841Sgabeblack@google.comsc_clock::sc_clock() :
9613065Sgabeblack@google.com    sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS),
9713065Sgabeblack@google.com            0.5, SC_ZERO_TIME, true)
9813065Sgabeblack@google.com{}
9912841Sgabeblack@google.com
10013065Sgabeblack@google.comsc_clock::sc_clock(const char *name) :
10113065Sgabeblack@google.com    sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true)
10213065Sgabeblack@google.com{}
10312841Sgabeblack@google.com
10412841Sgabeblack@google.comsc_clock::sc_clock(const char *name, const sc_time &period,
10512841Sgabeblack@google.com                   double duty_cycle, const sc_time &start_time,
10613065Sgabeblack@google.com                   bool posedge_first) :
10713065Sgabeblack@google.com    sc_interface(), sc_signal<bool>(name, posedge_first ? false : true),
10813065Sgabeblack@google.com    _period(period), _dutyCycle(duty_cycle), _startTime(start_time),
10913065Sgabeblack@google.com    _posedgeFirst(posedge_first)
11012841Sgabeblack@google.com{
11113065Sgabeblack@google.com    _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period);
11213065Sgabeblack@google.com    _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period);
11312841Sgabeblack@google.com}
11412841Sgabeblack@google.com
11512841Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
11613065Sgabeblack@google.com                   double duty_cycle) :
11713065Sgabeblack@google.com    sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME,
11813065Sgabeblack@google.com            true)
11913065Sgabeblack@google.com{}
12012841Sgabeblack@google.com
12112841Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
12212841Sgabeblack@google.com                   double duty_cycle, double start_time_v,
12313065Sgabeblack@google.com                   sc_time_unit start_time_tu, bool posedge_first) :
12413065Sgabeblack@google.com    sc_clock(name, sc_time(period_v, period_tu), duty_cycle,
12513065Sgabeblack@google.com            sc_time(start_time_v, start_time_tu), posedge_first)
12613065Sgabeblack@google.com{}
12712841Sgabeblack@google.com
12812878Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period, double duty_cycle,
12913065Sgabeblack@google.com                   double start_time, bool posedge_first) :
13013065Sgabeblack@google.com    sc_clock(name, sc_time(period, true), duty_cycle,
13113065Sgabeblack@google.com            sc_time(start_time, true), posedge_first)
13213065Sgabeblack@google.com{}
13313065Sgabeblack@google.com
13413065Sgabeblack@google.comsc_clock::~sc_clock()
13512878Sgabeblack@google.com{
13613065Sgabeblack@google.com    if (_gem5UpEdge->scheduled())
13713065Sgabeblack@google.com        ::sc_gem5::scheduler.deschedule(_gem5UpEdge);
13813065Sgabeblack@google.com    if (_gem5DownEdge->scheduled())
13913065Sgabeblack@google.com        ::sc_gem5::scheduler.deschedule(_gem5DownEdge);
14013065Sgabeblack@google.com    delete _gem5UpEdge;
14113065Sgabeblack@google.com    delete _gem5DownEdge;
14212878Sgabeblack@google.com}
14312878Sgabeblack@google.com
14412841Sgabeblack@google.comvoid
14512841Sgabeblack@google.comsc_clock::write(const bool &)
14612841Sgabeblack@google.com{
14713065Sgabeblack@google.com    panic("write() called on sc_clock.");
14812841Sgabeblack@google.com}
14912841Sgabeblack@google.com
15013065Sgabeblack@google.comconst sc_time &sc_clock::period() const { return _period; }
15113065Sgabeblack@google.comdouble sc_clock::duty_cycle() const { return _dutyCycle; }
15213065Sgabeblack@google.comconst sc_time &sc_clock::start_time() const { return _startTime; }
15313065Sgabeblack@google.combool sc_clock::posedge_first() const { return _posedgeFirst; }
15412841Sgabeblack@google.com
15512931Sgabeblack@google.comconst sc_time &
15612931Sgabeblack@google.comsc_clock::time_stamp()
15712931Sgabeblack@google.com{
15813200Sgabeblack@google.com    return sc_time_stamp();
15912931Sgabeblack@google.com}
16012931Sgabeblack@google.com
16113065Sgabeblack@google.comvoid
16213065Sgabeblack@google.comsc_clock::before_end_of_elaboration()
16313065Sgabeblack@google.com{
16413190Sgabeblack@google.com    _gem5UpEdge->createProcess();
16513190Sgabeblack@google.com    _gem5DownEdge->createProcess();
16613065Sgabeblack@google.com    if (_posedgeFirst) {
16713065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5UpEdge, _startTime);
16813065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5DownEdge,
16913065Sgabeblack@google.com                _startTime + _period * _dutyCycle);
17013065Sgabeblack@google.com    } else {
17113065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5DownEdge, _startTime);
17213065Sgabeblack@google.com        ::sc_gem5::scheduler.schedule(_gem5UpEdge,
17313065Sgabeblack@google.com                _startTime + _period * (1.0 - _dutyCycle));
17413065Sgabeblack@google.com    }
17513065Sgabeblack@google.com}
17612841Sgabeblack@google.com
17712841Sgabeblack@google.com} // namespace sc_core
178