diff -r 55df9c2b1128 -r d22fdcd09132 src/base/clock.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/base/clock.hh Sun Apr 18 22:38:00 2010 -0700 @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2010 The Hewlett-Packard Development Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Nathan Binkert + */ + +#ifndef __BASE_CLOCK_HH__ +#define __BASE_CLOCK_HH__ + +#include + +#include "base/types.hh" + +struct Clock +{ + const Tick period; + const Tick phase; + + Clock(Tick _period, Tick _phase = 0) + : period(_period), phase(_phase) + { + assert(phase >= 0 && phase < period); + } + + Tick + nextEdge(Tick when) const + { + // align edge to the next clock edge + assert(when >= curTick); + + // Take out the phase shift + Tick unshifted = when - phase; + + // Make sure we're at least to the next edge + Tick after = (unshifted + period - 1); + + // Take out the remainder and bring us back to the edge + Tick edge = after - after % period; // by taking out the remainder + + // add back in the phase shift + edge += phase; + + return edge; + } + + Tick + clockCycle(Tick when) const + { + // calculate the clock cycle of the given tick + return (when - phase) / period; + } +}; + +class ClockTicker : public Clock +{ + protected: + Tick cycle; + Tick tick; + + void + update() + { + if (tick == curTick) + return; + + // See if we did something the last cycle + tick += period; + + if (tick == curTick) { + // If so, increment the cycle count. + cycle += 1; + return; + } + + // If not, we must manually recalculate everything + + // If we're not on an edge, find the next cycle + cycle = clockCycle(curTick + period - 1); + tick = cycle * period + phase; + } + + public: + ClockTicker(Clock clock) + : Clock(clock), cycle(0), tick(0) + {} + + ClockTicker(Tick period, Tick phase = 0) + : Clock(period, phase), cycle(0), tick(0) + {} + + // Get the current cycle number. Keep track of the tick of the + // current cycle for easy math to calculate future ticks. + Tick + curCycle() + { + // Align cycle to the next clock edge. + update(); + return cycle; + } + + // @param cycles is the number of cycles in the future from the + // current cycle + // @return the tick of the specificed cycle number. + Tick + clockEdge(int cycles = 0) + { + // Align tick to the next clock edge. + update(); + + // Figure out when this future cycle is + return tick + cycles * period; + } + + Tick curEdge() { return clockEdge(0); } + Tick nextEdge() { return clockEdge(1); } +}; + +#endif // __BASE_CLOCK_HH__ diff -r 55df9c2b1128 -r d22fdcd09132 src/sim/periodic_event.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sim/periodic_event.hh Sun Apr 18 22:38:00 2010 -0700 @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2010 The Hewlett-Packard Development Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Nathan Binkert + */ + +#ifndef __SIM_PERIODIC_EVENT_HH__ +#define __SIM_PERIODIC_EVENT_HH__ + +#include "base/callback.hh" +#include "base/clock.hh" +#include "sim/eventq.hh" + +template +class PeriodicEvent : public Event +{ + private: + EventQueue *eventq; + ClockTicker *ticker; + T *object; + + typedef unsigned char FlagsType; + typedef ::Flags Flags; + Flags flags; + + static const FlagsType Enabled = 0x01; // Event is running + static const FlagsType Processing = 0x02; // Event is in process() + static const FlagsType MyTicker = 0x04; // Ticker needs delete + + public: + PeriodicEvent(EventQueue *q, Clock c, T *o, + Event::Priority p = Event::Default_Pri) + : Event(p), eventq(q), ticker(new ClockTicker(c)), object(o), flags(0) + { + flags.set(MyTicker); + } + + PeriodicEvent(EventQueue *q, ClockTicker &t, T *o, + Event::Priority p = Event::Default_Pri) + : Event(p), eventq(q), ticker(&t), object(o), flags(0) + { + } + + ~PeriodicEvent() + { + if (flags.isSet(MyTicker)) + delete ticker; + } + + bool enabled() const { return flags.isSet(Enabled); } + + void + enable() + { + if (enabled()) + return; + + flags.set(Enabled); + if (!scheduled()) { + if (!flags.isSet(Processing)) + eventq->schedule(this, ticker->clockEdge(0)); + } else if (squashed()) { + eventq->reschedule(this, ticker->clockEdge(0)); + } + } + + void + disable() + { + if (enabled()) { + flags.clear(Enabled); + if (scheduled()) + squash(); + } + } + + void + process() + { + flags.set(Processing); + (object->*F)(); + flags.clear(Processing); + if (enabled()) { + Tick when = ticker->clockEdge(1); + assert(when == curTick + ticker->period); + eventq->schedule(this, when); + } + } + + typedef MakeCallback EnableCallback; + typedef EventWrapper EnableEvent; +}; + +#endif // __SIM_PERIODIC_EVENT_HH__