sc_clock.cc revision 13194
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" 3912841Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name 4012841Sgabeblack@google.com 4113065Sgabeblack@google.comnamespace sc_gem5 4213065Sgabeblack@google.com{ 4313065Sgabeblack@google.com 4413065Sgabeblack@google.comclass ClockTick : public ScEvent 4513065Sgabeblack@google.com{ 4613065Sgabeblack@google.com private: 4713065Sgabeblack@google.com ::sc_core::sc_clock *clock; 4813065Sgabeblack@google.com ::sc_core::sc_time _period; 4913190Sgabeblack@google.com std::string name; 5013065Sgabeblack@google.com Process *p; 5113065Sgabeblack@google.com ProcessMemberFuncWrapper<::sc_core::sc_clock> funcWrapper; 5213065Sgabeblack@google.com 5313065Sgabeblack@google.com public: 5413065Sgabeblack@google.com ClockTick(::sc_core::sc_clock *clock, bool to, 5513065Sgabeblack@google.com ::sc_core::sc_time _period) : 5613065Sgabeblack@google.com ScEvent([this]() { tick(); }), 5713190Sgabeblack@google.com clock(clock), _period(_period), name(clock->basename()), p(nullptr), 5813065Sgabeblack@google.com funcWrapper(clock, to ? &::sc_core::sc_clock::tickUp : 5913065Sgabeblack@google.com &::sc_core::sc_clock::tickDown) 6013065Sgabeblack@google.com { 6113190Sgabeblack@google.com name += std::string(to ? "_posedge_action" : "_negedge_action"); 6213190Sgabeblack@google.com name = ::sc_core::sc_gen_unique_name(name.c_str()); 6313190Sgabeblack@google.com } 6413190Sgabeblack@google.com 6513190Sgabeblack@google.com void 6613190Sgabeblack@google.com createProcess() 6713190Sgabeblack@google.com { 6813190Sgabeblack@google.com p = new Method(name.c_str(), &funcWrapper, true); 6913194Sgabeblack@google.com p->dontInitialize(true); 7013135Sgabeblack@google.com scheduler.reg(p); 7113065Sgabeblack@google.com } 7213065Sgabeblack@google.com 7313065Sgabeblack@google.com ~ClockTick() 7413065Sgabeblack@google.com { 7513065Sgabeblack@google.com if (scheduled()) 7613065Sgabeblack@google.com scheduler.deschedule(this); 7713190Sgabeblack@google.com if (p) 7813190Sgabeblack@google.com p->popListNode(); 7913065Sgabeblack@google.com } 8013065Sgabeblack@google.com 8113065Sgabeblack@google.com void 8213065Sgabeblack@google.com tick() 8313065Sgabeblack@google.com { 8413065Sgabeblack@google.com scheduler.schedule(this, _period); 8513065Sgabeblack@google.com p->ready(); 8613065Sgabeblack@google.com } 8713065Sgabeblack@google.com}; 8813065Sgabeblack@google.com 8913065Sgabeblack@google.com}; 9013065Sgabeblack@google.com 9112841Sgabeblack@google.comnamespace sc_core 9212841Sgabeblack@google.com{ 9312841Sgabeblack@google.com 9412841Sgabeblack@google.comsc_clock::sc_clock() : 9513065Sgabeblack@google.com sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS), 9613065Sgabeblack@google.com 0.5, SC_ZERO_TIME, true) 9713065Sgabeblack@google.com{} 9812841Sgabeblack@google.com 9913065Sgabeblack@google.comsc_clock::sc_clock(const char *name) : 10013065Sgabeblack@google.com sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true) 10113065Sgabeblack@google.com{} 10212841Sgabeblack@google.com 10312841Sgabeblack@google.comsc_clock::sc_clock(const char *name, const sc_time &period, 10412841Sgabeblack@google.com double duty_cycle, const sc_time &start_time, 10513065Sgabeblack@google.com bool posedge_first) : 10613065Sgabeblack@google.com sc_interface(), sc_signal<bool>(name, posedge_first ? false : true), 10713065Sgabeblack@google.com _period(period), _dutyCycle(duty_cycle), _startTime(start_time), 10813065Sgabeblack@google.com _posedgeFirst(posedge_first) 10912841Sgabeblack@google.com{ 11013065Sgabeblack@google.com _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period); 11113065Sgabeblack@google.com _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period); 11212841Sgabeblack@google.com} 11312841Sgabeblack@google.com 11412841Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu, 11513065Sgabeblack@google.com double duty_cycle) : 11613065Sgabeblack@google.com sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME, 11713065Sgabeblack@google.com true) 11813065Sgabeblack@google.com{} 11912841Sgabeblack@google.com 12012841Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu, 12112841Sgabeblack@google.com double duty_cycle, double start_time_v, 12213065Sgabeblack@google.com sc_time_unit start_time_tu, bool posedge_first) : 12313065Sgabeblack@google.com sc_clock(name, sc_time(period_v, period_tu), duty_cycle, 12413065Sgabeblack@google.com sc_time(start_time_v, start_time_tu), posedge_first) 12513065Sgabeblack@google.com{} 12612841Sgabeblack@google.com 12712878Sgabeblack@google.comsc_clock::sc_clock(const char *name, double period, double duty_cycle, 12813065Sgabeblack@google.com double start_time, bool posedge_first) : 12913065Sgabeblack@google.com sc_clock(name, sc_time(period, true), duty_cycle, 13013065Sgabeblack@google.com sc_time(start_time, true), posedge_first) 13113065Sgabeblack@google.com{} 13213065Sgabeblack@google.com 13313065Sgabeblack@google.comsc_clock::~sc_clock() 13412878Sgabeblack@google.com{ 13513065Sgabeblack@google.com if (_gem5UpEdge->scheduled()) 13613065Sgabeblack@google.com ::sc_gem5::scheduler.deschedule(_gem5UpEdge); 13713065Sgabeblack@google.com if (_gem5DownEdge->scheduled()) 13813065Sgabeblack@google.com ::sc_gem5::scheduler.deschedule(_gem5DownEdge); 13913065Sgabeblack@google.com delete _gem5UpEdge; 14013065Sgabeblack@google.com delete _gem5DownEdge; 14112878Sgabeblack@google.com} 14212878Sgabeblack@google.com 14312841Sgabeblack@google.comvoid 14412841Sgabeblack@google.comsc_clock::write(const bool &) 14512841Sgabeblack@google.com{ 14613065Sgabeblack@google.com panic("write() called on sc_clock."); 14712841Sgabeblack@google.com} 14812841Sgabeblack@google.com 14913065Sgabeblack@google.comconst sc_time &sc_clock::period() const { return _period; } 15013065Sgabeblack@google.comdouble sc_clock::duty_cycle() const { return _dutyCycle; } 15113065Sgabeblack@google.comconst sc_time &sc_clock::start_time() const { return _startTime; } 15213065Sgabeblack@google.combool sc_clock::posedge_first() const { return _posedgeFirst; } 15312841Sgabeblack@google.com 15412931Sgabeblack@google.comconst sc_time & 15512931Sgabeblack@google.comsc_clock::time_stamp() 15612931Sgabeblack@google.com{ 15712931Sgabeblack@google.com warn("%s not implemented.\n", __PRETTY_FUNCTION__); 15812931Sgabeblack@google.com return *(const sc_time *)nullptr; 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