clocked_object.hh revision 9356
17506Stjones1@inf.ed.ac.uk/*
27506Stjones1@inf.ed.ac.uk * Copyright (c) 2012 ARM Limited
37506Stjones1@inf.ed.ac.uk * All rights reserved
47506Stjones1@inf.ed.ac.uk *
57506Stjones1@inf.ed.ac.uk * The license below extends only to copyright in the software and shall
67506Stjones1@inf.ed.ac.uk * not be construed as granting a license to any other intellectual
77506Stjones1@inf.ed.ac.uk * property including but not limited to intellectual property relating
87506Stjones1@inf.ed.ac.uk * to a hardware implementation of the functionality of the software
97506Stjones1@inf.ed.ac.uk * licensed hereunder.  You may use the software subject to the license
107506Stjones1@inf.ed.ac.uk * terms below provided that you ensure that this notice is replicated
117506Stjones1@inf.ed.ac.uk * unmodified and in its entirety in all distributions of the software,
127506Stjones1@inf.ed.ac.uk * modified or unmodified, in source code or in binary form.
137506Stjones1@inf.ed.ac.uk *
147506Stjones1@inf.ed.ac.uk * Redistribution and use in source and binary forms, with or without
157506Stjones1@inf.ed.ac.uk * modification, are permitted provided that the following conditions are
167506Stjones1@inf.ed.ac.uk * met: redistributions of source code must retain the above copyright
177506Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer;
187506Stjones1@inf.ed.ac.uk * redistributions in binary form must reproduce the above copyright
197506Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer in the
207506Stjones1@inf.ed.ac.uk * documentation and/or other materials provided with the distribution;
217506Stjones1@inf.ed.ac.uk * neither the name of the copyright holders nor the names of its
227506Stjones1@inf.ed.ac.uk * contributors may be used to endorse or promote products derived from
237506Stjones1@inf.ed.ac.uk * this software without specific prior written permission.
247506Stjones1@inf.ed.ac.uk *
257506Stjones1@inf.ed.ac.uk * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
267506Stjones1@inf.ed.ac.uk * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
277506Stjones1@inf.ed.ac.uk * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
287506Stjones1@inf.ed.ac.uk * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
297506Stjones1@inf.ed.ac.uk * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
307506Stjones1@inf.ed.ac.uk * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
317506Stjones1@inf.ed.ac.uk * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
327506Stjones1@inf.ed.ac.uk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
337506Stjones1@inf.ed.ac.uk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
347506Stjones1@inf.ed.ac.uk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
357506Stjones1@inf.ed.ac.uk * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
367698SAli.Saidi@ARM.com *
377506Stjones1@inf.ed.ac.uk * Authors: Andreas Hansson
387506Stjones1@inf.ed.ac.uk */
397506Stjones1@inf.ed.ac.uk
407506Stjones1@inf.ed.ac.uk/**
417506Stjones1@inf.ed.ac.uk * @file
427506Stjones1@inf.ed.ac.uk * ClockedObject declaration and implementation.
437506Stjones1@inf.ed.ac.uk */
447506Stjones1@inf.ed.ac.uk
457506Stjones1@inf.ed.ac.uk#ifndef __SIM_CLOCKED_OBJECT_HH__
467506Stjones1@inf.ed.ac.uk#define __SIM_CLOCKED_OBJECT_HH__
477506Stjones1@inf.ed.ac.uk
487506Stjones1@inf.ed.ac.uk#include "base/intmath.hh"
497506Stjones1@inf.ed.ac.uk#include "params/ClockedObject.hh"
507506Stjones1@inf.ed.ac.uk#include "sim/core.hh"
517506Stjones1@inf.ed.ac.uk#include "sim/sim_object.hh"
527506Stjones1@inf.ed.ac.uk
537506Stjones1@inf.ed.ac.uk/**
547506Stjones1@inf.ed.ac.uk * The ClockedObject class extends the SimObject with a clock and
557720Sgblack@eecs.umich.edu * accessor functions to relate ticks to the cycles of the object.
567506Stjones1@inf.ed.ac.uk */
577506Stjones1@inf.ed.ac.ukclass ClockedObject : public SimObject
588787Sgblack@eecs.umich.edu{
598787Sgblack@eecs.umich.edu
608787Sgblack@eecs.umich.edu  private:
618787Sgblack@eecs.umich.edu
628787Sgblack@eecs.umich.edu    // the tick value of the next clock edge (>= curTick()) at the
638787Sgblack@eecs.umich.edu    // time of the last call to update()
648787Sgblack@eecs.umich.edu    mutable Tick tick;
657693SAli.Saidi@ARM.com
667693SAli.Saidi@ARM.com    // The cycle counter value corresponding to the current value of
677693SAli.Saidi@ARM.com    // 'tick'
687693SAli.Saidi@ARM.com    mutable Cycles cycle;
697693SAli.Saidi@ARM.com
707693SAli.Saidi@ARM.com    /**
717693SAli.Saidi@ARM.com     * Prevent inadvertent use of the copy constructor and assignment
727811Ssteve.reinhardt@amd.com     * 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