gem5  v22.1.0.0
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 
28 #include "base/logging.hh"
29 #include "base/types.hh"
30 #include "sim/eventq.hh"
31 #include "systemc/core/kernel.hh"
38 #include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
40 
41 namespace sc_gem5
42 {
43 
44 class ClockTick : public ScEvent
45 {
46  private:
48  std::string name;
51 
52  public:
53  ClockTick(::sc_core::sc_clock *clock, bool to,
55  ScEvent([this]() { tick(); }),
56  _period(_period), name(clock->name()), p(nullptr),
59  {
60  name += std::string(to ? "_posedge_action" : "_negedge_action");
62  }
63 
64  void
66  {
67  p = new Method(name.c_str(), &funcWrapper, true);
68  p->dontInitialize(true);
69  scheduler.reg(p);
70  }
71 
73  {
74  if (scheduled())
75  scheduler.deschedule(this);
76  if (p)
77  p->popListNode();
78  }
79 
80  void
81  tick()
82  {
83  scheduler.schedule(this, _period);
84  p->ready();
85  }
86 };
87 
88 };
89 
90 namespace sc_core
91 {
92 
94  sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS),
95  0.5, SC_ZERO_TIME, true)
96 {}
97 
98 sc_clock::sc_clock(const char *name) :
99  sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true)
100 {}
101 
102 sc_clock::sc_clock(const char *name, const sc_time &period,
103  double duty_cycle, const sc_time &start_time,
104  bool posedge_first) :
105  sc_interface(), sc_signal<bool>(name, posedge_first ? false : true),
106  _period(period), _dutyCycle(duty_cycle), _startTime(start_time),
107  _posedgeFirst(posedge_first)
108 {
109  if (period == SC_ZERO_TIME) {
110  std::string msg =
111  "increase the period: clock '" +
112  std::string(name) + "'";
114  }
115 
116  if (duty_cycle * period == SC_ZERO_TIME) {
117  std::string msg =
118  "increase the period or increase the duty cycle: clock '" +
119  std::string(name) + "'";
121  }
122 
123  if (duty_cycle * period == period) {
124  std::string msg =
125  "increase the period or decrease the duty cycle: clock '" +
126  std::string(name) + "'";
128  }
129 
130  _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period);
131  _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period);
132 }
133 
134 sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
135  double duty_cycle) :
136  sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME,
137  true)
138 {}
139 
140 sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
141  double duty_cycle, double start_time_v,
142  sc_time_unit start_time_tu, bool posedge_first) :
143  sc_clock(name, sc_time(period_v, period_tu), duty_cycle,
144  sc_time(start_time_v, start_time_tu), posedge_first)
145 {}
146 
147 sc_clock::sc_clock(const char *name, double period, double duty_cycle,
148  double start_time, bool posedge_first) :
149  sc_clock(name, sc_time(period, true), duty_cycle,
150  sc_time(start_time, true), posedge_first)
151 {}
152 
154 {
155  if (_gem5UpEdge->scheduled())
157  if (_gem5DownEdge->scheduled())
159  delete _gem5UpEdge;
160  delete _gem5DownEdge;
161 }
162 
163 void
164 sc_clock::write(const bool &)
165 {
166  panic("write() called on sc_clock.");
167 }
168 
169 const sc_time &sc_clock::period() const { return _period; }
170 double sc_clock::duty_cycle() const { return _dutyCycle; }
171 const sc_time &sc_clock::start_time() const { return _startTime; }
172 bool sc_clock::posedge_first() const { return _posedgeFirst; }
173 
174 const sc_time &
176 {
177  return sc_time_stamp();
178 }
179 
180 void
182 {
185  if (_posedgeFirst) {
189  } else {
192  _startTime + _period * (1.0 - _dutyCycle));
193  }
194 }
195 
196 } // namespace sc_core
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
bool posedge_first() const
Definition: sc_clock.cc:172
double _dutyCycle
Definition: sc_clock.hh:95
::sc_gem5::ClockTick * _gem5UpEdge
Definition: sc_clock.hh:99
::sc_gem5::ClockTick * _gem5DownEdge
Definition: sc_clock.hh:100
static const sc_time & time_stamp()
Definition: sc_clock.cc:175
const sc_time & period() const
Definition: sc_clock.cc:169
sc_time _startTime
Definition: sc_clock.hh:96
double duty_cycle() const
Definition: sc_clock.cc:170
virtual ~sc_clock()
Definition: sc_clock.cc:153
virtual void before_end_of_elaboration()
Definition: sc_clock.cc:181
sc_time _period
Definition: sc_clock.hh:94
const sc_time & start_time() const
Definition: sc_clock.cc:171
virtual void write(const bool &)
Definition: sc_clock.cc:164
const char * name() const
Definition: sc_object.cc:44
ProcessMemberFuncWrapper<::sc_core::sc_clock > funcWrapper
Definition: sc_clock.cc:50
std::string name
Definition: sc_clock.cc:48
void createProcess()
Definition: sc_clock.cc:65
::sc_core::sc_time _period
Definition: sc_clock.cc:47
ClockTick(::sc_core::sc_clock *clock, bool to, ::sc_core::sc_time _period)
Definition: sc_clock.cc:53
bool dontInitialize()
Definition: process.hh:133
void deschedule(ScEvent *event)
Definition: scheduler.hh:280
void schedule(ScEvent *event, const ::sc_core::sc_time &delay)
Definition: scheduler.hh:253
void reg(Process *p)
Definition: scheduler.cc:146
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
Bitfield< 25, 21 > to
Definition: types.hh:96
const sc_time SC_ZERO_TIME
Definition: sc_time.cc:290
const char SC_ID_CLOCK_HIGH_TIME_ZERO_[]
Definition: messages.cc:36
const char SC_ID_CLOCK_PERIOD_ZERO_[]
Definition: messages.cc:35
sc_time_unit
Definition: sc_time.hh:40
@ SC_NS
Definition: sc_time.hh:43
const char * sc_gen_unique_name(const char *seed)
Definition: sc_module.cc:820
const char SC_ID_CLOCK_LOW_TIME_ZERO_[]
Definition: messages.cc:37
const sc_time & sc_time_stamp()
Definition: sc_main.cc:127
Scheduler scheduler
Definition: scheduler.cc:494
#define SC_REPORT_ERROR(msg_type, msg)
void popListNode()
Definition: list.hh:51
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:40 for gem5 by doxygen 1.9.1