2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andreas Hansson
42 * ClockedObject declaration and implementation.
45 #ifndef __SIM_CLOCKED_OBJECT_HH__
46 #define __SIM_CLOCKED_OBJECT_HH__
48 #include "base/intmath.hh"
49 #include "params/ClockedObject.hh"
50 #include "sim/sim_object.hh"
53 * The ClockedObject class extends the SimObject with a clock and
54 * accessor functions to relate ticks to the cycles of the object.
56 class ClockedObject : public SimObject
61 // the tick value of the next clock edge (>= curTick()) at the
62 // time of the last call to update()
65 // The cycle counter value corresponding to the current value of
70 * Prevent inadvertent use of the copy constructor and assignment
71 * operator by making them private.
73 ClockedObject(ClockedObject&);
74 ClockedObject& operator=(ClockedObject&);
77 * Align cycle and tick to the next clock edge if not already done.
81 // both tick and cycle are up-to-date and we are done, note
82 // that the >= is important as it captures cases where tick
83 // has already passed curTick()
84 if (tick >= curTick())
87 // optimise for the common case and see if the tick should be
88 // advanced by a single clock period
92 // see if we are done at this point
93 if (tick >= curTick())
96 // if not, we have to recalculate the cycle and tick, we
97 // perform the calculations in terms of relative cycles to
98 // allow changes to the clock period in the future
99 Cycles elapsedCycles(divCeil(curTick() - tick, clock));
100 cycle += elapsedCycles;
101 tick += elapsedCycles * clock;
106 // Clock period in ticks
110 * Create a clocked object and set the clock based on the
113 ClockedObject(const ClockedObjectParams* p) :
114 SimObject(p), tick(0), cycle(0), clock(p->clock)
118 * Virtual destructor due to inheritance.
120 virtual ~ClockedObject() { }
123 * Reset the object's clock using the current global tick value. Likely
124 * to be used only when the global clock is reset. Currently, this done
125 * only when Ruby is done warming up the memory system.
127 void resetClock() const
129 Cycles elapsedCycles(divCeil(curTick(), clock));
130 cycle = elapsedCycles;
131 tick = elapsedCycles * clock;
137 * Determine the tick when a cycle begins, by default the current
138 * one, but the argument also enables the caller to determine a
141 * @param cycles The number of cycles into the future
143 * @return The tick when the clock edge occurs
145 inline Tick clockEdge(Cycles cycles = Cycles(0)) const
147 // align tick to the next clock edge
150 // figure out when this future cycle is
151 return tick + clock * cycles;
155 * Determine the current cycle, corresponding to a tick aligned to
158 * @return The current cycle count
160 inline Cycles curCycle() const
162 // align cycle to the next clock edge.
169 * Based on the clock of the object, determine the tick when the
170 * next cycle begins, in other words, return the next clock edge.
172 * @return The tick when the next cycle starts
174 Tick nextCycle() const
175 { return clockEdge(); }
177 inline uint64_t frequency() const { return SimClock::Frequency / clock; }
179 inline Tick clockPeriod() const { return clock; }
181 inline Cycles ticksToCycles(Tick tick) const
182 { return Cycles(tick / clock); }
186 #endif //__SIM_CLOCKED_OBJECT_HH__