gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sc_clock.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Authors: Gabe Black
28  */
29 
30 #include "base/logging.hh"
31 #include "base/types.hh"
32 #include "sim/core.hh"
33 #include "sim/eventq.hh"
34 #include "systemc/core/kernel.hh"
41 #include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
43 
44 namespace sc_gem5
45 {
46 
47 class ClockTick : public ScEvent
48 {
49  private:
51  std::string name;
54 
55  public:
56  ClockTick(::sc_core::sc_clock *clock, bool to,
57  ::sc_core::sc_time _period) :
58  ScEvent([this]() { tick(); }),
59  _period(_period), name(clock->basename()), p(nullptr),
60  funcWrapper(clock, to ? &::sc_core::sc_clock::tickUp :
62  {
63  name += std::string(to ? "_posedge_action" : "_negedge_action");
64  name = ::sc_core::sc_gen_unique_name(name.c_str());
65  }
66 
67  void
69  {
70  p = new Method(name.c_str(), &funcWrapper, true);
71  p->dontInitialize(true);
72  scheduler.reg(p);
73  }
74 
76  {
77  if (scheduled())
78  scheduler.deschedule(this);
79  if (p)
80  p->popListNode();
81  }
82 
83  void
84  tick()
85  {
86  scheduler.schedule(this, _period);
87  p->ready();
88  }
89 };
90 
91 };
92 
93 namespace sc_core
94 {
95 
96 sc_clock::sc_clock() :
97  sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS),
98  0.5, SC_ZERO_TIME, true)
99 {}
100 
101 sc_clock::sc_clock(const char *name) :
102  sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true)
103 {}
104 
105 sc_clock::sc_clock(const char *name, const sc_time &period,
106  double duty_cycle, const sc_time &start_time,
107  bool posedge_first) :
108  sc_interface(), sc_signal<bool>(name, posedge_first ? false : true),
109  _period(period), _dutyCycle(duty_cycle), _startTime(start_time),
110  _posedgeFirst(posedge_first)
111 {
112  if (period == SC_ZERO_TIME) {
113  std::string msg =
114  "increase the period: clock '" +
115  std::string(name) + "'";
117  }
118 
119  if (duty_cycle * period == SC_ZERO_TIME) {
120  std::string msg =
121  "increase the period or increase the duty cycle: clock '" +
122  std::string(name) + "'";
124  }
125 
126  if (duty_cycle * period == period) {
127  std::string msg =
128  "increase the period or decrease the duty cycle: clock '" +
129  std::string(name) + "'";
131  }
132 
133  _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period);
134  _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period);
135 }
136 
137 sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
138  double duty_cycle) :
139  sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME,
140  true)
141 {}
142 
143 sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
144  double duty_cycle, double start_time_v,
145  sc_time_unit start_time_tu, bool posedge_first) :
146  sc_clock(name, sc_time(period_v, period_tu), duty_cycle,
147  sc_time(start_time_v, start_time_tu), posedge_first)
148 {}
149 
150 sc_clock::sc_clock(const char *name, double period, double duty_cycle,
151  double start_time, bool posedge_first) :
152  sc_clock(name, sc_time(period, true), duty_cycle,
153  sc_time(start_time, true), posedge_first)
154 {}
155 
157 {
158  if (_gem5UpEdge->scheduled())
159  ::sc_gem5::scheduler.deschedule(_gem5UpEdge);
160  if (_gem5DownEdge->scheduled())
161  ::sc_gem5::scheduler.deschedule(_gem5DownEdge);
162  delete _gem5UpEdge;
163  delete _gem5DownEdge;
164 }
165 
166 void
167 sc_clock::write(const bool &)
168 {
169  panic("write() called on sc_clock.");
170 }
171 
172 const sc_time &sc_clock::period() const { return _period; }
173 double sc_clock::duty_cycle() const { return _dutyCycle; }
174 const sc_time &sc_clock::start_time() const { return _startTime; }
175 bool sc_clock::posedge_first() const { return _posedgeFirst; }
176 
177 const sc_time &
179 {
180  return sc_time_stamp();
181 }
182 
183 void
185 {
188  if (_posedgeFirst) {
192  } else {
195  _startTime + _period * (1.0 - _dutyCycle));
196  }
197 }
198 
199 } // namespace sc_core
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
const char SC_ID_CLOCK_HIGH_TIME_ZERO_[]
Definition: messages.cc:38
void popListNode()
Definition: list.hh:53
const sc_time & period() const
Definition: sc_clock.cc:172
sc_time_unit
Definition: sc_time.hh:42
sc_time _startTime
Definition: sc_clock.hh:98
virtual void before_end_of_elaboration()
Definition: sc_clock.cc:184
const char * sc_gen_unique_name(const char *seed)
Definition: sc_module.cc:822
const char SC_ID_CLOCK_PERIOD_ZERO_[]
Definition: messages.cc:37
::sc_gem5::ClockTick * _gem5DownEdge
Definition: sc_clock.hh:102
const char * name() const
Definition: sc_object.cc:46
static const sc_time & time_stamp()
Definition: sc_clock.cc:178
bool dontInitialize()
Definition: process.hh:135
void reg(Process *p)
Definition: scheduler.cc:149
const sc_time & start_time() const
Definition: sc_clock.cc:174
ProcessMemberFuncWrapper<::sc_core::sc_clock > funcWrapper
Definition: sc_clock.cc:53
void deschedule(ScEvent *event)
Definition: scheduler.hh:265
Scheduler scheduler
Definition: scheduler.cc:491
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
bool posedge_first() const
Definition: sc_clock.cc:175
double duty_cycle() const
Definition: sc_clock.cc:173
::sc_gem5::ClockTick * _gem5UpEdge
Definition: sc_clock.hh:101
virtual void write(const bool &)
Definition: sc_clock.cc:167
const sc_time & sc_time_stamp()
Definition: sc_main.cc:130
virtual ~sc_clock()
Definition: sc_clock.cc:156
const sc_time SC_ZERO_TIME
Definition: sc_time.cc:292
const char SC_ID_CLOCK_LOW_TIME_ZERO_[]
Definition: messages.cc:39
sc_time _period
Definition: sc_clock.hh:96
ClockTick(::sc_core::sc_clock *clock, bool to, ::sc_core::sc_time _period)
Definition: sc_clock.cc:56
#define SC_REPORT_ERROR(msg_type, msg)
void createProcess()
Definition: sc_clock.cc:68
void schedule(ScEvent *event, const ::sc_core::sc_time &delay)
Definition: scheduler.hh:240
std::string name
Definition: sc_clock.cc:51
double _dutyCycle
Definition: sc_clock.hh:97
::sc_core::sc_time _period
Definition: sc_clock.cc:50

Generated on Fri Feb 28 2020 16:27:03 for gem5 by doxygen 1.8.13