gem5  v20.0.0.3
clocked_object.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013, 2015-2017, 2019-2020 ARM Limited
3  * Copyright (c) 2013 Cornell University
4  * All rights reserved
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are
17  * met: redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer;
19  * redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution;
22  * neither the name of the copyright holders nor the names of its
23  * contributors may be used to endorse or promote products derived from
24  * this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
44 #ifndef __SIM_CLOCKED_OBJECT_HH__
45 #define __SIM_CLOCKED_OBJECT_HH__
46 
47 
48 #include "params/ClockedObject.hh"
49 #include "sim/core.hh"
50 #include "sim/clock_domain.hh"
51 #include "sim/power_state.hh"
52 #include "sim/sim_object.hh"
53 
59 class Clocked
60 {
61 
62  private:
63  // the tick value of the next clock edge (>= curTick()) at the
64  // time of the last call to update()
65  mutable Tick tick;
66 
67  // The cycle counter value corresponding to the current value of
68  // 'tick'
69  mutable Cycles cycle;
70 
75  void
76  update() const
77  {
78  // both tick and cycle are up-to-date and we are done, note
79  // that the >= is important as it captures cases where tick
80  // has already passed curTick()
81  if (tick >= curTick())
82  return;
83 
84  // optimise for the common case and see if the tick should be
85  // advanced by a single clock period
86  tick += clockPeriod();
87  ++cycle;
88 
89  // see if we are done at this point
90  if (tick >= curTick())
91  return;
92 
93  // if not, we have to recalculate the cycle and tick, we
94  // perform the calculations in terms of relative cycles to
95  // allow changes to the clock period in the future
96  Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod()));
97  cycle += elapsedCycles;
98  tick += elapsedCycles * clockPeriod();
99  }
100 
105 
106  protected:
107 
112  Clocked(ClockDomain &clk_domain)
113  : tick(0), cycle(0), clockDomain(clk_domain)
114  {
115  // Register with the clock domain, so that if the clock domain
116  // frequency changes, we can update this object's tick.
117  clockDomain.registerWithClockDomain(this);
118  }
119 
120  Clocked(Clocked &) = delete;
121  Clocked &operator=(Clocked &) = delete;
122 
126  virtual ~Clocked() { }
127 
133  void
134  resetClock() const
135  {
136  Cycles elapsedCycles(divCeil(curTick(), clockPeriod()));
137  cycle = elapsedCycles;
138  tick = elapsedCycles * clockPeriod();
139  }
140 
145  virtual void clockPeriodUpdated() {}
146 
147  public:
148 
152  void
154  {
155  update();
157  }
158 
173  Tick
174  clockEdge(Cycles cycles=Cycles(0)) const
175  {
176  // align tick to the next clock edge
177  update();
178 
179  // figure out when this future cycle is
180  return tick + clockPeriod() * cycles;
181  }
182 
191  Cycles
192  curCycle() const
193  {
194  // align cycle to the next clock edge.
195  update();
196 
197  return cycle;
198  }
199 
210  Tick nextCycle() const { return clockEdge(Cycles(1)); }
211 
212  uint64_t frequency() const { return SimClock::Frequency / clockPeriod(); }
213 
214  Tick clockPeriod() const { return clockDomain.clockPeriod(); }
215 
216  double voltage() const { return clockDomain.voltage(); }
217 
218  Cycles
220  {
221  return Cycles(divCeil(t, clockPeriod()));
222  }
223 
224  Tick cyclesToTicks(Cycles c) const { return clockPeriod() * c; }
225 };
226 
231 class ClockedObject : public SimObject, public Clocked
232 {
233  public:
234  ClockedObject(const ClockedObjectParams *p);
235 
237  typedef ClockedObjectParams Params;
238  const Params *
239  params() const
240  {
241  return reinterpret_cast<const Params*>(_params);
242  }
243 
244  void serialize(CheckpointOut &cp) const override;
245  void unserialize(CheckpointIn &cp) override;
246 
248 };
249 
250 #endif //__SIM_CLOCKED_OBJECT_HH__
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:81
ClockedObjectParams Params
Parameters of ClockedObject.
virtual ~Clocked()
Virtual destructor due to inheritance.
Definition: cprintf.cc:40
Helper class for objects that have power states.
Definition: power_state.hh:61
Tick clockPeriod() const
double voltage() const
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition: core.cc:46
const Params * params() const
Clocked(ClockDomain &clk_domain)
Create a clocked object and set the clock domain based on the parameters.
Tick cyclesToTicks(Cycles c) const
void resetClock() const
Reset the object&#39;s clock using the current global tick value.
Tick curTick()
The current simulated tick.
Definition: core.hh:44
uint64_t frequency() const
uint64_t Tick
Tick count type.
Definition: types.hh:61
The ClockedObject class extends the SimObject with a clock and accessor functions to relate ticks to ...
void serialize(const ThreadContext &tc, CheckpointOut &cp)
Thread context serialization helpers.
Cycles curCycle() const
Determine the current cycle, corresponding to a tick aligned to a clock edge.
ClockDomain declarations.
Tick nextCycle() const
Based on the clock of the object, determine the start tick of the first cycle that is at least one cy...
Helper class for objects that need to be clocked.
Tick clockEdge(Cycles cycles=Cycles(0)) const
Determine the tick when a cycle begins, by default the current one, but the argument also enables the...
Bitfield< 29 > c
ClockDomain & clockDomain
The clock domain this clocked object belongs to.
virtual void clockPeriodUpdated()
A hook subclasses can implement so they can do any extra work that&#39;s needed when the clock rate is ch...
The ClockDomain provides clock to group of clocked objects bundled under the same clock domain...
Definition: clock_domain.hh:68
Cycles ticksToCycles(Tick t) const
std::ostream CheckpointOut
Definition: serialize.hh:63
Cycles cycle
void updateClockPeriod()
Update the tick to the current tick.
T divCeil(const T &a, const U &b)
Definition: intmath.hh:99
void registerWithClockDomain(Clocked *c)
Register a Clocked object with this ClockDomain.
void unserialize(ThreadContext &tc, CheckpointIn &cp)
Clocked & operator=(Clocked &)=delete
PowerState * powerState
PowerState declaration and implementation.
Bitfield< 5 > t
Bitfield< 0 > p
void update() const
Align cycle and tick to the next clock edge if not already done.
double voltage() const
Get the current voltage this clock domain operates at.
Definition: clock_domain.cc:70
Abstract superclass for simulation objects.
Definition: sim_object.hh:93
Tick clockPeriod() const
Get the clock period.

Generated on Fri Jul 3 2020 15:53:04 for gem5 by doxygen 1.8.13