sc_clock.cc revision 13135
12086SN/A/* 22086SN/A * Copyright 2018 Google, Inc. 32086SN/A * 42086SN/A * Redistribution and use in source and binary forms, with or without 52086SN/A * modification, are permitted provided that the following conditions are 62086SN/A * met: redistributions of source code must retain the above copyright 72086SN/A * notice, this list of conditions and the following disclaimer; 82086SN/A * redistributions in binary form must reproduce the above copyright 92086SN/A * notice, this list of conditions and the following disclaimer in the 102086SN/A * documentation and/or other materials provided with the distribution; 112086SN/A * neither the name of the copyright holders nor the names of its 122086SN/A * contributors may be used to endorse or promote products derived from 132086SN/A * this software without specific prior written permission. 142086SN/A * 152086SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 162086SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 172086SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 182086SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 192086SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 202086SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 212086SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 222086SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 232086SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 242086SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 252086SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 262086SN/A * 272086SN/A * Authors: Gabe Black 282665Ssaidi@eecs.umich.edu */ 292665Ssaidi@eecs.umich.edu 302665Ssaidi@eecs.umich.edu#include "base/logging.hh" 312086SN/A#include "base/types.hh" 322086SN/A#include "sim/core.hh" 332086SN/A#include "sim/eventq.hh" 342086SN/A#include "systemc/core/kernel.hh" 352086SN/A#include "systemc/core/process_types.hh" 362086SN/A#include "systemc/core/sched_event.hh" 372086SN/A#include "systemc/core/scheduler.hh" 382086SN/A#include "systemc/ext/channel/sc_clock.hh" 392086SN/A#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name 402086SN/A 412086SN/Anamespace sc_gem5 422086SN/A{ 432086SN/A 442086SN/Aclass ClockTick : public ScEvent 452086SN/A{ 462152SN/A private: 472152SN/A ::sc_core::sc_clock *clock; 482152SN/A ::sc_core::sc_time _period; 492086SN/A std::string _name; 502086SN/A Process *p; 512086SN/A ProcessMemberFuncWrapper<::sc_core::sc_clock> funcWrapper; 522152SN/A std::string _procName; 532152SN/A 542152SN/A public: 552152SN/A ClockTick(::sc_core::sc_clock *clock, bool to, 562152SN/A ::sc_core::sc_time _period) : 572152SN/A ScEvent([this]() { tick(); }), 582152SN/A clock(clock), _period(_period), _name(clock->name()), 592086SN/A funcWrapper(clock, to ? &::sc_core::sc_clock::tickUp : 602086SN/A &::sc_core::sc_clock::tickDown) 612086SN/A { 622152SN/A _name += (to ? ".up_tick" : ".down_tick"); 632597SN/A _procName = _name + ".p"; 642597SN/A p = new Method(_procName.c_str(), &funcWrapper); 652447SN/A scheduler.reg(p); 662086SN/A scheduler.dontInitialize(p); 672086SN/A } 682086SN/A 692152SN/A ~ClockTick() 702086SN/A { 712086SN/A if (scheduled()) 722152SN/A scheduler.deschedule(this); 732086SN/A p->popListNode(); 742152SN/A } 752086SN/A 762152SN/A void 772152SN/A tick() 782152SN/A { 792152SN/A scheduler.schedule(this, _period); 802152SN/A p->ready(); 812152SN/A } 822152SN/A}; 832152SN/A 842152SN/A}; 852086SN/A 862086SN/Anamespace sc_core 87{ 88 89sc_clock::sc_clock() : 90 sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS), 91 0.5, SC_ZERO_TIME, true) 92{} 93 94sc_clock::sc_clock(const char *name) : 95 sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true) 96{} 97 98sc_clock::sc_clock(const char *name, const sc_time &period, 99 double duty_cycle, const sc_time &start_time, 100 bool posedge_first) : 101 sc_interface(), sc_signal<bool>(name, posedge_first ? false : true), 102 _period(period), _dutyCycle(duty_cycle), _startTime(start_time), 103 _posedgeFirst(posedge_first) 104{ 105 _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period); 106 _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period); 107} 108 109sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu, 110 double duty_cycle) : 111 sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME, 112 true) 113{} 114 115sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu, 116 double duty_cycle, double start_time_v, 117 sc_time_unit start_time_tu, bool posedge_first) : 118 sc_clock(name, sc_time(period_v, period_tu), duty_cycle, 119 sc_time(start_time_v, start_time_tu), posedge_first) 120{} 121 122sc_clock::sc_clock(const char *name, double period, double duty_cycle, 123 double start_time, bool posedge_first) : 124 sc_clock(name, sc_time(period, true), duty_cycle, 125 sc_time(start_time, true), posedge_first) 126{} 127 128sc_clock::~sc_clock() 129{ 130 if (_gem5UpEdge->scheduled()) 131 ::sc_gem5::scheduler.deschedule(_gem5UpEdge); 132 if (_gem5DownEdge->scheduled()) 133 ::sc_gem5::scheduler.deschedule(_gem5DownEdge); 134 delete _gem5UpEdge; 135 delete _gem5DownEdge; 136} 137 138void 139sc_clock::write(const bool &) 140{ 141 panic("write() called on sc_clock."); 142} 143 144const sc_time &sc_clock::period() const { return _period; } 145double sc_clock::duty_cycle() const { return _dutyCycle; } 146const sc_time &sc_clock::start_time() const { return _startTime; } 147bool sc_clock::posedge_first() const { return _posedgeFirst; } 148 149const sc_time & 150sc_clock::time_stamp() 151{ 152 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 153 return *(const sc_time *)nullptr; 154} 155 156void 157sc_clock::before_end_of_elaboration() 158{ 159 if (_posedgeFirst) { 160 ::sc_gem5::scheduler.schedule(_gem5UpEdge, _startTime); 161 ::sc_gem5::scheduler.schedule(_gem5DownEdge, 162 _startTime + _period * _dutyCycle); 163 } else { 164 ::sc_gem5::scheduler.schedule(_gem5DownEdge, _startTime); 165 ::sc_gem5::scheduler.schedule(_gem5UpEdge, 166 _startTime + _period * (1.0 - _dutyCycle)); 167 } 168} 169 170} // namespace sc_core 171