clocked_object.hh revision 10000:99bd071911cf
14683Sgblack@eecs.umich.edu/*
24683Sgblack@eecs.umich.edu * Copyright (c) 2012-2013 ARM Limited
34683Sgblack@eecs.umich.edu * Copyright (c) 2013 Cornell University
44683Sgblack@eecs.umich.edu * All rights reserved
54683Sgblack@eecs.umich.edu *
64683Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
74683Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
84683Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
94683Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
104683Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
114683Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
124683Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
134683Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
144683Sgblack@eecs.umich.edu *
154683Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
164683Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
174683Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
184683Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
194683Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
204683Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
214683Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
224683Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
234683Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
244683Sgblack@eecs.umich.edu * this software without specific prior written permission.
254683Sgblack@eecs.umich.edu *
264683Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
274683Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
284683Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
294683Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
304683Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
314683Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
324683Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
334683Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
344683Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
354683Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
364683Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
374683Sgblack@eecs.umich.edu *
384683Sgblack@eecs.umich.edu * Authors: Andreas Hansson
394683Sgblack@eecs.umich.edu *          Christopher Torng
404683Sgblack@eecs.umich.edu */
414683Sgblack@eecs.umich.edu
424683Sgblack@eecs.umich.edu/**
434683Sgblack@eecs.umich.edu * @file
444683Sgblack@eecs.umich.edu * ClockedObject declaration and implementation.
454683Sgblack@eecs.umich.edu */
464683Sgblack@eecs.umich.edu
474683Sgblack@eecs.umich.edu#ifndef __SIM_CLOCKED_OBJECT_HH__
484683Sgblack@eecs.umich.edu#define __SIM_CLOCKED_OBJECT_HH__
494683Sgblack@eecs.umich.edu
504683Sgblack@eecs.umich.edu#include "base/intmath.hh"
514683Sgblack@eecs.umich.edu#include "base/misc.hh"
524683Sgblack@eecs.umich.edu#include "params/ClockedObject.hh"
534683Sgblack@eecs.umich.edu#include "sim/core.hh"
544683Sgblack@eecs.umich.edu#include "sim/clock_domain.hh"
554683Sgblack@eecs.umich.edu#include "sim/sim_object.hh"
564683Sgblack@eecs.umich.edu
574683Sgblack@eecs.umich.edu/**
584683Sgblack@eecs.umich.edu * The ClockedObject class extends the SimObject with a clock and
594683Sgblack@eecs.umich.edu * accessor functions to relate ticks to the cycles of the object.
604683Sgblack@eecs.umich.edu */
614683Sgblack@eecs.umich.educlass ClockedObject : public SimObject
624683Sgblack@eecs.umich.edu{
634683Sgblack@eecs.umich.edu
644683Sgblack@eecs.umich.edu  private:
654683Sgblack@eecs.umich.edu
664683Sgblack@eecs.umich.edu    // the tick value of the next clock edge (>= curTick()) at the
674683Sgblack@eecs.umich.edu    // time of the last call to update()
684683Sgblack@eecs.umich.edu    mutable Tick tick;
694683Sgblack@eecs.umich.edu
704683Sgblack@eecs.umich.edu    // The cycle counter value corresponding to the current value of
714683Sgblack@eecs.umich.edu    // 'tick'
724683Sgblack@eecs.umich.edu    mutable Cycles cycle;
734683Sgblack@eecs.umich.edu
744683Sgblack@eecs.umich.edu    /**
754683Sgblack@eecs.umich.edu     * Prevent inadvertent use of the copy constructor and assignment
764683Sgblack@eecs.umich.edu     * operator by making them private.
774683Sgblack@eecs.umich.edu     */
784683Sgblack@eecs.umich.edu    ClockedObject(ClockedObject&);
794683Sgblack@eecs.umich.edu    ClockedObject& operator=(ClockedObject&);
804683Sgblack@eecs.umich.edu
814683Sgblack@eecs.umich.edu    /**
824683Sgblack@eecs.umich.edu     *  Align cycle and tick to the next clock edge if not already done.
834683Sgblack@eecs.umich.edu     */
844683Sgblack@eecs.umich.edu    void update() const
854683Sgblack@eecs.umich.edu    {
864683Sgblack@eecs.umich.edu        // both tick and cycle are up-to-date and we are done, note
874683Sgblack@eecs.umich.edu        // that the >= is important as it captures cases where tick
884683Sgblack@eecs.umich.edu        // has already passed curTick()
894683Sgblack@eecs.umich.edu        if (tick >= curTick())
904683Sgblack@eecs.umich.edu            return;
914683Sgblack@eecs.umich.edu
92        // optimise for the common case and see if the tick should be
93        // advanced by a single clock period
94        tick += clockPeriod();
95        ++cycle;
96
97        // see if we are done at this point
98        if (tick >= curTick())
99            return;
100
101        // if not, we have to recalculate the cycle and tick, we
102        // perform the calculations in terms of relative cycles to
103        // allow changes to the clock period in the future
104        Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod()));
105        cycle += elapsedCycles;
106        tick += elapsedCycles * clockPeriod();
107    }
108
109    /**
110     * The clock domain this clocked object belongs to
111     */
112    ClockDomain &clockDomain;
113
114  protected:
115
116    /**
117     * Create a clocked object and set the clock domain based on the
118     * parameters.
119     */
120    ClockedObject(const ClockedObjectParams* p) :
121        SimObject(p), tick(0), cycle(0), clockDomain(*p->clk_domain)
122    {
123        // Register with the clock domain, so that if the clock domain
124        // frequency changes, we can update this object's tick.
125        clockDomain.registerWithClockDomain(this);
126    }
127
128    /**
129     * Virtual destructor due to inheritance.
130     */
131    virtual ~ClockedObject() { }
132
133    /**
134     * Reset the object's clock using the current global tick value. Likely
135     * to be used only when the global clock is reset. Currently, this done
136     * only when Ruby is done warming up the memory system.
137     */
138    void resetClock() const
139    {
140        Cycles elapsedCycles(divCeil(curTick(), clockPeriod()));
141        cycle = elapsedCycles;
142        tick = elapsedCycles * clockPeriod();
143    }
144
145  public:
146
147    /**
148     * Update the tick to the current tick.
149     *
150     */
151    inline void updateClockPeriod() const
152    {
153        update();
154    }
155
156    /**
157     * Determine the tick when a cycle begins, by default the current
158     * one, but the argument also enables the caller to determine a
159     * future cycle.
160     *
161     * @param cycles The number of cycles into the future
162     *
163     * @return The tick when the clock edge occurs
164     */
165    inline Tick clockEdge(Cycles cycles = Cycles(0)) const
166    {
167        // align tick to the next clock edge
168        update();
169
170        // figure out when this future cycle is
171        return tick + clockPeriod() * cycles;
172    }
173
174    /**
175     * Determine the current cycle, corresponding to a tick aligned to
176     * a clock edge.
177     *
178     * @return The current cycle count
179     */
180    inline Cycles curCycle() const
181    {
182        // align cycle to the next clock edge.
183        update();
184
185        return cycle;
186    }
187
188    /**
189     * Based on the clock of the object, determine the tick when the next
190     * cycle begins, in other words, return the next clock edge.
191     * (This can never be the current tick.)
192     *
193     * @return The tick when the next cycle starts
194     */
195    Tick nextCycle() const
196    { return clockEdge(Cycles(1)); }
197
198    inline uint64_t frequency() const
199    {
200        return SimClock::Frequency / clockPeriod();
201    }
202
203    inline Tick clockPeriod() const
204    {
205        return clockDomain.clockPeriod();
206    }
207
208    inline Cycles ticksToCycles(Tick t) const
209    { return Cycles(divCeil(t, clockPeriod())); }
210
211};
212
213#endif //__SIM_CLOCKED_OBJECT_HH__
214