1/* 2 * Copyright (c) 2012-2013 ARM Limited 3 * Copyright (c) 2013 Cornell University 4 * All rights reserved 5 * 6 * The license below extends only to copyright in the software and shall 7 * not be construed as granting a license to any other intellectual 8 * property including but not limited to intellectual property relating 9 * to a hardware implementation of the functionality of the software 10 * licensed hereunder. You may use the software subject to the license 11 * terms below provided that you ensure that this notice is replicated 12 * unmodified and in its entirety in all distributions of the software, 13 * modified or unmodified, in source code or in binary form. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions are 17 * met: redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer; 19 * redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution; 22 * neither the name of the copyright holders nor the names of its 23 * contributors may be used to endorse or promote products derived from 24 * this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 * 38 * Authors: Andreas Hansson 39 * Christopher Torng 40 */ 41 42/** 43 * @file 44 * ClockedObject declaration and implementation. 45 */ 46 47#ifndef __SIM_CLOCKED_OBJECT_HH__ 48#define __SIM_CLOCKED_OBJECT_HH__ 49 50#include "base/intmath.hh" 51#include "base/misc.hh" 52#include "params/ClockedObject.hh" 53#include "sim/core.hh" 54#include "sim/clock_domain.hh" 55#include "sim/sim_object.hh" 56 57/**
| 1/* 2 * Copyright (c) 2012-2013 ARM Limited 3 * Copyright (c) 2013 Cornell University 4 * All rights reserved 5 * 6 * The license below extends only to copyright in the software and shall 7 * not be construed as granting a license to any other intellectual 8 * property including but not limited to intellectual property relating 9 * to a hardware implementation of the functionality of the software 10 * licensed hereunder. You may use the software subject to the license 11 * terms below provided that you ensure that this notice is replicated 12 * unmodified and in its entirety in all distributions of the software, 13 * modified or unmodified, in source code or in binary form. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions are 17 * met: redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer; 19 * redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution; 22 * neither the name of the copyright holders nor the names of its 23 * contributors may be used to endorse or promote products derived from 24 * this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 * 38 * Authors: Andreas Hansson 39 * Christopher Torng 40 */ 41 42/** 43 * @file 44 * ClockedObject declaration and implementation. 45 */ 46 47#ifndef __SIM_CLOCKED_OBJECT_HH__ 48#define __SIM_CLOCKED_OBJECT_HH__ 49 50#include "base/intmath.hh" 51#include "base/misc.hh" 52#include "params/ClockedObject.hh" 53#include "sim/core.hh" 54#include "sim/clock_domain.hh" 55#include "sim/sim_object.hh" 56 57/**
|
58 * The ClockedObject class extends the SimObject with a clock and 59 * accessor functions to relate ticks to the cycles of the object.
| 58 * Helper class for objects that need to be clocked. Clocked objects 59 * typically inherit from this class. Objects that need SimObject 60 * functionality as well should inherit from ClockedObject.
|
60 */
| 61 */
|
61class ClockedObject : public SimObject
| 62class Clocked
|
62{ 63 64 private:
| 63{ 64 65 private:
|
65
| |
66 // the tick value of the next clock edge (>= curTick()) at the 67 // time of the last call to update() 68 mutable Tick tick; 69 70 // The cycle counter value corresponding to the current value of 71 // 'tick' 72 mutable Cycles cycle; 73 74 /**
| 66 // the tick value of the next clock edge (>= curTick()) at the 67 // time of the last call to update() 68 mutable Tick tick; 69 70 // The cycle counter value corresponding to the current value of 71 // 'tick' 72 mutable Cycles cycle; 73 74 /**
|
75 * Prevent inadvertent use of the copy constructor and assignment 76 * operator by making them private. 77 */ 78 ClockedObject(ClockedObject&); 79 ClockedObject& operator=(ClockedObject&); 80 81 /**
| |
82 * Align cycle and tick to the next clock edge if not already done. When 83 * complete, tick must be at least curTick(). 84 */ 85 void update() const 86 { 87 // both tick and cycle are up-to-date and we are done, note 88 // that the >= is important as it captures cases where tick 89 // has already passed curTick() 90 if (tick >= curTick()) 91 return; 92 93 // optimise for the common case and see if the tick should be 94 // advanced by a single clock period 95 tick += clockPeriod(); 96 ++cycle; 97 98 // see if we are done at this point 99 if (tick >= curTick()) 100 return; 101 102 // if not, we have to recalculate the cycle and tick, we 103 // perform the calculations in terms of relative cycles to 104 // allow changes to the clock period in the future 105 Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod())); 106 cycle += elapsedCycles; 107 tick += elapsedCycles * clockPeriod(); 108 } 109 110 /** 111 * The clock domain this clocked object belongs to 112 */ 113 ClockDomain &clockDomain; 114 115 protected: 116 117 /** 118 * Create a clocked object and set the clock domain based on the 119 * parameters. 120 */
| 75 * Align cycle and tick to the next clock edge if not already done. When 76 * complete, tick must be at least curTick(). 77 */ 78 void update() const 79 { 80 // both tick and cycle are up-to-date and we are done, note 81 // that the >= is important as it captures cases where tick 82 // has already passed curTick() 83 if (tick >= curTick()) 84 return; 85 86 // optimise for the common case and see if the tick should be 87 // advanced by a single clock period 88 tick += clockPeriod(); 89 ++cycle; 90 91 // see if we are done at this point 92 if (tick >= curTick()) 93 return; 94 95 // if not, we have to recalculate the cycle and tick, we 96 // perform the calculations in terms of relative cycles to 97 // allow changes to the clock period in the future 98 Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod())); 99 cycle += elapsedCycles; 100 tick += elapsedCycles * clockPeriod(); 101 } 102 103 /** 104 * The clock domain this clocked object belongs to 105 */ 106 ClockDomain &clockDomain; 107 108 protected: 109 110 /** 111 * Create a clocked object and set the clock domain based on the 112 * parameters. 113 */
|
121 ClockedObject(const ClockedObjectParams* p) : 122 SimObject(p), tick(0), cycle(0), clockDomain(*p->clk_domain)
| 114 Clocked(ClockDomain &clk_domain) 115 : tick(0), cycle(0), clockDomain(clk_domain)
|
123 { 124 // Register with the clock domain, so that if the clock domain 125 // frequency changes, we can update this object's tick. 126 clockDomain.registerWithClockDomain(this); 127 } 128
| 116 { 117 // Register with the clock domain, so that if the clock domain 118 // frequency changes, we can update this object's tick. 119 clockDomain.registerWithClockDomain(this); 120 } 121
|
| 122 Clocked(Clocked &) = delete; 123 Clocked &operator=(Clocked &) = delete; 124
|
129 /** 130 * Virtual destructor due to inheritance. 131 */
| 125 /** 126 * Virtual destructor due to inheritance. 127 */
|
132 virtual ~ClockedObject() { }
| 128 virtual ~Clocked() { }
|
133 134 /** 135 * Reset the object's clock using the current global tick value. Likely 136 * to be used only when the global clock is reset. Currently, this done 137 * only when Ruby is done warming up the memory system. 138 */ 139 void resetClock() const 140 { 141 Cycles elapsedCycles(divCeil(curTick(), clockPeriod())); 142 cycle = elapsedCycles; 143 tick = elapsedCycles * clockPeriod(); 144 } 145 146 public: 147 148 /** 149 * Update the tick to the current tick. 150 * 151 */ 152 inline void updateClockPeriod() const 153 { 154 update(); 155 } 156 157 /** 158 * Determine the tick when a cycle begins, by default the current one, but 159 * the argument also enables the caller to determine a future cycle. When 160 * curTick() is on a clock edge, the number of cycles in the parameter is 161 * added to curTick() to be returned. When curTick() is not aligned to a 162 * clock edge, the number of cycles in the parameter is added to the next 163 * clock edge. 164 * 165 * @param cycles The number of cycles into the future 166 * 167 * @return The start tick when the requested clock edge occurs. Precisely, 168 * this tick can be 169 * curTick() + [0, clockPeriod()) + clockPeriod() * cycles 170 */ 171 inline Tick clockEdge(Cycles cycles = Cycles(0)) const 172 { 173 // align tick to the next clock edge 174 update(); 175 176 // figure out when this future cycle is 177 return tick + clockPeriod() * cycles; 178 } 179 180 /** 181 * Determine the current cycle, corresponding to a tick aligned to 182 * a clock edge. 183 * 184 * @return When curTick() is on a clock edge, return the Cycle corresponding 185 * to that clock edge. When curTick() is not on a clock edge, return the 186 * Cycle corresponding to the next clock edge. 187 */ 188 inline Cycles curCycle() const 189 { 190 // align cycle to the next clock edge. 191 update(); 192 193 return cycle; 194 } 195 196 /** 197 * Based on the clock of the object, determine the start tick of the first 198 * cycle that is at least one cycle in the future. When curTick() is at the 199 * current cycle edge, this returns the next clock edge. When calling this 200 * during the middle of a cycle, this returns 2 clock edges in the future. 201 * 202 * @return The start tick of the first cycle that is at least one cycle in 203 * the future. Precisely, the returned tick can be in the range 204 * curTick() + [clockPeriod(), 2 * clockPeriod()) 205 */ 206 Tick nextCycle() const 207 { return clockEdge(Cycles(1)); } 208 209 inline uint64_t frequency() const 210 { 211 return SimClock::Frequency / clockPeriod(); 212 } 213 214 inline Tick clockPeriod() const 215 { 216 return clockDomain.clockPeriod(); 217 } 218 219 inline Cycles ticksToCycles(Tick t) const 220 { return Cycles(divCeil(t, clockPeriod())); } 221 222}; 223
| 129 130 /** 131 * Reset the object's clock using the current global tick value. Likely 132 * to be used only when the global clock is reset. Currently, this done 133 * only when Ruby is done warming up the memory system. 134 */ 135 void resetClock() const 136 { 137 Cycles elapsedCycles(divCeil(curTick(), clockPeriod())); 138 cycle = elapsedCycles; 139 tick = elapsedCycles * clockPeriod(); 140 } 141 142 public: 143 144 /** 145 * Update the tick to the current tick. 146 * 147 */ 148 inline void updateClockPeriod() const 149 { 150 update(); 151 } 152 153 /** 154 * Determine the tick when a cycle begins, by default the current one, but 155 * the argument also enables the caller to determine a future cycle. When 156 * curTick() is on a clock edge, the number of cycles in the parameter is 157 * added to curTick() to be returned. When curTick() is not aligned to a 158 * clock edge, the number of cycles in the parameter is added to the next 159 * clock edge. 160 * 161 * @param cycles The number of cycles into the future 162 * 163 * @return The start tick when the requested clock edge occurs. Precisely, 164 * this tick can be 165 * curTick() + [0, clockPeriod()) + clockPeriod() * cycles 166 */ 167 inline Tick clockEdge(Cycles cycles = Cycles(0)) const 168 { 169 // align tick to the next clock edge 170 update(); 171 172 // figure out when this future cycle is 173 return tick + clockPeriod() * cycles; 174 } 175 176 /** 177 * Determine the current cycle, corresponding to a tick aligned to 178 * a clock edge. 179 * 180 * @return When curTick() is on a clock edge, return the Cycle corresponding 181 * to that clock edge. When curTick() is not on a clock edge, return the 182 * Cycle corresponding to the next clock edge. 183 */ 184 inline Cycles curCycle() const 185 { 186 // align cycle to the next clock edge. 187 update(); 188 189 return cycle; 190 } 191 192 /** 193 * Based on the clock of the object, determine the start tick of the first 194 * cycle that is at least one cycle in the future. When curTick() is at the 195 * current cycle edge, this returns the next clock edge. When calling this 196 * during the middle of a cycle, this returns 2 clock edges in the future. 197 * 198 * @return The start tick of the first cycle that is at least one cycle in 199 * the future. Precisely, the returned tick can be in the range 200 * curTick() + [clockPeriod(), 2 * clockPeriod()) 201 */ 202 Tick nextCycle() const 203 { return clockEdge(Cycles(1)); } 204 205 inline uint64_t frequency() const 206 { 207 return SimClock::Frequency / clockPeriod(); 208 } 209 210 inline Tick clockPeriod() const 211 { 212 return clockDomain.clockPeriod(); 213 } 214 215 inline Cycles ticksToCycles(Tick t) const 216 { return Cycles(divCeil(t, clockPeriod())); } 217 218}; 219
|
| 220/** 221 * The ClockedObject class extends the SimObject with a clock and 222 * accessor functions to relate ticks to the cycles of the object. 223 */ 224class ClockedObject 225 : public SimObject, public Clocked 226{ 227 public: 228 ClockedObject(const ClockedObjectParams *p) 229 : SimObject(p), Clocked(*p->clk_domain) { } 230}; 231
|
224#endif //__SIM_CLOCKED_OBJECT_HH__
| 232#endif //__SIM_CLOCKED_OBJECT_HH__
|