clocked_object.hh revision 9356:b279bad40aa3
1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Andreas Hansson
38 */
39
40/**
41 * @file
42 * ClockedObject declaration and implementation.
43 */
44
45#ifndef __SIM_CLOCKED_OBJECT_HH__
46#define __SIM_CLOCKED_OBJECT_HH__
47
48#include "base/intmath.hh"
49#include "params/ClockedObject.hh"
50#include "sim/core.hh"
51#include "sim/sim_object.hh"
52
53/**
54 * The ClockedObject class extends the SimObject with a clock and
55 * accessor functions to relate ticks to the cycles of the object.
56 */
57class ClockedObject : public SimObject
58{
59
60  private:
61
62    // the tick value of the next clock edge (>= curTick()) at the
63    // time of the last call to update()
64    mutable Tick tick;
65
66    // The cycle counter value corresponding to the current value of
67    // 'tick'
68    mutable Cycles cycle;
69
70    /**
71     * Prevent inadvertent use of the copy constructor and assignment
72     * operator by making them private.
73     */
74    ClockedObject(ClockedObject&);
75    ClockedObject& operator=(ClockedObject&);
76
77    /**
78     *  Align cycle and tick to the next clock edge if not already done.
79     */
80    void update() const
81    {
82        // both tick and cycle are up-to-date and we are done, note
83        // that the >= is important as it captures cases where tick
84        // has already passed curTick()
85        if (tick >= curTick())
86            return;
87
88        // optimise for the common case and see if the tick should be
89        // advanced by a single clock period
90        tick += clock;
91        ++cycle;
92
93        // see if we are done at this point
94        if (tick >= curTick())
95            return;
96
97        // if not, we have to recalculate the cycle and tick, we
98        // perform the calculations in terms of relative cycles to
99        // allow changes to the clock period in the future
100        Cycles elapsedCycles(divCeil(curTick() - tick, clock));
101        cycle += elapsedCycles;
102        tick += elapsedCycles * clock;
103    }
104
105  protected:
106
107    // Clock period in ticks
108    Tick clock;
109
110    /**
111     * Create a clocked object and set the clock based on the
112     * parameters.
113     */
114    ClockedObject(const ClockedObjectParams* p) :
115        SimObject(p), tick(0), cycle(0), clock(p->clock)
116    { }
117
118    /**
119     * Virtual destructor due to inheritance.
120     */
121    virtual ~ClockedObject() { }
122
123    /**
124     * Reset the object's clock using the current global tick value. Likely
125     * to be used only when the global clock is reset. Currently, this done
126     * only when Ruby is done warming up the memory system.
127     */
128    void resetClock() const
129    {
130        Cycles elapsedCycles(divCeil(curTick(), clock));
131        cycle = elapsedCycles;
132        tick = elapsedCycles * clock;
133    }
134
135  public:
136
137    /**
138     * Determine the tick when a cycle begins, by default the current
139     * one, but the argument also enables the caller to determine a
140     * future cycle.
141     *
142     * @param cycles The number of cycles into the future
143     *
144     * @return The tick when the clock edge occurs
145     */
146    inline Tick clockEdge(Cycles cycles = Cycles(0)) const
147    {
148        // align tick to the next clock edge
149        update();
150
151        // figure out when this future cycle is
152        return tick + clock * cycles;
153    }
154
155    /**
156     * Determine the current cycle, corresponding to a tick aligned to
157     * a clock edge.
158     *
159     * @return The current cycle count
160     */
161    inline Cycles curCycle() const
162    {
163        // align cycle to the next clock edge.
164        update();
165
166        return cycle;
167    }
168
169    /**
170     * Based on the clock of the object, determine the tick when the
171     * next cycle begins, in other words, return the next clock edge.
172     *
173     * @return The tick when the next cycle starts
174     */
175    Tick nextCycle() const
176    { return clockEdge(); }
177
178    inline uint64_t frequency() const { return SimClock::Frequency / clock; }
179
180    inline Tick clockPeriod() const { return clock; }
181
182    inline Cycles ticksToCycles(Tick tick) const
183    { return Cycles(tick / clock); }
184
185};
186
187#endif //__SIM_CLOCKED_OBJECT_HH__
188