clocked_object.hh revision 9179
110428Sandreas.hansson@arm.com/* 210428Sandreas.hansson@arm.com * Copyright (c) 2012 ARM Limited 310428Sandreas.hansson@arm.com * All rights reserved 410428Sandreas.hansson@arm.com * 510428Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall 610428Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual 710428Sandreas.hansson@arm.com * property including but not limited to intellectual property relating 810428Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software 910428Sandreas.hansson@arm.com * licensed hereunder. You may use the software subject to the license 1010428Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated 1110428Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software, 1210428Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form. 1310428Sandreas.hansson@arm.com * 1410428Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without 1510428Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are 1610428Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright 1710428Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer; 1810428Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright 1910428Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the 2010428Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution; 2110428Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its 2210428Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from 2310428Sandreas.hansson@arm.com * this software without specific prior written permission. 2410428Sandreas.hansson@arm.com * 2510428Sandreas.hansson@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2610428Sandreas.hansson@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2710428Sandreas.hansson@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2810428Sandreas.hansson@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2910428Sandreas.hansson@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3010428Sandreas.hansson@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3110428Sandreas.hansson@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3210428Sandreas.hansson@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3310428Sandreas.hansson@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3410428Sandreas.hansson@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3510428Sandreas.hansson@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3610428Sandreas.hansson@arm.com * 3710428Sandreas.hansson@arm.com * Authors: Andreas Hansson 3810428Sandreas.hansson@arm.com */ 3910428Sandreas.hansson@arm.com 4010428Sandreas.hansson@arm.com/** 4110428Sandreas.hansson@arm.com * @file 4210428Sandreas.hansson@arm.com * ClockedObject declaration and implementation. 4310428Sandreas.hansson@arm.com */ 4410428Sandreas.hansson@arm.com 4510428Sandreas.hansson@arm.com#ifndef __SIM_CLOCKED_OBJECT_HH__ 4610428Sandreas.hansson@arm.com#define __SIM_CLOCKED_OBJECT_HH__ 4710428Sandreas.hansson@arm.com 4810428Sandreas.hansson@arm.com#include "base/intmath.hh" 4910428Sandreas.hansson@arm.com#include "params/ClockedObject.hh" 5010428Sandreas.hansson@arm.com#include "sim/sim_object.hh" 5110428Sandreas.hansson@arm.com 5210428Sandreas.hansson@arm.com/** 5310428Sandreas.hansson@arm.com * The ClockedObject class extends the SimObject with a clock and 5410428Sandreas.hansson@arm.com * accessor functions to relate ticks to the cycles of the object. 5510428Sandreas.hansson@arm.com */ 5610428Sandreas.hansson@arm.comclass ClockedObject : public SimObject 5710428Sandreas.hansson@arm.com{ 5810428Sandreas.hansson@arm.com 5910428Sandreas.hansson@arm.com private: 6010428Sandreas.hansson@arm.com 6110428Sandreas.hansson@arm.com // the tick value of the next clock edge (>= curTick()) at the 6210428Sandreas.hansson@arm.com // time of the last call to update() 6310428Sandreas.hansson@arm.com mutable Tick tick; 6410428Sandreas.hansson@arm.com 65 // The cycle counter value corresponding to the current value of 66 // 'tick' 67 mutable Tick cycle; 68 69 /** 70 * Prevent inadvertent use of the copy constructor and assignment 71 * operator by making them private. 72 */ 73 ClockedObject(ClockedObject&); 74 ClockedObject& operator=(ClockedObject&); 75 76 /** 77 * Align cycle and tick to the next clock edge if not already done. 78 */ 79 void update() const 80 { 81 // both tick and cycle are up-to-date and we are done, note 82 // that the >= is important as it captures cases where tick 83 // has already passed curTick() 84 if (tick >= curTick()) 85 return; 86 87 // optimise for the common case and see if the tick should be 88 // advanced by a single clock period 89 tick += clock; 90 ++cycle; 91 92 // see if we are done at this point 93 if (tick >= curTick()) 94 return; 95 96 // if not, we have to recalculate the cycle and tick, we 97 // perform the calculations in terms of relative cycles to 98 // allow changes to the clock period in the future 99 Tick elapsedCycles = divCeil(curTick() - tick, clock); 100 cycle += elapsedCycles; 101 tick += elapsedCycles * clock; 102 } 103 104 protected: 105 106 // Clock period in ticks 107 Tick clock; 108 109 /** 110 * Create a clocked object and set the clock based on the 111 * parameters. 112 */ 113 ClockedObject(const ClockedObjectParams* p) : 114 SimObject(p), tick(0), cycle(0), clock(p->clock) 115 { } 116 117 /** 118 * Virtual destructor due to inheritance. 119 */ 120 virtual ~ClockedObject() { } 121 122 public: 123 124 /** 125 * Determine the tick when a cycle begins, by default the current 126 * one, but the argument also enables the caller to determine a 127 * future cycle. 128 * 129 * @param cycles The number of cycles into the future 130 * 131 * @return The tick when the clock edge occurs 132 */ 133 inline Tick clockEdge(int cycles = 0) const 134 { 135 // align tick to the next clock edge 136 update(); 137 138 // figure out when this future cycle is 139 return tick + ticks(cycles); 140 } 141 142 /** 143 * Determine the current cycle, corresponding to a tick aligned to 144 * a clock edge. 145 * 146 * @return The current cycle 147 */ 148 inline Tick curCycle() const 149 { 150 // align cycle to the next clock edge. 151 update(); 152 153 return cycle; 154 } 155 156 /** 157 * Based on the clock of the object, determine the tick when the 158 * next cycle begins, in other words, return the next clock edge. 159 * 160 * @return The tick when the next cycle starts 161 */ 162 Tick nextCycle() const 163 { return clockEdge(); } 164 165 inline Tick frequency() const { return SimClock::Frequency / clock; } 166 167 inline Tick ticks(int cycles) const { return clock * cycles; } 168 169 inline Tick clockPeriod() const { return clock; } 170 171 inline Tick tickToCycle(Tick tick) const { return tick / clock; } 172 173}; 174 175#endif //__SIM_CLOCKED_OBJECT_HH__ 176